Clang Project

clang_source_code/test/Analysis/llvm-conventions.cpp
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.llvm.Conventions \
2// RUN:   -std=c++14 -verify  %s
3
4#include "Inputs/system-header-simulator-cxx.h"
5
6//===----------------------------------------------------------------------===//
7// Forward declarations for StringRef tests.
8//===----------------------------------------------------------------------===//
9
10using size_type = size_t;
11
12namespace std {
13
14template <class T>
15struct numeric_limits { const static bool is_signed; };
16
17} // end of namespace std
18
19namespace llvm {
20
21template <class T>
22struct iterator_range;
23
24template <class Func>
25struct function_ref;
26
27struct hash_code;
28
29template <class T>
30struct SmallVectorImpl;
31
32struct APInt;
33
34class StringRef {
35public:
36  static const size_t npos = ~size_t(0);
37  using iterator = const char *;
38  using const_iterator = const char *;
39  using size_type = size_t;
40
41  /*implicit*/ StringRef() = default;
42  StringRef(std::nullptr_t) = delete;
43  /*implicit*/ StringRef(const char *Str);
44  /*implicit*/ constexpr StringRef(const char *data, size_t length);
45  /*implicit*/ StringRef(const std::string &Str);
46
47  static StringRef withNullAsEmpty(const char *data);
48  iterator begin() const;
49  iterator end() const;
50  const unsigned char *bytes_begin() const;
51  const unsigned char *bytes_end() const;
52  iterator_range<const unsigned char *> bytes() const;
53  const char *data() const;
54  bool empty() const;
55  size_t size() const;
56  char front() const;
57  char back() const;
58  template <typename Allocator>
59  StringRef copy(Allocator &A) const;
60  bool equals(StringRef RHS) const;
61  bool equals_lower(StringRef RHS) const;
62  int compare(StringRef RHS) const;
63  int compare_lower(StringRef RHS) const;
64  int compare_numeric(StringRef RHS) const;
65  unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
66                         unsigned MaxEditDistance = 0) const;
67  std::string str() const;
68  char operator[](size_t Index) const;
69  template <typename T>
70  typename std::enable_if<std::is_same<T, std::string>::value,
71                          StringRef>::type &
72  operator=(T &&Str) = delete;
73  operator std::string() const;
74  bool startswith(StringRef Prefix) const;
75  bool startswith_lower(StringRef Prefix) const;
76  bool endswith(StringRef Suffix) const;
77  bool endswith_lower(StringRef Suffix) const;
78  size_t find(char C, size_t From = 0) const;
79  size_t find_lower(char C, size_t From = 0) const;
80  size_t find_if(function_ref<bool(char)> F, size_t From = 0) const;
81  size_t find_if_not(function_ref<bool(char)> F, size_t From = 0) const;
82  size_t find(StringRef Str, size_t From = 0) const;
83  size_t find_lower(StringRef Str, size_t From = 0) const;
84  size_t rfind(char C, size_t From = npos) const;
85  size_t rfind_lower(char C, size_t From = npos) const;
86  size_t rfind(StringRef Str) const;
87  size_t rfind_lower(StringRef Str) const;
88  size_t find_first_of(char C, size_t From = 0) const;
89  size_t find_first_of(StringRef Chars, size_t From = 0) const;
90  size_t find_first_not_of(char C, size_t From = 0) const;
91  size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
92  size_t find_last_of(char C, size_t From = npos) const;
93  size_t find_last_of(StringRef Chars, size_t From = npos) const;
94  size_t find_last_not_of(char C, size_t From = npos) const;
95  size_t find_last_not_of(StringRef Chars, size_t From = npos) const;
96  bool contains(StringRef Other) const;
97  bool contains(char C) const;
98  bool contains_lower(StringRef Other) const;
99  bool contains_lower(char C) const;
100  size_t count(char C) const;
101  size_t count(StringRef Str) const;
102  template <typename T>
103  typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
104  getAsInteger(unsigned Radix, T &Result) const;
105  template <typename T>
106  typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
107  getAsInteger(unsigned Radix, T &Result) const;
108  template <typename T>
109  typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
110  consumeInteger(unsigned Radix, T &Result);
111  template <typename T>
112  typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
113  consumeInteger(unsigned Radix, T &Result);
114  bool getAsInteger(unsigned Radix, APInt &Result) const;
115  bool getAsDouble(double &Result, bool AllowInexact = true) const;
116  std::string lower() const;
117  std::string upper() const;
118  StringRef substr(size_t Start, size_t N = npos) const;
119  StringRef take_front(size_t N = 1) const;
120  StringRef take_back(size_t N = 1) const;
121  StringRef take_while(function_ref<bool(char)> F) const;
122  StringRef take_until(function_ref<bool(char)> F) const;
123  StringRef drop_front(size_t N = 1) const;
124  StringRef drop_back(size_t N = 1) const;
125  StringRef drop_while(function_ref<bool(char)> F) const;
126  StringRef drop_until(function_ref<bool(char)> F) const;
127  bool consume_front(StringRef Prefix);
128  bool consume_back(StringRef Suffix);
129  StringRef slice(size_t Start, size_t End) const;
130  std::pair<StringRef, StringRef> split(char Separator) const;
131  std::pair<StringRef, StringRef> split(StringRef Separator) const;
132  std::pair<StringRef, StringRef> rsplit(StringRef Separator) const;
133  void split(SmallVectorImpl<StringRef> &A,
134             StringRef Separator, int MaxSplit = -1,
135             bool KeepEmpty = true) const;
136  void split(SmallVectorImpl<StringRef> &A, char Separator, int MaxSplit = -1,
137             bool KeepEmpty = true) const;
138  std::pair<StringRef, StringRef> rsplit(char Separator) const;
139  StringRef ltrim(char Char) const;
140  StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const;
141  StringRef rtrim(char Char) const;
142  StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const;
143  StringRef trim(char Char) const;
144  StringRef trim(StringRef Chars = " \t\n\v\f\r") const;
145};
146
147inline bool operator==(StringRef LHS, StringRef RHS);
148inline bool operator!=(StringRef LHS, StringRef RHS);
149inline bool operator<(StringRef LHS, StringRef RHS);
150inline bool operator<=(StringRef LHS, StringRef RHS);
151inline bool operator>(StringRef LHS, StringRef RHS);
152inline bool operator>=(StringRef LHS, StringRef RHS);
153inline std::string &operator+=(std::string &buffer, StringRef string);
154hash_code hash_value(StringRef S);
155
156} // end of namespace llvm
157
158//===----------------------------------------------------------------------===//
159// Tests for StringRef.
160//===----------------------------------------------------------------------===//
161
162void temporarayStringToStringRefAssignmentTest() {
163  // TODO: Emit a warning.
164  llvm::StringRef Ref = std::string("Yimmy yummy test.");
165}
166
167void assigningStringToStringRefWithLongerLifetimeTest() {
168  llvm::StringRef Ref;
169  {
170    // TODO: Emit a warning.
171    std::string TmpStr("This is a fine string.");
172    Ref = TmpStr;
173  }
174}
175
176std::string getTemporaryString() {
177  return "One two three.";
178}
179
180void assigningTempStringFromFunctionToStringRefTest() {
181  // TODO: Emit a warning.
182  llvm::StringRef Ref = getTemporaryString();
183}
184
185//===----------------------------------------------------------------------===//
186// Forward declaration for Clang AST nodes.
187//===----------------------------------------------------------------------===//
188
189namespace llvm {
190
191template <class T, int Size>
192struct SmallVector {};
193
194} // end of namespace llvm
195
196namespace clang {
197
198struct Type;
199struct Decl;
200struct Stmt;
201struct Attr;
202
203} // end of namespace clang
204
205//===----------------------------------------------------------------------===//
206// Tests for Clang AST nodes.
207//===----------------------------------------------------------------------===//
208
209namespace clang {
210
211struct Type {
212  std::string str; // expected-warning{{AST class 'Type' has a field 'str' that allocates heap memory (type std::string)}}
213};
214
215} // end of namespace clang
216
217namespace clang {
218
219struct Decl {
220  llvm::SmallVector<int, 5> Vec; // expected-warning{{AST class 'Decl' has a field 'Vec' that allocates heap memory (type llvm::SmallVector<int, 5>)}}
221};
222
223} // end of namespace clang
224