diff options
author | Roman Lebedev <lebedev.ri@gmail.com> | 2017-10-26 13:18:14 +0000 |
---|---|---|
committer | Roman Lebedev <lebedev.ri@gmail.com> | 2017-10-26 13:18:14 +0000 |
commit | 5f17d5ca46b48156b96f81e7c32d381a0ca65b41 (patch) | |
tree | 48e4daafadf6b5d89ba3ede2a1b2165234782d71 /lib/Sema/Sema.cpp | |
parent | 8084ad7fc6453b51a6422b6c73e41644910d7bd3 (diff) | |
download | clang-5f17d5ca46b48156b96f81e7c32d381a0ca65b41.tar.gz |
[Sema] -Wzero-as-null-pointer-constant: don't warn for system macros other than NULL.
Summary:
The warning was initially introduced in D32914 by @thakis,
and the concerns were raised there, and later in 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.
Though, it still makes sense to complain about `NULL` macro.
While there, add more tests.
Reviewers: dblaikie, thakis, rsmith, rjmccall, aaron.ballman
Reviewed By: thakis
Subscribers: Rakete1111, hans, cfe-commits, thakis, malcolm.parsons
Tags: #clang
Differential Revision: https://reviews.llvm.org/D38954
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316662 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/Sema.cpp')
-rw-r--r-- | lib/Sema/Sema.cpp | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp index a9d6cb4c58..58bca42677 100644 --- a/lib/Sema/Sema.cpp +++ b/lib/Sema/Sema.cpp @@ -436,12 +436,24 @@ void Sema::diagnoseNullableToNonnullConversion(QualType DstType, } void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr* E) { + if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant, + E->getLocStart())) + return; + // nullptr only exists from C++11 on, so don't warn on its absence earlier. + if (!getLangOpts().CPlusPlus11) + return; + if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer) return; if (E->IgnoreParenImpCasts()->getType()->isNullPtrType()) return; - // nullptr only exists from C++11 on, so don't warn on its absence earlier. - if (!getLangOpts().CPlusPlus11) + + // If it is a macro from system header, and if the macro name is not "NULL", + // do not warn. + SourceLocation MaybeMacroLoc = E->getLocStart(); + if (Diags.getSuppressSystemWarnings() && + SourceMgr.isInSystemMacro(MaybeMacroLoc) && + !findMacroSpelling(MaybeMacroLoc, "NULL")) return; Diag(E->getLocStart(), diag::warn_zero_as_null_pointer_constant) |