[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / builtins.c
blobb669ee68cdd9540a66bb1429120c838aa2ba53ec
1 // RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wstrlcpy-strlcat-size -Wno-string-plus-int -Wno-bit-int-extension -triple=i686-apple-darwin9
2 // This test needs to set the target because it uses __builtin_ia32_vec_ext_v4si
4 int test1(float a, int b) {
5 return __builtin_isless(a, b);
7 int test2(int a, int b) {
8 return __builtin_islessequal(a, b); // expected-error {{floating point type}}
11 int test3(double a, float b) {
12 return __builtin_isless(a, b);
14 int test4(int* a, double b) {
15 return __builtin_islessequal(a, b); // expected-error {{floating point type}}
18 int test5(float a, long double b) {
19 return __builtin_isless(a, b, b); // expected-error {{too many arguments}}
21 int test6(float a, long double b) {
22 return __builtin_islessequal(a); // expected-error {{too few arguments}}
26 #define CFSTR __builtin___CFStringMakeConstantString
27 void test7(void) {
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-error {{incompatible integer to pointer conversion}}
32 X = CFSTR("foo", "bar"); // expected-error {{too many arguments to function call}}
36 // atomics.
38 void test9(short v) {
39 unsigned i, old;
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 C2y extension}}
45 // PR7600: Pointers are implicitly casted to integers and back.
46 void *old_ptr = __sync_val_compare_and_swap((void**)0, 0, 0);
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) {
54 // overloaded atomics should be declared only once.
55 void test9_1(volatile int* ptr, int val) {
56 __sync_fetch_and_add_4(ptr, val);
58 void test9_2(volatile int* ptr, int val) {
59 __sync_fetch_and_add(ptr, 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);
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);
74 void test10(void) __attribute__((noreturn));
76 void test10(void) {
77 __asm__("int3");
78 __builtin_unreachable();
80 // No warning about falling off the end of a noreturn function.
83 void test11(int X) {
84 switch (X) {
85 case __builtin_eh_return_data_regno(0): // constant foldable.
86 break;
89 __builtin_eh_return_data_regno(X); // expected-error {{argument to '__builtin_eh_return_data_regno' must be a constant integer}}
92 // PR5062
93 void test12(void) __attribute__((__noreturn__));
94 void test12(void) {
95 __builtin_trap(); // no warning because trap is noreturn.
98 void test_unknown_builtin(int a, int b) {
99 __builtin_isles(a, b); // expected-error{{use of unknown builtin}}
102 int test13(void) {
103 __builtin_eh_return(0, 0); // no warning, eh_return never returns.
106 void test14(void) {
107 int old;
108 old = __sync_fetch_and_min((volatile int *)&old, 1);
111 void test15(const char *s) {
112 __builtin_printf("string is %s\n", s);
115 // PR7885
116 int test16(void) {
117 return __builtin_constant_p() + // expected-error{{too few arguments}}
118 __builtin_constant_p(1, 2); // expected-error {{too many arguments}}
121 // __builtin_constant_p cannot resolve non-constants as a file scoped array.
122 int expr;
123 char y[__builtin_constant_p(expr) ? -1 : 1]; // no warning, the builtin is false.
125 // no warning, the builtin is false.
126 struct foo { int a; };
127 struct foo x = (struct foo) { __builtin_constant_p(42) ? 37 : 927 };
129 const int test17_n = 0;
130 const char test17_c[] = {1, 2, 3, 0};
131 const char test17_d[] = {1, 2, 3, 4}; // Like test17_c but not NUL-terminated.
132 typedef int __attribute__((vector_size(16))) IntVector;
133 struct Aggregate { int n; char c; };
134 enum Enum { EnumValue1, EnumValue2 };
136 typedef __typeof(sizeof(int)) size_t;
137 size_t strlen(const char *);
139 void test17(void) {
140 #define ASSERT(...) { enum { folded = (__VA_ARGS__) }; int arr[folded ? 1 : -1]; }
141 #define T(...) ASSERT(__builtin_constant_p(__VA_ARGS__))
142 #define F(...) ASSERT(!__builtin_constant_p(__VA_ARGS__))
144 // __builtin_constant_p returns 1 if the argument folds to:
145 // - an arithmetic constant with value which is known at compile time
146 T(test17_n);
147 T(&test17_c[3] - test17_c);
148 T(3i + 5); // expected-warning {{imaginary constant}}
149 T(4.2 * 7.6);
150 T(EnumValue1);
151 T((enum Enum)(int)EnumValue2);
153 // - the address of the first character of a string literal, losslessly cast
154 // to any type
155 T("string literal");
156 T((double*)"string literal");
157 T("string literal" + 0);
158 T((long)"string literal");
160 // ... and otherwise returns 0.
161 F("string literal" + 1);
162 F(&test17_n);
163 F(test17_c);
164 F(&test17_c);
165 F(&test17_d);
166 F((struct Aggregate){0, 1});
167 F((IntVector){0, 1, 2, 3});
168 F(test17);
170 // Ensure that a technique used in glibc is handled correctly.
171 #define OPT(...) (__builtin_constant_p(__VA_ARGS__) && strlen(__VA_ARGS__) < 4)
172 // FIXME: These are incorrectly treated as ICEs because strlen is treated as
173 // a builtin.
174 ASSERT(OPT("abc")); // expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
175 ASSERT(!OPT("abcd")); // expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
176 // In these cases, the strlen is non-constant, but the __builtin_constant_p
177 // is 0: the array size is not an ICE but is foldable.
178 ASSERT(!OPT(test17_c));
179 ASSERT(!OPT(&test17_c[0]));
180 ASSERT(!OPT((char*)test17_c));
181 // NOTE: test17_d is not NUL-termintated, so calling strlen on it is UB.
182 ASSERT(!OPT(test17_d)); // expected-warning {{folding}}
183 ASSERT(!OPT(&test17_d[0])); // expected-warning {{folding}}
184 ASSERT(!OPT((char*)test17_d)); // expected-warning {{folding}}
186 #undef OPT
187 #undef T
188 #undef F
191 void test18(void) {
192 char src[1024];
193 char dst[2048];
194 size_t result;
195 void *ptr;
197 ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src), sizeof(dst));
198 result = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst));
199 result = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst));
201 ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src)); // expected-error {{too few arguments to function call}}
202 ptr = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst)); // expected-error {{incompatible integer to pointer conversion}}
203 ptr = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst)); // expected-error {{incompatible integer to pointer conversion}}
206 void no_ms_builtins(void) {
207 __assume(1); // expected-error {{call to undeclared function '__assume'; ISO C99 and later do not support implicit function declarations}}
208 __noop(1); // expected-error {{call to undeclared function '__noop'; ISO C99 and later do not support implicit function declarations}}
209 __debugbreak(); // expected-error {{call to undeclared function '__debugbreak'; ISO C99 and later do not support implicit function declarations}}
212 void unavailable(void) {
213 __builtin_operator_new(0); // expected-error {{'__builtin_operator_new' is only available in C++}}
214 __builtin_operator_delete(0); // expected-error {{'__builtin_operator_delete' is only available in C++}}
217 size_t strlcpy(char * restrict dst, const char * restrict src, size_t size);
218 size_t strlcat(char * restrict dst, const char * restrict src, size_t size);
220 void Test19(void)
222 static char b[40];
223 static char buf[20];
225 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}} \\
226 // expected-note {{change size argument to be the size of the destination}}
227 __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}} \
228 // expected-note {{change size argument to be the size of the destination}} \
229 // expected-warning {{'strlcpy' will always overflow; destination buffer has size 20, but size argument is 40}}
231 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}} \
232 // expected-note {{change size argument to be the size of the destination}}
234 __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}} \
235 // expected-note {{change size argument to be the size of the destination}} \
236 // expected-warning {{'strlcat' will always overflow; destination buffer has size 20, but size argument is 40}}
239 char * Test20(char *p, const char *in, unsigned n)
241 static char buf[10];
243 __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}}
245 __builtin___memcpy_chk (p, "abcde", n, __builtin_object_size (p, 0));
247 __builtin___memcpy_chk (&buf[5], "abcde", 5, __builtin_object_size (&buf[5], 0));
249 __builtin___memcpy_chk (&buf[5], "abcde", n, __builtin_object_size (&buf[5], 0));
251 __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}}
253 return buf;
256 typedef void (fn_t)(int);
258 void test_builtin_launder(char *p, void *vp, const void *cvp,
259 const volatile int *ip, float *restrict fp,
260 fn_t *fn) {
261 __builtin_launder(); // expected-error {{too few arguments to function call, expected 1, have 0}}
262 __builtin_launder(p, p); // expected-error {{too many arguments to function call, expected 1, have 2}}
263 int x;
264 __builtin_launder(x); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
265 char *d = __builtin_launder(p);
266 __builtin_launder(vp); // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}
267 __builtin_launder(cvp); // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}
268 const volatile int *id = __builtin_launder(ip);
269 int *id2 = __builtin_launder(ip); // expected-warning {{discards qualifiers}}
270 float *fd = __builtin_launder(fp);
271 __builtin_launder(fn); // expected-error {{function pointer argument to '__builtin_launder' is not allowed}}
274 void test21(const int *ptr) {
275 __sync_fetch_and_add(ptr, 1); // expected-error{{address argument to atomic builtin cannot be const-qualified ('const int *' invalid)}}
276 __atomic_fetch_add(ptr, 1, 0); // expected-error {{address argument to atomic operation must be a pointer to non-const type ('const int *' invalid)}}
279 void test_ei_i42i(_BitInt(42) *ptr, int value) {
280 __sync_fetch_and_add(ptr, value); // expected-error {{atomic memory operand must have a power-of-two size}}
281 // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
282 __sync_nand_and_fetch(ptr, value); // expected-error {{atomic memory operand must have a power-of-two size}}
284 __atomic_fetch_add(ptr, 1, 0); // expected-error {{argument to atomic builtin of type '_BitInt' is not supported}}
287 void test_ei_i64i(_BitInt(64) *ptr, int value) {
288 __sync_fetch_and_add(ptr, value); // expect success
289 // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
290 __sync_nand_and_fetch(ptr, value); // expect success
292 __atomic_fetch_add(ptr, 1, 0); // expected-error {{argument to atomic builtin of type '_BitInt' is not supported}}
295 void test_ei_ii42(int *ptr, _BitInt(42) value) {
296 __sync_fetch_and_add(ptr, value); // expect success
297 // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
298 __sync_nand_and_fetch(ptr, value); // expect success
301 void test_ei_ii64(int *ptr, _BitInt(64) value) {
302 __sync_fetch_and_add(ptr, value); // expect success
303 // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
304 __sync_nand_and_fetch(ptr, value); // expect success
307 void test_ei_i42i42(_BitInt(42) *ptr, _BitInt(42) value) {
308 __sync_fetch_and_add(ptr, value); // expected-error {{atomic memory operand must have a power-of-two size}}
309 // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
310 __sync_nand_and_fetch(ptr, value); // expected-error {{atomic memory operand must have a power-of-two size}}
313 void test_ei_i64i64(_BitInt(64) *ptr, _BitInt(64) value) {
314 __sync_fetch_and_add(ptr, value); // expect success
315 // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
316 __sync_nand_and_fetch(ptr, value); // expect success
319 void test22(void) {
320 (void)__builtin_signbit(); // expected-error{{too few arguments to function call, expected 1, have 0}}
321 (void)__builtin_signbit(1.0, 2.0, 3.0); // expected-error{{too many arguments to function call, expected 1, have 3}}
322 (void)__builtin_signbit(1); // expected-error {{floating point classification requires argument of floating point type (passed in 'int')}}
323 (void)__builtin_signbit(1.0);
324 (void)__builtin_signbit(1.0f);
325 (void)__builtin_signbit(1.0L);
327 (void)__builtin_signbitf(); // expected-error{{too few arguments to function call, expected 1, have 0}}
328 (void)__builtin_signbitf(1.0, 2.0, 3.0); // expected-error{{too many arguments to function call, expected 1, have 3}}
329 (void)__builtin_signbitf(1);
330 (void)__builtin_signbitf(1.0);
331 (void)__builtin_signbitf(1.0f);
332 (void)__builtin_signbitf(1.0L);
334 (void)__builtin_signbitl(); // expected-error{{too few arguments to function call, expected 1, have 0}}
335 (void)__builtin_signbitl(1.0, 2.0, 3.0); // expected-error{{too many arguments to function call, expected 1, have 3}}
336 (void)__builtin_signbitl(1);
337 (void)__builtin_signbitl(1.0);
338 (void)__builtin_signbitl(1.0f);
339 (void)__builtin_signbitl(1.0L);
342 #define memcpy(x,y,z) __builtin___memcpy_chk(x,y,z, __builtin_object_size(x,0))
343 #define my_memcpy(x,y,z) __builtin___memcpy_chk(x,y,z, __builtin_object_size(x,0))
345 void test23(void) {
346 char src[1024];
347 char buf[10];
348 memcpy(buf, src, 11); // expected-warning{{'memcpy' will always overflow; destination buffer has size 10, but size argument is 11}}
349 my_memcpy(buf, src, 11); // expected-warning{{'memcpy' will always overflow; destination buffer has size 10, but size argument is 11}}
352 // Test that __builtin_is_constant_evaluated() is not allowed in C
353 int test_cxx_builtin(void) {
354 // expected-error@+1 {{use of unknown builtin '__builtin_is_constant_evaluated'}}
355 return __builtin_is_constant_evaluated();
358 void test_builtin_complex(void) {
359 __builtin_complex(); // expected-error {{too few}}
360 __builtin_complex(1); // expected-error {{too few}}
361 __builtin_complex(1, 2, 3); // expected-error {{too many}}
363 _Static_assert(_Generic(__builtin_complex(1.0f, 2.0f), _Complex float: 1, default: 0), "");
364 _Static_assert(_Generic(__builtin_complex(1.0, 2.0), _Complex double: 1, default: 0), "");
365 _Static_assert(_Generic(__builtin_complex(1.0l, 2.0l), _Complex long double: 1, default: 0), "");
367 __builtin_complex(1, 2); // expected-error {{argument type 'int' is not a real floating point type}}
368 __builtin_complex(1, 2.0); // expected-error {{argument type 'int' is not a real floating point type}}
369 __builtin_complex(1.0, 2); // expected-error {{argument type 'int' is not a real floating point type}}
371 __builtin_complex(1.0, 2.0f); // expected-error {{arguments are of different types ('double' vs 'float')}}
372 __builtin_complex(1.0f, 2.0); // expected-error {{arguments are of different types ('float' vs 'double')}}
375 _Complex double builtin_complex_static_init = __builtin_complex(1.0, 2.0);
377 int test_is_fpclass(float x, int mask) {
378 int x1 = __builtin_isfpclass(x, 1024); // expected-error {{argument value 1024 is outside the valid range [0, 1023]}}
379 int x2 = __builtin_isfpclass(3, 3); // expected-error{{floating point classification requires argument of floating point type (passed in 'int')}}
380 int x3 = __builtin_isfpclass(x, 3, x); // expected-error{{too many arguments to function call, expected 2, have 3}}
381 int x4 = __builtin_isfpclass(x); // expected-error{{too few arguments to function call, expected 2, have 1}}
382 int x5 = __builtin_isfpclass(x, mask); // expected-error{{argument to '__builtin_isfpclass' must be a constant integer}}
383 int x6 = __builtin_isfpclass(x, -1); // expected-error{{argument value -1 is outside the valid range [0, 1023]}}
384 float _Complex c = x;
385 int x7 = __builtin_isfpclass(c, 3); // expected-error{{floating point classification requires argument of floating point type (passed in '_Complex float')}}