diff options
author | Eric Liu <ioeric@google.com> | 2016-03-29 16:31:53 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2016-03-29 16:31:53 +0000 |
commit | 4c1ef97adb1ecdc4ed3ee4a9ee7d5546aecda014 (patch) | |
tree | 3d1c1f5466abeda93688c97d03b55425a3619b01 /clang/lib/Tooling/Refactoring.cpp | |
parent | 3db0b85fc8b0f914934c0f0b4c5eb28b64bd18a9 (diff) | |
download | bcm5719-llvm-4c1ef97adb1ecdc4ed3ee4a9ee7d5546aecda014.tar.gz bcm5719-llvm-4c1ef97adb1ecdc4ed3ee4a9ee7d5546aecda014.zip |
Added formatAndApplyAllReplacements that works on multiple files in libTooling.
Summary:
formatAndApplyAllReplacements takes a set of Replacements, applies them on a
Rewriter, and reformats the changed code.
Reviewers: klimek, djasper
Subscribers: ioeric, klimek, cfe-commits
Differential Revision: http://reviews.llvm.org/D17852
llvm-svn: 264745
Diffstat (limited to 'clang/lib/Tooling/Refactoring.cpp')
-rw-r--r-- | clang/lib/Tooling/Refactoring.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/clang/lib/Tooling/Refactoring.cpp b/clang/lib/Tooling/Refactoring.cpp index d32452f6f29..3a152755858 100644 --- a/clang/lib/Tooling/Refactoring.cpp +++ b/clang/lib/Tooling/Refactoring.cpp @@ -14,6 +14,7 @@ #include "clang/Basic/DiagnosticOptions.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" +#include "clang/Format/Format.h" #include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Lex/Lexer.h" #include "clang/Rewrite/Core/Rewriter.h" @@ -61,5 +62,29 @@ int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) { return Rewrite.overwriteChangedFiles() ? 1 : 0; } +bool formatAndApplyAllReplacements(const Replacements &Replaces, + Rewriter &Rewrite, StringRef Style) { + SourceManager &SM = Rewrite.getSourceMgr(); + FileManager &Files = SM.getFileManager(); + + auto FileToReplaces = groupReplacementsByFile(Replaces); + + bool Result = true; + for (auto &FileAndReplaces : FileToReplaces) { + const std::string FilePath = FileAndReplaces.first; + auto &CurReplaces = FileAndReplaces.second; + + const FileEntry *Entry = Files.getFile(FilePath); + FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User); + StringRef Code = SM.getBufferData(ID); + + format::FormatStyle CurStyle = format::getStyle(Style, FilePath, "LLVM"); + Replacements NewReplacements = + format::formatReplacements(Code, CurReplaces, CurStyle); + Result = applyAllReplacements(NewReplacements, Rewrite) && Result; + } + return Result; +} + } // end namespace tooling } // end namespace clang |