1 // RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wextra -std=c++11 2 3 #include <stdint.h> 4 5 void f(intptr_t offset) { 6 // A zero offset from a nullptr is OK. 7 char *f = (char*)nullptr + 0; 8 int *g = (int*)0 + 0; 9 f = (char*)nullptr - 0; 10 g = (int*)nullptr - 0; 11 // adding other values is undefined. 12 f = (char*)nullptr + offset; // expected-warning {{arithmetic on a null pointer treated as a cast from integer to pointer is a GNU extension}} 13 // Cases that don't match the GNU inttoptr idiom get a different warning. 14 f = (char*)0 - offset; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior if the offset is nonzero}} 15 g = (int*)0 + offset; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior if the offset is nonzero}} 16 } 17 18 // Value-dependent pointer arithmetic should not produce a nullptr warning. 19 template<char *P> 20 char* g(intptr_t offset) { 21 return P + offset; 22 } 23 24 // Value-dependent offsets should not produce a nullptr warning. 25 template<intptr_t N> 26 char *h() { 27 return (char*)nullptr + N; 28 } 29