blob: da37dd397bbaf5b484d5a7c886bd69b5fff5ef15 [file] [log] [blame]
Hans Wennborg9cfdae32011-06-03 18:00:36 +00001// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
2// RUN: %clang_cc1 -Wparentheses -fixit %s -o - | %clang_cc1 -Wparentheses -Werror -
3
4bool someConditionFunc();
5
6void conditional_op(int x, int y, bool b) {
Chandler Carruth9d456242011-06-16 01:05:12 +00007 (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
8 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
9 // expected-note {{place parentheses around the '+' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000010
Chandler Carruth9d456242011-06-16 01:05:12 +000011 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
12 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
13 // expected-note {{place parentheses around the '-' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000014
Chandler Carruth9d456242011-06-16 01:05:12 +000015 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
16 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
17 // expected-note {{place parentheses around the '*' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000018}
Hans Wennborg2f072b42011-06-09 17:06:51 +000019
20class Stream {
21public:
22 operator int();
23 Stream &operator<<(int);
24 Stream &operator<<(const char*);
David Blaikieb3f55c52012-10-05 00:41:03 +000025 Stream &operator>>(int);
26 Stream &operator>>(const char*);
Hans Wennborg2f072b42011-06-09 17:06:51 +000027};
28
29void f(Stream& s, bool b) {
Chandler Carruth9d456242011-06-16 01:05:12 +000030 (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \
31 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
32 // expected-note {{place parentheses around the '<<' expression to silence this warning}}
Hans Wennborg2f072b42011-06-09 17:06:51 +000033}
Chandler Carruth14d251c2011-06-21 17:22:09 +000034
35struct S {
36 operator int() { return 42; }
37 friend S operator+(const S &lhs, bool) { return S(); }
38};
39
40void test(S *s, bool (S::*m_ptr)()) {
41 (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \
42 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
43 // expected-note {{place parentheses around the '+' expression to silence this warning}}
44
Hans Wennborgcb4d7c22011-09-12 12:07:30 +000045 (void)((*s + true) ? "foo" : "bar"); // No warning.
46
Chandler Carruth14d251c2011-06-21 17:22:09 +000047 // Don't crash on unusual member call expressions.
48 (void)((s->*m_ptr)() ? "foo" : "bar");
49}
David Blaikieb3f55c52012-10-05 00:41:03 +000050
51void test(int a, int b, int c) {
David Blaikie5f531a42012-10-19 18:26:06 +000052 (void)(a >> b + c); // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000053 expected-note {{place parentheses around the '+' expression to silence this warning}}
David Blaikie5f531a42012-10-19 18:26:06 +000054 (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000055 expected-note {{place parentheses around the '-' expression to silence this warning}}
56 Stream() << b + c;
David Blaikie5f531a42012-10-19 18:26:06 +000057 Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000058 expected-note {{place parentheses around the '+' expression to silence this warning}}
59}
Benjamin Kramer66dca6e2013-03-30 11:56:00 +000060
61namespace PR15628 {
62 struct BlockInputIter {
63 void* operator++(int);
64 void* operator--(int);
65 };
66
67 void test(BlockInputIter i) {
68 (void)(i++ ? true : false); // no-warning
69 (void)(i-- ? true : false); // no-warning
70 }
71}