parentheses.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
  2. // RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
  3. // Test the various warnings under -Wparentheses
  4. void if_assign(void) {
  5. int i;
  6. if (i = 4) {} // expected-warning {{assignment as a condition}} \
  7. // expected-note{{place parentheses around the assignment to silence this warning}} \
  8. // expected-note{{use '==' to turn this assignment into an equality comparison}}
  9. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:7-[[@LINE-3]]:7}:"("
  10. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:12-[[@LINE-4]]:12}:")"
  11. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:9-[[@LINE-5]]:10}:"=="
  12. if ((i = 4)) {}
  13. }
  14. void bitwise_rel(unsigned i) {
  15. (void)(i & 0x2 == 0); // expected-warning {{& has lower precedence than ==}} \
  16. // expected-note{{place parentheses around the '==' expression to silence this warning}} \
  17. // expected-note{{place parentheses around the & expression to evaluate it first}}
  18. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"("
  19. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
  20. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
  21. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:17-[[@LINE-6]]:17}:")"
  22. (void)(0 == i & 0x2); // expected-warning {{& has lower precedence than ==}} \
  23. // expected-note{{place parentheses around the '==' expression to silence this warning}} \
  24. // expected-note{{place parentheses around the & expression to evaluate it first}}
  25. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  26. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
  27. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
  28. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:22-[[@LINE-6]]:22}:")"
  29. (void)(i & 0xff < 30); // expected-warning {{& has lower precedence than <}} \
  30. // expected-note{{place parentheses around the '<' expression to silence this warning}} \
  31. // expected-note{{place parentheses around the & expression to evaluate it first}}
  32. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"("
  33. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:23-[[@LINE-4]]:23}:")"
  34. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
  35. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:18-[[@LINE-6]]:18}:")"
  36. (void)((i & 0x2) == 0);
  37. (void)(i & (0x2 == 0));
  38. // Eager logical op
  39. (void)(i == 1 | i == 2 | i == 3);
  40. (void)(i != 1 & i != 2 & i != 3);
  41. (void)(i & i | i); // expected-warning {{'&' within '|'}} \
  42. // expected-note {{place parentheses around the '&' expression to silence this warning}}
  43. // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"("
  44. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")"
  45. (void)(i | i & i); // expected-warning {{'&' within '|'}} \
  46. // expected-note {{place parentheses around the '&' expression to silence this warning}}
  47. // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"("
  48. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:19-[[@LINE-3]]:19}:")"
  49. (void)(i ||
  50. i && i); // expected-warning {{'&&' within '||'}} \
  51. // expected-note {{place parentheses around the '&&' expression to silence this warning}}
  52. // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"("
  53. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
  54. (void)(i || i && "w00t"); // no warning.
  55. (void)("w00t" && i || i); // no warning.
  56. (void)(i || i && "w00t" || i); // expected-warning {{'&&' within '||'}} \
  57. // expected-note {{place parentheses around the '&&' expression to silence this warning}}
  58. // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
  59. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
  60. (void)(i || "w00t" && i || i); // expected-warning {{'&&' within '||'}} \
  61. // expected-note {{place parentheses around the '&&' expression to silence this warning}}
  62. // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
  63. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
  64. (void)(i && i || 0); // no warning.
  65. (void)(0 || i && i); // no warning.
  66. }
  67. _Bool someConditionFunc();
  68. void conditional_op(int x, int y, _Bool b, void* p) {
  69. (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
  70. // expected-note {{place parentheses around the '+' expression to silence this warning}} \
  71. // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
  72. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  73. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")"
  74. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
  75. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")"
  76. (void)((x + someConditionFunc()) ? 1 : 2); // no warning
  77. (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
  78. // expected-note {{place parentheses around the '-' expression to silence this warning}} \
  79. // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
  80. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  81. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"
  82. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
  83. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
  84. (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
  85. // expected-note {{place parentheses around the '*' expression to silence this warning}} \
  86. // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
  87. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  88. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
  89. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
  90. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")"
  91. (void)(x / !x ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '/'}} \
  92. // expected-note {{place parentheses around the '/' expression to silence this warning}} \
  93. // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
  94. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  95. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
  96. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
  97. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:24-[[@LINE-6]]:24}:")"
  98. (void)(x % 2 ? 1 : 2); // no warning
  99. (void)(x + p ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}
  100. (void)(p + x ? 1 : 2); // no warning
  101. (void)(p + b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}
  102. (void)(x + y > 0 ? 1 : 2); // no warning
  103. (void)(x + (y > 0) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}
  104. }
  105. // RUN: not %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s -check-prefix=CHECK-FLAG
  106. // CHECK-FLAG: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]