Home | History | Annotate | Download | only in Sema
      1 // RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -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 
      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
     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 
     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.
     55 void test9_1(volatile int* ptr, int val) {
     56   __sync_fetch_and_add_4(ptr, val);
     57 }
     58 void test9_2(volatile int* ptr, int val) {
     59   __sync_fetch_and_add(ptr, val);
     60 }
     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 
     69 // rdar://7236819
     70 void test10(void) __attribute__((noreturn));
     71 
     72 void test10(void) {
     73   __asm__("int3");
     74   __builtin_unreachable();
     75 
     76   // No warning about falling off the end of a noreturn function.
     77 }
     78 
     79 void test11(int X) {
     80   switch (X) {
     81   case __builtin_eh_return_data_regno(0):  // constant foldable.
     82     break;
     83   }
     84 
     85   __builtin_eh_return_data_regno(X);  // expected-error {{argument to '__builtin_eh_return_data_regno' must be a constant integer}}
     86 }
     87 
     88 // PR5062
     89 void test12(void) __attribute__((__noreturn__));
     90 void test12(void) {
     91   __builtin_trap();  // no warning because trap is noreturn.
     92 }
     93 
     94 void test_unknown_builtin(int a, int b) {
     95   __builtin_isles(a, b); // expected-error{{use of unknown builtin}} \
     96                          // expected-note{{did you mean '__builtin_isless'?}}
     97 }
     98 
     99 int test13() {
    100   __builtin_eh_return(0, 0); // no warning, eh_return never returns.
    101 }
    102 
    103 // <rdar://problem/8228293>
    104 void test14() {
    105   int old;
    106   old = __sync_fetch_and_min((volatile int *)&old, 1);
    107 }
    108 
    109 // <rdar://problem/8336581>
    110 void test15(const char *s) {
    111   __builtin_printf("string is %s\n", s);
    112 }
    113 
    114 // PR7885
    115 int test16() {
    116   return __builtin_constant_p() + // expected-error{{too few arguments}}
    117          __builtin_constant_p(1, 2); // expected-error {{too many arguments}}
    118 }
    119 
    120 const int test17_n = 0;
    121 const char test17_c[] = {1, 2, 3, 0};
    122 const char test17_d[] = {1, 2, 3, 4};
    123 typedef int __attribute__((vector_size(16))) IntVector;
    124 struct Aggregate { int n; char c; };
    125 enum Enum { EnumValue1, EnumValue2 };
    126 
    127 typedef __typeof(sizeof(int)) size_t;
    128 size_t strlen(const char *);
    129 
    130 void test17() {
    131 #define ASSERT(...) { int arr[(__VA_ARGS__) ? 1 : -1]; }
    132 #define T(...) ASSERT(__builtin_constant_p(__VA_ARGS__))
    133 #define F(...) ASSERT(!__builtin_constant_p(__VA_ARGS__))
    134 
    135   // __builtin_constant_p returns 1 if the argument folds to:
    136   //  - an arithmetic constant with value which is known at compile time
    137   T(test17_n);
    138   T(&test17_c[3] - test17_c);
    139   T(3i + 5); // expected-warning {{imaginary constant}}
    140   T(4.2 * 7.6);
    141   T(EnumValue1);
    142   T((enum Enum)(int)EnumValue2);
    143 
    144   //  - the address of the first character of a string literal, losslessly cast
    145   //    to any type
    146   T("string literal");
    147   T((double*)"string literal");
    148   T("string literal" + 0);
    149   T((long)"string literal");
    150 
    151   // ... and otherwise returns 0.
    152   F("string literal" + 1);
    153   F(&test17_n);
    154   F(test17_c);
    155   F(&test17_c);
    156   F(&test17_d);
    157   F((struct Aggregate){0, 1});
    158   F((IntVector){0, 1, 2, 3});
    159 
    160   // Ensure that a technique used in glibc is handled correctly.
    161 #define OPT(...) (__builtin_constant_p(__VA_ARGS__) && strlen(__VA_ARGS__) < 4)
    162   // FIXME: These are incorrectly treated as ICEs because strlen is treated as
    163   // a builtin.
    164   ASSERT(OPT("abc"));
    165   ASSERT(!OPT("abcd"));
    166   // In these cases, the strlen is non-constant, but the __builtin_constant_p
    167   // is 0: the array size is not an ICE but is foldable.
    168   ASSERT(!OPT(test17_c));        // expected-warning {{folded}}
    169   ASSERT(!OPT(&test17_c[0]));    // expected-warning {{folded}}
    170   ASSERT(!OPT((char*)test17_c)); // expected-warning {{folded}}
    171   ASSERT(!OPT(test17_d));        // expected-warning {{folded}}
    172   ASSERT(!OPT(&test17_d[0]));    // expected-warning {{folded}}
    173   ASSERT(!OPT((char*)test17_d)); // expected-warning {{folded}}
    174 
    175 #undef OPT
    176 #undef T
    177 #undef F
    178 }
    179 
    180 void test18() {
    181   char src[1024];
    182   char dst[2048];
    183   size_t result;
    184   void *ptr;
    185 
    186   ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src), sizeof(dst));
    187   result = __builtin___strlcpy_chk(dst, src, sizeof(src), sizeof(dst));
    188   result = __builtin___strlcat_chk(dst, src, sizeof(src), sizeof(dst));
    189 
    190   ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src));      // expected-error {{too few arguments to function call}}
    191   ptr = __builtin___strlcpy_chk(dst, src, sizeof(src), sizeof(dst)); // expected-warning {{incompatible integer to pointer conversion}}
    192   ptr = __builtin___strlcat_chk(dst, src, sizeof(src), sizeof(dst)); // expected-warning {{incompatible integer to pointer conversion}}
    193 }
    194