Clang Project

clang_source_code/test/Sema/builtins.c
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
4int test1(float a, int b) {
5  return __builtin_isless(a, b); // expected-note {{declared here}}
6}
7int test2(int a, int b) {
8  return __builtin_islessequal(a, b);  // expected-error {{floating point type}}
9}
10
11int test3(double a, float b) {
12  return __builtin_isless(a, b);
13}
14int test4(int* a, double b) {
15  return __builtin_islessequal(a, b);  // expected-error {{floating point type}}
16}
17
18int test5(float a, long double b) {
19  return __builtin_isless(a, b, b);  // expected-error {{too many arguments}}
20}
21int test6(float a, long double b) {
22  return __builtin_islessequal(a);  // expected-error {{too few arguments}}
23}
24
25
26#define CFSTR __builtin___CFStringMakeConstantString
27void 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
38void 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.
55void test9_1(volatile int* ptr, int val) {
56  __sync_fetch_and_add_4(ptr, val);
57}
58void test9_2(volatile int* ptr, int val) {
59  __sync_fetch_and_add(ptr, val);
60}
61void 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
69void 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
75void test10(void) __attribute__((noreturn));
76
77void test10(void) {
78  __asm__("int3");
79  __builtin_unreachable();
80
81  // No warning about falling off the end of a noreturn function.
82}
83
84void 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
94void test12(void) __attribute__((__noreturn__));
95void test12(void) {
96  __builtin_trap();  // no warning because trap is noreturn.
97}
98
99void 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
104int test13() {
105  __builtin_eh_return(0, 0); // no warning, eh_return never returns.
106}
107
108// <rdar://problem/8228293>
109void test14() {
110  int old;
111  old = __sync_fetch_and_min((volatile int *)&old, 1);
112}
113
114// <rdar://problem/8336581>
115void test15(const char *s) {
116  __builtin_printf("string is %s\n", s);
117}
118
119// PR7885
120int 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.
126int expr;
127char y[__builtin_constant_p(expr) ? -1 : 1]; // no warning, the builtin is false.
128
129// no warning, the builtin is false.
130struct foo { int a; };
131struct foo x = (struct foo) { __builtin_constant_p(42) ? 37 : 927 };
132
133const int test17_n = 0;
134const char test17_c[] = {1, 2, 3, 0};
135const char test17_d[] = {1, 2, 3, 4};
136typedef int __attribute__((vector_size(16))) IntVector;
137struct Aggregate { int n; char c; };
138enum Enum { EnumValue1, EnumValue2 };
139
140typedef __typeof(sizeof(int)) size_t;
141size_t strlen(const char *);
142
143void test17() {
144#define ASSERT(...) { int arr[(__VA_ARGS__) ? 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 {{folded}}
183  ASSERT(!OPT(&test17_c[0]));    // expected-warning {{folded}}
184  ASSERT(!OPT((char*)test17_c)); // expected-warning {{folded}}
185  ASSERT(!OPT(test17_d));        // expected-warning {{folded}}
186  ASSERT(!OPT(&test17_d[0]));    // expected-warning {{folded}}
187  ASSERT(!OPT((char*)test17_d)); // expected-warning {{folded}}
188
189#undef OPT
190#undef T
191#undef F
192}
193
194void 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
209void 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
215void 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
221size_t strlcpy(char * restrict dst, const char * restrict src, size_t size);
222size_t strlcat(char * restrict dst, const char * restrict src, size_t size);
223
224void 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
244char * 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
261typedef void (fn_t)(int);
262
263void 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
279void 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
284void test22(void) {
285  (void)__builtin_signbit(); // expected-error{{too few arguments to function call, expected 1, have 0}}
286  (void)__builtin_signbit(1.0, 2.0, 3.0); // expected-error{{too many arguments to function call, expected 1, have 3}}
287  (void)__builtin_signbit(1); // expected-error {{floating point classification requires argument of floating point type (passed in 'int')}}
288  (void)__builtin_signbit(1.0);
289  (void)__builtin_signbit(1.0f);
290  (void)__builtin_signbit(1.0L);
291
292  (void)__builtin_signbitf(); // expected-error{{too few arguments to function call, expected 1, have 0}}
293  (void)__builtin_signbitf(1.0, 2.0, 3.0); // expected-error{{too many arguments to function call, expected 1, have 3}}
294  (void)__builtin_signbitf(1);
295  (void)__builtin_signbitf(1.0);
296  (void)__builtin_signbitf(1.0f);
297  (void)__builtin_signbitf(1.0L);
298
299  (void)__builtin_signbitl(); // expected-error{{too few arguments to function call, expected 1, have 0}}
300  (void)__builtin_signbitl(1.0, 2.0, 3.0); // expected-error{{too many arguments to function call, expected 1, have 3}}
301  (void)__builtin_signbitl(1);
302  (void)__builtin_signbitl(1.0);
303  (void)__builtin_signbitl(1.0f);
304  (void)__builtin_signbitl(1.0L);
305}
306
307// rdar://43909200
308#define memcpy(x,y,z) __builtin___memcpy_chk(x,y,z, __builtin_object_size(x,0))
309#define my_memcpy(x,y,z) __builtin___memcpy_chk(x,y,z, __builtin_object_size(x,0))
310
311void test23() {
312  char src[1024];
313  char buf[10];
314  memcpy(buf, src, 11); // expected-warning{{'memcpy' will always overflow; destination buffer has size 10, but size argument is 11}}
315  my_memcpy(buf, src, 11); // expected-warning{{'memcpy' will always overflow; destination buffer has size 10, but size argument is 11}}
316}
317