blob: 7288f8cb2f0b00b164d1ab9d8b715a65525c6a75 [file] [log] [blame]
Hans Wennborg9cfdae32011-06-03 18:00:36 +00001// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
Richard Trieuaea52012013-04-18 00:56:23 +00002// RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
Hans Wennborg9cfdae32011-06-03 18:00:36 +00003
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 '+'}} \
Richard Trieuaea52012013-04-18 00:56:23 +00008 // 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}:")"
Hans Wennborg9cfdae32011-06-03 18:00:36 +000014
Chandler Carruth9d456242011-06-16 01:05:12 +000015 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
Richard Trieuaea52012013-04-18 00:56:23 +000016 // 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}:")"
Hans Wennborg9cfdae32011-06-03 18:00:36 +000022
Chandler Carruth9d456242011-06-16 01:05:12 +000023 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
Richard Trieuaea52012013-04-18 00:56:23 +000024 // 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}:")"
Hans Wennborg9cfdae32011-06-03 18:00:36 +000030}
Hans Wennborg2f072b42011-06-09 17:06:51 +000031
32class Stream {
33public:
34 operator int();
35 Stream &operator<<(int);
36 Stream &operator<<(const char*);
David Blaikieb3f55c52012-10-05 00:41:03 +000037 Stream &operator>>(int);
38 Stream &operator>>(const char*);
Hans Wennborg2f072b42011-06-09 17:06:51 +000039};
40
41void f(Stream& s, bool b) {
Chandler Carruth9d456242011-06-16 01:05:12 +000042 (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \
Richard Trieuaea52012013-04-18 00:56:23 +000043 // 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
Richard Trieu2a6e5282013-04-17 02:12:45 +000050 (void)(s << 5 == 1); // expected-warning {{overloaded operator << has lower precedence than comparison operator}} \
Richard Trieuaea52012013-04-18 00:56:23 +000051 // expected-note {{place parentheses around comparison expression to evaluate it first}} \
52 // expected-note {{place parentheses around the '<<' expression to silence this warning}}
53
Richard Trieu2a6e5282013-04-17 02:12:45 +000054 (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has lower precedence than comparison operator}} \
Richard Trieuaea52012013-04-18 00:56:23 +000055 // expected-note {{place parentheses around comparison expression to evaluate it first}} \
56 // expected-note {{place parentheses around the '>>' expression to silence this warning}}
57 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:"("
58 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:21-[[@LINE-4]]:21}:")"
59 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
60 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:")"
Hans Wennborg2f072b42011-06-09 17:06:51 +000061}
Chandler Carruth14d251c2011-06-21 17:22:09 +000062
63struct S {
64 operator int() { return 42; }
65 friend S operator+(const S &lhs, bool) { return S(); }
66};
67
68void test(S *s, bool (S::*m_ptr)()) {
69 (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \
Richard Trieuaea52012013-04-18 00:56:23 +000070 // expected-note {{place parentheses around the '+' expression to silence this warning}} \
71 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
72 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
73 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:19-[[@LINE-4]]:19}:")"
74 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
75 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:35-[[@LINE-6]]:35}:")"
Chandler Carruth14d251c2011-06-21 17:22:09 +000076
Hans Wennborgcb4d7c22011-09-12 12:07:30 +000077 (void)((*s + true) ? "foo" : "bar"); // No warning.
78
Chandler Carruth14d251c2011-06-21 17:22:09 +000079 // Don't crash on unusual member call expressions.
80 (void)((s->*m_ptr)() ? "foo" : "bar");
81}
David Blaikieb3f55c52012-10-05 00:41:03 +000082
83void test(int a, int b, int c) {
David Blaikie5f531a42012-10-19 18:26:06 +000084 (void)(a >> b + c); // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000085 expected-note {{place parentheses around the '+' expression to silence this warning}}
Richard Trieuaea52012013-04-18 00:56:23 +000086 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
87 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
88
David Blaikie5f531a42012-10-19 18:26:06 +000089 (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000090 expected-note {{place parentheses around the '-' expression to silence this warning}}
Richard Trieuaea52012013-04-18 00:56:23 +000091 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"("
92 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")"
93
David Blaikieb3f55c52012-10-05 00:41:03 +000094 Stream() << b + c;
David Blaikie5f531a42012-10-19 18:26:06 +000095 Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
David Blaikieb3f55c52012-10-05 00:41:03 +000096 expected-note {{place parentheses around the '+' expression to silence this warning}}
Richard Trieuaea52012013-04-18 00:56:23 +000097 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
98 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
David Blaikieb3f55c52012-10-05 00:41:03 +000099}
Benjamin Kramer66dca6e2013-03-30 11:56:00 +0000100
101namespace PR15628 {
102 struct BlockInputIter {
103 void* operator++(int);
104 void* operator--(int);
105 };
106
107 void test(BlockInputIter i) {
108 (void)(i++ ? true : false); // no-warning
109 (void)(i-- ? true : false); // no-warning
110 }
111}