xref: /aosp_15_r20/external/clang/test/Sema/builtins.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wstrlcpy-strlcat-size -Wno-string-plus-int -triple=i686-apple-darwin9
2*67e74705SXin Li // This test needs to set the target because it uses __builtin_ia32_vec_ext_v4si
3*67e74705SXin Li 
test1(float a,int b)4*67e74705SXin Li int test1(float a, int b) {
5*67e74705SXin Li   return __builtin_isless(a, b); // expected-note {{declared here}}
6*67e74705SXin Li }
test2(int a,int b)7*67e74705SXin Li int test2(int a, int b) {
8*67e74705SXin Li   return __builtin_islessequal(a, b);  // expected-error {{floating point type}}
9*67e74705SXin Li }
10*67e74705SXin Li 
test3(double a,float b)11*67e74705SXin Li int test3(double a, float b) {
12*67e74705SXin Li   return __builtin_isless(a, b);
13*67e74705SXin Li }
test4(int * a,double b)14*67e74705SXin Li int test4(int* a, double b) {
15*67e74705SXin Li   return __builtin_islessequal(a, b);  // expected-error {{floating point type}}
16*67e74705SXin Li }
17*67e74705SXin Li 
test5(float a,long double b)18*67e74705SXin Li int test5(float a, long double b) {
19*67e74705SXin Li   return __builtin_isless(a, b, b);  // expected-error {{too many arguments}}
20*67e74705SXin Li }
test6(float a,long double b)21*67e74705SXin Li int test6(float a, long double b) {
22*67e74705SXin Li   return __builtin_islessequal(a);  // expected-error {{too few arguments}}
23*67e74705SXin Li }
24*67e74705SXin Li 
25*67e74705SXin Li 
26*67e74705SXin Li #define CFSTR __builtin___CFStringMakeConstantString
test7()27*67e74705SXin Li void test7() {
28*67e74705SXin Li   const void *X;
29*67e74705SXin Li   X = CFSTR("\242"); // expected-warning {{input conversion stopped}}
30*67e74705SXin Li   X = CFSTR("\0"); // no-warning
31*67e74705SXin Li   X = CFSTR(242); // expected-error {{CFString literal is not a string constant}} expected-warning {{incompatible integer to pointer conversion}}
32*67e74705SXin Li   X = CFSTR("foo", "bar"); // expected-error {{too many arguments to function call}}
33*67e74705SXin Li }
34*67e74705SXin Li 
35*67e74705SXin Li 
36*67e74705SXin Li // atomics.
37*67e74705SXin Li 
test9(short v)38*67e74705SXin Li void test9(short v) {
39*67e74705SXin Li   unsigned i, old;
40*67e74705SXin Li 
41*67e74705SXin Li   old = __sync_fetch_and_add();  // expected-error {{too few arguments to function call}}
42*67e74705SXin Li   old = __sync_fetch_and_add(&old);  // expected-error {{too few arguments to function call}}
43*67e74705SXin Li   old = __sync_fetch_and_add((unsigned*)0, 42i); // expected-warning {{imaginary constants are a GNU extension}}
44*67e74705SXin Li 
45*67e74705SXin Li   // PR7600: Pointers are implicitly casted to integers and back.
46*67e74705SXin Li   void *old_ptr = __sync_val_compare_and_swap((void**)0, 0, 0);
47*67e74705SXin Li 
48*67e74705SXin Li   // Ensure the return type is correct even when implicit casts are stripped
49*67e74705SXin Li   // away. This triggers an assertion while checking the comparison otherwise.
50*67e74705SXin Li   if (__sync_fetch_and_add(&old, 1) == 1) {
51*67e74705SXin Li   }
52*67e74705SXin Li }
53*67e74705SXin Li 
54*67e74705SXin Li // overloaded atomics should be declared only once.
test9_1(volatile int * ptr,int val)55*67e74705SXin Li void test9_1(volatile int* ptr, int val) {
56*67e74705SXin Li   __sync_fetch_and_add_4(ptr, val);
57*67e74705SXin Li }
test9_2(volatile int * ptr,int val)58*67e74705SXin Li void test9_2(volatile int* ptr, int val) {
59*67e74705SXin Li   __sync_fetch_and_add(ptr, val);
60*67e74705SXin Li }
test9_3(volatile int * ptr,int val)61*67e74705SXin Li void test9_3(volatile int* ptr, int val) {
62*67e74705SXin Li   __sync_fetch_and_add_4(ptr, val);
63*67e74705SXin Li   __sync_fetch_and_add(ptr, val);
64*67e74705SXin Li   __sync_fetch_and_add(ptr, val);
65*67e74705SXin Li   __sync_fetch_and_add_4(ptr, val);
66*67e74705SXin Li   __sync_fetch_and_add_4(ptr, val);
67*67e74705SXin Li }
68*67e74705SXin Li 
test9_4(volatile int * ptr,int val)69*67e74705SXin Li void test9_4(volatile int* ptr, int val) {
70*67e74705SXin Li   // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
71*67e74705SXin Li   __sync_fetch_and_nand(ptr, val);
72*67e74705SXin Li }
73*67e74705SXin Li 
74*67e74705SXin Li // rdar://7236819
75*67e74705SXin Li void test10(void) __attribute__((noreturn));
76*67e74705SXin Li 
test10(void)77*67e74705SXin Li void test10(void) {
78*67e74705SXin Li   __asm__("int3");
79*67e74705SXin Li   __builtin_unreachable();
80*67e74705SXin Li 
81*67e74705SXin Li   // No warning about falling off the end of a noreturn function.
82*67e74705SXin Li }
83*67e74705SXin Li 
test11(int X)84*67e74705SXin Li void test11(int X) {
85*67e74705SXin Li   switch (X) {
86*67e74705SXin Li   case __builtin_eh_return_data_regno(0):  // constant foldable.
87*67e74705SXin Li     break;
88*67e74705SXin Li   }
89*67e74705SXin Li 
90*67e74705SXin Li   __builtin_eh_return_data_regno(X);  // expected-error {{argument to '__builtin_eh_return_data_regno' must be a constant integer}}
91*67e74705SXin Li }
92*67e74705SXin Li 
93*67e74705SXin Li // PR5062
94*67e74705SXin Li void test12(void) __attribute__((__noreturn__));
test12(void)95*67e74705SXin Li void test12(void) {
96*67e74705SXin Li   __builtin_trap();  // no warning because trap is noreturn.
97*67e74705SXin Li }
98*67e74705SXin Li 
test_unknown_builtin(int a,int b)99*67e74705SXin Li void test_unknown_builtin(int a, int b) {
100*67e74705SXin Li   __builtin_isles(a, b); // expected-error{{use of unknown builtin}} \
101*67e74705SXin Li                          // expected-note{{did you mean '__builtin_isless'?}}
102*67e74705SXin Li }
103*67e74705SXin Li 
test13()104*67e74705SXin Li int test13() {
105*67e74705SXin Li   __builtin_eh_return(0, 0); // no warning, eh_return never returns.
106*67e74705SXin Li }
107*67e74705SXin Li 
108*67e74705SXin Li // <rdar://problem/8228293>
test14()109*67e74705SXin Li void test14() {
110*67e74705SXin Li   int old;
111*67e74705SXin Li   old = __sync_fetch_and_min((volatile int *)&old, 1);
112*67e74705SXin Li }
113*67e74705SXin Li 
114*67e74705SXin Li // <rdar://problem/8336581>
test15(const char * s)115*67e74705SXin Li void test15(const char *s) {
116*67e74705SXin Li   __builtin_printf("string is %s\n", s);
117*67e74705SXin Li }
118*67e74705SXin Li 
119*67e74705SXin Li // PR7885
test16()120*67e74705SXin Li int test16() {
121*67e74705SXin Li   return __builtin_constant_p() + // expected-error{{too few arguments}}
122*67e74705SXin Li          __builtin_constant_p(1, 2); // expected-error {{too many arguments}}
123*67e74705SXin Li }
124*67e74705SXin Li 
125*67e74705SXin Li const int test17_n = 0;
126*67e74705SXin Li const char test17_c[] = {1, 2, 3, 0};
127*67e74705SXin Li const char test17_d[] = {1, 2, 3, 4};
128*67e74705SXin Li typedef int __attribute__((vector_size(16))) IntVector;
129*67e74705SXin Li struct Aggregate { int n; char c; };
130*67e74705SXin Li enum Enum { EnumValue1, EnumValue2 };
131*67e74705SXin Li 
132*67e74705SXin Li typedef __typeof(sizeof(int)) size_t;
133*67e74705SXin Li size_t strlen(const char *);
134*67e74705SXin Li 
test17()135*67e74705SXin Li void test17() {
136*67e74705SXin Li #define ASSERT(...) { int arr[(__VA_ARGS__) ? 1 : -1]; }
137*67e74705SXin Li #define T(...) ASSERT(__builtin_constant_p(__VA_ARGS__))
138*67e74705SXin Li #define F(...) ASSERT(!__builtin_constant_p(__VA_ARGS__))
139*67e74705SXin Li 
140*67e74705SXin Li   // __builtin_constant_p returns 1 if the argument folds to:
141*67e74705SXin Li   //  - an arithmetic constant with value which is known at compile time
142*67e74705SXin Li   T(test17_n);
143*67e74705SXin Li   T(&test17_c[3] - test17_c);
144*67e74705SXin Li   T(3i + 5); // expected-warning {{imaginary constant}}
145*67e74705SXin Li   T(4.2 * 7.6);
146*67e74705SXin Li   T(EnumValue1);
147*67e74705SXin Li   T((enum Enum)(int)EnumValue2);
148*67e74705SXin Li 
149*67e74705SXin Li   //  - the address of the first character of a string literal, losslessly cast
150*67e74705SXin Li   //    to any type
151*67e74705SXin Li   T("string literal");
152*67e74705SXin Li   T((double*)"string literal");
153*67e74705SXin Li   T("string literal" + 0);
154*67e74705SXin Li   T((long)"string literal");
155*67e74705SXin Li 
156*67e74705SXin Li   // ... and otherwise returns 0.
157*67e74705SXin Li   F("string literal" + 1);
158*67e74705SXin Li   F(&test17_n);
159*67e74705SXin Li   F(test17_c);
160*67e74705SXin Li   F(&test17_c);
161*67e74705SXin Li   F(&test17_d);
162*67e74705SXin Li   F((struct Aggregate){0, 1});
163*67e74705SXin Li   F((IntVector){0, 1, 2, 3});
164*67e74705SXin Li 
165*67e74705SXin Li   // Ensure that a technique used in glibc is handled correctly.
166*67e74705SXin Li #define OPT(...) (__builtin_constant_p(__VA_ARGS__) && strlen(__VA_ARGS__) < 4)
167*67e74705SXin Li   // FIXME: These are incorrectly treated as ICEs because strlen is treated as
168*67e74705SXin Li   // a builtin.
169*67e74705SXin Li   ASSERT(OPT("abc"));
170*67e74705SXin Li   ASSERT(!OPT("abcd"));
171*67e74705SXin Li   // In these cases, the strlen is non-constant, but the __builtin_constant_p
172*67e74705SXin Li   // is 0: the array size is not an ICE but is foldable.
173*67e74705SXin Li   ASSERT(!OPT(test17_c));        // expected-warning {{folded}}
174*67e74705SXin Li   ASSERT(!OPT(&test17_c[0]));    // expected-warning {{folded}}
175*67e74705SXin Li   ASSERT(!OPT((char*)test17_c)); // expected-warning {{folded}}
176*67e74705SXin Li   ASSERT(!OPT(test17_d));        // expected-warning {{folded}}
177*67e74705SXin Li   ASSERT(!OPT(&test17_d[0]));    // expected-warning {{folded}}
178*67e74705SXin Li   ASSERT(!OPT((char*)test17_d)); // expected-warning {{folded}}
179*67e74705SXin Li 
180*67e74705SXin Li #undef OPT
181*67e74705SXin Li #undef T
182*67e74705SXin Li #undef F
183*67e74705SXin Li }
184*67e74705SXin Li 
test18()185*67e74705SXin Li void test18() {
186*67e74705SXin Li   char src[1024];
187*67e74705SXin Li   char dst[2048];
188*67e74705SXin Li   size_t result;
189*67e74705SXin Li   void *ptr;
190*67e74705SXin Li 
191*67e74705SXin Li   ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src), sizeof(dst));
192*67e74705SXin Li   result = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst));
193*67e74705SXin Li   result = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst));
194*67e74705SXin Li 
195*67e74705SXin Li   ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src));      // expected-error {{too few arguments to function call}}
196*67e74705SXin Li   ptr = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst)); // expected-warning {{incompatible integer to pointer conversion}}
197*67e74705SXin Li   ptr = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst)); // expected-warning {{incompatible integer to pointer conversion}}
198*67e74705SXin Li }
199*67e74705SXin Li 
no_ms_builtins()200*67e74705SXin Li void no_ms_builtins() {
201*67e74705SXin Li   __assume(1); // expected-warning {{implicit declaration}}
202*67e74705SXin Li   __noop(1); // expected-warning {{implicit declaration}}
203*67e74705SXin Li   __debugbreak(); // expected-warning {{implicit declaration}}
204*67e74705SXin Li }
205*67e74705SXin Li 
unavailable()206*67e74705SXin Li void unavailable() {
207*67e74705SXin Li   __builtin_operator_new(0); // expected-error {{'__builtin_operator_new' is only available in C++}}
208*67e74705SXin Li   __builtin_operator_delete(0); // expected-error {{'__builtin_operator_delete' is only available in C++}}
209*67e74705SXin Li }
210*67e74705SXin Li 
211*67e74705SXin Li // rdar://18259539
212*67e74705SXin Li size_t strlcpy(char * restrict dst, const char * restrict src, size_t size);
213*67e74705SXin Li size_t strlcat(char * restrict dst, const char * restrict src, size_t size);
214*67e74705SXin Li 
Test19(void)215*67e74705SXin Li void Test19(void)
216*67e74705SXin Li {
217*67e74705SXin Li         static char b[40];
218*67e74705SXin Li         static char buf[20];
219*67e74705SXin Li 
220*67e74705SXin Li         strlcpy(buf, b, sizeof(b)); // expected-warning {{size argument in 'strlcpy' call appears to be size of the source; expected the size of the destination}} \\
221*67e74705SXin Li                                     // expected-note {{change size argument to be the size of the destination}}
222*67e74705SXin Li         __builtin___strlcpy_chk(buf, b, sizeof(b), __builtin_object_size(buf, 0)); // expected-warning {{size argument in '__builtin___strlcpy_chk' call appears to be size of the source; expected the size of the destination}} \
223*67e74705SXin Li                                     // expected-note {{change size argument to be the size of the destination}} \
224*67e74705SXin Li 				    // expected-warning {{'__builtin___strlcpy_chk' will always overflow destination buffer}}
225*67e74705SXin Li 
226*67e74705SXin Li         strlcat(buf, b, sizeof(b)); // expected-warning {{size argument in 'strlcat' call appears to be size of the source; expected the size of the destination}} \
227*67e74705SXin Li                                     // expected-note {{change size argument to be the size of the destination}}
228*67e74705SXin Li 
229*67e74705SXin Li         __builtin___strlcat_chk(buf, b, sizeof(b), __builtin_object_size(buf, 0)); // expected-warning {{size argument in '__builtin___strlcat_chk' call appears to be size of the source; expected the size of the destination}} \
230*67e74705SXin Li                                                                                    // expected-note {{change size argument to be the size of the destination}} \
231*67e74705SXin Li 				                                                   // expected-warning {{'__builtin___strlcat_chk' will always overflow destination buffer}}
232*67e74705SXin Li }
233*67e74705SXin Li 
234*67e74705SXin Li // rdar://11076881
Test20(char * p,const char * in,unsigned n)235*67e74705SXin Li char * Test20(char *p, const char *in, unsigned n)
236*67e74705SXin Li {
237*67e74705SXin Li     static char buf[10];
238*67e74705SXin Li 
239*67e74705SXin Li     __builtin___memcpy_chk (&buf[6], in, 5, __builtin_object_size (&buf[6], 0)); // expected-warning {{'__builtin___memcpy_chk' will always overflow destination buffer}}
240*67e74705SXin Li 
241*67e74705SXin Li     __builtin___memcpy_chk (p, "abcde", n, __builtin_object_size (p, 0));
242*67e74705SXin Li 
243*67e74705SXin Li     __builtin___memcpy_chk (&buf[5], "abcde", 5, __builtin_object_size (&buf[5], 0));
244*67e74705SXin Li 
245*67e74705SXin Li     __builtin___memcpy_chk (&buf[5], "abcde", n, __builtin_object_size (&buf[5], 0));
246*67e74705SXin Li 
247*67e74705SXin Li     __builtin___memcpy_chk (&buf[6], "abcde", 5, __builtin_object_size (&buf[6], 0)); // expected-warning {{'__builtin___memcpy_chk' will always overflow destination buffer}}
248*67e74705SXin Li 
249*67e74705SXin Li     return buf;
250*67e74705SXin Li }
251