diff options
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 8 | ||||
-rw-r--r-- | clang/test/SemaCXX/conversion.cpp | 20 |
2 files changed, 26 insertions, 2 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 0c4b59e3ce1..98c9ceb1d65 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/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/clang/test/SemaCXX/conversion.cpp b/clang/test/SemaCXX/conversion.cpp index 2c83147ff4c..4c4089c6aae 100644 --- a/clang/test/SemaCXX/conversion.cpp +++ b/clang/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)); + } +} |