2014-02-26 10:36:06 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -fblocks -Wnull-arithmetic -verify -Wno-string-plus-int -Wno-tautological-pointer-compare %s
|
2011-06-17 05:36:56 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
void f() {
|
|
|
|
int a;
|
|
|
|
bool b;
|
2011-06-20 15:38:51 +08:00
|
|
|
void (^c)();
|
|
|
|
class X;
|
|
|
|
void (X::*d) ();
|
|
|
|
extern void e();
|
|
|
|
int f[2];
|
|
|
|
const void *v;
|
2011-06-17 05:36:56 +08:00
|
|
|
|
|
|
|
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}}
|
|
|
|
|
2011-06-20 15:38:51 +08:00
|
|
|
// Check for warnings or errors when doing arithmetic on pointers and other
|
|
|
|
// types.
|
|
|
|
v = 0 ? NULL + &a : &a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
|
|
|
|
v = 0 ? NULL + c : c + NULL; // \
|
|
|
|
expected-error {{invalid operands to binary expression ('long' and 'void (^)()')}} \
|
|
|
|
expected-error {{invalid operands to binary expression ('void (^)()' and 'long')}}
|
|
|
|
v = 0 ? NULL + d : d + NULL; // \
|
|
|
|
expected-error {{invalid operands to binary expression ('long' and 'void (X::*)()')}} \
|
|
|
|
expected-error {{invalid operands to binary expression ('void (X::*)()' and 'long')}}
|
2011-06-27 16:02:19 +08:00
|
|
|
v = 0 ? NULL + e : e + NULL; // expected-error 2{{arithmetic on a pointer to the function type 'void ()'}}
|
2011-06-20 15:38:51 +08:00
|
|
|
v = 0 ? NULL + f : f + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
|
|
|
|
v = 0 ? NULL + "f" : "f" + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
|
|
|
|
|
2011-06-17 05:36:56 +08:00
|
|
|
// 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}}
|
|
|
|
|
2011-08-12 06:38:21 +08:00
|
|
|
b = a < NULL || a > NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
|
|
|
|
b = NULL < a || NULL > a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
|
|
|
|
b = a <= NULL || a >= NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
|
|
|
|
b = NULL <= a || NULL >= a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
|
|
|
|
b = a == NULL || a != NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
|
|
|
|
b = NULL == a || NULL != a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
|
2011-06-17 05:36:56 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-06-19 17:05:14 +08:00
|
|
|
b = NULL < NULL || NULL > NULL;
|
|
|
|
b = NULL <= NULL || NULL >= NULL;
|
|
|
|
b = NULL == NULL || NULL != NULL;
|
|
|
|
|
2011-08-12 06:38:21 +08:00
|
|
|
b = ((NULL)) != a; // expected-warning{{comparison between NULL and non-pointer (NULL and 'int')}}
|
2011-06-17 05:36:56 +08:00
|
|
|
|
2011-06-20 15:38:51 +08:00
|
|
|
// Check that even non-standard pointers don't warn.
|
2011-06-17 05:36:56 +08:00
|
|
|
b = c == NULL || NULL == c || c != NULL || NULL != c;
|
|
|
|
b = d == NULL || NULL == d || d != NULL || NULL != d;
|
2011-06-19 17:05:14 +08:00
|
|
|
b = e == NULL || NULL == e || e != NULL || NULL != e;
|
|
|
|
b = f == NULL || NULL == f || f != NULL || NULL != f;
|
|
|
|
b = "f" == NULL || NULL == "f" || "f" != NULL || NULL != "f";
|
2012-11-08 08:41:20 +08:00
|
|
|
|
|
|
|
return NULL; // expected-error{{void function 'f' should not return a value}}
|
2011-06-17 05:36:56 +08:00
|
|
|
}
|