1 // RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wstrlcpy-strlcat-size -Wno-string-plus-int -triple=i686-apple-darwin9
2 // This test needs to set the target because it uses __builtin_ia32_vec_ext_v4si
3 
test1(float a,int b)4 int test1(float a, int b) {
5   return __builtin_isless(a, b); // expected-note {{declared here}}
6 }
test2(int a,int b)7 int test2(int a, int b) {
8   return __builtin_islessequal(a, b);  // expected-error {{floating point type}}
9 }
10 
test3(double a,float b)11 int test3(double a, float b) {
12   return __builtin_isless(a, b);
13 }
test4(int * a,double b)14 int test4(int* a, double b) {
15   return __builtin_islessequal(a, b);  // expected-error {{floating point type}}
16 }
17 
test5(float a,long double b)18 int test5(float a, long double b) {
19   return __builtin_isless(a, b, b);  // expected-error {{too many arguments}}
20 }
test6(float a,long double b)21 int test6(float a, long double b) {
22   return __builtin_islessequal(a);  // expected-error {{too few arguments}}
23 }
24 
25 
26 #define CFSTR __builtin___CFStringMakeConstantString
test7()27 void test7() {
28   const void *X;
29   X = CFSTR("\242"); // expected-warning {{input conversion stopped}}
30   X = CFSTR("\0"); // no-warning
31   X = CFSTR(242); // expected-error {{CFString literal is not a string constant}} expected-warning {{incompatible integer to pointer conversion}}
32   X = CFSTR("foo", "bar"); // expected-error {{too many arguments to function call}}
33 }
34 
35 
36 // atomics.
37 
test9(short v)38 void test9(short v) {
39   unsigned i, old;
40 
41   old = __sync_fetch_and_add();  // expected-error {{too few arguments to function call}}
42   old = __sync_fetch_and_add(&old);  // expected-error {{too few arguments to function call}}
43   old = __sync_fetch_and_add((unsigned*)0, 42i); // expected-warning {{imaginary constants are a GNU extension}}
44 
45   // PR7600: Pointers are implicitly casted to integers and back.
46   void *old_ptr = __sync_val_compare_and_swap((void**)0, 0, 0);
47 
48   // Ensure the return type is correct even when implicit casts are stripped
49   // away. This triggers an assertion while checking the comparison otherwise.
50   if (__sync_fetch_and_add(&old, 1) == 1) {
51   }
52 }
53 
54 // overloaded atomics should be declared only once.
test9_1(volatile int * ptr,int val)55 void test9_1(volatile int* ptr, int val) {
56   __sync_fetch_and_add_4(ptr, val);
57 }
test9_2(volatile int * ptr,int val)58 void test9_2(volatile int* ptr, int val) {
59   __sync_fetch_and_add(ptr, val);
60 }
test9_3(volatile int * ptr,int val)61 void test9_3(volatile int* ptr, int val) {
62   __sync_fetch_and_add_4(ptr, val);
63   __sync_fetch_and_add(ptr, val);
64   __sync_fetch_and_add(ptr, val);
65   __sync_fetch_and_add_4(ptr, val);
66   __sync_fetch_and_add_4(ptr, val);
67 }
68 
test9_4(volatile int * ptr,int val)69 void test9_4(volatile int* ptr, int val) {
70   // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
71   __sync_fetch_and_nand(ptr, val);
72 }
73 
74 // rdar://7236819
75 void test10(void) __attribute__((noreturn));
76 
test10(void)77 void test10(void) {
78   __asm__("int3");
79   __builtin_unreachable();
80 
81   // No warning about falling off the end of a noreturn function.
82 }
83 
test11(int X)84 void test11(int X) {
85   switch (X) {
86   case __builtin_eh_return_data_regno(0):  // constant foldable.
87     break;
88   }
89 
90   __builtin_eh_return_data_regno(X);  // expected-error {{argument to '__builtin_eh_return_data_regno' must be a constant integer}}
91 }
92 
93 // PR5062
94 void test12(void) __attribute__((__noreturn__));
test12(void)95 void test12(void) {
96   __builtin_trap();  // no warning because trap is noreturn.
97 }
98 
test_unknown_builtin(int a,int b)99 void test_unknown_builtin(int a, int b) {
100   __builtin_isles(a, b); // expected-error{{use of unknown builtin}} \
101                          // expected-note{{did you mean '__builtin_isless'?}}
102 }
103 
test13()104 int test13() {
105   __builtin_eh_return(0, 0); // no warning, eh_return never returns.
106 }
107 
108 // <rdar://problem/8228293>
test14()109 void test14() {
110   int old;
111   old = __sync_fetch_and_min((volatile int *)&old, 1);
112 }
113 
114 // <rdar://problem/8336581>
test15(const char * s)115 void test15(const char *s) {
116   __builtin_printf("string is %s\n", s);
117 }
118 
119 // PR7885
test16()120 int test16() {
121   return __builtin_constant_p() + // expected-error{{too few arguments}}
122          __builtin_constant_p(1, 2); // expected-error {{too many arguments}}
123 }
124 
125 // __builtin_constant_p cannot resolve non-constants as a file scoped array.
126 int expr;
127 char y[__builtin_constant_p(expr) ? -1 : 1]; // no warning, the builtin is false.
128 
129 // no warning, the builtin is false.
130 struct foo { int a; };
131 struct foo x = (struct foo) { __builtin_constant_p(42) ? 37 : 927 };
132 
133 const int test17_n = 0;
134 const char test17_c[] = {1, 2, 3, 0};
135 const char test17_d[] = {1, 2, 3, 4};
136 typedef int __attribute__((vector_size(16))) IntVector;
137 struct Aggregate { int n; char c; };
138 enum Enum { EnumValue1, EnumValue2 };
139 
140 typedef __typeof(sizeof(int)) size_t;
141 size_t strlen(const char *);
142 
test17()143 void test17() {
144 #define ASSERT(...) { enum { folded = (__VA_ARGS__) }; int arr[folded ? 1 : -1]; }
145 #define T(...) ASSERT(__builtin_constant_p(__VA_ARGS__))
146 #define F(...) ASSERT(!__builtin_constant_p(__VA_ARGS__))
147 
148   // __builtin_constant_p returns 1 if the argument folds to:
149   //  - an arithmetic constant with value which is known at compile time
150   T(test17_n);
151   T(&test17_c[3] - test17_c);
152   T(3i + 5); // expected-warning {{imaginary constant}}
153   T(4.2 * 7.6);
154   T(EnumValue1);
155   T((enum Enum)(int)EnumValue2);
156 
157   //  - the address of the first character of a string literal, losslessly cast
158   //    to any type
159   T("string literal");
160   T((double*)"string literal");
161   T("string literal" + 0);
162   T((long)"string literal");
163 
164   // ... and otherwise returns 0.
165   F("string literal" + 1);
166   F(&test17_n);
167   F(test17_c);
168   F(&test17_c);
169   F(&test17_d);
170   F((struct Aggregate){0, 1});
171   F((IntVector){0, 1, 2, 3});
172   F(test17);
173 
174   // Ensure that a technique used in glibc is handled correctly.
175 #define OPT(...) (__builtin_constant_p(__VA_ARGS__) && strlen(__VA_ARGS__) < 4)
176   // FIXME: These are incorrectly treated as ICEs because strlen is treated as
177   // a builtin.
178   ASSERT(OPT("abc"));
179   ASSERT(!OPT("abcd"));
180   // In these cases, the strlen is non-constant, but the __builtin_constant_p
181   // is 0: the array size is not an ICE but is foldable.
182   ASSERT(!OPT(test17_c));        // expected-warning {{folding}}
183   ASSERT(!OPT(&test17_c[0]));    // expected-warning {{folding}}
184   ASSERT(!OPT((char*)test17_c)); // expected-warning {{folding}}
185   ASSERT(!OPT(test17_d));        // expected-warning {{folding}}
186   ASSERT(!OPT(&test17_d[0]));    // expected-warning {{folding}}
187   ASSERT(!OPT((char*)test17_d)); // expected-warning {{folding}}
188 
189 #undef OPT
190 #undef T
191 #undef F
192 }
193 
test18()194 void test18() {
195   char src[1024];
196   char dst[2048];
197   size_t result;
198   void *ptr;
199 
200   ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src), sizeof(dst));
201   result = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst));
202   result = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst));
203 
204   ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src));      // expected-error {{too few arguments to function call}}
205   ptr = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst)); // expected-warning {{incompatible integer to pointer conversion}}
206   ptr = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst)); // expected-warning {{incompatible integer to pointer conversion}}
207 }
208 
no_ms_builtins()209 void no_ms_builtins() {
210   __assume(1); // expected-warning {{implicit declaration}}
211   __noop(1); // expected-warning {{implicit declaration}}
212   __debugbreak(); // expected-warning {{implicit declaration}}
213 }
214 
unavailable()215 void unavailable() {
216   __builtin_operator_new(0); // expected-error {{'__builtin_operator_new' is only available in C++}}
217   __builtin_operator_delete(0); // expected-error {{'__builtin_operator_delete' is only available in C++}}
218 }
219 
220 // rdar://18259539
221 size_t strlcpy(char * restrict dst, const char * restrict src, size_t size);
222 size_t strlcat(char * restrict dst, const char * restrict src, size_t size);
223 
Test19(void)224 void Test19(void)
225 {
226         static char b[40];
227         static char buf[20];
228 
229         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}} \\
230                                     // expected-note {{change size argument to be the size of the destination}}
231         __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}} \
232                                     // expected-note {{change size argument to be the size of the destination}} \
233 				    // expected-warning {{'strlcpy' will always overflow; destination buffer has size 20, but size argument is 40}}
234 
235         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}} \
236                                     // expected-note {{change size argument to be the size of the destination}}
237 
238         __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}} \
239                                                                                    // expected-note {{change size argument to be the size of the destination}} \
240 				                                                   // expected-warning {{'strlcat' will always overflow; destination buffer has size 20, but size argument is 40}}
241 }
242 
243 // rdar://11076881
Test20(char * p,const char * in,unsigned n)244 char * Test20(char *p, const char *in, unsigned n)
245 {
246     static char buf[10];
247 
248     __builtin___memcpy_chk (&buf[6], in, 5, __builtin_object_size (&buf[6], 0)); // expected-warning {{'memcpy' will always overflow; destination buffer has size 4, but size argument is 5}}
249 
250     __builtin___memcpy_chk (p, "abcde", n, __builtin_object_size (p, 0));
251 
252     __builtin___memcpy_chk (&buf[5], "abcde", 5, __builtin_object_size (&buf[5], 0));
253 
254     __builtin___memcpy_chk (&buf[5], "abcde", n, __builtin_object_size (&buf[5], 0));
255 
256     __builtin___memcpy_chk (&buf[6], "abcde", 5, __builtin_object_size (&buf[6], 0)); // expected-warning {{'memcpy' will always overflow; destination buffer has size 4, but size argument is 5}}
257 
258     return buf;
259 }
260 
261 typedef void (fn_t)(int);
262 
test_builtin_launder(char * p,void * vp,const void * cvp,const volatile int * ip,float * restrict fp,fn_t * fn)263 void test_builtin_launder(char *p, void *vp, const void *cvp,
264                           const volatile int *ip, float *restrict fp,
265                           fn_t *fn) {
266   __builtin_launder(); // expected-error {{too few arguments to function call, expected 1, have 0}}
267   __builtin_launder(p, p); // expected-error {{too many arguments to function call, expected 1, have 2}}
268   int x;
269   __builtin_launder(x); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
270   char *d = __builtin_launder(p);
271   __builtin_launder(vp);  // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}
272   __builtin_launder(cvp); // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}
273   const volatile int *id = __builtin_launder(ip);
274   int *id2 = __builtin_launder(ip); // expected-warning {{discards qualifiers}}
275   float *fd = __builtin_launder(fp);
276   __builtin_launder(fn); // expected-error {{function pointer argument to '__builtin_launder' is not allowed}}
277 }
278 
test21(const int * ptr)279 void test21(const int *ptr) {
280   __sync_fetch_and_add(ptr, 1); // expected-error{{address argument to atomic builtin cannot be const-qualified ('const int *' invalid)}}
281   __atomic_fetch_add(ptr, 1, 0);  // expected-error {{address argument to atomic operation must be a pointer to non-const type ('const int *' invalid)}}
282 }
283 
284 void test_ei_i42i(_ExtInt(42) *ptr, int value) {
285   __sync_fetch_and_add(ptr, value); // expected-error {{Atomic memory operand must have a power-of-two size}}
286   // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
287   __sync_nand_and_fetch(ptr, value); // expected-error {{Atomic memory operand must have a power-of-two size}}
288 
289   __atomic_fetch_add(ptr, 1, 0); // expected-error {{argument to atomic builtin of type '_ExtInt' is not supported}}
290 }
291 
292 void test_ei_i64i(_ExtInt(64) *ptr, int value) {
293   __sync_fetch_and_add(ptr, value); // expect success
294   // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
295   __sync_nand_and_fetch(ptr, value); // expect success
296 
297   __atomic_fetch_add(ptr, 1, 0); // expected-error {{argument to atomic builtin of type '_ExtInt' is not supported}}
298 }
299 
300 void test_ei_ii42(int *ptr, _ExtInt(42) value) {
301   __sync_fetch_and_add(ptr, value); // expect success
302   // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
303   __sync_nand_and_fetch(ptr, value); // expect success
304 }
305 
306 void test_ei_ii64(int *ptr, _ExtInt(64) value) {
307   __sync_fetch_and_add(ptr, value); // expect success
308   // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
309   __sync_nand_and_fetch(ptr, value); // expect success
310 }
311 
312 void test_ei_i42i42(_ExtInt(42) *ptr, _ExtInt(42) value) {
313   __sync_fetch_and_add(ptr, value); // expected-error {{Atomic memory operand must have a power-of-two size}}
314   // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
315   __sync_nand_and_fetch(ptr, value); // expected-error {{Atomic memory operand must have a power-of-two size}}
316 }
317 
318 void test_ei_i64i64(_ExtInt(64) *ptr, _ExtInt(64) value) {
319   __sync_fetch_and_add(ptr, value); // expect success
320   // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
321   __sync_nand_and_fetch(ptr, value); // expect success
322 }
323 
test22(void)324 void test22(void) {
325   (void)__builtin_signbit(); // expected-error{{too few arguments to function call, expected 1, have 0}}
326   (void)__builtin_signbit(1.0, 2.0, 3.0); // expected-error{{too many arguments to function call, expected 1, have 3}}
327   (void)__builtin_signbit(1); // expected-error {{floating point classification requires argument of floating point type (passed in 'int')}}
328   (void)__builtin_signbit(1.0);
329   (void)__builtin_signbit(1.0f);
330   (void)__builtin_signbit(1.0L);
331 
332   (void)__builtin_signbitf(); // expected-error{{too few arguments to function call, expected 1, have 0}}
333   (void)__builtin_signbitf(1.0, 2.0, 3.0); // expected-error{{too many arguments to function call, expected 1, have 3}}
334   (void)__builtin_signbitf(1);
335   (void)__builtin_signbitf(1.0);
336   (void)__builtin_signbitf(1.0f);
337   (void)__builtin_signbitf(1.0L);
338 
339   (void)__builtin_signbitl(); // expected-error{{too few arguments to function call, expected 1, have 0}}
340   (void)__builtin_signbitl(1.0, 2.0, 3.0); // expected-error{{too many arguments to function call, expected 1, have 3}}
341   (void)__builtin_signbitl(1);
342   (void)__builtin_signbitl(1.0);
343   (void)__builtin_signbitl(1.0f);
344   (void)__builtin_signbitl(1.0L);
345 }
346 
347 // rdar://43909200
348 #define memcpy(x,y,z) __builtin___memcpy_chk(x,y,z, __builtin_object_size(x,0))
349 #define my_memcpy(x,y,z) __builtin___memcpy_chk(x,y,z, __builtin_object_size(x,0))
350 
test23()351 void test23() {
352   char src[1024];
353   char buf[10];
354   memcpy(buf, src, 11); // expected-warning{{'memcpy' will always overflow; destination buffer has size 10, but size argument is 11}}
355   my_memcpy(buf, src, 11); // expected-warning{{'memcpy' will always overflow; destination buffer has size 10, but size argument is 11}}
356 }
357 
358 // Test that __builtin_is_constant_evaluated() is not allowed in C
test_cxx_builtin()359 int test_cxx_builtin() {
360   // expected-error@+1 {{use of unknown builtin '__builtin_is_constant_evaluated'}}
361   return __builtin_is_constant_evaluated();
362 }
363 
test_builtin_complex()364 void test_builtin_complex() {
365   __builtin_complex(); // expected-error {{too few}}
366   __builtin_complex(1); // expected-error {{too few}}
367   __builtin_complex(1, 2, 3); // expected-error {{too many}}
368 
369   _Static_assert(_Generic(__builtin_complex(1.0f, 2.0f), _Complex float: 1, default: 0), "");
370   _Static_assert(_Generic(__builtin_complex(1.0, 2.0), _Complex double: 1, default: 0), "");
371   _Static_assert(_Generic(__builtin_complex(1.0l, 2.0l), _Complex long double: 1, default: 0), "");
372 
373   __builtin_complex(1, 2); // expected-error {{argument type 'int' is not a real floating point type}}
374   __builtin_complex(1, 2.0); // expected-error {{argument type 'int' is not a real floating point type}}
375   __builtin_complex(1.0, 2); // expected-error {{argument type 'int' is not a real floating point type}}
376 
377   __builtin_complex(1.0, 2.0f); // expected-error {{arguments are of different types ('double' vs 'float')}}
378   __builtin_complex(1.0f, 2.0); // expected-error {{arguments are of different types ('float' vs 'double')}}
379 }
380 
381 _Complex double builtin_complex_static_init = __builtin_complex(1.0, 2.0);
382