aboutsummaryrefslogtreecommitdiff
path: root/test/SemaCXX/null_in_arithmetic_ops.cpp
diff options
context:
space:
mode:
authorRichard Trieu <rtrieu@google.com>2011-06-16 21:36:56 +0000
committerRichard Trieu <rtrieu@google.com>2011-06-16 21:36:56 +0000
commit3e95ba94fd34c5f6420c57d7732f601875074681 (patch)
treef9d91882f472c5ab6fc330c73d0819f5e98ed7f9 /test/SemaCXX/null_in_arithmetic_ops.cpp
parenta92d7e7a55a35b28437103130904a6401bf35408 (diff)
Add a new warning when a NULL constant is used in arithmetic operations. The warning will fire on cases such as:
int x = 1 + NULL; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133196 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/SemaCXX/null_in_arithmetic_ops.cpp')
-rw-r--r--test/SemaCXX/null_in_arithmetic_ops.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/test/SemaCXX/null_in_arithmetic_ops.cpp b/test/SemaCXX/null_in_arithmetic_ops.cpp
new file mode 100644
index 0000000000..547c936c3e
--- /dev/null
+++ b/test/SemaCXX/null_in_arithmetic_ops.cpp
@@ -0,0 +1,70 @@
+// RUN: %clang_cc1 -fsyntax-only -fblocks -verify %s
+#include <stddef.h>
+
+void f() {
+ int a;
+ bool b;
+
+ a = 0 ? NULL + a : a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
+ a = 0 ? NULL - a : a - NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
+ a = 0 ? NULL / a : a / NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \
+ // expected-warning {{division by zero is undefined}}
+ a = 0 ? NULL * a : a * NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
+ a = 0 ? NULL >> a : a >> NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
+ a = 0 ? NULL << a : a << NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
+ a = 0 ? NULL % a : a % NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \
+ expected-warning {{remainder by zero is undefined}}
+ a = 0 ? NULL & a : a & NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
+ a = 0 ? NULL | a : a | NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
+ a = 0 ? NULL ^ a : a ^ NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
+
+ // Using two NULLs should only give one error instead of two.
+ a = NULL + NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a = NULL - NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a = NULL / NULL; // expected-warning{{use of NULL in arithmetic operation}} \
+ // expected-warning{{division by zero is undefined}}
+ a = NULL * NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a = NULL >> NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a = NULL << NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a = NULL % NULL; // expected-warning{{use of NULL in arithmetic operation}} \
+ // expected-warning{{remainder by zero is undefined}}
+ a = NULL & NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a = NULL | NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a = NULL ^ NULL; // expected-warning{{use of NULL in arithmetic operation}}
+
+ a += NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a -= NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a /= NULL; // expected-warning{{use of NULL in arithmetic operation}} \
+ // expected-warning{{division by zero is undefined}}
+ a *= NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a >>= NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a <<= NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a %= NULL; // expected-warning{{use of NULL in arithmetic operation}} \
+ // expected-warning{{remainder by zero is undefined}}
+ a &= NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a |= NULL; // expected-warning{{use of NULL in arithmetic operation}}
+ a ^= NULL; // expected-warning{{use of NULL in arithmetic operation}}
+
+ b = a < NULL || NULL < a; // expected-warning 2{{use of NULL in arithmetic operation}}
+ b = a > NULL || NULL > a; // expected-warning 2{{use of NULL in arithmetic operation}}
+ b = a <= NULL || NULL <= a; // expected-warning 2{{use of NULL in arithmetic operation}}
+ b = a >= NULL || NULL >= a; // expected-warning 2{{use of NULL in arithmetic operation}}
+ b = a == NULL || NULL == a; // expected-warning 2{{use of NULL in arithmetic operation}}
+ b = a != NULL || NULL != a; // expected-warning 2{{use of NULL in arithmetic operation}}
+
+ b = &a < NULL || NULL < &a || &a > NULL || NULL > &a;
+ b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a;
+ b = &a == NULL || NULL == &a || &a != NULL || NULL != &a;
+
+ b = 0 == a;
+ b = 0 == &a;
+
+ b = ((NULL)) != a; // expected-warning{{use of NULL in arithmetic operation}}
+
+ void (^c)();
+ b = c == NULL || NULL == c || c != NULL || NULL != c;
+
+ class X;
+ void (X::*d) ();
+ b = d == NULL || NULL == d || d != NULL || NULL != d;
+}