builtins.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. int test1(float a, int b) {
  4. return __builtin_isless(a, b); // expected-note {{declared here}}
  5. }
  6. int test2(int a, int b) {
  7. return __builtin_islessequal(a, b); // expected-error {{floating point type}}
  8. }
  9. int test3(double a, float b) {
  10. return __builtin_isless(a, b);
  11. }
  12. int test4(int* a, double b) {
  13. return __builtin_islessequal(a, b); // expected-error {{floating point type}}
  14. }
  15. int test5(float a, long double b) {
  16. return __builtin_isless(a, b, b); // expected-error {{too many arguments}}
  17. }
  18. int test6(float a, long double b) {
  19. return __builtin_islessequal(a); // expected-error {{too few arguments}}
  20. }
  21. #define CFSTR __builtin___CFStringMakeConstantString
  22. void test7() {
  23. const void *X;
  24. X = CFSTR("\242"); // expected-warning {{input conversion stopped}}
  25. X = CFSTR("\0"); // no-warning
  26. X = CFSTR(242); // expected-error {{CFString literal is not a string constant}} expected-warning {{incompatible integer to pointer conversion}}
  27. X = CFSTR("foo", "bar"); // expected-error {{too many arguments to function call}}
  28. }
  29. // atomics.
  30. void test9(short v) {
  31. unsigned i, old;
  32. old = __sync_fetch_and_add(); // expected-error {{too few arguments to function call}}
  33. old = __sync_fetch_and_add(&old); // expected-error {{too few arguments to function call}}
  34. old = __sync_fetch_and_add((unsigned*)0, 42i); // expected-warning {{imaginary constants are a GNU extension}}
  35. // PR7600: Pointers are implicitly casted to integers and back.
  36. void *old_ptr = __sync_val_compare_and_swap((void**)0, 0, 0);
  37. // Ensure the return type is correct even when implicit casts are stripped
  38. // away. This triggers an assertion while checking the comparison otherwise.
  39. if (__sync_fetch_and_add(&old, 1) == 1) {
  40. }
  41. }
  42. // overloaded atomics should be declared only once.
  43. void test9_1(volatile int* ptr, int val) {
  44. __sync_fetch_and_add_4(ptr, val);
  45. }
  46. void test9_2(volatile int* ptr, int val) {
  47. __sync_fetch_and_add(ptr, val);
  48. }
  49. void test9_3(volatile int* ptr, int val) {
  50. __sync_fetch_and_add_4(ptr, val);
  51. __sync_fetch_and_add(ptr, val);
  52. __sync_fetch_and_add(ptr, val);
  53. __sync_fetch_and_add_4(ptr, val);
  54. __sync_fetch_and_add_4(ptr, val);
  55. }
  56. void test9_4(volatile int* ptr, int val) {
  57. // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}}
  58. __sync_fetch_and_nand(ptr, val);
  59. }
  60. // rdar://7236819
  61. void test10(void) __attribute__((noreturn));
  62. void test10(void) {
  63. __asm__("int3");
  64. __builtin_unreachable();
  65. // No warning about falling off the end of a noreturn function.
  66. }
  67. void test11(int X) {
  68. switch (X) {
  69. case __builtin_eh_return_data_regno(0): // constant foldable.
  70. break;
  71. }
  72. __builtin_eh_return_data_regno(X); // expected-error {{argument to '__builtin_eh_return_data_regno' must be a constant integer}}
  73. }
  74. // PR5062
  75. void test12(void) __attribute__((__noreturn__));
  76. void test12(void) {
  77. __builtin_trap(); // no warning because trap is noreturn.
  78. }
  79. void test_unknown_builtin(int a, int b) {
  80. __builtin_isles(a, b); // expected-error{{use of unknown builtin}} \
  81. // expected-note{{did you mean '__builtin_isless'?}}
  82. }
  83. int test13() {
  84. __builtin_eh_return(0, 0); // no warning, eh_return never returns.
  85. }
  86. // <rdar://problem/8228293>
  87. void test14() {
  88. int old;
  89. old = __sync_fetch_and_min((volatile int *)&old, 1);
  90. }
  91. // <rdar://problem/8336581>
  92. void test15(const char *s) {
  93. __builtin_printf("string is %s\n", s);
  94. }
  95. // PR7885
  96. int test16() {
  97. return __builtin_constant_p() + // expected-error{{too few arguments}}
  98. __builtin_constant_p(1, 2); // expected-error {{too many arguments}}
  99. }
  100. const int test17_n = 0;
  101. const char test17_c[] = {1, 2, 3, 0};
  102. const char test17_d[] = {1, 2, 3, 4};
  103. typedef int __attribute__((vector_size(16))) IntVector;
  104. struct Aggregate { int n; char c; };
  105. enum Enum { EnumValue1, EnumValue2 };
  106. typedef __typeof(sizeof(int)) size_t;
  107. size_t strlen(const char *);
  108. void test17() {
  109. #define ASSERT(...) { int arr[(__VA_ARGS__) ? 1 : -1]; }
  110. #define T(...) ASSERT(__builtin_constant_p(__VA_ARGS__))
  111. #define F(...) ASSERT(!__builtin_constant_p(__VA_ARGS__))
  112. // __builtin_constant_p returns 1 if the argument folds to:
  113. // - an arithmetic constant with value which is known at compile time
  114. T(test17_n);
  115. T(&test17_c[3] - test17_c);
  116. T(3i + 5); // expected-warning {{imaginary constant}}
  117. T(4.2 * 7.6);
  118. T(EnumValue1);
  119. T((enum Enum)(int)EnumValue2);
  120. // - the address of the first character of a string literal, losslessly cast
  121. // to any type
  122. T("string literal");
  123. T((double*)"string literal");
  124. T("string literal" + 0);
  125. T((long)"string literal");
  126. // ... and otherwise returns 0.
  127. F("string literal" + 1);
  128. F(&test17_n);
  129. F(test17_c);
  130. F(&test17_c);
  131. F(&test17_d);
  132. F((struct Aggregate){0, 1});
  133. F((IntVector){0, 1, 2, 3});
  134. // Ensure that a technique used in glibc is handled correctly.
  135. #define OPT(...) (__builtin_constant_p(__VA_ARGS__) && strlen(__VA_ARGS__) < 4)
  136. // FIXME: These are incorrectly treated as ICEs because strlen is treated as
  137. // a builtin.
  138. ASSERT(OPT("abc"));
  139. ASSERT(!OPT("abcd"));
  140. // In these cases, the strlen is non-constant, but the __builtin_constant_p
  141. // is 0: the array size is not an ICE but is foldable.
  142. ASSERT(!OPT(test17_c)); // expected-warning {{folded}}
  143. ASSERT(!OPT(&test17_c[0])); // expected-warning {{folded}}
  144. ASSERT(!OPT((char*)test17_c)); // expected-warning {{folded}}
  145. ASSERT(!OPT(test17_d)); // expected-warning {{folded}}
  146. ASSERT(!OPT(&test17_d[0])); // expected-warning {{folded}}
  147. ASSERT(!OPT((char*)test17_d)); // expected-warning {{folded}}
  148. #undef OPT
  149. #undef T
  150. #undef F
  151. }
  152. void test18() {
  153. char src[1024];
  154. char dst[2048];
  155. size_t result;
  156. void *ptr;
  157. ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src), sizeof(dst));
  158. result = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst));
  159. result = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst));
  160. ptr = __builtin___memccpy_chk(dst, src, '\037', sizeof(src)); // expected-error {{too few arguments to function call}}
  161. ptr = __builtin___strlcpy_chk(dst, src, sizeof(dst), sizeof(dst)); // expected-warning {{incompatible integer to pointer conversion}}
  162. ptr = __builtin___strlcat_chk(dst, src, sizeof(dst), sizeof(dst)); // expected-warning {{incompatible integer to pointer conversion}}
  163. }
  164. void no_ms_builtins() {
  165. __assume(1); // expected-warning {{implicit declaration}}
  166. __noop(1); // expected-warning {{implicit declaration}}
  167. __debugbreak(); // expected-warning {{implicit declaration}}
  168. }
  169. void unavailable() {
  170. __builtin_operator_new(0); // expected-error {{'__builtin_operator_new' is only available in C++}}
  171. __builtin_operator_delete(0); // expected-error {{'__builtin_operator_delete' is only available in C++}}
  172. }
  173. // rdar://18259539
  174. size_t strlcpy(char * restrict dst, const char * restrict src, size_t size);
  175. size_t strlcat(char * restrict dst, const char * restrict src, size_t size);
  176. void Test19(void)
  177. {
  178. static char b[40];
  179. static char buf[20];
  180. 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}} \\
  181. // expected-note {{change size argument to be the size of the destination}}
  182. __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}} \
  183. // expected-note {{change size argument to be the size of the destination}} \
  184. // expected-warning {{'__builtin___strlcpy_chk' will always overflow destination buffer}}
  185. 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}} \
  186. // expected-note {{change size argument to be the size of the destination}}
  187. __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}} \
  188. // expected-note {{change size argument to be the size of the destination}} \
  189. // expected-warning {{'__builtin___strlcat_chk' will always overflow destination buffer}}
  190. }
  191. // rdar://11076881
  192. char * Test20(char *p, const char *in, unsigned n)
  193. {
  194. static char buf[10];
  195. __builtin___memcpy_chk (&buf[6], in, 5, __builtin_object_size (&buf[6], 0)); // expected-warning {{'__builtin___memcpy_chk' will always overflow destination buffer}}
  196. __builtin___memcpy_chk (p, "abcde", n, __builtin_object_size (p, 0));
  197. __builtin___memcpy_chk (&buf[5], "abcde", 5, __builtin_object_size (&buf[5], 0));
  198. __builtin___memcpy_chk (&buf[5], "abcde", n, __builtin_object_size (&buf[5], 0));
  199. __builtin___memcpy_chk (&buf[6], "abcde", 5, __builtin_object_size (&buf[6], 0)); // expected-warning {{'__builtin___memcpy_chk' will always overflow destination buffer}}
  200. return buf;
  201. }