summaryrefslogtreecommitdiffstats
path: root/clang/test
diff options
context:
space:
mode:
authorHans Wennborg <hans@hanshq.net>2011-06-03 18:00:36 +0000
committerHans Wennborg <hans@hanshq.net>2011-06-03 18:00:36 +0000
commitcf9bac4bc9dd89bf15491f261d9f9ae695d898d0 (patch)
treeb12d7ac2d0fcd42993c8a2a43cc3393b422c4303 /clang/test
parentea31988f63b7a34b3aeefa60dbf9bb5fbd843e96 (diff)
downloadbcm5719-llvm-cf9bac4bc9dd89bf15491f261d9f9ae695d898d0.tar.gz
bcm5719-llvm-cf9bac4bc9dd89bf15491f261d9f9ae695d898d0.zip
Warn about missing parentheses for conditional operator.
Warn in cases such as "x + someCondition ? 42 : 0;", where the condition expression looks arithmetic, and has a right-hand side that looks boolean. This (partly) addresses http://llvm.org/bugs/show_bug.cgi?id=9969 llvm-svn: 132565
Diffstat (limited to 'clang/test')
-rw-r--r--clang/test/Sema/parentheses.c24
-rw-r--r--clang/test/Sema/parentheses.cpp18
2 files changed, 41 insertions, 1 deletions
diff --git a/clang/test/Sema/parentheses.c b/clang/test/Sema/parentheses.c
index a25ded66a66..fa3c3c26b04 100644
--- a/clang/test/Sema/parentheses.c
+++ b/clang/test/Sema/parentheses.c
@@ -39,6 +39,28 @@ void bitwise_rel(unsigned i) {
(void)(0 || i && i); // no warning.
}
+_Bool someConditionFunc();
+
+void conditional_op(int x, int y, _Bool b) {
+ (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{?: has lower precedence than +}} \
+ // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
+ // expected-note {{place parentheses around the + expression to silence this warning}}
+
+ (void)(x - b ? 1 : 2); // expected-warning {{?: has lower precedence than -}} \
+ // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
+ // expected-note {{place parentheses around the - expression to silence this warning}}
+
+ (void)(x * (x == y) ? 1 : 2); // expected-warning {{?: has lower precedence than *}} \
+ // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
+ // expected-note {{place parentheses around the * expression to silence this warning}}
+
+ (void)(x / !x ? 1 : 2); // expected-warning {{?: has lower precedence than /}} \
+ // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
+ // expected-note {{place parentheses around the / expression to silence this warning}}
+
+
+ (void)(x % 2 ? 1 : 2); // no warning
+}
+
// RUN: %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s
// CHECK: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]
-
diff --git a/clang/test/Sema/parentheses.cpp b/clang/test/Sema/parentheses.cpp
new file mode 100644
index 00000000000..ad1f399c649
--- /dev/null
+++ b/clang/test/Sema/parentheses.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
+// RUN: %clang_cc1 -Wparentheses -fixit %s -o - | %clang_cc1 -Wparentheses -Werror -
+
+bool someConditionFunc();
+
+void conditional_op(int x, int y, bool b) {
+ (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{?: has lower precedence than +}} \
+ // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
+ // expected-note {{place parentheses around the + expression to silence this warning}}
+
+ (void)(x - b ? 1 : 2); // expected-warning {{?: has lower precedence than -}} \
+ // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
+ // expected-note {{place parentheses around the - expression to silence this warning}}
+
+ (void)(x * (x == y) ? 1 : 2); // expected-warning {{?: has lower precedence than *}} \
+ // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
+ // expected-note {{place parentheses around the * expression to silence this warning}}
+}
OpenPOWER on IntegriCloud