19081d3d8SHaojian Wu // RUN: %check_clang_tidy %s readability-uppercase-literal-suffix %t -- -- -I %clang_tidy_headers
289a1d03eSRichard // RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
39081d3d8SHaojian Wu // RUN: clang-tidy %t.cpp -checks='-*,readability-uppercase-literal-suffix' -fix -- -I %clang_tidy_headers
49081d3d8SHaojian Wu // RUN: clang-tidy %t.cpp -checks='-*,readability-uppercase-literal-suffix' -warnings-as-errors='-*,readability-uppercase-literal-suffix' -- -I %clang_tidy_headers
589a1d03eSRichard 
69081d3d8SHaojian Wu #include "integral_constant.h"
789a1d03eSRichard 
integer_suffix()889a1d03eSRichard void integer_suffix() {
989a1d03eSRichard   static constexpr auto v0 = __LINE__; // synthetic
1089a1d03eSRichard   static_assert(v0 == 9 || v0 == 5, "");
1189a1d03eSRichard 
1289a1d03eSRichard   static constexpr auto v1 = __cplusplus; // synthetic, long
1389a1d03eSRichard 
1489a1d03eSRichard   static constexpr auto v2 = 1; // no literal
1589a1d03eSRichard   static_assert(is_same<decltype(v2), const int>::value, "");
1689a1d03eSRichard   static_assert(v2 == 1, "");
1789a1d03eSRichard 
1889a1d03eSRichard   // Unsigned
1989a1d03eSRichard 
2089a1d03eSRichard   static constexpr auto v3 = 1u;
2189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: integer literal has suffix 'u', which is not uppercase
2289a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v3 = 1u;
2389a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~
24*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: U{{$}}
2589a1d03eSRichard   // CHECK-FIXES: static constexpr auto v3 = 1U;
2689a1d03eSRichard   static_assert(is_same<decltype(v3), const unsigned int>::value, "");
2789a1d03eSRichard   static_assert(v3 == 1, "");
2889a1d03eSRichard 
2989a1d03eSRichard   static constexpr auto v4 = 1U; // OK.
3089a1d03eSRichard   static_assert(is_same<decltype(v4), const unsigned int>::value, "");
3189a1d03eSRichard   static_assert(v4 == 1, "");
3289a1d03eSRichard 
3389a1d03eSRichard   // Long
3489a1d03eSRichard 
3589a1d03eSRichard   static constexpr auto v5 = 1l;
3689a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: integer literal has suffix 'l', which is not uppercase
3789a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v5 = 1l;
3889a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~
39*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: L{{$}}
4089a1d03eSRichard   // CHECK-FIXES: static constexpr auto v5 = 1L;
4189a1d03eSRichard   static_assert(is_same<decltype(v5), const long>::value, "");
4289a1d03eSRichard   static_assert(v5 == 1, "");
4389a1d03eSRichard 
4489a1d03eSRichard   static constexpr auto v6 = 1L; // OK.
4589a1d03eSRichard   static_assert(is_same<decltype(v6), const long>::value, "");
4689a1d03eSRichard   static_assert(v6 == 1, "");
4789a1d03eSRichard 
4889a1d03eSRichard   // Long Long
4989a1d03eSRichard 
5089a1d03eSRichard   static constexpr auto v7 = 1ll;
5189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: integer literal has suffix 'll', which is not uppercase
5289a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v7 = 1ll;
5389a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~
54*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: LL{{$}}
5589a1d03eSRichard   // CHECK-FIXES: static constexpr auto v7 = 1LL;
5689a1d03eSRichard   static_assert(is_same<decltype(v7), const long long>::value, "");
5789a1d03eSRichard   static_assert(v7 == 1, "");
5889a1d03eSRichard 
5989a1d03eSRichard   static constexpr auto v8 = 1LL; // OK.
6089a1d03eSRichard   static_assert(is_same<decltype(v8), const long long>::value, "");
6189a1d03eSRichard   static_assert(v8 == 1, "");
6289a1d03eSRichard 
6389a1d03eSRichard   // Unsigned Long
6489a1d03eSRichard 
6589a1d03eSRichard   static constexpr auto v9 = 1ul;
6689a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: integer literal has suffix 'ul', which is not uppercase
6789a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v9 = 1ul;
6889a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~
69*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: UL{{$}}
7089a1d03eSRichard   // CHECK-FIXES: static constexpr auto v9 = 1UL;
7189a1d03eSRichard   static_assert(is_same<decltype(v9), const unsigned long>::value, "");
7289a1d03eSRichard   static_assert(v9 == 1, "");
7389a1d03eSRichard 
7489a1d03eSRichard   static constexpr auto v10 = 1uL;
7589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'uL', which is not uppercase
7689a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v10 = 1uL;
7789a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~
78*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: UL{{$}}
7989a1d03eSRichard   // CHECK-FIXES: static constexpr auto v10 = 1UL;
8089a1d03eSRichard   static_assert(is_same<decltype(v10), const unsigned long>::value, "");
8189a1d03eSRichard   static_assert(v10 == 1, "");
8289a1d03eSRichard 
8389a1d03eSRichard   static constexpr auto v11 = 1Ul;
8489a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'Ul', which is not uppercase
8589a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v11 = 1Ul;
8689a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~
87*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: UL{{$}}
8889a1d03eSRichard   // CHECK-FIXES: static constexpr auto v11 = 1UL;
8989a1d03eSRichard   static_assert(is_same<decltype(v11), const unsigned long>::value, "");
9089a1d03eSRichard   static_assert(v11 == 1, "");
9189a1d03eSRichard 
9289a1d03eSRichard   static constexpr auto v12 = 1UL; // OK.
9389a1d03eSRichard   static_assert(is_same<decltype(v12), const unsigned long>::value, "");
9489a1d03eSRichard   static_assert(v12 == 1, "");
9589a1d03eSRichard 
9689a1d03eSRichard   // Long Unsigned
9789a1d03eSRichard 
9889a1d03eSRichard   static constexpr auto v13 = 1lu;
9989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'lu', which is not uppercase
10089a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v13 = 1lu;
10189a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~
102*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: LU{{$}}
10389a1d03eSRichard   // CHECK-FIXES: static constexpr auto v13 = 1LU;
10489a1d03eSRichard   static_assert(is_same<decltype(v13), const unsigned long>::value, "");
10589a1d03eSRichard   static_assert(v13 == 1, "");
10689a1d03eSRichard 
10789a1d03eSRichard   static constexpr auto v14 = 1Lu;
10889a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'Lu', which is not uppercase
10989a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v14 = 1Lu;
11089a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~
111*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: LU{{$}}
11289a1d03eSRichard   // CHECK-FIXES: static constexpr auto v14 = 1LU;
11389a1d03eSRichard   static_assert(is_same<decltype(v14), const unsigned long>::value, "");
11489a1d03eSRichard   static_assert(v14 == 1, "");
11589a1d03eSRichard 
11689a1d03eSRichard   static constexpr auto v15 = 1lU;
11789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'lU', which is not uppercase
11889a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v15 = 1lU;
11989a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~
120*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: LU{{$}}
12189a1d03eSRichard   // CHECK-FIXES: static constexpr auto v15 = 1LU;
12289a1d03eSRichard   static_assert(is_same<decltype(v15), const unsigned long>::value, "");
12389a1d03eSRichard   static_assert(v15 == 1, "");
12489a1d03eSRichard 
12589a1d03eSRichard   static constexpr auto v16 = 1LU; // OK.
12689a1d03eSRichard   static_assert(is_same<decltype(v16), const unsigned long>::value, "");
12789a1d03eSRichard   static_assert(v16 == 1, "");
12889a1d03eSRichard 
12989a1d03eSRichard   // Unsigned Long Long
13089a1d03eSRichard 
13189a1d03eSRichard   static constexpr auto v17 = 1ull;
13289a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'ull', which is not uppercase
13389a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v17 = 1ull;
13489a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~~
135*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: ULL{{$}}
13689a1d03eSRichard   // CHECK-FIXES: static constexpr auto v17 = 1ULL;
13789a1d03eSRichard   static_assert(is_same<decltype(v17), const unsigned long long>::value, "");
13889a1d03eSRichard   static_assert(v17 == 1, "");
13989a1d03eSRichard 
14089a1d03eSRichard   static constexpr auto v18 = 1uLL;
14189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'uLL', which is not uppercase
14289a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v18 = 1uLL;
14389a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~~
144*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: ULL{{$}}
14589a1d03eSRichard   // CHECK-FIXES: static constexpr auto v18 = 1ULL;
14689a1d03eSRichard   static_assert(is_same<decltype(v18), const unsigned long long>::value, "");
14789a1d03eSRichard   static_assert(v18 == 1, "");
14889a1d03eSRichard 
14989a1d03eSRichard   static constexpr auto v19 = 1Ull;
15089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'Ull', which is not uppercase
15189a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v19 = 1Ull;
15289a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~~
153*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: ULL{{$}}
15489a1d03eSRichard   // CHECK-FIXES: static constexpr auto v19 = 1ULL;
15589a1d03eSRichard   static_assert(is_same<decltype(v19), const unsigned long long>::value, "");
15689a1d03eSRichard   static_assert(v19 == 1, "");
15789a1d03eSRichard 
15889a1d03eSRichard   static constexpr auto v20 = 1ULL; // OK.
15989a1d03eSRichard   static_assert(is_same<decltype(v20), const unsigned long long>::value, "");
16089a1d03eSRichard   static_assert(v20 == 1, "");
16189a1d03eSRichard 
16289a1d03eSRichard   // Long Long Unsigned
16389a1d03eSRichard 
16489a1d03eSRichard   static constexpr auto v21 = 1llu;
16589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'llu', which is not uppercase
16689a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v21 = 1llu;
16789a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~~
168*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: LLU{{$}}
16989a1d03eSRichard   // CHECK-FIXES: static constexpr auto v21 = 1LLU;
17089a1d03eSRichard   static_assert(is_same<decltype(v21), const unsigned long long>::value, "");
17189a1d03eSRichard   static_assert(v21 == 1, "");
17289a1d03eSRichard 
17389a1d03eSRichard   static constexpr auto v22 = 1LLu;
17489a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'LLu', which is not uppercase
17589a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v22 = 1LLu;
17689a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~~
177*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: LLU{{$}}
17889a1d03eSRichard   // CHECK-FIXES: static constexpr auto v22 = 1LLU;
17989a1d03eSRichard   static_assert(is_same<decltype(v22), const unsigned long long>::value, "");
18089a1d03eSRichard   static_assert(v22 == 1, "");
18189a1d03eSRichard 
18289a1d03eSRichard   static constexpr auto v23 = 1llU;
18389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'llU', which is not uppercase
18489a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v23 = 1llU;
18589a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~~~
186*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: LLU{{$}}
18789a1d03eSRichard   // CHECK-FIXES: static constexpr auto v23 = 1LLU;
18889a1d03eSRichard   static_assert(is_same<decltype(v23), const unsigned long long>::value, "");
18989a1d03eSRichard   static_assert(v23 == 1, "");
19089a1d03eSRichard 
19189a1d03eSRichard   static constexpr auto v24 = 1LLU; // OK.
19289a1d03eSRichard   static_assert(is_same<decltype(v24), const unsigned long long>::value, "");
19389a1d03eSRichard   static_assert(v24 == 1, "");
19489a1d03eSRichard }
19589a1d03eSRichard 
integer_complex_suffix()19689a1d03eSRichard void integer_complex_suffix() {
19789a1d03eSRichard   // _Complex, I
19889a1d03eSRichard 
19989a1d03eSRichard   static constexpr auto v25 = 1i;
20089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'i', which is not uppercase
20189a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v25 = 1i;
20289a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~
203*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: I{{$}}
20489a1d03eSRichard   // CHECK-FIXES: static constexpr auto v25 = 1I;
20589a1d03eSRichard   static_assert(is_same<decltype(v25), const _Complex int>::value, "");
20689a1d03eSRichard   static_assert(v25 == 1I, "");
20789a1d03eSRichard 
20889a1d03eSRichard   static constexpr auto v26 = 1I; // OK.
20989a1d03eSRichard   static_assert(is_same<decltype(v26), const _Complex int>::value, "");
21089a1d03eSRichard   static_assert(v26 == 1I, "");
21189a1d03eSRichard 
21289a1d03eSRichard   // _Complex, J
21389a1d03eSRichard 
21489a1d03eSRichard   static constexpr auto v27 = 1j;
21589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: integer literal has suffix 'j', which is not uppercase
21689a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto v27 = 1j;
21789a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~
218*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: J{{$}}
21989a1d03eSRichard   // CHECK-FIXES: static constexpr auto v27 = 1J;
22089a1d03eSRichard   static_assert(is_same<decltype(v27), const _Complex int>::value, "");
22189a1d03eSRichard   static_assert(v27 == 1J, "");
22289a1d03eSRichard 
22389a1d03eSRichard   static constexpr auto v28 = 1J; // OK.
22489a1d03eSRichard   static_assert(is_same<decltype(v28), const _Complex int>::value, "");
22589a1d03eSRichard   static_assert(v28 == 1J, "");
22689a1d03eSRichard }
22789a1d03eSRichard 
macros()22889a1d03eSRichard void macros() {
22989a1d03eSRichard #define PASSTHROUGH(X) X
23089a1d03eSRichard   static constexpr auto m0 = PASSTHROUGH(1u);
23189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:42: warning: integer literal has suffix 'u', which is not uppercase
23289a1d03eSRichard   // CHECK-MESSAGES-NEXT: static constexpr auto m0 = PASSTHROUGH(1u);
23389a1d03eSRichard   // CHECK-MESSAGES-NEXT: ^~
234*f63155aaSTimm Bäder   // CHECK-MESSAGES-NEXT: U{{$}}
23589a1d03eSRichard   // CHECK-FIXES: static constexpr auto m0 = PASSTHROUGH(1U);
23689a1d03eSRichard   static_assert(is_same<decltype(m0), const unsigned int>::value, "");
23789a1d03eSRichard   static_assert(m0 == 1, "");
23889a1d03eSRichard 
23989a1d03eSRichard   // This location is inside a macro, no warning on that by default.
24089a1d03eSRichard #define MACRO 1u
24189a1d03eSRichard   int foo = MACRO;
24289a1d03eSRichard }
24389a1d03eSRichard 
24489a1d03eSRichard // Check that user-defined literals do not cause any diags.
24589a1d03eSRichard 
24689a1d03eSRichard unsigned long long int operator"" _ull(unsigned long long int);
user_defined_literals()24789a1d03eSRichard void user_defined_literals() {
24889a1d03eSRichard   1_ull;
24989a1d03eSRichard }
25089a1d03eSRichard 
25189a1d03eSRichard template <unsigned alignment>
template_test()25289a1d03eSRichard void template_test() {
25389a1d03eSRichard   static_assert(alignment, "");
25489a1d03eSRichard }
actual_template_test()25589a1d03eSRichard void actual_template_test() {
25689a1d03eSRichard   template_test<4>();
25789a1d03eSRichard }
25889a1d03eSRichard 
25989a1d03eSRichard const int table[6] = {};
read_test()26089a1d03eSRichard void read_test() {
26189a1d03eSRichard   for (auto i : table) {
26289a1d03eSRichard   }
26389a1d03eSRichard }
26489a1d03eSRichard 
26589a1d03eSRichard namespace {
26689a1d03eSRichard enum a { b };
operator &(a,a)26789a1d03eSRichard constexpr bool operator&(a, a) { return int(); }
26889a1d03eSRichard template <a l>
c()26989a1d03eSRichard void c() { l &a(); }
27089a1d03eSRichard void d();
d()27189a1d03eSRichard void d() { c<b>(); }
27289a1d03eSRichard } // namespace
27389a1d03eSRichard 
27489a1d03eSRichard // Check that non-type template parameters do not cause any diags.
27589a1d03eSRichard // https://bugs.llvm.org/show_bug.cgi?id=51790
27689a1d03eSRichard template <int capacity>
27789a1d03eSRichard struct Vector {
27889a1d03eSRichard   static constexpr int kCapacity = capacity;
27989a1d03eSRichard };
28089a1d03eSRichard 
28189a1d03eSRichard template <int capacity>
28289a1d03eSRichard constexpr int Vector<capacity>::kCapacity;
28389a1d03eSRichard // CHECK-MESSAGES-NOT: :[[@LINE-1]]:22: warning: integer literal has suffix 'ity', which is not uppercase
28489a1d03eSRichard 
28589a1d03eSRichard template <int foo1u>
28689a1d03eSRichard struct Foo {
28789a1d03eSRichard   static constexpr int kFoo = foo1u;
28889a1d03eSRichard };
28989a1d03eSRichard 
29089a1d03eSRichard template <int foo1u>
29189a1d03eSRichard constexpr int Foo<foo1u>::kFoo;
29289a1d03eSRichard // CHECK-MESSAGES-NOT: :[[@LINE-1]]:19: warning: integer literal has suffix 'u', which is not uppercase
29389a1d03eSRichard 
29489a1d03eSRichard // The template needs to be instantiated for diagnostics to show up
test_non_type_template_parameter()29589a1d03eSRichard void test_non_type_template_parameter() {
29689a1d03eSRichard   int x = Vector<10>::kCapacity;
29789a1d03eSRichard   int f = Foo<10>::kFoo;
29889a1d03eSRichard }
299