exprs.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
  2. // PR 8876 - don't warn about trivially unreachable null derefs. Note that
  3. // we put this here because the reachability analysis only kicks in for
  4. // suppressing false positives when code has no errors.
  5. #define PR8876(err_ptr) do {\
  6. if (err_ptr) *(int*)(err_ptr) = 1;\
  7. } while (0)
  8. #define PR8876_pos(err_ptr) do {\
  9. if (!err_ptr) *(int*)(err_ptr) = 1;\
  10. } while (0)
  11. // Test that we don't report divide-by-zero errors in unreachable code.
  12. // This test should be left as is, as it also tests CFG functionality.
  13. void radar9171946() {
  14. if (0) {
  15. 0 / (0 ? 1 : 0); // expected-warning {{expression result unused}}
  16. }
  17. }
  18. int test_pr8876() {
  19. PR8876(0); // no-warning
  20. PR8876_pos(0); // expected-warning{{indirection of non-volatile null pointer will be deleted, not trap}} expected-note{{consider using __builtin_trap() or qualifying pointer with 'volatile'}}
  21. return 0;
  22. }
  23. // PR 8183 - Handle null pointer constants on the left-side of the '&&', and reason about
  24. // this when determining the reachability of the null pointer dereference on the right side.
  25. void pr8183(unsigned long long test)
  26. {
  27. (void)((((void*)0)) && (*((unsigned long long*)(((void*)0))) = ((unsigned long long)((test)) % (unsigned long long)((1000000000))))); // no-warning
  28. (*((unsigned long long*)(((void*)0))) = ((unsigned long long)((test)) % (unsigned long long)((1000000000)))); // expected-warning {{indirection of non-volatile null pointer will be deleted, not trap}} expected-note {{consider using __builtin_trap() or qualifying pointer with 'volatile'}}
  29. }
  30. // PR1966
  31. _Complex double test1() {
  32. return __extension__ 1.0if;
  33. }
  34. _Complex double test2() {
  35. return 1.0if; // expected-warning {{imaginary constants are a GNU extension}}
  36. }
  37. // rdar://6097308
  38. void test3() {
  39. int x;
  40. (__extension__ x) = 10;
  41. }
  42. // rdar://6162726
  43. void test4() {
  44. static int var;
  45. var =+ 5; // expected-warning {{use of unary operator that may be intended as compound assignment (+=)}}
  46. var =- 5; // expected-warning {{use of unary operator that may be intended as compound assignment (-=)}}
  47. var = +5; // no warning when space between the = and +.
  48. var = -5;
  49. var =+5; // no warning when the subexpr of the unary op has no space before it.
  50. var =-5;
  51. #define FIVE 5
  52. var=-FIVE; // no warning with macros.
  53. var=-FIVE;
  54. }
  55. // rdar://6319320
  56. void test5(int *X, float *P) {
  57. (float*)X = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
  58. #define FOO ((float*) X)
  59. FOO = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
  60. }
  61. void test6() {
  62. int X;
  63. X(); // expected-error {{called object type 'int' is not a function or function pointer}}
  64. }
  65. void test7(int *P, _Complex float Gamma) {
  66. P = (P-42) + Gamma*4; // expected-error {{invalid operands to binary expression ('int *' and '_Complex float')}}
  67. }
  68. // rdar://6095061
  69. int test8(void) {
  70. int i;
  71. __builtin_choose_expr (0, 42, i) = 10;
  72. return i;
  73. }
  74. // PR3386
  75. struct f { int x : 4; float y[]; };
  76. int test9(struct f *P) {
  77. int R;
  78. R = __alignof(P->x); // expected-error {{invalid application of 'alignof' to bit-field}}
  79. R = __alignof(P->y); // ok.
  80. R = sizeof(P->x); // expected-error {{invalid application of 'sizeof' to bit-field}}
  81. return R;
  82. }
  83. // PR3562
  84. void test10(int n,...) {
  85. struct S {
  86. double a[n]; // expected-error {{fields must have a constant size}}
  87. } s;
  88. double x = s.a[0]; // should not get another error here.
  89. }
  90. #define MYMAX(A,B) __extension__ ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
  91. struct mystruct {int A; };
  92. void test11(struct mystruct P, float F) {
  93. MYMAX(P, F); // expected-error {{invalid operands to binary expression ('typeof (P)' (aka 'struct mystruct') and 'typeof (F)' (aka 'float'))}}
  94. }
  95. // PR3753
  96. int test12(const char *X) {
  97. return X == "foo"; // expected-warning {{comparison against a string literal is unspecified (use strncmp instead)}}
  98. }
  99. int test12b(const char *X) {
  100. return sizeof(X == "foo"); // no-warning
  101. }
  102. // rdar://6719156
  103. void test13(
  104. void (^P)()) { // expected-error {{blocks support disabled - compile with -fblocks}}
  105. P();
  106. P = ^(){}; // expected-error {{blocks support disabled - compile with -fblocks}}
  107. }
  108. void test14() {
  109. typedef long long __m64 __attribute__((__vector_size__(8)));
  110. typedef short __v4hi __attribute__((__vector_size__(8)));
  111. // Ok.
  112. __v4hi a;
  113. __m64 mask = (__m64)((__v4hi)a > (__v4hi)a);
  114. }
  115. // PR5242
  116. typedef unsigned long *test15_t;
  117. test15_t test15(void) {
  118. return (test15_t)0 + (test15_t)0; // expected-error {{invalid operands to binary expression ('test15_t' (aka 'unsigned long *') and 'test15_t')}}
  119. }
  120. // rdar://7446395
  121. void test16(float x) { x == ((void*) 0); } // expected-error {{invalid operands to binary expression}}
  122. // PR6004
  123. void test17(int x) {
  124. x = x / 0; // expected-warning {{division by zero is undefined}}
  125. x = x % 0; // expected-warning {{remainder by zero is undefined}}
  126. x /= 0; // expected-warning {{division by zero is undefined}}
  127. x %= 0; // expected-warning {{remainder by zero is undefined}}
  128. x = sizeof(x/0); // no warning.
  129. }
  130. // PR6501 & PR11857
  131. void test18_a(int a); // expected-note 2 {{'test18_a' declared here}}
  132. void test18_b(int); // expected-note {{'test18_b' declared here}}
  133. void test18_c(int a, int b); // expected-note 2 {{'test18_c' declared here}}
  134. void test18_d(int a, ...); // expected-note {{'test18_d' declared here}}
  135. void test18_e(int a, int b, ...); // expected-note {{'test18_e' declared here}}
  136. void test18(int b) {
  137. test18_a(b, b); // expected-error {{too many arguments to function call, expected single argument 'a', have 2}}
  138. test18_a(); // expected-error {{too few arguments to function call, single argument 'a' was not specified}}
  139. test18_b(); // expected-error {{too few arguments to function call, expected 1, have 0}}
  140. test18_c(b); // expected-error {{too few arguments to function call, expected 2, have 1}}
  141. test18_c(b, b, b); // expected-error {{too many arguments to function call, expected 2, have 3}}
  142. test18_d(); // expected-error {{too few arguments to function call, at least argument 'a' must be specified}}
  143. test18_e(); // expected-error {{too few arguments to function call, expected at least 2, have 0}}
  144. }
  145. // PR7569
  146. void test19() {
  147. *(int*)0 = 0; // expected-warning {{indirection of non-volatile null pointer}} \
  148. // expected-note {{consider using __builtin_trap}}
  149. *(volatile int*)0 = 0; // Ok.
  150. // rdar://9269271
  151. int x = *(int*)0; // expected-warning {{indirection of non-volatile null pointer}} \
  152. // expected-note {{consider using __builtin_trap}}
  153. int x2 = *(volatile int*)0; // Ok.
  154. int *p = &(*(int*)0); // Ok;
  155. }
  156. int test20(int x) {
  157. return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \
  158. // expected-note {{use '&' for a bitwise operation}} \
  159. // expected-note {{remove constant to silence this warning}}
  160. return x && sizeof(int) == 4; // no warning, RHS is logical op.
  161. // no warning, this is an idiom for "true" in old C style.
  162. return x && (signed char)1;
  163. return x || 0;
  164. return x || 1;
  165. return x || -1; // expected-warning {{use of logical '||' with constant operand}} \
  166. // expected-note {{use '|' for a bitwise operation}}
  167. return x || 5; // expected-warning {{use of logical '||' with constant operand}} \
  168. // expected-note {{use '|' for a bitwise operation}}
  169. return x && 0;
  170. return x && 1;
  171. return x && -1; // expected-warning {{use of logical '&&' with constant operand}} \
  172. // expected-note {{use '&' for a bitwise operation}} \
  173. // expected-note {{remove constant to silence this warning}}
  174. return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \
  175. // expected-note {{use '&' for a bitwise operation}} \
  176. // expected-note {{remove constant to silence this warning}}
  177. return x || (0);
  178. return x || (1);
  179. return x || (-1); // expected-warning {{use of logical '||' with constant operand}} \
  180. // expected-note {{use '|' for a bitwise operation}}
  181. return x || (5); // expected-warning {{use of logical '||' with constant operand}} \
  182. // expected-note {{use '|' for a bitwise operation}}
  183. return x && (0);
  184. return x && (1);
  185. return x && (-1); // expected-warning {{use of logical '&&' with constant operand}} \
  186. // expected-note {{use '&' for a bitwise operation}} \
  187. // expected-note {{remove constant to silence this warning}}
  188. return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \
  189. // expected-note {{use '&' for a bitwise operation}} \
  190. // expected-note {{remove constant to silence this warning}}
  191. }
  192. struct Test21; // expected-note 2 {{forward declaration}}
  193. void test21(volatile struct Test21 *ptr) {
  194. void test21_help(void);
  195. (test21_help(), *ptr); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
  196. (*ptr, test21_help()); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
  197. }
  198. // Make sure we do function/array decay.
  199. void test22() {
  200. if ("help")
  201. (void) 0;
  202. if (test22) // expected-warning {{address of function 'test22' will always evaluate to 'true'}} \
  203. // expected-note {{prefix with the address-of operator to silence this warning}}
  204. (void) 0;
  205. if (&test22)
  206. (void) 0;
  207. }