diff options
author | Richard Trieu <rtrieu@google.com> | 2016-02-13 00:58:53 +0000 |
---|---|---|
committer | Richard Trieu <rtrieu@google.com> | 2016-02-13 00:58:53 +0000 |
commit | 0a5e166a0ba60dff9f004f6721c5476f29386169 (patch) | |
tree | 7e44cf21071905cda13e6effbf13363c5ea146f2 /clang/lib/Sema | |
parent | 5b79ede65a2636c4676dfa49a23076fd870cd2ac (diff) | |
download | bcm5719-llvm-0a5e166a0ba60dff9f004f6721c5476f29386169.tar.gz bcm5719-llvm-0a5e166a0ba60dff9f004f6721c5476f29386169.zip |
Make -Wnull-conversion more useful.
When a null constant is used in a macro, walk through the macro stack to
determine where the null constant is written and where the context is located.
Only warn if both locations are within the same macro expansion. This helps
function-like macros which involve pointers be treated as if they were
functions.
llvm-svn: 260776
Diffstat (limited to 'clang/lib/Sema')
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 44b3a18d707..7192eb50149 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -7265,14 +7265,21 @@ void DiagnoseNullConversion(Sema &S, Expr *E, QualType T, SourceLocation CC) { SourceLocation Loc = E->getSourceRange().getBegin(); + // Venture through the macro stacks to get to the source of macro arguments. + // The new location is a better location than the complete location that was + // passed in. + while (S.SourceMgr.isMacroArgExpansion(Loc)) + Loc = S.SourceMgr.getImmediateMacroCallerLoc(Loc); + + while (S.SourceMgr.isMacroArgExpansion(CC)) + CC = S.SourceMgr.getImmediateMacroCallerLoc(CC); + // __null is usually wrapped in a macro. Go up a macro if that is the case. - if (NullKind == Expr::NPCK_GNUNull) { - if (Loc.isMacroID()) { - StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics( - Loc, S.SourceMgr, S.getLangOpts()); - if (MacroName == "NULL") - Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; - } + if (NullKind == Expr::NPCK_GNUNull && Loc.isMacroID()) { + StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics( + 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. |