blob: 3b42ab44feb9ea415569c5a4a940e569d885d218 [file] [log] [blame]
Richard Trieu3bb8b562014-02-26 02:36:06 +00001// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -fblocks -Wnull-arithmetic -verify -Wno-string-plus-int -Wno-tautological-pointer-compare %s
Richard Trieu701fb362011-06-16 21:36:56 +00002#include <stddef.h>
3
4void f() {
5 int a;
6 bool b;
Chandler Carruth4f04b432011-06-20 07:38:51 +00007 void (^c)();
8 class X;
9 void (X::*d) ();
10 extern void e();
11 int f[2];
12 const void *v;
Richard Trieu701fb362011-06-16 21:36:56 +000013
14 a = 0 ? NULL + a : a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
15 a = 0 ? NULL - a : a - NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
16 a = 0 ? NULL / a : a / NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \
17 // expected-warning {{division by zero is undefined}}
18 a = 0 ? NULL * a : a * NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
19 a = 0 ? NULL >> a : a >> NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
20 a = 0 ? NULL << a : a << NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
21 a = 0 ? NULL % a : a % NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \
22 expected-warning {{remainder by zero is undefined}}
23 a = 0 ? NULL & a : a & NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
24 a = 0 ? NULL | a : a | NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
25 a = 0 ? NULL ^ a : a ^ NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
26
Chandler Carruth4f04b432011-06-20 07:38:51 +000027 // Check for warnings or errors when doing arithmetic on pointers and other
28 // types.
29 v = 0 ? NULL + &a : &a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
30 v = 0 ? NULL + c : c + NULL; // \
31 expected-error {{invalid operands to binary expression ('long' and 'void (^)()')}} \
32 expected-error {{invalid operands to binary expression ('void (^)()' and 'long')}}
33 v = 0 ? NULL + d : d + NULL; // \
34 expected-error {{invalid operands to binary expression ('long' and 'void (X::*)()')}} \
35 expected-error {{invalid operands to binary expression ('void (X::*)()' and 'long')}}
Chandler Carruthc9332212011-06-27 08:02:19 +000036 v = 0 ? NULL + e : e + NULL; // expected-error 2{{arithmetic on a pointer to the function type 'void ()'}}
Chandler Carruth4f04b432011-06-20 07:38:51 +000037 v = 0 ? NULL + f : f + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
38 v = 0 ? NULL + "f" : "f" + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
39
Richard Trieu701fb362011-06-16 21:36:56 +000040 // Using two NULLs should only give one error instead of two.
41 a = NULL + NULL; // expected-warning{{use of NULL in arithmetic operation}}
42 a = NULL - NULL; // expected-warning{{use of NULL in arithmetic operation}}
43 a = NULL / NULL; // expected-warning{{use of NULL in arithmetic operation}} \
44 // expected-warning{{division by zero is undefined}}
45 a = NULL * NULL; // expected-warning{{use of NULL in arithmetic operation}}
46 a = NULL >> NULL; // expected-warning{{use of NULL in arithmetic operation}}
47 a = NULL << NULL; // expected-warning{{use of NULL in arithmetic operation}}
48 a = NULL % NULL; // expected-warning{{use of NULL in arithmetic operation}} \
49 // expected-warning{{remainder by zero is undefined}}
50 a = NULL & NULL; // expected-warning{{use of NULL in arithmetic operation}}
51 a = NULL | NULL; // expected-warning{{use of NULL in arithmetic operation}}
52 a = NULL ^ NULL; // expected-warning{{use of NULL in arithmetic operation}}
53
54 a += NULL; // expected-warning{{use of NULL in arithmetic operation}}
55 a -= NULL; // expected-warning{{use of NULL in arithmetic operation}}
56 a /= NULL; // expected-warning{{use of NULL in arithmetic operation}} \
57 // expected-warning{{division by zero is undefined}}
58 a *= NULL; // expected-warning{{use of NULL in arithmetic operation}}
59 a >>= NULL; // expected-warning{{use of NULL in arithmetic operation}}
60 a <<= NULL; // expected-warning{{use of NULL in arithmetic operation}}
61 a %= NULL; // expected-warning{{use of NULL in arithmetic operation}} \
62 // expected-warning{{remainder by zero is undefined}}
63 a &= NULL; // expected-warning{{use of NULL in arithmetic operation}}
64 a |= NULL; // expected-warning{{use of NULL in arithmetic operation}}
65 a ^= NULL; // expected-warning{{use of NULL in arithmetic operation}}
66
Richard Trieuaee9e762011-08-11 22:38:21 +000067 b = a < NULL || a > NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
68 b = NULL < a || NULL > a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
69 b = a <= NULL || a >= NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
70 b = NULL <= a || NULL >= a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
71 b = a == NULL || a != NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
72 b = NULL == a || NULL != a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
Richard Trieu701fb362011-06-16 21:36:56 +000073
74 b = &a < NULL || NULL < &a || &a > NULL || NULL > &a;
75 b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a;
76 b = &a == NULL || NULL == &a || &a != NULL || NULL != &a;
77
78 b = 0 == a;
79 b = 0 == &a;
80
Chandler Carruthe1db1cf2011-06-19 09:05:14 +000081 b = NULL < NULL || NULL > NULL;
82 b = NULL <= NULL || NULL >= NULL;
83 b = NULL == NULL || NULL != NULL;
84
Richard Trieuaee9e762011-08-11 22:38:21 +000085 b = ((NULL)) != a; // expected-warning{{comparison between NULL and non-pointer (NULL and 'int')}}
Richard Trieu701fb362011-06-16 21:36:56 +000086
Chandler Carruth4f04b432011-06-20 07:38:51 +000087 // Check that even non-standard pointers don't warn.
Richard Trieu701fb362011-06-16 21:36:56 +000088 b = c == NULL || NULL == c || c != NULL || NULL != c;
Richard Trieu701fb362011-06-16 21:36:56 +000089 b = d == NULL || NULL == d || d != NULL || NULL != d;
Chandler Carruthe1db1cf2011-06-19 09:05:14 +000090 b = e == NULL || NULL == e || e != NULL || NULL != e;
Chandler Carruthe1db1cf2011-06-19 09:05:14 +000091 b = f == NULL || NULL == f || f != NULL || NULL != f;
92 b = "f" == NULL || NULL == "f" || "f" != NULL || NULL != "f";
David Blaikiea1edff02012-11-08 00:41:20 +000093
94 return NULL; // expected-error{{void function 'f' should not return a value}}
Richard Trieu701fb362011-06-16 21:36:56 +000095}