summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDavid Bolvansky <david.bolvansky@gmail.com>2019-09-22 22:00:48 +0000
committerDavid Bolvansky <david.bolvansky@gmail.com>2019-09-22 22:00:48 +0000
commitb796ddf8a372d34c5d24107f59ead4cfbe850297 (patch)
tree80bf35e71325a52c7ad4e22643a2c6933a01718b /test
parentc60de9debdf43be97665726bd9e58a8284df541a (diff)
downloadclang-b796ddf8a372d34c5d24107f59ead4cfbe850297.tar.gz
[Diagnostics] Warn if ?: with integer constants always evaluates to true
Extracted from D63082. GCC has this warning under -Wint-in-bool-context, but as noted in the D63082's review, we should put it under TautologicalConstantCompare. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@372531 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test')
-rw-r--r--test/Sema/warn-integer-constants-in-ternary.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/test/Sema/warn-integer-constants-in-ternary.c b/test/Sema/warn-integer-constants-in-ternary.c
new file mode 100644
index 0000000000..287b91ecdb
--- /dev/null
+++ b/test/Sema/warn-integer-constants-in-ternary.c
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -x c -fsyntax-only -verify -Wtautological-constant-compare %s
+// RUN: %clang_cc1 -x c -fsyntax-only -verify %s
+// RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wtautological-constant-compare %s
+// RUN: %clang_cc1 -x c++ -fsyntax-only -verify %s
+
+#define ONE 1
+#define TWO 2
+
+#define TERN(c, l, r) c ? l : r
+
+#ifdef __cplusplus
+typedef bool boolean;
+#else
+typedef _Bool boolean;
+#endif
+
+void test(boolean a) {
+ boolean r;
+ r = a ? (1) : TWO;
+ r = a ? 3 : TWO; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
+ r = a ? -2 : 0; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
+ r = a ? 3 : -2; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
+ r = a ? 0 : TWO;
+ r = a ? 3 : ONE; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
+ r = a ? ONE : 0;
+ r = a ? 0 : -0;
+ r = a ? 1 : 0;
+ r = a ? ONE : 0;
+ r = a ? ONE : ONE;
+ r = TERN(a, 4, 8); // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
+ r = TERN(a, -1, -8); // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
+}