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 const int test17_n = 0;
126 const char test17_c[] = {1, 2, 3, 0};
127 const char test17_d[] = {1, 2, 3, 4};
128 typedef int __attribute__((vector_size(16))) IntVector;
129 struct Aggregate { int n; char c; };
130 enum Enum { EnumValue1, EnumValue2 };
131
132 typedef __typeof(sizeof(int)) size_t;
133 size_t strlen(const char *);
134
test17()135 void test17() {
136 #define ASSERT(...) { int arr[(__VA_ARGS__) ? 1 : -1]; }
137 #define T(...) ASSERT(__builtin_constant_p(__VA_ARGS__))
138 #define F(...) ASSERT(!__builtin_constant_p(__VA_ARGS__))
139
140 // __builtin_constant_p returns 1 if the argument folds to:
141 // - an arithmetic constant with value which is known at compile time
142 T(test17_n);
143 T(&test17_c[3] - test17_c);
144 T(3i + 5); // expected-warning {{imaginary constant}}
145 T(4.2 * 7.6);
146 T(EnumValue1);
147 T((enum Enum)(int)EnumValue2);
148
149 // - the address of the first character of a string literal, losslessly cast
150 // to any type
151 T("string literal");
152 T((double*)"string literal");
153 T("string literal" + 0);
154 T((long)"string literal");
155
156 // ... and otherwise returns 0.
157 F("string literal" + 1);
158 F(&test17_n);
159 F(test17_c);
160 F(&test17_c);
161 F(&test17_d);
162 F((struct Aggregate){0, 1});
163 F((IntVector){0, 1, 2, 3});
164
165 // Ensure that a technique used in glibc is handled correctly.
166 #define OPT(...) (__builtin_constant_p(__VA_ARGS__) && strlen(__VA_ARGS__) < 4)
167 // FIXME: These are incorrectly treated as ICEs because strlen is treated as
168 // a builtin.
169 ASSERT(OPT("abc"));
170 ASSERT(!OPT("abcd"));
171 // In these cases, the strlen is non-constant, but the __builtin_constant_p
172 // is 0: the array size is not an ICE but is foldable.
173 ASSERT(!OPT(test17_c)); // expected-warning {{folded}}
174 ASSERT(!OPT(&test17_c[0])); // expected-warning {{folded}}
175 ASSERT(!OPT((char*)test17_c)); // expected-warning {{folded}}
176 ASSERT(!OPT(test17_d)); // expected-warning {{folded}}
177 ASSERT(!OPT(&test17_d[0])); // expected-warning {{folded}}
178 ASSERT(!OPT((char*)test17_d)); // expected-warning {{folded}}
179
180 #undef OPT
181 #undef T
182 #undef F
183 }
184
test18()185 void test18() {
186 char src[1024];
187 char dst[2048];
188 size_t result;
189 void *ptr;
190
191 ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src), sizeof(dst));
192 result = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst));
193 result = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst));
194
195 ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src)); // expected-error {{too few arguments to function call}}
196 ptr = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst)); // expected-warning {{incompatible integer to pointer conversion}}
197 ptr = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst)); // expected-warning {{incompatible integer to pointer conversion}}
198 }
199
no_ms_builtins()200 void no_ms_builtins() {
201 __assume(1); // expected-warning {{implicit declaration}}
202 __noop(1); // expected-warning {{implicit declaration}}
203 __debugbreak(); // expected-warning {{implicit declaration}}
204 }
205
unavailable()206 void unavailable() {
207 __builtin_operator_new(0); // expected-error {{'__builtin_operator_new' is only available in C++}}
208 __builtin_operator_delete(0); // expected-error {{'__builtin_operator_delete' is only available in C++}}
209 }
210
211 // rdar://18259539
212 size_t strlcpy(char * restrict dst, const char * restrict src, size_t size);
213 size_t strlcat(char * restrict dst, const char * restrict src, size_t size);
214
Test19(void)215 void Test19(void)
216 {
217 static char b[40];
218 static char buf[20];
219
220 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 // expected-note {{change size argument to be the size of the destination}}
222 __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 // expected-note {{change size argument to be the size of the destination}} \
224 // expected-warning {{'__builtin___strlcpy_chk' will always overflow destination buffer}}
225
226 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 // expected-note {{change size argument to be the size of the destination}}
228
229 __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 // expected-note {{change size argument to be the size of the destination}} \
231 // expected-warning {{'__builtin___strlcat_chk' will always overflow destination buffer}}
232 }
233
234 // rdar://11076881
Test20(char * p,const char * in,unsigned n)235 char * Test20(char *p, const char *in, unsigned n)
236 {
237 static char buf[10];
238
239 __builtin___memcpy_chk (&buf[6], in, 5, __builtin_object_size (&buf[6], 0)); // expected-warning {{'__builtin___memcpy_chk' will always overflow destination buffer}}
240
241 __builtin___memcpy_chk (p, "abcde", n, __builtin_object_size (p, 0));
242
243 __builtin___memcpy_chk (&buf[5], "abcde", 5, __builtin_object_size (&buf[5], 0));
244
245 __builtin___memcpy_chk (&buf[5], "abcde", n, __builtin_object_size (&buf[5], 0));
246
247 __builtin___memcpy_chk (&buf[6], "abcde", 5, __builtin_object_size (&buf[6], 0)); // expected-warning {{'__builtin___memcpy_chk' will always overflow destination buffer}}
248
249 return buf;
250 }
251