diff options
| author | Stefanus Du Toit <stefanus.dutoit@rapidmind.com> | 2013-03-01 19:47:09 +0000 |
|---|---|---|
| committer | Stefanus Du Toit <stefanus.dutoit@rapidmind.com> | 2013-03-01 19:47:09 +0000 |
| commit | 169949724ef148c3731675b31b1c3ddb3d73b116 (patch) | |
| tree | 2b4af7240909ae47713438fecd2881472f496c96 /clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp | |
| parent | 801916baf1cc85d6983519cab06338e4a27369ac (diff) | |
| download | bcm5719-llvm-169949724ef148c3731675b31b1c3ddb3d73b116.tar.gz bcm5719-llvm-169949724ef148c3731675b31b1c3ddb3d73b116.zip | |
cpp11-migrate: Factor out duplicate code in UseNullPtr
This moves the actual replacement code into a separate
function. There is still a bit of code duplication to
go from macros to expansion areas, but that code will
need to be fixed anyways to resolve bugs around macro
replacement.
Reviewed by: Tareq Siraj, Edwin Vane
llvm-svn: 176372
Diffstat (limited to 'clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp')
| -rw-r--r-- | clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp b/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp index 22cdbc65e87..621862e368d 100644 --- a/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp +++ b/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp @@ -20,11 +20,27 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/RecursiveASTVisitor.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 +/// 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, + SourceLocation StartLoc, SourceLocation EndLoc) { + if (SM.isFromSameFile(StartLoc, EndLoc) && SM.isFromMainFile(StartLoc)) { + CharSourceRange Range(SourceRange(StartLoc, EndLoc), true); + Replace.insert(tooling::Replacement(SM, Range, "nullptr")); + return true; + } else + return false; +} + +} + /// \brief Looks for a sequences of 0 or more explicit casts with an implicit /// null-to-pointer cast within. /// @@ -37,8 +53,8 @@ using namespace clang; class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> { public: CastSequenceVisitor(tooling::Replacements &R, SourceManager &SM, - unsigned &AcceptedChanges) : - Replace(R), SM(SM), AcceptedChanges(AcceptedChanges), FirstCast(0) {} + unsigned &AcceptedChanges) + : Replace(R), SM(SM), AcceptedChanges(AcceptedChanges), FirstCast(0) {} // Only VisitStmt is overridden as we shouldn't find other base AST types // within a cast expression. @@ -61,12 +77,9 @@ public: StartLoc = SM.getFileLoc(StartLoc); EndLoc = SM.getFileLoc(EndLoc); - if (SM.getFileID(StartLoc) == SM.getFileID(EndLoc) && - SM.isFromMainFile(StartLoc) && SM.isFromMainFile(EndLoc)) { - CharSourceRange Range(SourceRange(StartLoc, EndLoc), true); - Replace.insert(tooling::Replacement(SM, Range, "nullptr")); - ++AcceptedChanges; - } + AcceptedChanges += + ReplaceWithNullptr(Replace, SM, StartLoc, EndLoc) ? 1 : 0; + ResetFirstCast(); } @@ -83,7 +96,6 @@ private: CastExpr *FirstCast; }; - void NullptrFixer::run(const ast_matchers::MatchFinder::MatchResult &Result) { SourceManager &SM = *Result.SourceManager; @@ -93,7 +105,7 @@ void NullptrFixer::run(const ast_matchers::MatchFinder::MatchResult &Result) { // use CastSequenceVisitor to identify sequences of explicit casts that can // be converted into 'nullptr'. CastSequenceVisitor Visitor(Replace, SM, AcceptedChanges); - Visitor.TraverseStmt(const_cast<CastExpr*>(NullCast)); + Visitor.TraverseStmt(const_cast<CastExpr *>(NullCast)); } const CastExpr *Cast = Result.Nodes.getNodeAs<CastExpr>(ImplicitCastNode); @@ -101,18 +113,11 @@ void NullptrFixer::run(const ast_matchers::MatchFinder::MatchResult &Result) { SourceLocation StartLoc = Cast->getLocStart(); SourceLocation EndLoc = Cast->getLocEnd(); - if (SM.getFileID(StartLoc) != SM.getFileID(EndLoc)) - return; - // If the start/end location is a macro, get the expansion location. StartLoc = SM.getFileLoc(StartLoc); EndLoc = SM.getFileLoc(EndLoc); - if (!SM.isFromMainFile(StartLoc) || !SM.isFromMainFile(EndLoc)) - return; - - CharSourceRange Range(SourceRange(StartLoc, EndLoc), true); - Replace.insert(tooling::Replacement(SM, Range, "nullptr")); - ++AcceptedChanges; + AcceptedChanges += + ReplaceWithNullptr(Replace, SM, StartLoc, EndLoc) ? 1 : 0; } } |

