Home | History | Annotate | Download | only in Sema
      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 
      4 bool someConditionFunc();
      5 
      6 void conditional_op(int x, int y, bool b) {
      7   (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
      8                                            // expected-note {{place parentheses around the '+' expression to silence this warning}} \
      9                                            // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
     10   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
     11   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")"
     12   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
     13   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")"
     14 
     15   (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' 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]]:10-[[@LINE-3]]:10}:"("
     19   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"
     20   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
     21   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
     22 
     23   (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
     24                                 // expected-note {{place parentheses around the '*' expression to silence this warning}} \
     25                                 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
     26   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
     27   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
     28   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
     29   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")"
     30 }
     31 
     32 class Stream {
     33 public:
     34   operator int();
     35   Stream &operator<<(int);
     36   Stream &operator<<(const char*);
     37   Stream &operator>>(int);
     38   Stream &operator>>(const char*);
     39 };
     40 
     41 void f(Stream& s, bool b) {
     42   (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \
     43                                   // expected-note {{place parentheses around the '<<' expression to silence this warning}} \
     44                                   // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
     45   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
     46   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
     47   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
     48   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:32-[[@LINE-6]]:32}:")"
     49 
     50   (void)(s << 5 == 1); // expected-warning {{overloaded operator << has lower 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   (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has lower precedence than comparison operator}} \
     59                        // expected-note {{place parentheses around the '>>' expression to silence this warning}} \
     60                        // expected-note {{place parentheses around comparison expression to evaluate it first}}
     61   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
     62   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
     63   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
     64   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
     65 }
     66 
     67 struct S {
     68   operator int() { return 42; }
     69   friend S operator+(const S &lhs, bool) { return S(); }
     70 };
     71 
     72 void test(S *s, bool (S::*m_ptr)()) {
     73   (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \
     74                                      // expected-note {{place parentheses around the '+' expression to silence this warning}} \
     75                                      // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
     76   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
     77   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:19-[[@LINE-4]]:19}:")"
     78   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
     79   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:35-[[@LINE-6]]:35}:")"
     80 
     81   (void)((*s + true) ? "foo" : "bar"); // No warning.
     82 
     83   // Don't crash on unusual member call expressions.
     84   (void)((s->*m_ptr)() ? "foo" : "bar");
     85 }
     86 
     87 void test(int a, int b, int c) {
     88   (void)(a >> b + c); // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
     89                          expected-note {{place parentheses around the '+' expression to silence this warning}}
     90   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
     91   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
     92 
     93   (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \
     94                          expected-note {{place parentheses around the '-' expression to silence this warning}}
     95   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"("
     96   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")"
     97 
     98   Stream() << b + c;
     99   Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
    100                         expected-note {{place parentheses around the '+' expression to silence this warning}}
    101   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
    102   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
    103 }
    104 
    105 namespace PR15628 {
    106   struct BlockInputIter {
    107     void* operator++(int);
    108     void* operator--(int);
    109   };
    110 
    111   void test(BlockInputIter i) {
    112     (void)(i++ ? true : false); // no-warning
    113     (void)(i-- ? true : false); // no-warning
    114   }
    115 }
    116