From: Richard Trieu Date: Sat, 9 Jan 2016 01:10:17 +0000 (+0000) Subject: Only take NULL macros instead of all macros into account for -Wnull-conversion. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=99f87648b04fe2db3c91b816a919ad2dba7d0003;p=clang Only take NULL macros instead of all macros into account for -Wnull-conversion. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@257240 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 0c4b59e3ce..98c9ceb1d6 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -7066,8 +7066,12 @@ static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T, // __null is usually wrapped in a macro. Go up a macro if that is the case. if (NullKind == Expr::NPCK_GNUNull) { - if (Loc.isMacroID()) - Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; + if (Loc.isMacroID()) { + StringRef MacroName = + Lexer::getImmediateMacroName(Loc, S.SourceMgr, S.getLangOpts()); + if (MacroName == "NULL") + Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; + } } // Only warn if the null and context location are in the same macro expansion. diff --git a/test/SemaCXX/conversion.cpp b/test/SemaCXX/conversion.cpp index 2c83147ff4..4c4089c6aa 100644 --- a/test/SemaCXX/conversion.cpp +++ b/test/SemaCXX/conversion.cpp @@ -208,3 +208,23 @@ namespace test9 { return EXIT(); } } + +// Test NULL macro inside a macro has same warnings nullptr inside a macro. +namespace test10 { +#define test1(cond) \ + ((cond) ? nullptr : NULL) +#define test2(cond) \ + ((cond) ? NULL : nullptr) + +#define assert(cond) \ + ((cond) ? foo() : bar()) + void foo(); + void bar(); + + void run(int x) { + if (test1(x)) {} + if (test2(x)) {} + assert(test1(x)); + assert(test2(x)); + } +}