[PATCH] D38954: [Sema] -Wzero-as-null-pointer-constant: don't warn for system macros.
Roman Lebedev via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 16 07:21:02 PDT 2017
lebedev.ri created this revision.
lebedev.ri added a project: clang.
The warning was initially introduced in https://reviews.llvm.org/D32914 by @thakis,
and the concerns were raised there, and later in https://reviews.llvm.org/rL302247
and PR33771.
I do believe that it makes sense to relax the diagnostic
e.g. in this case, when the expression originates from the
system header, which can not be modified. This prevents
adoption for the diagnostic for codebases which use pthreads
(PTHREAD_MUTEX_INITIALIZER), gtest, etc.
As @malcolm.parsons suggests, it *may* make sense to also
not warn for the template types, but it is not obvious to
me how to do that in here.
While there, add more tests.
Repository:
rL LLVM
https://reviews.llvm.org/D38954
Files:
lib/Sema/Sema.cpp
test/SemaCXX/Inputs/warn-zero-nullptr.h
test/SemaCXX/warn-zero-nullptr.cpp
Index: test/SemaCXX/warn-zero-nullptr.cpp
===================================================================
--- test/SemaCXX/warn-zero-nullptr.cpp
+++ test/SemaCXX/warn-zero-nullptr.cpp
@@ -1,4 +1,9 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s -Wzero-as-null-pointer-constant -std=c++11
+// RUN: %clang_cc1 -fsyntax-only -verify %s -isystem %S/Inputs -Wzero-as-null-pointer-constant -std=c++11
+
+#include <warn-zero-nullptr.h>
+
+#define MACRO (0)
+#define MCRO(x) (x)
struct S {};
@@ -15,6 +20,8 @@
void (*fp2)() = __null; // expected-warning{{zero as null pointer constant}}
int (S::*mp2) = __null; // expected-warning{{zero as null pointer constant}}
+void fy(void* v = MACRO); // expected-warning{{zero as null pointer constant}}
+void fz(void* v = MCRO(0)); // expected-warning{{zero as null pointer constant}}
void f0(void* v = 0); // expected-warning{{zero as null pointer constant}}
void f1(void* v);
@@ -25,3 +32,44 @@
// Warn on these too. Matches gcc and arguably makes sense.
void* pp = (decltype(nullptr))0; // expected-warning{{zero as null pointer constant}}
void* pp2 = static_cast<decltype(nullptr)>(0); // expected-warning{{zero as null pointer constant}}
+
+template <typename T> void TmplFunc0(T var) {}
+void Func0Test() {
+ TmplFunc0<int>(0);
+ TmplFunc0<int*>(0); // expected-warning {{zero as null pointer constant}}
+ TmplFunc0<void*>(0); // expected-warning {{zero as null pointer constant}}
+}
+
+// FIXME: this one should *NOT* warn.
+template <typename T> void TmplFunc1(int a, T default_value = 0) {} // expected-warning{{zero as null pointer constant}} expected-warning{{zero as null pointer constant}}
+void FuncTest() {
+ TmplFunc1<int>(0);
+ TmplFunc1<int*>(0); // expected-note {{in instantiation of default function argument expression for 'TmplFunc1<int *>' required here}}
+ TmplFunc1<void*>(0); // expected-note {{in instantiation of default function argument expression for 'TmplFunc1<void *>' required here}}
+}
+
+template<typename T>
+class TemplateClass0 {
+ public:
+ explicit TemplateClass0(T var) {}
+};
+void TemplateClass0Test() {
+ TemplateClass0<int> a(0);
+ TemplateClass0<int*> b(0); // expected-warning {{zero as null pointer constant}}
+ TemplateClass0<void*> c(0); // expected-warning {{zero as null pointer constant}}
+}
+
+template<typename T>
+class TemplateClass1 {
+ public:
+// FIXME: this one should *NOT* warn.
+ explicit TemplateClass1(int a, T default_value = 0) {} // expected-warning{{zero as null pointer constant}} expected-warning{{zero as null pointer constant}}
+};
+void IgnoreSubstTemplateType1() {
+ TemplateClass1<int> a(1);
+ TemplateClass1<int*> b(1); // expected-note {{in instantiation of default function argument expression for 'TemplateClass1<int *>' required here}}
+ TemplateClass1<void*> c(1); // expected-note {{in instantiation of default function argument expression for 'TemplateClass1<void *>' required here}}
+}
+
+// Do not warn on macros from system headers.
+void* sys_init = SYSTEM_MACRO;
Index: test/SemaCXX/Inputs/warn-zero-nullptr.h
===================================================================
--- /dev/null
+++ test/SemaCXX/Inputs/warn-zero-nullptr.h
@@ -0,0 +1 @@
+#define SYSTEM_MACRO (0)
Index: lib/Sema/Sema.cpp
===================================================================
--- lib/Sema/Sema.cpp
+++ lib/Sema/Sema.cpp
@@ -440,6 +440,9 @@
return;
if (E->getType()->isNullPtrType())
return;
+ // If the expr was expanded from the macro from system header, don't warn.
+ if (SourceMgr.isInSystemMacro(E->getLocStart()))
+ return;
// nullptr only exists from C++11 on, so don't warn on its absence earlier.
if (!getLangOpts().CPlusPlus11)
return;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38954.119153.patch
Type: text/x-patch
Size: 3727 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171016/e05de73e/attachment.bin>
More information about the cfe-commits
mailing list