diff options
Diffstat (limited to 'clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp')
| -rw-r--r-- | clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp b/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp index 7fcd16e9e5e..f0ad46880aa 100644 --- a/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp +++ b/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp @@ -20,13 +20,15 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Lex/Lexer.h" + using namespace clang::ast_matchers; using namespace clang::tooling; using namespace clang; namespace { -/// \brief Replaces the provided range with the text "nullptr", but only if +/// \brief Replaces the provided range with the text "nullptr", but only if /// the start and end location are both in main file. /// Returns true if and only if a replacement was made. bool ReplaceWithNullptr(tooling::Replacements &Replace, SourceManager &SM, @@ -114,13 +116,32 @@ void NullptrFixer::run(const ast_matchers::MatchFinder::MatchResult &Result) { const CastExpr *Cast = Result.Nodes.getNodeAs<CastExpr>(ImplicitCastNode); if (Cast) { const Expr *E = Cast->IgnoreParenImpCasts(); - SourceLocation StartLoc = E->getLocStart(); SourceLocation EndLoc = E->getLocEnd(); - // If the start/end location is a macro, get the expansion location. - StartLoc = SM.getFileLoc(StartLoc); - EndLoc = SM.getFileLoc(EndLoc); + // If the start/end location is a macro argument expansion, get the + // expansion location. If its a macro body expansion, check to see if its + // coming from a macro called NULL. + if (SM.isMacroArgExpansion(StartLoc) && SM.isMacroArgExpansion(EndLoc)) { + StartLoc = SM.getFileLoc(StartLoc); + EndLoc = SM.getFileLoc(EndLoc); + } else if (SM.isMacroBodyExpansion(StartLoc) && + SM.isMacroBodyExpansion(EndLoc)) { + llvm::StringRef ImmediateMacroName = clang::Lexer::getImmediateMacroName( + StartLoc, SM, Result.Context->getLangOpts()); + if (ImmediateMacroName != "NULL") + return; + + SourceLocation MacroCallerStartLoc = + SM.getImmediateMacroCallerLoc(StartLoc); + SourceLocation MacroCallerEndLoc = SM.getImmediateMacroCallerLoc(EndLoc); + + if (MacroCallerStartLoc.isFileID() && MacroCallerEndLoc.isFileID()) { + StartLoc = SM.getFileLoc(StartLoc); + EndLoc = SM.getFileLoc(EndLoc); + } else + return; + } AcceptedChanges += ReplaceWithNullptr(Replace, SM, StartLoc, EndLoc) ? 1 : 0; |

