diff options
Diffstat (limited to 'lib/Tooling/RefactoringCallbacks.cpp')
-rw-r--r-- | lib/Tooling/RefactoringCallbacks.cpp | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/lib/Tooling/RefactoringCallbacks.cpp b/lib/Tooling/RefactoringCallbacks.cpp index 4de125ec02aa..e900c23e4f64 100644 --- a/lib/Tooling/RefactoringCallbacks.cpp +++ b/lib/Tooling/RefactoringCallbacks.cpp @@ -39,11 +39,16 @@ ReplaceStmtWithText::ReplaceStmtWithText(StringRef FromId, StringRef ToText) void ReplaceStmtWithText::run( const ast_matchers::MatchFinder::MatchResult &Result) { - if (const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId)) { - Replace.insert(tooling::Replacement( + if (const Stmt *FromMatch = Result.Nodes.getNodeAs<Stmt>(FromId)) { + auto Err = Replace.add(tooling::Replacement( *Result.SourceManager, - CharSourceRange::getTokenRange(FromMatch->getSourceRange()), - ToText)); + CharSourceRange::getTokenRange(FromMatch->getSourceRange()), ToText)); + // FIXME: better error handling. For now, just print error message in the + // release version. + if (Err) { + llvm::errs() << llvm::toString(std::move(Err)) << "\n"; + assert(false); + } } } @@ -52,11 +57,18 @@ ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId) void ReplaceStmtWithStmt::run( const ast_matchers::MatchFinder::MatchResult &Result) { - const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId); - const Stmt *ToMatch = Result.Nodes.getStmtAs<Stmt>(ToId); - if (FromMatch && ToMatch) - Replace.insert(replaceStmtWithStmt( - *Result.SourceManager, *FromMatch, *ToMatch)); + const Stmt *FromMatch = Result.Nodes.getNodeAs<Stmt>(FromId); + const Stmt *ToMatch = Result.Nodes.getNodeAs<Stmt>(ToId); + if (FromMatch && ToMatch) { + auto Err = Replace.add( + replaceStmtWithStmt(*Result.SourceManager, *FromMatch, *ToMatch)); + // FIXME: better error handling. For now, just print error message in the + // release version. + if (Err) { + llvm::errs() << llvm::toString(std::move(Err)) << "\n"; + assert(false); + } + } } ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id, @@ -65,14 +77,28 @@ ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id, void ReplaceIfStmtWithItsBody::run( const ast_matchers::MatchFinder::MatchResult &Result) { - if (const IfStmt *Node = Result.Nodes.getStmtAs<IfStmt>(Id)) { + if (const IfStmt *Node = Result.Nodes.getNodeAs<IfStmt>(Id)) { const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse(); if (Body) { - Replace.insert(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body)); + auto Err = + Replace.add(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body)); + // FIXME: better error handling. For now, just print error message in the + // release version. + if (Err) { + llvm::errs() << llvm::toString(std::move(Err)) << "\n"; + assert(false); + } } else if (!PickTrueBranch) { // If we want to use the 'else'-branch, but it doesn't exist, delete // the whole 'if'. - Replace.insert(replaceStmtWithText(*Result.SourceManager, *Node, "")); + auto Err = + Replace.add(replaceStmtWithText(*Result.SourceManager, *Node, "")); + // FIXME: better error handling. For now, just print error message in the + // release version. + if (Err) { + llvm::errs() << llvm::toString(std::move(Err)) << "\n"; + assert(false); + } } } } |