diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Tooling/Refactoring.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/Tooling/Refactoring.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/contrib/llvm/tools/clang/lib/Tooling/Refactoring.cpp b/contrib/llvm/tools/clang/lib/Tooling/Refactoring.cpp index d32452f6f293..28d535aeb45f 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/Refactoring.cpp +++ b/contrib/llvm/tools/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,33 @@ 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 (const 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"); + auto NewReplacements = + format::formatReplacements(Code, CurReplaces, CurStyle); + if (!NewReplacements) { + llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n"; + return false; + } + Result = applyAllReplacements(*NewReplacements, Rewrite) && Result; + } + return Result; +} + } // end namespace tooling } // end namespace clang |