parentheses.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. bool someConditionFunc();
  4. void conditional_op(int x, int y, bool b) {
  5. (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
  6. // expected-note {{place parentheses around the '+' expression to silence this warning}} \
  7. // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
  8. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  9. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")"
  10. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
  11. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")"
  12. (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
  13. // expected-note {{place parentheses around the '-' expression to silence this warning}} \
  14. // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
  15. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  16. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"
  17. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
  18. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
  19. (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
  20. // expected-note {{place parentheses around the '*' expression to silence this warning}} \
  21. // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
  22. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  23. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
  24. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
  25. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")"
  26. }
  27. class Stream {
  28. public:
  29. operator int();
  30. Stream &operator<<(int);
  31. Stream &operator<<(const char*);
  32. Stream &operator>>(int);
  33. Stream &operator>>(const char*);
  34. };
  35. void f(Stream& s, bool b) {
  36. (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \
  37. // expected-note {{place parentheses around the '<<' expression to silence this warning}} \
  38. // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
  39. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  40. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
  41. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
  42. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:32-[[@LINE-6]]:32}:")"
  43. (void)(s << 5 == 1); // expected-warning {{overloaded operator << has higher precedence than comparison operator}} \
  44. // expected-note {{place parentheses around the '<<' expression to silence this warning}} \
  45. // expected-note {{place parentheses around comparison expression to evaluate it first}}
  46. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  47. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
  48. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
  49. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
  50. (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has higher precedence than comparison operator}} \
  51. // expected-note {{place parentheses around the '>>' expression to silence this warning}} \
  52. // expected-note {{place parentheses around comparison expression to evaluate it first}}
  53. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  54. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
  55. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
  56. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
  57. }
  58. struct S {
  59. operator int() { return 42; }
  60. friend S operator+(const S &lhs, bool) { return S(); }
  61. };
  62. void test(S *s, bool (S::*m_ptr)()) {
  63. (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \
  64. // expected-note {{place parentheses around the '+' expression to silence this warning}} \
  65. // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
  66. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
  67. // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:19-[[@LINE-4]]:19}:")"
  68. // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
  69. // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:35-[[@LINE-6]]:35}:")"
  70. (void)((*s + true) ? "foo" : "bar"); // No warning.
  71. // Don't crash on unusual member call expressions.
  72. (void)((s->*m_ptr)() ? "foo" : "bar");
  73. }
  74. void test(int a, int b, int c) {
  75. (void)(a >> b + c); // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
  76. expected-note {{place parentheses around the '+' expression to silence this warning}}
  77. // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
  78. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
  79. (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \
  80. expected-note {{place parentheses around the '-' expression to silence this warning}}
  81. // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"("
  82. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")"
  83. Stream() << b + c;
  84. Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
  85. expected-note {{place parentheses around the '+' expression to silence this warning}}
  86. // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
  87. // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
  88. }
  89. namespace PR15628 {
  90. struct BlockInputIter {
  91. void* operator++(int);
  92. void* operator--(int);
  93. };
  94. void test(BlockInputIter i) {
  95. (void)(i++ ? true : false); // no-warning
  96. (void)(i-- ? true : false); // no-warning
  97. }
  98. }
  99. namespace PR20735 {
  100. struct X {
  101. static int state;
  102. static int get();
  103. int get_num();
  104. int num;
  105. };
  106. namespace ns {
  107. int num = 0;
  108. int get();
  109. }
  110. void test(X x) {
  111. if (5 & x.get_num() != 0) {}
  112. // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
  113. // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
  114. // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
  115. // CHECK: place parentheses around the '!=' expression to silence this warning
  116. // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
  117. // fix-it:"{{.*}}":{[[@LINE-6]]:29-[[@LINE-6]]:29}:")"
  118. // CHECK: place parentheses around the & expression to evaluate it first
  119. // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
  120. // fix-it:"{{.*}}":{[[@LINE-9]]:24-[[@LINE-9]]:24}:")"
  121. if (5 & x.num != 0) {}
  122. // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
  123. // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
  124. // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
  125. // CHECK: place parentheses around the '!=' expression to silence this warning
  126. // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
  127. // fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
  128. // CHECK: place parentheses around the & expression to evaluate it first
  129. // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
  130. // fix-it:"{{.*}}":{[[@LINE-9]]:18-[[@LINE-9]]:18}:")"
  131. if (5 & x.state != 0) {}
  132. // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
  133. // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
  134. // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
  135. // CHECK: place parentheses around the '!=' expression to silence this warning
  136. // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
  137. // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"
  138. // CHECK: place parentheses around the & expression to evaluate it first
  139. // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
  140. // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"
  141. if (5 & x.get() != 0) {}
  142. // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
  143. // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
  144. // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
  145. // CHECK: place parentheses around the '!=' expression to silence this warning
  146. // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
  147. // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"
  148. // CHECK: place parentheses around the & expression to evaluate it first
  149. // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
  150. // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"
  151. if (5 & X::state != 0) {}
  152. // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
  153. // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
  154. // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
  155. // CHECK: place parentheses around the '!=' expression to silence this warning
  156. // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
  157. // fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
  158. // CHECK: place parentheses around the & expression to evaluate it first
  159. // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
  160. // fix-it:"{{.*}}":{[[@LINE-9]]:21-[[@LINE-9]]:21}:")"
  161. if (5 & X::get() != 0) {}
  162. // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
  163. // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
  164. // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
  165. // CHECK: place parentheses around the '!=' expression to silence this warning
  166. // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
  167. // fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
  168. // CHECK: place parentheses around the & expression to evaluate it first
  169. // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
  170. // fix-it:"{{.*}}":{[[@LINE-9]]:21-[[@LINE-9]]:21}:")"
  171. if (5 & ns::get() != 0) {}
  172. // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
  173. // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
  174. // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
  175. // CHECK: place parentheses around the '!=' expression to silence this warning
  176. // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
  177. // fix-it:"{{.*}}":{[[@LINE-6]]:27-[[@LINE-6]]:27}:")"
  178. // CHECK: place parentheses around the & expression to evaluate it first
  179. // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
  180. // fix-it:"{{.*}}":{[[@LINE-9]]:22-[[@LINE-9]]:22}:")"
  181. if (5 & ns::num != 0) {}
  182. // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
  183. // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
  184. // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
  185. // CHECK: place parentheses around the '!=' expression to silence this warning
  186. // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
  187. // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"
  188. // CHECK: place parentheses around the & expression to evaluate it first
  189. // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
  190. // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"
  191. }
  192. }