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/Refactoring.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'clang/lib/Tooling/Refactoring.cpp') diff --git a/clang/lib/Tooling/Refactoring.cpp b/clang/lib/Tooling/Refactoring.cpp index d48713c69f0..5565b5499c7 100644 --- a/clang/lib/Tooling/Refactoring.cpp +++ b/clang/lib/Tooling/Refactoring.cpp @@ -30,7 +30,9 @@ RefactoringTool::RefactoringTool( std::shared_ptr PCHContainerOps) : ClangTool(Compilations, SourcePaths, PCHContainerOps) {} -Replacements &RefactoringTool::getReplacements() { return Replace; } +std::map &RefactoringTool::getReplacements() { + return FileToReplaces; +} int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) { if (int Result = run(ActionFactory)) { @@ -54,20 +56,22 @@ int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) { } bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) { - return tooling::applyAllReplacements(Replace, Rewrite); + bool Result = true; + for (const auto &Entry : FileToReplaces) + Result = tooling::applyAllReplacements(Entry.second, Rewrite) && Result; + return Result; } int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) { return Rewrite.overwriteChangedFiles() ? 1 : 0; } -bool formatAndApplyAllReplacements(const Replacements &Replaces, - Rewriter &Rewrite, StringRef Style) { +bool formatAndApplyAllReplacements( + const std::map &FileToReplaces, Rewriter &Rewrite, + StringRef Style) { SourceManager &SM = Rewrite.getSourceMgr(); FileManager &Files = SM.getFileManager(); - auto FileToReplaces = groupReplacementsByFile(Replaces); - bool Result = true; for (const auto &FileAndReplaces : FileToReplaces) { const std::string &FilePath = FileAndReplaces.first; -- cgit v1.2.3