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