blob: 0e2925e0748daea41d72f32cdd202c5c03bd3294 [file] [log] [blame]
Fariborz Jahanianab4fe982014-09-12 18:44:36 +00001// RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wstrlcpy-strlcat-size -Wno-string-plus-int -triple=i686-apple-darwin9
Eli Friedmand042a962008-05-25 04:43:38 +00002// This test needs to set the target because it uses __builtin_ia32_vec_ext_v4si
Chris Lattner2da14fb2007-12-20 00:26:33 +00003
4int test1(float a, int b) {
Richard Smithf9b15102013-08-17 00:46:16 +00005 return __builtin_isless(a, b); // expected-note {{declared here}}
Chris Lattner2da14fb2007-12-20 00:26:33 +00006}
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
Chris Lattnerdc046542009-05-08 06:58:22 +000027void test7() {
Chris Lattnerd866c5a2009-12-30 22:10:22 +000028 const void *X;
Fariborz Jahanian56603ef2010-09-07 19:38:13 +000029 X = CFSTR("\242"); // expected-warning {{input conversion stopped}}
Ted Kremenek6cd01872011-03-15 21:18:52 +000030 X = CFSTR("\0"); // no-warning
Richard Trieu553b2b22011-12-15 00:38:15 +000031 X = CFSTR(242); // expected-error {{CFString literal is not a string constant}} expected-warning {{incompatible integer to pointer conversion}}
Chris Lattnerd866c5a2009-12-30 22:10:22 +000032 X = CFSTR("foo", "bar"); // expected-error {{too many arguments to function call}}
Chris Lattner2da14fb2007-12-20 00:26:33 +000033}
34
Chris Lattner4dd27102008-05-05 22:18:14 +000035
Chris Lattnerdc046542009-05-08 06:58:22 +000036// atomics.
37
Mike Stump753d1202009-07-22 00:43:08 +000038void test9(short v) {
Chris Lattnerdc046542009-05-08 06:58:22 +000039 unsigned i, old;
Gabor Greifde1f5892010-10-15 08:48:51 +000040
Chris Lattnerdc046542009-05-08 06:58:22 +000041 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}}
Eli Friedman8f88f062012-11-02 01:40:23 +000043 old = __sync_fetch_and_add((unsigned*)0, 42i); // expected-warning {{imaginary constants are a GNU extension}}
Chandler Carruth741e5ce2010-07-09 18:59:35 +000044
45 // PR7600: Pointers are implicitly casted to integers and back.
46 void *old_ptr = __sync_val_compare_and_swap((void**)0, 0, 0);
Chandler Carruthbc8cab12010-07-18 07:23:17 +000047
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 }
Chris Lattnerdc046542009-05-08 06:58:22 +000052}
Chris Lattnerbf206382009-09-21 03:09:59 +000053
Abramo Bagnara6cba23a2012-09-22 09:05:22 +000054// 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}
Chris Lattnerbf206382009-09-21 03:09:59 +000068
Hal Finkeld2208b52014-10-02 20:53:50 +000069void 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
Chris Lattnerbf206382009-09-21 03:09:59 +000074// rdar://7236819
75void test10(void) __attribute__((noreturn));
76
77void test10(void) {
Gabor Greifde1f5892010-10-15 08:48:51 +000078 __asm__("int3");
Chris Lattnerbf206382009-09-21 03:09:59 +000079 __builtin_unreachable();
Gabor Greifde1f5892010-10-15 08:48:51 +000080
Chris Lattnerbf206382009-09-21 03:09:59 +000081 // No warning about falling off the end of a noreturn function.
82}
Chris Lattnerd545ad12009-09-23 06:06:36 +000083
84void test11(int X) {
Gabor Greifde1f5892010-10-15 08:48:51 +000085 switch (X) {
Chris Lattnerd545ad12009-09-23 06:06:36 +000086 case __builtin_eh_return_data_regno(0): // constant foldable.
87 break;
88 }
89
Eric Christopher63448c32010-04-19 18:23:02 +000090 __builtin_eh_return_data_regno(X); // expected-error {{argument to '__builtin_eh_return_data_regno' must be a constant integer}}
Chris Lattnerd545ad12009-09-23 06:06:36 +000091}
92
Chris Lattnera26b4712009-09-26 21:16:00 +000093// PR5062
94void test12(void) __attribute__((__noreturn__));
95void test12(void) {
96 __builtin_trap(); // no warning because trap is noreturn.
97}
Douglas Gregor56fbc372009-09-28 21:14:19 +000098
99void test_unknown_builtin(int a, int b) {
Hans Wennborg2fb8b912011-12-06 09:46:12 +0000100 __builtin_isles(a, b); // expected-error{{use of unknown builtin}} \
101 // expected-note{{did you mean '__builtin_isless'?}}
Douglas Gregor56fbc372009-09-28 21:14:19 +0000102}
Benjamin Kramere93c3902010-07-26 22:04:15 +0000103
104int test13() {
105 __builtin_eh_return(0, 0); // no warning, eh_return never returns.
106}
Douglas Gregor45bd34a2010-07-28 18:42:27 +0000107
108// <rdar://problem/8228293>
109void test14() {
110 int old;
111 old = __sync_fetch_and_min((volatile int *)&old, 1);
112}
Douglas Gregor334a10a2010-08-25 15:47:31 +0000113
114// <rdar://problem/8336581>
115void test15(const char *s) {
116 __builtin_printf("string is %s\n", s);
117}
Chris Lattner17c0eac2010-10-12 17:47:42 +0000118
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}
Richard Smith10c7c902011-12-09 02:04:48 +0000124
125const int test17_n = 0;
126const char test17_c[] = {1, 2, 3, 0};
127const char test17_d[] = {1, 2, 3, 4};
128typedef int __attribute__((vector_size(16))) IntVector;
129struct Aggregate { int n; char c; };
130enum Enum { EnumValue1, EnumValue2 };
131
132typedef __typeof(sizeof(int)) size_t;
133size_t strlen(const char *);
134
135void 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}
Bill Wendlingf1c31912012-12-18 21:09:25 +0000184
185void 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));
Fariborz Jahanianab4fe982014-09-12 18:44:36 +0000192 result = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst));
193 result = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst));
Bill Wendlingf1c31912012-12-18 21:09:25 +0000194
195 ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src)); // expected-error {{too few arguments to function call}}
Fariborz Jahanianab4fe982014-09-12 18:44:36 +0000196 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}}
Bill Wendlingf1c31912012-12-18 21:09:25 +0000198}
Reid Klecknercf8933d2013-11-13 22:47:22 +0000199
200void no_ms_builtins() {
201 __assume(1); // expected-warning {{implicit declaration}}
202 __noop(1); // expected-warning {{implicit declaration}}
203 __debugbreak(); // expected-warning {{implicit declaration}}
204}
Richard Smith760520b2014-06-03 23:27:44 +0000205
206void 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}
Fariborz Jahanianab4fe982014-09-12 18:44:36 +0000210
211// rdar://18259539
212size_t strlcpy(char * restrict dst, const char * restrict src, size_t size);
213size_t strlcat(char * restrict dst, const char * restrict src, size_t size);
214
215void 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}} \
Fariborz Jahanian3e6a0be2014-09-18 17:58:27 +0000223 // expected-note {{change size argument to be the size of the destination}} \
224 // expected-warning {{'__builtin___strlcpy_chk' will always overflow destination buffer}}
Fariborz Jahanianab4fe982014-09-12 18:44:36 +0000225
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}}
Fariborz Jahanian3e6a0be2014-09-18 17:58:27 +0000228
Fariborz Jahanianab4fe982014-09-12 18:44:36 +0000229 __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}} \
Fariborz Jahanian3e6a0be2014-09-18 17:58:27 +0000230 // 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
235char * 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;
Fariborz Jahanianab4fe982014-09-12 18:44:36 +0000250}