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