Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wno-string-plus-int -triple=i686-apple-darwin9 |
Eli Friedman | 1435202 | 2008-05-25 04:43:38 +0000 | [diff] [blame] | 2 | // This test needs to set the target because it uses __builtin_ia32_vec_ext_v4si |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 3 | |
| 4 | int test1(float a, int b) { |
| 5 | return __builtin_isless(a, b); |
| 6 | } |
| 7 | int test2(int a, int b) { |
| 8 | return __builtin_islessequal(a, b); // expected-error {{floating point type}} |
| 9 | } |
| 10 | |
| 11 | int test3(double a, float b) { |
| 12 | return __builtin_isless(a, b); |
| 13 | } |
| 14 | int test4(int* a, double b) { |
| 15 | return __builtin_islessequal(a, b); // expected-error {{floating point type}} |
| 16 | } |
| 17 | |
| 18 | int test5(float a, long double b) { |
| 19 | return __builtin_isless(a, b, b); // expected-error {{too many arguments}} |
| 20 | } |
| 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 |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 27 | void test7() { |
Chris Lattner | 634785c | 2009-12-30 22:10:22 +0000 | [diff] [blame] | 28 | const void *X; |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 29 | X = CFSTR("\242"); // expected-warning {{input conversion stopped}} |
Ted Kremenek | db9e9e6 | 2011-03-15 21:18:52 +0000 | [diff] [blame] | 30 | X = CFSTR("\0"); // no-warning |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 31 | X = CFSTR(242); // expected-error {{CFString literal is not a string constant}} expected-warning {{incompatible integer to pointer conversion}} |
Chris Lattner | 634785c | 2009-12-30 22:10:22 +0000 | [diff] [blame] | 32 | X = CFSTR("foo", "bar"); // expected-error {{too many arguments to function call}} |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 36 | // atomics. |
| 37 | |
Mike Stump | d1969d8 | 2009-07-22 00:43:08 +0000 | [diff] [blame] | 38 | void test9(short v) { |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 39 | unsigned i, old; |
Gabor Greif | 2fb985b | 2010-10-15 08:48:51 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 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}} |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 43 | old = __sync_fetch_and_add((unsigned*)0, 42i); // expected-warning {{imaginary constants are an 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); |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 52 | } |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | // rdar://7236819 |
| 56 | void test10(void) __attribute__((noreturn)); |
| 57 | |
| 58 | void test10(void) { |
Gabor Greif | 2fb985b | 2010-10-15 08:48:51 +0000 | [diff] [blame] | 59 | __asm__("int3"); |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 60 | __builtin_unreachable(); |
Gabor Greif | 2fb985b | 2010-10-15 08:48:51 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 62 | // No warning about falling off the end of a noreturn function. |
| 63 | } |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 64 | |
| 65 | void test11(int X) { |
Gabor Greif | 2fb985b | 2010-10-15 08:48:51 +0000 | [diff] [blame] | 66 | switch (X) { |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 67 | case __builtin_eh_return_data_regno(0): // constant foldable. |
| 68 | break; |
| 69 | } |
| 70 | |
Eric Christopher | 5e89655 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 71 | __builtin_eh_return_data_regno(X); // expected-error {{argument to '__builtin_eh_return_data_regno' must be a constant integer}} |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 50dd255 | 2009-09-26 21:16:00 +0000 | [diff] [blame] | 74 | // PR5062 |
| 75 | void test12(void) __attribute__((__noreturn__)); |
| 76 | void test12(void) { |
| 77 | __builtin_trap(); // no warning because trap is noreturn. |
| 78 | } |
Douglas Gregor | 9a8c9a2 | 2009-09-28 21:14:19 +0000 | [diff] [blame] | 79 | |
| 80 | void test_unknown_builtin(int a, int b) { |
Hans Wennborg | 122de3e | 2011-12-06 09:46:12 +0000 | [diff] [blame] | 81 | __builtin_isles(a, b); // expected-error{{use of unknown builtin}} \ |
| 82 | // expected-note{{did you mean '__builtin_isless'?}} |
Douglas Gregor | 9a8c9a2 | 2009-09-28 21:14:19 +0000 | [diff] [blame] | 83 | } |
Benjamin Kramer | dee832c | 2010-07-26 22:04:15 +0000 | [diff] [blame] | 84 | |
| 85 | int test13() { |
| 86 | __builtin_eh_return(0, 0); // no warning, eh_return never returns. |
| 87 | } |
Douglas Gregor | c46111e | 2010-07-28 18:42:27 +0000 | [diff] [blame] | 88 | |
| 89 | // <rdar://problem/8228293> |
| 90 | void test14() { |
| 91 | int old; |
| 92 | old = __sync_fetch_and_min((volatile int *)&old, 1); |
| 93 | } |
Douglas Gregor | 1e32ca6 | 2010-08-25 15:47:31 +0000 | [diff] [blame] | 94 | |
| 95 | // <rdar://problem/8336581> |
| 96 | void test15(const char *s) { |
| 97 | __builtin_printf("string is %s\n", s); |
| 98 | } |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 99 | |
| 100 | // PR7885 |
| 101 | int test16() { |
| 102 | return __builtin_constant_p() + // expected-error{{too few arguments}} |
| 103 | __builtin_constant_p(1, 2); // expected-error {{too many arguments}} |
| 104 | } |
Richard Smith | e052d46 | 2011-12-09 02:04:48 +0000 | [diff] [blame] | 105 | |
| 106 | const int test17_n = 0; |
| 107 | const char test17_c[] = {1, 2, 3, 0}; |
| 108 | const char test17_d[] = {1, 2, 3, 4}; |
| 109 | typedef int __attribute__((vector_size(16))) IntVector; |
| 110 | struct Aggregate { int n; char c; }; |
| 111 | enum Enum { EnumValue1, EnumValue2 }; |
| 112 | |
| 113 | typedef __typeof(sizeof(int)) size_t; |
| 114 | size_t strlen(const char *); |
| 115 | |
| 116 | void test17() { |
| 117 | #define ASSERT(...) { int arr[(__VA_ARGS__) ? 1 : -1]; } |
| 118 | #define T(...) ASSERT(__builtin_constant_p(__VA_ARGS__)) |
| 119 | #define F(...) ASSERT(!__builtin_constant_p(__VA_ARGS__)) |
| 120 | |
| 121 | // __builtin_constant_p returns 1 if the argument folds to: |
| 122 | // - an arithmetic constant with value which is known at compile time |
| 123 | T(test17_n); |
| 124 | T(&test17_c[3] - test17_c); |
| 125 | T(3i + 5); // expected-warning {{imaginary constant}} |
| 126 | T(4.2 * 7.6); |
| 127 | T(EnumValue1); |
| 128 | T((enum Enum)(int)EnumValue2); |
| 129 | |
| 130 | // - the address of the first character of a string literal, losslessly cast |
| 131 | // to any type |
| 132 | T("string literal"); |
| 133 | T((double*)"string literal"); |
| 134 | T("string literal" + 0); |
| 135 | T((long)"string literal"); |
| 136 | |
| 137 | // ... and otherwise returns 0. |
| 138 | F("string literal" + 1); |
| 139 | F(&test17_n); |
| 140 | F(test17_c); |
| 141 | F(&test17_c); |
| 142 | F(&test17_d); |
| 143 | F((struct Aggregate){0, 1}); |
| 144 | F((IntVector){0, 1, 2, 3}); |
| 145 | |
| 146 | // Ensure that a technique used in glibc is handled correctly. |
| 147 | #define OPT(...) (__builtin_constant_p(__VA_ARGS__) && strlen(__VA_ARGS__) < 4) |
| 148 | // FIXME: These are incorrectly treated as ICEs because strlen is treated as |
| 149 | // a builtin. |
| 150 | ASSERT(OPT("abc")); |
| 151 | ASSERT(!OPT("abcd")); |
| 152 | // In these cases, the strlen is non-constant, but the __builtin_constant_p |
| 153 | // is 0: the array size is not an ICE but is foldable. |
| 154 | ASSERT(!OPT(test17_c)); // expected-warning {{folded}} |
| 155 | ASSERT(!OPT(&test17_c[0])); // expected-warning {{folded}} |
| 156 | ASSERT(!OPT((char*)test17_c)); // expected-warning {{folded}} |
| 157 | ASSERT(!OPT(test17_d)); // expected-warning {{folded}} |
| 158 | ASSERT(!OPT(&test17_d[0])); // expected-warning {{folded}} |
| 159 | ASSERT(!OPT((char*)test17_d)); // expected-warning {{folded}} |
| 160 | |
| 161 | #undef OPT |
| 162 | #undef T |
| 163 | #undef F |
| 164 | } |