1 // RUN: %check_clang_tidy %s readability-uppercase-literal-suffix %t -- -- -I %S
2 // RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
3 // RUN: clang-tidy %t.cpp -checks='-*,readability-uppercase-literal-suffix' -fix -- -I %S
4 // RUN: clang-tidy %t.cpp -checks='-*,readability-uppercase-literal-suffix' -warnings-as-errors='-*,readability-uppercase-literal-suffix' -- -I %S
5
6 #include "readability-uppercase-literal-suffix.h"
7
integer_suffix()8 void integer_suffix() {
9 static constexpr auto v0 = __LINE__; // synthetic
10 static_assert(v0 == 9 || v0 == 5, "");
11
12 static constexpr auto v1 = __cplusplus; // synthetic, long
13
14 static constexpr auto v2 = 1; // no literal
15 static_assert(is_same<decltype(v2), const int>::value, "");
16 static_assert(v2 == 1, "");
17
18 // Unsigned
19
20 static constexpr auto v3 = 1u;
21 // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: integer literal has suffix 'u', which is not uppercase
22 // CHECK-MESSAGES-NEXT: static constexpr auto v3 = 1u;
23 // CHECK-MESSAGES-NEXT: ^~
24 // CHECK-MESSAGES-NEXT: {{^ *}}U{{$}}
25 // CHECK-FIXES: static constexpr auto v3 = 1U;
26 static_assert(is_same<decltype(v3), const unsigned int>::value, "");
27 static_assert(v3 == 1, "");
28
29 static constexpr auto v4 = 1U; // OK.
30 static_assert(is_same<decltype(v4), const unsigned int>::value, "");
31 static_assert(v4 == 1, "");
32
33 // Long
34
35 static constexpr auto v5 = 1l;
36 // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: integer literal has suffix 'l', which is not uppercase
37 // CHECK-MESSAGES-NEXT: static constexpr auto v5 = 1l;
38 // CHECK-MESSAGES-NEXT: ^~
39 // CHECK-MESSAGES-NEXT: {{^ *}}L{{$}}
40 // CHECK-FIXES: static constexpr auto v5 = 1L;
41 static_assert(is_same<decltype(v5), const long>::value, "");
42 static_assert(v5 == 1, "");
43
44 static constexpr auto v6 = 1L; // OK.
45 static_assert(is_same<decltype(v6), const long>::value, "");
46 static_assert(v6 == 1, "");
47
48 // Long Long
49
50 static constexpr auto v7 = 1ll;
51 // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: integer literal has suffix 'll', which is not uppercase
52 // CHECK-MESSAGES-NEXT: static constexpr auto v7 = 1ll;
53 // CHECK-MESSAGES-NEXT: ^~~
54 // CHECK-MESSAGES-NEXT: {{^ *}}LL{{$}}
55 // CHECK-FIXES: static constexpr auto v7 = 1LL;
56 static_assert(is_same<decltype(v7), const long long>::value, "");
57 static_assert(v7 == 1, "");
58
59 static constexpr auto v8 = 1LL; // OK.
60 static_assert(is_same<decltype(v8), const long long>::value, "");
61 static_assert(v8 == 1, "");
62
63 // Unsigned Long
64
65 static constexpr auto v9 = 1ul;
66 // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: integer literal has suffix 'ul', which is not uppercase
67 // CHECK-MESSAGES-NEXT: static constexpr auto v9 = 1ul;
68 // CHECK-MESSAGES-NEXT: ^~~
69 // CHECK-MESSAGES-NEXT: {{^ *}}UL{{$}}
70 // CHECK-FIXES: static constexpr auto v9 = 1UL;
71 static_assert(is_same<decltype(v9), const unsigned long>::value, "");
72 static_assert(v9 == 1, "");
73
74 static constexpr auto v10 = 1uL;
75 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'uL', which is not uppercase
76 // CHECK-MESSAGES-NEXT: static constexpr auto v10 = 1uL;
77 // CHECK-MESSAGES-NEXT: ^~~
78 // CHECK-MESSAGES-NEXT: {{^ *}}UL{{$}}
79 // CHECK-FIXES: static constexpr auto v10 = 1UL;
80 static_assert(is_same<decltype(v10), const unsigned long>::value, "");
81 static_assert(v10 == 1, "");
82
83 static constexpr auto v11 = 1Ul;
84 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'Ul', which is not uppercase
85 // CHECK-MESSAGES-NEXT: static constexpr auto v11 = 1Ul;
86 // CHECK-MESSAGES-NEXT: ^~~
87 // CHECK-MESSAGES-NEXT: {{^ *}}UL{{$}}
88 // CHECK-FIXES: static constexpr auto v11 = 1UL;
89 static_assert(is_same<decltype(v11), const unsigned long>::value, "");
90 static_assert(v11 == 1, "");
91
92 static constexpr auto v12 = 1UL; // OK.
93 static_assert(is_same<decltype(v12), const unsigned long>::value, "");
94 static_assert(v12 == 1, "");
95
96 // Long Unsigned
97
98 static constexpr auto v13 = 1lu;
99 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'lu', which is not uppercase
100 // CHECK-MESSAGES-NEXT: static constexpr auto v13 = 1lu;
101 // CHECK-MESSAGES-NEXT: ^~~
102 // CHECK-MESSAGES-NEXT: {{^ *}}LU{{$}}
103 // CHECK-FIXES: static constexpr auto v13 = 1LU;
104 static_assert(is_same<decltype(v13), const unsigned long>::value, "");
105 static_assert(v13 == 1, "");
106
107 static constexpr auto v14 = 1Lu;
108 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'Lu', which is not uppercase
109 // CHECK-MESSAGES-NEXT: static constexpr auto v14 = 1Lu;
110 // CHECK-MESSAGES-NEXT: ^~~
111 // CHECK-MESSAGES-NEXT: {{^ *}}LU{{$}}
112 // CHECK-FIXES: static constexpr auto v14 = 1LU;
113 static_assert(is_same<decltype(v14), const unsigned long>::value, "");
114 static_assert(v14 == 1, "");
115
116 static constexpr auto v15 = 1lU;
117 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'lU', which is not uppercase
118 // CHECK-MESSAGES-NEXT: static constexpr auto v15 = 1lU;
119 // CHECK-MESSAGES-NEXT: ^~~
120 // CHECK-MESSAGES-NEXT: {{^ *}}LU{{$}}
121 // CHECK-FIXES: static constexpr auto v15 = 1LU;
122 static_assert(is_same<decltype(v15), const unsigned long>::value, "");
123 static_assert(v15 == 1, "");
124
125 static constexpr auto v16 = 1LU; // OK.
126 static_assert(is_same<decltype(v16), const unsigned long>::value, "");
127 static_assert(v16 == 1, "");
128
129 // Unsigned Long Long
130
131 static constexpr auto v17 = 1ull;
132 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'ull', which is not uppercase
133 // CHECK-MESSAGES-NEXT: static constexpr auto v17 = 1ull;
134 // CHECK-MESSAGES-NEXT: ^~~~
135 // CHECK-MESSAGES-NEXT: {{^ *}}ULL{{$}}
136 // CHECK-FIXES: static constexpr auto v17 = 1ULL;
137 static_assert(is_same<decltype(v17), const unsigned long long>::value, "");
138 static_assert(v17 == 1, "");
139
140 static constexpr auto v18 = 1uLL;
141 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'uLL', which is not uppercase
142 // CHECK-MESSAGES-NEXT: static constexpr auto v18 = 1uLL;
143 // CHECK-MESSAGES-NEXT: ^~~~
144 // CHECK-MESSAGES-NEXT: {{^ *}}ULL{{$}}
145 // CHECK-FIXES: static constexpr auto v18 = 1ULL;
146 static_assert(is_same<decltype(v18), const unsigned long long>::value, "");
147 static_assert(v18 == 1, "");
148
149 static constexpr auto v19 = 1Ull;
150 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'Ull', which is not uppercase
151 // CHECK-MESSAGES-NEXT: static constexpr auto v19 = 1Ull;
152 // CHECK-MESSAGES-NEXT: ^~~~
153 // CHECK-MESSAGES-NEXT: {{^ *}}ULL{{$}}
154 // CHECK-FIXES: static constexpr auto v19 = 1ULL;
155 static_assert(is_same<decltype(v19), const unsigned long long>::value, "");
156 static_assert(v19 == 1, "");
157
158 static constexpr auto v20 = 1ULL; // OK.
159 static_assert(is_same<decltype(v20), const unsigned long long>::value, "");
160 static_assert(v20 == 1, "");
161
162 // Long Long Unsigned
163
164 static constexpr auto v21 = 1llu;
165 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'llu', which is not uppercase
166 // CHECK-MESSAGES-NEXT: static constexpr auto v21 = 1llu;
167 // CHECK-MESSAGES-NEXT: ^~~~
168 // CHECK-MESSAGES-NEXT: {{^ *}}LLU{{$}}
169 // CHECK-FIXES: static constexpr auto v21 = 1LLU;
170 static_assert(is_same<decltype(v21), const unsigned long long>::value, "");
171 static_assert(v21 == 1, "");
172
173 static constexpr auto v22 = 1LLu;
174 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'LLu', which is not uppercase
175 // CHECK-MESSAGES-NEXT: static constexpr auto v22 = 1LLu;
176 // CHECK-MESSAGES-NEXT: ^~~~
177 // CHECK-MESSAGES-NEXT: {{^ *}}LLU{{$}}
178 // CHECK-FIXES: static constexpr auto v22 = 1LLU;
179 static_assert(is_same<decltype(v22), const unsigned long long>::value, "");
180 static_assert(v22 == 1, "");
181
182 static constexpr auto v23 = 1llU;
183 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'llU', which is not uppercase
184 // CHECK-MESSAGES-NEXT: static constexpr auto v23 = 1llU;
185 // CHECK-MESSAGES-NEXT: ^~~~
186 // CHECK-MESSAGES-NEXT: {{^ *}}LLU{{$}}
187 // CHECK-FIXES: static constexpr auto v23 = 1LLU;
188 static_assert(is_same<decltype(v23), const unsigned long long>::value, "");
189 static_assert(v23 == 1, "");
190
191 static constexpr auto v24 = 1LLU; // OK.
192 static_assert(is_same<decltype(v24), const unsigned long long>::value, "");
193 static_assert(v24 == 1, "");
194 }
195
integer_complex_suffix()196 void integer_complex_suffix() {
197 // _Complex, I
198
199 static constexpr auto v25 = 1i;
200 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'i', which is not uppercase
201 // CHECK-MESSAGES-NEXT: static constexpr auto v25 = 1i;
202 // CHECK-MESSAGES-NEXT: ^~
203 // CHECK-MESSAGES-NEXT: {{^ *}}I{{$}}
204 // CHECK-FIXES: static constexpr auto v25 = 1I;
205 static_assert(is_same<decltype(v25), const _Complex int>::value, "");
206 static_assert(v25 == 1I, "");
207
208 static constexpr auto v26 = 1I; // OK.
209 static_assert(is_same<decltype(v26), const _Complex int>::value, "");
210 static_assert(v26 == 1I, "");
211
212 // _Complex, J
213
214 static constexpr auto v27 = 1j;
215 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'j', which is not uppercase
216 // CHECK-MESSAGES-NEXT: static constexpr auto v27 = 1j;
217 // CHECK-MESSAGES-NEXT: ^~
218 // CHECK-MESSAGES-NEXT: {{^ *}}J{{$}}
219 // CHECK-FIXES: static constexpr auto v27 = 1J;
220 static_assert(is_same<decltype(v27), const _Complex int>::value, "");
221 static_assert(v27 == 1J, "");
222
223 static constexpr auto v28 = 1J; // OK.
224 static_assert(is_same<decltype(v28), const _Complex int>::value, "");
225 static_assert(v28 == 1J, "");
226 }
227
macros()228 void macros() {
229 #define PASSTHROUGH(X) X
230 static constexpr auto m0 = PASSTHROUGH(1u);
231 // CHECK-MESSAGES: :[[@LINE-1]]:42: warning: integer literal has suffix 'u', which is not uppercase
232 // CHECK-MESSAGES-NEXT: static constexpr auto m0 = PASSTHROUGH(1u);
233 // CHECK-MESSAGES-NEXT: ^~
234 // CHECK-MESSAGES-NEXT: {{^ *}}U{{$}}
235 // CHECK-FIXES: static constexpr auto m0 = PASSTHROUGH(1U);
236 static_assert(is_same<decltype(m0), const unsigned int>::value, "");
237 static_assert(m0 == 1, "");
238
239 // This location is inside a macro, no warning on that by default.
240 #define MACRO 1u
241 int foo = MACRO;
242 }
243
244 // Check that user-defined literals do not cause any diags.
245
246 unsigned long long int operator"" _ull(unsigned long long int);
user_defined_literals()247 void user_defined_literals() {
248 1_ull;
249 }
250
251 template <unsigned alignment>
template_test()252 void template_test() {
253 static_assert(alignment, "");
254 }
actual_template_test()255 void actual_template_test() {
256 template_test<4>();
257 }
258
259 const int table[6] = {};
read_test()260 void read_test() {
261 for (auto i : table) {
262 }
263 }
264
265 namespace {
266 enum a { b };
operator &(a,a)267 constexpr bool operator&(a, a) { return int(); }
268 template <a l>
c()269 void c() { l &a(); }
270 void d();
d()271 void d() { c<b>(); }
272 } // namespace
273