summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp')
-rw-r--r--clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp21
1 files changed, 12 insertions, 9 deletions
diff --git a/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp b/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp
index 38e44d21e23..6f476113367 100644
--- a/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp
+++ b/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp
@@ -37,8 +37,9 @@ static llvm::cl::opt<std::string> UserNullMacroNames(
llvm::cl::init(""));
bool isReplaceableRange(SourceLocation StartLoc, SourceLocation EndLoc,
- const SourceManager &SM) {
- return SM.isFromSameFile(StartLoc, EndLoc) && SM.isFromMainFile(StartLoc);
+ const SourceManager &SM, const Transform &Owner) {
+ return SM.isFromSameFile(StartLoc, EndLoc) &&
+ Owner.isFileModifiable(SM, StartLoc);
}
/// \brief Replaces the provided range with the text "nullptr", but only if
@@ -152,10 +153,10 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
public:
CastSequenceVisitor(tooling::Replacements &R, ASTContext &Context,
const UserMacroNames &UserNullMacros,
- unsigned &AcceptedChanges)
+ unsigned &AcceptedChanges, const Transform &Owner)
: Replace(R), SM(Context.getSourceManager()), Context(Context),
UserNullMacros(UserNullMacros), AcceptedChanges(AcceptedChanges),
- FirstSubExpr(0), PruneSubtree(false) {}
+ Owner(Owner), FirstSubExpr(0), PruneSubtree(false) {}
bool TraverseStmt(Stmt *S) {
// Stop traversing down the tree if requested.
@@ -191,7 +192,7 @@ public:
if (SM.isMacroArgExpansion(StartLoc) && SM.isMacroArgExpansion(EndLoc)) {
SourceLocation FileLocStart = SM.getFileLoc(StartLoc),
FileLocEnd = SM.getFileLoc(EndLoc);
- if (isReplaceableRange(FileLocStart, FileLocEnd, SM) &&
+ if (isReplaceableRange(FileLocStart, FileLocEnd, SM, Owner) &&
allArgUsesValid(C)) {
ReplaceWithNullptr(Replace, SM, FileLocStart, FileLocEnd);
++AcceptedChanges;
@@ -214,7 +215,7 @@ public:
EndLoc = SM.getFileLoc(EndLoc);
}
- if (!isReplaceableRange(StartLoc, EndLoc, SM)) {
+ if (!isReplaceableRange(StartLoc, EndLoc, SM, Owner)) {
return skipSubTree();
}
ReplaceWithNullptr(Replace, SM, StartLoc, EndLoc);
@@ -419,14 +420,16 @@ private:
ASTContext &Context;
const UserMacroNames &UserNullMacros;
unsigned &AcceptedChanges;
+ const Transform &Owner;
Expr *FirstSubExpr;
bool PruneSubtree;
};
} // namespace
NullptrFixer::NullptrFixer(clang::tooling::Replacements &Replace,
- unsigned &AcceptedChanges, RiskLevel)
- : Replace(Replace), AcceptedChanges(AcceptedChanges) {
+ unsigned &AcceptedChanges, RiskLevel,
+ const Transform &Owner)
+ : Replace(Replace), AcceptedChanges(AcceptedChanges), Owner(Owner) {
if (!UserNullMacroNames.empty()) {
llvm::StringRef S = UserNullMacroNames;
S.split(UserNullMacros, ",");
@@ -441,6 +444,6 @@ void NullptrFixer::run(const ast_matchers::MatchFinder::MatchResult &Result) {
// null-to-pointer cast within use CastSequenceVisitor to identify sequences
// of explicit casts that can be converted into 'nullptr'.
CastSequenceVisitor Visitor(Replace, *Result.Context, UserNullMacros,
- AcceptedChanges);
+ AcceptedChanges, Owner);
Visitor.TraverseStmt(const_cast<CastExpr *>(NullCast));
}
OpenPOWER on IntegriCloud