13bb8b56aSRichard Trieu // RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -fblocks -Wnull-arithmetic -verify -Wno-string-plus-int -Wno-tautological-pointer-compare %s
2701fb36bSRichard Trieu #include <stddef.h>
3701fb36bSRichard Trieu 
f()4701fb36bSRichard Trieu void f() {
5701fb36bSRichard Trieu   int a;
6701fb36bSRichard Trieu   bool b;
74f04b436SChandler Carruth   void (^c)();
84f04b436SChandler Carruth   class X;
94f04b436SChandler Carruth   void (X::*d) ();
104f04b436SChandler Carruth   extern void e();
114f04b436SChandler Carruth   int f[2];
124f04b436SChandler Carruth   const void *v;
13701fb36bSRichard Trieu 
14701fb36bSRichard Trieu   a = 0 ? NULL + a : a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
15701fb36bSRichard Trieu   a = 0 ? NULL - a : a - NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
16701fb36bSRichard Trieu   a = 0 ? NULL / a : a / NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \
17701fb36bSRichard Trieu                                // expected-warning {{division by zero is undefined}}
18701fb36bSRichard Trieu   a = 0 ? NULL * a : a * NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
19701fb36bSRichard Trieu   a = 0 ? NULL >> a : a >> NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
20701fb36bSRichard Trieu   a = 0 ? NULL << a : a << NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
21701fb36bSRichard Trieu   a = 0 ? NULL % a : a % NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \
22701fb36bSRichard Trieu                                   expected-warning {{remainder by zero is undefined}}
23701fb36bSRichard Trieu   a = 0 ? NULL & a : a & NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
24701fb36bSRichard Trieu   a = 0 ? NULL | a : a | NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
25701fb36bSRichard Trieu   a = 0 ? NULL ^ a : a ^ NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
26701fb36bSRichard Trieu 
274f04b436SChandler Carruth   // Check for warnings or errors when doing arithmetic on pointers and other
284f04b436SChandler Carruth   // types.
294f04b436SChandler Carruth   v = 0 ? NULL + &a : &a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
304f04b436SChandler Carruth   v = 0 ? NULL + c : c + NULL; // \
314f04b436SChandler Carruth     expected-error {{invalid operands to binary expression ('long' and 'void (^)()')}} \
324f04b436SChandler Carruth     expected-error {{invalid operands to binary expression ('void (^)()' and 'long')}}
334f04b436SChandler Carruth   v = 0 ? NULL + d : d + NULL; // \
344f04b436SChandler Carruth     expected-error {{invalid operands to binary expression ('long' and 'void (X::*)()')}} \
354f04b436SChandler Carruth     expected-error {{invalid operands to binary expression ('void (X::*)()' and 'long')}}
36c9332218SChandler Carruth   v = 0 ? NULL + e : e + NULL; // expected-error 2{{arithmetic on a pointer to the function type 'void ()'}}
374f04b436SChandler Carruth   v = 0 ? NULL + f : f + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
384f04b436SChandler Carruth   v = 0 ? NULL + "f" : "f" + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
394f04b436SChandler Carruth 
40701fb36bSRichard Trieu   // Using two NULLs should only give one error instead of two.
41701fb36bSRichard Trieu   a = NULL + NULL; // expected-warning{{use of NULL in arithmetic operation}}
42701fb36bSRichard Trieu   a = NULL - NULL; // expected-warning{{use of NULL in arithmetic operation}}
43701fb36bSRichard Trieu   a = NULL / NULL; // expected-warning{{use of NULL in arithmetic operation}} \
44701fb36bSRichard Trieu                    // expected-warning{{division by zero is undefined}}
45701fb36bSRichard Trieu   a = NULL * NULL; // expected-warning{{use of NULL in arithmetic operation}}
46701fb36bSRichard Trieu   a = NULL >> NULL; // expected-warning{{use of NULL in arithmetic operation}}
47701fb36bSRichard Trieu   a = NULL << NULL; // expected-warning{{use of NULL in arithmetic operation}}
48701fb36bSRichard Trieu   a = NULL % NULL; // expected-warning{{use of NULL in arithmetic operation}} \
49701fb36bSRichard Trieu                    // expected-warning{{remainder by zero is undefined}}
50701fb36bSRichard Trieu   a = NULL & NULL; // expected-warning{{use of NULL in arithmetic operation}}
51701fb36bSRichard Trieu   a = NULL | NULL; // expected-warning{{use of NULL in arithmetic operation}}
52701fb36bSRichard Trieu   a = NULL ^ NULL; // expected-warning{{use of NULL in arithmetic operation}}
53701fb36bSRichard Trieu 
54701fb36bSRichard Trieu   a += NULL; // expected-warning{{use of NULL in arithmetic operation}}
55701fb36bSRichard Trieu   a -= NULL; // expected-warning{{use of NULL in arithmetic operation}}
56701fb36bSRichard Trieu   a /= NULL; // expected-warning{{use of NULL in arithmetic operation}} \
57701fb36bSRichard Trieu              // expected-warning{{division by zero is undefined}}
58701fb36bSRichard Trieu   a *= NULL; // expected-warning{{use of NULL in arithmetic operation}}
59701fb36bSRichard Trieu   a >>= NULL; // expected-warning{{use of NULL in arithmetic operation}}
60701fb36bSRichard Trieu   a <<= NULL; // expected-warning{{use of NULL in arithmetic operation}}
61701fb36bSRichard Trieu   a %= NULL; // expected-warning{{use of NULL in arithmetic operation}} \
62701fb36bSRichard Trieu              // expected-warning{{remainder by zero is undefined}}
63701fb36bSRichard Trieu   a &= NULL; // expected-warning{{use of NULL in arithmetic operation}}
64701fb36bSRichard Trieu   a |= NULL; // expected-warning{{use of NULL in arithmetic operation}}
65701fb36bSRichard Trieu   a ^= NULL; // expected-warning{{use of NULL in arithmetic operation}}
66701fb36bSRichard Trieu 
67aee9e767SRichard Trieu   b = a < NULL || a > NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
68aee9e767SRichard Trieu   b = NULL < a || NULL > a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
69aee9e767SRichard Trieu   b = a <= NULL || a >= NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
70aee9e767SRichard Trieu   b = NULL <= a || NULL >= a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
71aee9e767SRichard Trieu   b = a == NULL || a != NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
72aee9e767SRichard Trieu   b = NULL == a || NULL != a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
73701fb36bSRichard Trieu 
74*5e9746f5SRichard Smith   b = &a < NULL || NULL < &a || &a > NULL || NULL > &a; // expected-error 4{{ordered comparison between pointer and zero}}
75*5e9746f5SRichard Smith   b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a; // expected-error 4{{ordered comparison between pointer and zero}}
76701fb36bSRichard Trieu   b = &a == NULL || NULL == &a || &a != NULL || NULL != &a;
77701fb36bSRichard Trieu 
78701fb36bSRichard Trieu   b = 0 == a;
79701fb36bSRichard Trieu   b = 0 == &a;
80701fb36bSRichard Trieu 
81e1db1cf0SChandler Carruth   b = NULL < NULL || NULL > NULL;
82e1db1cf0SChandler Carruth   b = NULL <= NULL || NULL >= NULL;
83e1db1cf0SChandler Carruth   b = NULL == NULL || NULL != NULL;
84e1db1cf0SChandler Carruth 
85aee9e767SRichard Trieu   b = ((NULL)) != a;  // expected-warning{{comparison between NULL and non-pointer (NULL and 'int')}}
86701fb36bSRichard Trieu 
874f04b436SChandler Carruth   // Check that even non-standard pointers don't warn.
88701fb36bSRichard Trieu   b = c == NULL || NULL == c || c != NULL || NULL != c;
89701fb36bSRichard Trieu   b = d == NULL || NULL == d || d != NULL || NULL != d;
90e1db1cf0SChandler Carruth   b = e == NULL || NULL == e || e != NULL || NULL != e;
91e1db1cf0SChandler Carruth   b = f == NULL || NULL == f || f != NULL || NULL != f;
92e1db1cf0SChandler Carruth   b = "f" == NULL || NULL == "f" || "f" != NULL || NULL != "f";
93a1edff00SDavid Blaikie 
94a1edff00SDavid Blaikie   return NULL; // expected-error{{void function 'f' should not return a value}}
95701fb36bSRichard Trieu }
96