1 // RUN: %check_clang_tidy %s abseil-redundant-strcat-calls %t
2 
3 int strlen(const char *);
4 
5 // Here we mimic the hierarchy of ::string.
6 // We need to do so because we are matching on the fully qualified name of the
7 // methods.
8 struct __sso_string_base {};
9 namespace __gnu_cxx {
10 template <typename A, typename B, typename C, typename D = __sso_string_base>
11 class __versa_string {
12  public:
13   const char *c_str() const;
14   const char *data() const;
15   int size() const;
16   int capacity() const;
17   int length() const;
18   bool empty() const;
19   char &operator[](int);
20   void clear();
21   void resize(int);
22   int compare(const __versa_string &) const;
23 };
24 }  // namespace __gnu_cxx
25 
26 namespace std {
27 template <typename T>
28 class char_traits {};
29 template <typename T>
30 class allocator {};
31 }  // namespace std
32 
33 template <typename A, typename B = std::char_traits<A>,
34           typename C = std::allocator<A>>
35 class basic_string : public __gnu_cxx::__versa_string<A, B, C> {
36  public:
37   basic_string();
38   basic_string(const basic_string &);
39   basic_string(const char *, C = C());
40   basic_string(const char *, int, C = C());
41   basic_string(const basic_string &, int, int, C = C());
42   ~basic_string();
43 
44   basic_string &operator+=(const basic_string &);
45 };
46 
47 template <typename A, typename B, typename C>
48 basic_string<A, B, C> operator+(const basic_string<A, B, C> &,
49                                 const basic_string<A, B, C> &);
50 template <typename A, typename B, typename C>
51 basic_string<A, B, C> operator+(const basic_string<A, B, C> &, const char *);
52 
53 typedef basic_string<char> string;
54 
55 bool operator==(const string &, const string &);
56 bool operator==(const string &, const char *);
57 bool operator==(const char *, const string &);
58 
59 bool operator!=(const string &, const string &);
60 bool operator<(const string &, const string &);
61 bool operator>(const string &, const string &);
62 bool operator<=(const string &, const string &);
63 bool operator>=(const string &, const string &);
64 
65 namespace std {
66 template <typename _CharT, typename _Traits = char_traits<_CharT>,
67           typename _Alloc = allocator<_CharT>>
68 class basic_string;
69 
70 template <typename _CharT, typename _Traits, typename _Alloc>
71 class basic_string {
72  public:
73   basic_string();
74   basic_string(const basic_string &);
75   basic_string(const char *, const _Alloc & = _Alloc());
76   basic_string(const char *, int, const _Alloc & = _Alloc());
77   basic_string(const basic_string &, int, int, const _Alloc & = _Alloc());
78   ~basic_string();
79 
80   basic_string &operator+=(const basic_string &);
81 
82   unsigned size() const;
83   unsigned length() const;
84   bool empty() const;
85 };
86 
87 typedef basic_string<char> string;
88 }  // namespace std
89 
90 namespace absl {
91 
92 class string_view {
93  public:
94   typedef std::char_traits<char> traits_type;
95 
96   string_view();
97   string_view(const char *);
98   string_view(const string &);
99   string_view(const char *, int);
100   string_view(string_view, int);
101 
102   template <typename A>
103   explicit operator ::basic_string<char, traits_type, A>() const;
104 
105   const char *data() const;
106   int size() const;
107   int length() const;
108 };
109 
110 bool operator==(string_view A, string_view B);
111 
112 struct AlphaNum {
113   AlphaNum(int i);
114   AlphaNum(double f);
115   AlphaNum(const char *c_str);
116   AlphaNum(const string &str);
117   AlphaNum(const string_view &pc);
118 
119  private:
120   AlphaNum(const AlphaNum &);
121   AlphaNum &operator=(const AlphaNum &);
122 };
123 
124 string StrCat(const AlphaNum &A);
125 string StrCat(const AlphaNum &A, const AlphaNum &B);
126 string StrCat(const AlphaNum &A, const AlphaNum &B, const AlphaNum &C);
127 string StrCat(const AlphaNum &A, const AlphaNum &B, const AlphaNum &C,
128               const AlphaNum &D);
129 
130 // Support 5 or more arguments
131 template <typename... AV>
132 string StrCat(const AlphaNum &A, const AlphaNum &B, const AlphaNum &C,
133               const AlphaNum &D, const AlphaNum &E, const AV &... args);
134 
135 void StrAppend(string *Dest, const AlphaNum &A);
136 void StrAppend(string *Dest, const AlphaNum &A, const AlphaNum &B);
137 void StrAppend(string *Dest, const AlphaNum &A, const AlphaNum &B,
138                const AlphaNum &C);
139 void StrAppend(string *Dest, const AlphaNum &A, const AlphaNum &B,
140                const AlphaNum &C, const AlphaNum &D);
141 
142 // Support 5 or more arguments
143 template <typename... AV>
144 void StrAppend(string *Dest, const AlphaNum &A, const AlphaNum &B,
145                const AlphaNum &C, const AlphaNum &D, const AlphaNum &E,
146                const AV &... args);
147 
148 }  // namespace absl
149 
150 using absl::AlphaNum;
151 using absl::StrAppend;
152 using absl::StrCat;
153 
Positives()154 void Positives() {
155   string S = StrCat(1, StrCat("A", StrCat(1.1)));
156   // CHECK-MESSAGES: [[@LINE-1]]:14: warning: multiple calls to 'absl::StrCat' can be flattened into a single call
157 
158   S = StrCat(StrCat(StrCat(StrCat(StrCat(1)))));
159   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: multiple calls to 'absl::StrCat' can be flattened into a single call
160 
161   // TODO: should trigger. The issue here is that in the current
162   // implementation we ignore any StrCat with StrCat ancestors. Therefore
163   // inserting anything in between calls will disable triggering the deepest
164   // ones.
165   // s = StrCat(Identity(StrCat(StrCat(1, 2), StrCat(3, 4))));
166 
167   StrAppend(&S, 001, StrCat(1, 2, "3"), StrCat("FOO"));
168   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple calls to 'absl::StrCat' can be flattened into a single call
169 
170   StrAppend(&S, 001, StrCat(StrCat(1, 2), "3"), StrCat("FOO"));
171   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple calls to 'absl::StrCat' can be flattened into a single call
172 
173   // Too many args. Ignore for now.
174   S = StrCat(1, 2, StrCat(3, 4, 5, 6, 7), 8, 9, 10,
175              StrCat(11, 12, 13, 14, 15, 16, 17, 18), 19, 20, 21, 22, 23, 24, 25,
176              26, 27);
177   // CHECK-MESSAGES: :[[@LINE-3]]:7: warning: multiple calls to 'absl::StrCat' can be flattened into a single call
178   StrAppend(&S, StrCat(1, 2, 3, 4, 5), StrCat(6, 7, 8, 9, 10));
179   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: multiple calls to 'absl::StrCat' can be flattened into a single call
180 }
181 
Negatives()182 void Negatives() {
183   // One arg. It is used for conversion. Ignore.
184   string S = StrCat(1);
185 
186 #define A_MACRO(x, y, z) StrCat(x, y, z)
187   S = A_MACRO(1, 2, StrCat("A", "B"));
188 }
189