[clang-tools-extra] r176372 - cpp11-migrate: Factor out duplicate code in UseNullPtr
Stefanus Du Toit
stefanus.dutoit at rapidmind.com
Fri Mar 1 11:47:09 PST 2013
Author: sdt
Date: Fri Mar 1 13:47:09 2013
New Revision: 176372
URL: http://llvm.org/viewvc/llvm-project?rev=176372&view=rev
Log:
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
Modified:
clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrActions.cpp
Modified: clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrActions.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrActions.cpp?rev=176372&r1=176371&r2=176372&view=diff
==============================================================================
--- clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrActions.cpp (original)
+++ clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrActions.cpp Fri Mar 1 13:47:09 2013
@@ -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_matcher
// 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_matcher
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;
}
}
More information about the cfe-commits
mailing list