From 40ef2fb363bcd89cb4a88157f5fa97a38d795a1f Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Mon, 1 Aug 2016 10:16:37 +0000 Subject: Implement tooling::Replacements as a class. Summary: - Implement clang::tooling::Replacements as a class to provide interfaces to control how replacements for a single file are combined and provide guarantee on the order of replacements being applied. - tooling::Replacements only contains replacements for the same file now. Use std::map to represent multi-file replacements. - Error handling for the interface change will be improved in followup patches. Reviewers: djasper, klimek Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D21748 llvm-svn: 277335 --- clang/lib/Tooling/RefactoringCallbacks.cpp | 38 +++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'clang/lib/Tooling/RefactoringCallbacks.cpp') diff --git a/clang/lib/Tooling/RefactoringCallbacks.cpp b/clang/lib/Tooling/RefactoringCallbacks.cpp index 4de125ec02a..af25fd87b26 100644 --- a/clang/lib/Tooling/RefactoringCallbacks.cpp +++ b/clang/lib/Tooling/RefactoringCallbacks.cpp @@ -40,10 +40,14 @@ ReplaceStmtWithText::ReplaceStmtWithText(StringRef FromId, StringRef ToText) void ReplaceStmtWithText::run( const ast_matchers::MatchFinder::MatchResult &Result) { if (const Stmt *FromMatch = Result.Nodes.getStmtAs(FromId)) { - Replace.insert(tooling::Replacement( + 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(!Err); } } @@ -54,9 +58,15 @@ void ReplaceStmtWithStmt::run( const ast_matchers::MatchFinder::MatchResult &Result) { const Stmt *FromMatch = Result.Nodes.getStmtAs(FromId); const Stmt *ToMatch = Result.Nodes.getStmtAs(ToId); - if (FromMatch && ToMatch) - Replace.insert(replaceStmtWithStmt( - *Result.SourceManager, *FromMatch, *ToMatch)); + 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(!Err); + } } ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id, @@ -68,11 +78,23 @@ void ReplaceIfStmtWithItsBody::run( if (const IfStmt *Node = Result.Nodes.getStmtAs(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(!Err); } 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(!Err); } } } -- cgit v1.2.3