llvm-project/clang/test/SemaCXX/null_in_arithmetic_ops.cpp
Richard Smith 5e9746f520 DR583, DR1512: Implement a rewrite to C++'s 'composite pointer type' rules.
This has two significant effects:

1) Direct relational comparisons between null pointer constants (0 and nullopt)
   and pointers are now ill-formed. This was always the case for C, and it
   appears that C++ only ever permitted by accident. For instance, cases like
     nullptr < &a
   are now rejected.

2) Comparisons and conditional operators between differently-cv-qualified
   pointer types now work, and produce a composite type that both source
   pointer types can convert to (when possible). For instance, comparison
   between 'int **' and 'const int **' is now valid, and uses an intermediate
   type of 'const int *const *'.

Clang previously supported #2 as an extension.

We do not accept the cases in #1 as an extension. I've tested a fair amount of
code to check that this doesn't break it, but if it turns out that someone is
relying on this, we can easily add it back as an extension.

This is a re-commit of r284800.

llvm-svn: 284890
2016-10-21 22:00:42 +00:00

96 lines
5.7 KiB
C++

// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -fblocks -Wnull-arithmetic -verify -Wno-string-plus-int -Wno-tautological-pointer-compare %s
#include <stddef.h>
void f() {
int a;
bool b;
void (^c)();
class X;
void (X::*d) ();
extern void e();
int f[2];
const void *v;
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}}
// 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')}}
v = 0 ? NULL + e : e + NULL; // expected-error 2{{arithmetic on a pointer to the function type 'void ()'}}
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}}
// 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 || 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')}}
b = &a < NULL || NULL < &a || &a > NULL || NULL > &a; // expected-error 4{{ordered comparison between pointer and zero}}
b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a; // expected-error 4{{ordered comparison between pointer and zero}}
b = &a == NULL || NULL == &a || &a != NULL || NULL != &a;
b = 0 == a;
b = 0 == &a;
b = NULL < NULL || NULL > NULL;
b = NULL <= NULL || NULL >= NULL;
b = NULL == NULL || NULL != NULL;
b = ((NULL)) != a; // expected-warning{{comparison between NULL and non-pointer (NULL and 'int')}}
// Check that even non-standard pointers don't warn.
b = c == NULL || NULL == c || c != NULL || NULL != c;
b = d == NULL || NULL == d || d != NULL || NULL != d;
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";
return NULL; // expected-error{{void function 'f' should not return a value}}
}