summaryrefslogtreecommitdiffstats
path: root/clang/test/Sema/warn-tautological-compare.c
diff options
context:
space:
mode:
authorFariborz Jahanian <fjahanian@apple.com>2014-11-11 19:59:16 +0000
committerFariborz Jahanian <fjahanian@apple.com>2014-11-11 19:59:16 +0000
commitc5fd4844da5233d514a43762d1670a912d7ce3ac (patch)
treeda6e7139c3ace3f9bc837af0d7b0f96e310f9a68 /clang/test/Sema/warn-tautological-compare.c
parent944547deab183814b199018a7389df91cd384cdb (diff)
downloadbcm5719-llvm-c5fd4844da5233d514a43762d1670a912d7ce3ac.tar.gz
bcm5719-llvm-c5fd4844da5233d514a43762d1670a912d7ce3ac.zip
Patch to warn when logical evaluation of operand evalutes to a true value;
That this is a c-only patch. c++ already has this warning. This addresses rdar://18716393 llvm-svn: 221702
Diffstat (limited to 'clang/test/Sema/warn-tautological-compare.c')
-rw-r--r--clang/test/Sema/warn-tautological-compare.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/clang/test/Sema/warn-tautological-compare.c b/clang/test/Sema/warn-tautological-compare.c
new file mode 100644
index 00000000000..2856eddc7b0
--- /dev/null
+++ b/clang/test/Sema/warn-tautological-compare.c
@@ -0,0 +1,80 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -verify %s
+// rdar://18716393
+
+extern int a[] __attribute__((weak));
+int b[] = {8,13,21};
+struct {
+ int x[10];
+} c;
+const char str[] = "text";
+
+void ignore() {
+ if (!a) {}
+}
+void test() {
+ if (!b) {} // expected-warning {{address of array 'b' will always evaluate to 'true'}}
+ if (b == 0) {} // expected-warning {{comparison of array 'b' equal to a null pointer is always false}}
+ if (!c.x) {} // expected-warning {{address of array 'c.x' will always evaluate to 'true'}}
+ if (c.x == 0) {} // expected-warning {{comparison of array 'c.x' equal to a null pointer is always false}}
+ if (!str) {} // expected-warning {{address of array 'str' will always evaluate to 'true'}}
+ if (0 == str) {} // expected-warning {{comparison of array 'str' equal to a null pointer is always false}}
+}
+
+int array[2];
+int test1()
+{
+ if (!array) { // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+ return array[0];
+ } else if (array != 0) { // expected-warning {{comparison of array 'array' not equal to a null pointer is always true}}
+ return array[1];
+ }
+ if (array == 0) // expected-warning {{comparison of array 'array' equal to a null pointer is always false}}
+ return 1;
+ return 0;
+}
+
+#define NULL (void*)0
+
+int test2(int* pointer, char ch, void * pv) {
+ if (!&pointer) { // expected-warning {{address of 'pointer' will always evaluate to 'true'}}
+ return 0;
+ }
+
+ if (&pointer) { // expected-warning {{address of 'pointer' will always evaluate to 'true'}}
+ return 0;
+ }
+
+ if (&pointer == NULL) {} // expected-warning {{comparison of address of 'pointer' equal to a null pointer is always false}}
+
+ if (&pointer != NULL) {} // expected-warning {{comparison of address of 'pointer' not equal to a null pointer is always true}}
+
+ return 1;
+}
+
+void test3() {
+ if (array) { } // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+ if (array != 0) {} // expected-warning {{comparison of array 'array' not equal to a null pointer is always true}}
+ if (!array) { } // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+ if (array == 0) {} // expected-warning {{comparison of array 'array' equal to a null pointer is always false}}
+
+ if (array[0] &&
+ array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+
+ if (array[0] ||
+ array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+
+ if (array[0] &&
+ !array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+ if (array[0] ||
+ !array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+
+ if (array && // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+ array[0]) {}
+ if (!array || // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+ array[0]) {}
+
+ if (array || // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+ (!array && array[0])) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
+ }
+
+
OpenPOWER on IntegriCloud