blob: 145414a53ac05c7f8a0f59b896ee581b6ef66675 [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}}
Hans Wennborg2f072b42011-06-09 17:06:51 +000032}
Chandler Carruth14d251c2011-06-21 17:22:09 +000033
34struct S {
35 operator int() { return 42; }
36 friend S operator+(const S &lhs, bool) { return S(); }
37};
38
39void test(S *s, bool (S::*m_ptr)()) {
40 (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \
41 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
42 // expected-note {{place parentheses around the '+' expression to silence this warning}}
43
Hans Wennborgcb4d7c22011-09-12 12:07:30 +000044 (void)((*s + true) ? "foo" : "bar"); // No warning.
45
Chandler Carruth14d251c2011-06-21 17:22:09 +000046 // Don't crash on unusual member call expressions.
47 (void)((s->*m_ptr)() ? "foo" : "bar");
48}
David Blaikieb3f55c52012-10-05 00:41:03 +000049
50void test(int a, int b, int c) {
David Blaikie5f531a42012-10-19 18:26:06 +000051 (void)(a >> b + c); // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000052 expected-note {{place parentheses around the '+' expression to silence this warning}}
David Blaikie5f531a42012-10-19 18:26:06 +000053 (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000054 expected-note {{place parentheses around the '-' expression to silence this warning}}
55 Stream() << b + c;
David Blaikie5f531a42012-10-19 18:26:06 +000056 Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000057 expected-note {{place parentheses around the '+' expression to silence this warning}}
58}
Benjamin Kramer66dca6e2013-03-30 11:56:00 +000059
60namespace PR15628 {
61 struct BlockInputIter {
62 void* operator++(int);
63 void* operator--(int);
64 };
65
66 void test(BlockInputIter i) {
67 (void)(i++ ? true : false); // no-warning
68 (void)(i-- ? true : false); // no-warning
69 }
70}