blob: 58edc0ba294da9ef3afbea305836b085c9c59c51 [file] [log] [blame]
Hans Wennborg9cfdae32011-06-03 18:00:36 +00001// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
Hans Wennborg9cfdae32011-06-03 18:00:36 +00002
3bool someConditionFunc();
4
5void conditional_op(int x, int y, bool b) {
Chandler Carruth9d456242011-06-16 01:05:12 +00006 (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
7 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
8 // expected-note {{place parentheses around the '+' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +00009
Chandler Carruth9d456242011-06-16 01:05:12 +000010 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
11 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
12 // expected-note {{place parentheses around the '-' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000013
Chandler Carruth9d456242011-06-16 01:05:12 +000014 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
15 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
16 // expected-note {{place parentheses around the '*' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000017}
Hans Wennborg2f072b42011-06-09 17:06:51 +000018
19class Stream {
20public:
21 operator int();
22 Stream &operator<<(int);
23 Stream &operator<<(const char*);
David Blaikieb3f55c52012-10-05 00:41:03 +000024 Stream &operator>>(int);
25 Stream &operator>>(const char*);
Hans Wennborg2f072b42011-06-09 17:06:51 +000026};
27
28void f(Stream& s, bool b) {
Chandler Carruth9d456242011-06-16 01:05:12 +000029 (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \
30 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
31 // expected-note {{place parentheses around the '<<' expression to silence this warning}}
Richard Trieu2a6e5282013-04-17 02:12:45 +000032 (void)(s << 5 == 1); // expected-warning {{overloaded operator << has lower precedence than comparison operator}} \
33 // expected-note {{place parentheses around the '<<' expression to silence this warning}} \
34 // expected-note {{place parentheses around comparison expression to evaluate it first}}
35 (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has lower precedence than comparison operator}} \
36 // expected-note {{place parentheses around the '>>' expression to silence this warning}} \
37 // expected-note {{place parentheses around comparison expression to evaluate it first}}
Hans Wennborg2f072b42011-06-09 17:06:51 +000038}
Chandler Carruth14d251c2011-06-21 17:22:09 +000039
40struct S {
41 operator int() { return 42; }
42 friend S operator+(const S &lhs, bool) { return S(); }
43};
44
45void test(S *s, bool (S::*m_ptr)()) {
46 (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \
47 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
48 // expected-note {{place parentheses around the '+' expression to silence this warning}}
49
Hans Wennborgcb4d7c22011-09-12 12:07:30 +000050 (void)((*s + true) ? "foo" : "bar"); // No warning.
51
Chandler Carruth14d251c2011-06-21 17:22:09 +000052 // Don't crash on unusual member call expressions.
53 (void)((s->*m_ptr)() ? "foo" : "bar");
54}
David Blaikieb3f55c52012-10-05 00:41:03 +000055
56void test(int a, int b, int c) {
David Blaikie5f531a42012-10-19 18:26:06 +000057 (void)(a >> 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}}
David Blaikie5f531a42012-10-19 18:26:06 +000059 (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000060 expected-note {{place parentheses around the '-' expression to silence this warning}}
61 Stream() << b + c;
David Blaikie5f531a42012-10-19 18:26:06 +000062 Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000063 expected-note {{place parentheses around the '+' expression to silence this warning}}
64}
Benjamin Kramer66dca6e2013-03-30 11:56:00 +000065
66namespace PR15628 {
67 struct BlockInputIter {
68 void* operator++(int);
69 void* operator--(int);
70 };
71
72 void test(BlockInputIter i) {
73 (void)(i++ ? true : false); // no-warning
74 (void)(i-- ? true : false); // no-warning
75 }
76}