From 6b21c4350c8ad7aecf499ade331d147f9927ed64 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 29 Apr 2014 01:19:21 +0000 Subject: [PATCH] Let stddef.h redefine NULL if __need_NULL is set, as needed by glibc, PR12997. See the bug and the cfe-commits thread "[patch] Let stddef.h redefine NULL if __need_NULL is set" for discussion. Fixes PR12997 and is similar to the __need_wint_t bits already in this file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@207482 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Headers/stddef.h | 16 ++++++++++++++++ test/Headers/needsnull.cpp | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 test/Headers/needsnull.cpp diff --git a/lib/Headers/stddef.h b/lib/Headers/stddef.h index 97126ed152..9c90fea4de 100644 --- a/lib/Headers/stddef.h +++ b/lib/Headers/stddef.h @@ -76,6 +76,7 @@ typedef __WCHAR_TYPE__ wchar_t; #else # define NULL ((void*)0) #endif +#undef __need_NULL #ifdef __cplusplus #if defined(_MSC_EXTENSIONS) && defined(_NATIVE_NULLPTR_SUPPORTED) @@ -102,6 +103,21 @@ typedef double max_align_t; #endif /* __STDDEF_H */ +/* Some C libraries set __need_NULL and expects NULL to be defined again. */ +#if defined(__need_NULL) +#undef NULL +#ifdef __cplusplus +# if !defined(__MINGW32__) && !defined(_MSC_VER) +# define NULL __null +# else +# define NULL 0 +# endif +#else +# define NULL ((void*)0) +#endif +#undef __need_NULL +#endif + /* Some C libraries expect to see a wint_t here. Others (notably MinGW) will use __WINT_TYPE__ directly; accommodate both by requiring __need_wint_t */ #if defined(__need_wint_t) diff --git a/test/Headers/needsnull.cpp b/test/Headers/needsnull.cpp new file mode 100644 index 0000000000..267cb5ddc3 --- /dev/null +++ b/test/Headers/needsnull.cpp @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -fsyntax-only -triple i386-linux-gnu -verify -Wsentinel %s +// expected-no-diagnostics + +#include + +// linux/stddef.h does something like this for cpp files: +#undef NULL +#define NULL 0 + +// glibc (and other) headers then define __need_NULL and rely on stddef.h +// to redefine NULL to the correct value again. +#define __need_NULL +#include + +// gtk headers then use __attribute__((sentinel)), which doesn't work if NULL +// is 0. +void f(const char* c, ...) __attribute__((sentinel)); +void g() { + f("", NULL); // Shouldn't warn. +} -- 2.40.0