xref: /aosp_15_r20/external/clang/test/Sema/PR16678.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -DX32TYPE=ULONG -triple powerpc-unknown-linux-gnu -std=c89 -x c %s -verify
2*67e74705SXin Li // RUN: %clang_cc1 -DX32TYPE=ULONG -triple powerpc-unknown-linux-gnu -std=iso9899:199409 -x c %s -verify
3*67e74705SXin Li // RUN: %clang_cc1 -DX32TYPE=ULONG -triple powerpc-unknown-linux-gnu -std=c++98 -x c++ %s -verify
4*67e74705SXin Li // RUN: %clang_cc1 -DX32TYPE=LLONG -triple powerpc-unknown-linux-gnu -std=c99 -x c %s -verify
5*67e74705SXin Li // RUN: %clang_cc1 -DX32TYPE=LLONG -triple powerpc-unknown-linux-gnu -std=c11 -x c %s -verify
6*67e74705SXin Li // RUN: %clang_cc1 -DX32TYPE=LLONG -triple powerpc-unknown-linux-gnu -std=c++11 -x c++ %s -verify
7*67e74705SXin Li // RUN: %clang_cc1 -DX32TYPE=LLONG -triple powerpc-unknown-linux-gnu -std=c++1y -x c++ %s -verify
8*67e74705SXin Li // RUN: %clang_cc1 -DX32TYPE=LLONG -triple powerpc-unknown-linux-gnu -std=c++1z -x c++ %s -verify
9*67e74705SXin Li // RUN: %clang_cc1 -DX64TYPE=ULONG -triple powerpc64-unknown-linux-gnu -std=c89 -x c %s -verify
10*67e74705SXin Li // RUN: %clang_cc1 -DX64TYPE=ULONG -triple powerpc64-unknown-linux-gnu -std=iso9899:199409 -x c %s -verify
11*67e74705SXin Li // RUN: %clang_cc1 -DX64TYPE=ULONG -triple powerpc64-unknown-linux-gnu -std=c++98 -x c++ %s -verify
12*67e74705SXin Li // RUN: %clang_cc1 -DX64TYPE=ULLONG -triple powerpc64-unknown-linux-gnu -std=c99 -x c %s -verify
13*67e74705SXin Li // RUN: %clang_cc1 -DX64TYPE=ULLONG -triple powerpc64-unknown-linux-gnu -std=c11 -x c %s -verify
14*67e74705SXin Li // RUN: %clang_cc1 -DX64TYPE=ULLONG -triple powerpc64-unknown-linux-gnu -std=c++11 -x c++ %s -verify
15*67e74705SXin Li // RUN: %clang_cc1 -DX64TYPE=ULLONG -triple powerpc64-unknown-linux-gnu -std=c++1y -x c++ %s -verify
16*67e74705SXin Li // RUN: %clang_cc1 -DX64TYPE=ULLONG -triple powerpc64-unknown-linux-gnu -std=c++1z -x c++ %s -verify
17*67e74705SXin Li 
18*67e74705SXin Li #ifdef X64TYPE
19*67e74705SXin Li #define X32TYPE long
20*67e74705SXin Li #endif
21*67e74705SXin Li 
22*67e74705SXin Li #define IS_ULONG_ULONG 1
23*67e74705SXin Li #define IS_ULONG2(X) IS_ULONG_##X
24*67e74705SXin Li #define IS_ULONG(X) IS_ULONG2(X)
25*67e74705SXin Li 
26*67e74705SXin Li #if !defined(X64TYPE) && !IS_ULONG(X32TYPE)
27*67e74705SXin Li // expected-no-diagnostics
28*67e74705SXin Li #endif
29*67e74705SXin Li 
30*67e74705SXin Li typedef unsigned long ULONG;
31*67e74705SXin Li typedef long long LLONG;
32*67e74705SXin Li typedef unsigned long long ULLONG;
33*67e74705SXin Li 
34*67e74705SXin Li 
35*67e74705SXin Li /******************************************************************************
36*67e74705SXin Li  * Test 2^31 as a decimal literal with no suffix and with the "l" and "L" cases.
37*67e74705SXin Li  ******************************************************************************/
38*67e74705SXin Li extern X32TYPE x32;
39*67e74705SXin Li extern __typeof__(2147483648) x32;
40*67e74705SXin Li extern __typeof__(2147483648l) x32;
41*67e74705SXin Li extern __typeof__(2147483648L) x32;
42*67e74705SXin Li 
43*67e74705SXin Li #if IS_ULONG(X32TYPE)
44*67e74705SXin Li #if !__cplusplus
45*67e74705SXin Li 
46*67e74705SXin Li /******************************************************************************
47*67e74705SXin Li  * Under pre-C99 ISO C, unsigned long is attempted for decimal integer literals
48*67e74705SXin Li  * that do not have a suffix containing "u" or "U" if the literal does not fit
49*67e74705SXin Li  * within the range of int or long. See 6.1.3.2 paragraph 5.
50*67e74705SXin Li  ******************************************************************************/
51*67e74705SXin Li // expected-warning@39 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will have type 'long long' in C99 onwards}}
52*67e74705SXin Li // expected-warning@40 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will have type 'long long' in C99 onwards}}
53*67e74705SXin Li // expected-warning@41 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will have type 'long long' in C99 onwards}}
54*67e74705SXin Li #else
55*67e74705SXin Li 
56*67e74705SXin Li /******************************************************************************
57*67e74705SXin Li  * Under pre-C++11 ISO C++, the same holds if the literal contains an "l" or "L"
58*67e74705SXin Li  * in its suffix; otherwise, the behavior is undefined. See 2.13.1 [lex.icon]
59*67e74705SXin Li  * paragraph 2.
60*67e74705SXin Li  ******************************************************************************/
61*67e74705SXin Li // expected-warning@39 {{integer literal is too large to be represented in type 'long' and is subject to undefined behavior under C++98, interpreting as 'unsigned long'; this literal will have type 'long long' in C++11 onwards}}
62*67e74705SXin Li // expected-warning@40 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C++98; this literal will have type 'long long' in C++11 onwards}}
63*67e74705SXin Li // expected-warning@41 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C++98; this literal will have type 'long long' in C++11 onwards}}
64*67e74705SXin Li #endif
65*67e74705SXin Li #endif
66*67e74705SXin Li 
67*67e74705SXin Li 
68*67e74705SXin Li #ifdef X64TYPE
69*67e74705SXin Li 
70*67e74705SXin Li /******************************************************************************
71*67e74705SXin Li  * Test 2^63 as a decimal literal with no suffix and with the "l" and "L" cases.
72*67e74705SXin Li  ******************************************************************************/
73*67e74705SXin Li extern X64TYPE x64;
74*67e74705SXin Li extern __typeof__(9223372036854775808) x64;
75*67e74705SXin Li extern __typeof__(9223372036854775808l) x64;
76*67e74705SXin Li extern __typeof__(9223372036854775808L) x64;
77*67e74705SXin Li 
78*67e74705SXin Li #if IS_ULONG(X64TYPE)
79*67e74705SXin Li 
80*67e74705SXin Li #if !__cplusplus
81*67e74705SXin Li 
82*67e74705SXin Li /******************************************************************************
83*67e74705SXin Li  * Under pre-C99 ISO C, unsigned long is attempted for decimal integer literals
84*67e74705SXin Li  * that do not have a suffix containing "u" or "U" if the literal does not fit
85*67e74705SXin Li  * within the range of int or long. See 6.1.3.2 paragraph 5.
86*67e74705SXin Li  ******************************************************************************/
87*67e74705SXin Li // expected-warning@74 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will be ill-formed in C99 onwards}}
88*67e74705SXin Li // expected-warning@75 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will be ill-formed in C99 onwards}}
89*67e74705SXin Li // expected-warning@76 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will be ill-formed in C99 onwards}}
90*67e74705SXin Li #else
91*67e74705SXin Li 
92*67e74705SXin Li /******************************************************************************
93*67e74705SXin Li  * Under pre-C++11 ISO C++, the same holds if the literal contains an "l" or "L"
94*67e74705SXin Li  * in its suffix; otherwise, the behavior is undefined. See 2.13.1 [lex.icon]
95*67e74705SXin Li  * paragraph 2.
96*67e74705SXin Li  ******************************************************************************/
97*67e74705SXin Li // expected-warning@74 {{integer literal is too large to be represented in type 'long' and is subject to undefined behavior under C++98, interpreting as 'unsigned long'; this literal will be ill-formed in C++11 onwards}}
98*67e74705SXin Li // expected-warning@75 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C++98; this literal will be ill-formed in C++11 onwards}}
99*67e74705SXin Li // expected-warning@76 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C++98; this literal will be ill-formed in C++11 onwards}}
100*67e74705SXin Li #endif
101*67e74705SXin Li #else
102*67e74705SXin Li 
103*67e74705SXin Li /******************************************************************************
104*67e74705SXin Li  * The status quo in C99/C++11-and-later modes for the literals in question is
105*67e74705SXin Li  * to interpret them as unsigned as an extension.
106*67e74705SXin Li  ******************************************************************************/
107*67e74705SXin Li // expected-warning@74 {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}}
108*67e74705SXin Li // expected-warning@75 {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}}
109*67e74705SXin Li // expected-warning@76 {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}}
110*67e74705SXin Li #endif
111*67e74705SXin Li #endif
112*67e74705SXin Li 
113*67e74705SXin Li 
114*67e74705SXin Li /******************************************************************************
115*67e74705SXin Li  * Test preprocessor arithmetic with 2^31 as a decimal literal with no suffix
116*67e74705SXin Li  * and with the "l" and "L" cases.
117*67e74705SXin Li  ******************************************************************************/
118*67e74705SXin Li #if !IS_ULONG(X32TYPE)
119*67e74705SXin Li 
120*67e74705SXin Li /******************************************************************************
121*67e74705SXin Li  * If the literal is signed without need for the modified range of the signed
122*67e74705SXin Li  * integer types within the controlling constant expression for conditional
123*67e74705SXin Li  * inclusion, then it will also be signed with said modified range.
124*67e74705SXin Li  ******************************************************************************/
125*67e74705SXin Li #define EXPR(X) ((X - X) - 1 < 0)
126*67e74705SXin Li #else
127*67e74705SXin Li 
128*67e74705SXin Li /******************************************************************************
129*67e74705SXin Li  * Strictly speaking, in pre-C99/C++11 ISO C/C++, the preprocessor arithmetic is
130*67e74705SXin Li  * evaluated with the range of long/unsigned long; however, both Clang and GCC
131*67e74705SXin Li  * evaluate using 64-bits even when long/unsigned long are 32-bits outside of
132*67e74705SXin Li  * preprocessing.
133*67e74705SXin Li  *
134*67e74705SXin Li  * If the range used becomes 32-bits, then this test will enforce the treatment
135*67e74705SXin Li  * as unsigned of the literals in question.
136*67e74705SXin Li  *
137*67e74705SXin Li  * Note:
138*67e74705SXin Li  * Under pre-C99/C++11 ISO C/C++, whether the interpretation of the literal is
139*67e74705SXin Li  * affected by the modified range of the signed and unsigned integer types
140*67e74705SXin Li  * within the controlling constant expression for conditional inclusion is
141*67e74705SXin Li  * unclear.
142*67e74705SXin Li  ******************************************************************************/
143*67e74705SXin Li #define PP_LONG_MAX ((0ul - 1ul) >> 1)
144*67e74705SXin Li #define EXPR(X)                                                                \
145*67e74705SXin Li   (PP_LONG_MAX >= 0x80000000 || (X - X) - 1 > 0) // either 2^31 fits into a
146*67e74705SXin Li                                                  // preprocessor "long" or the
147*67e74705SXin Li                                                  // literals in question are
148*67e74705SXin Li                                                  // unsigned
149*67e74705SXin Li #endif
150*67e74705SXin Li 
151*67e74705SXin Li #if !(EXPR(2147483648) && EXPR(2147483648l) && EXPR(2147483648L))
152*67e74705SXin Li #error Unexpected signedness or conversion behavior
153*67e74705SXin Li #endif
154