summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2016-03-01 12:37:30 +0000
committerManuel Klimek <klimek@google.com>2016-03-01 12:37:30 +0000
commitb12e5a5ccd8e5ca71a3722a4b5103a794ca5515f (patch)
tree2be713f424b906e73a5320edca75855f6b37226e /clang/lib/Format
parent109702ccb16f33a5b0305a1d1287413c9a88adca (diff)
downloadbcm5719-llvm-b12e5a5ccd8e5ca71a3722a4b5103a794ca5515f.tar.gz
bcm5719-llvm-b12e5a5ccd8e5ca71a3722a4b5103a794ca5515f.zip
Add functions to apply replacements and reformat them.
This is a commonly useful feature to have, and we have implemented it multiple times with different kinds of bugs. This implementation centralizes the idea in a set of functions that we can then use from the various tools. Reverts r262234, which is a revert of r262232, and puts the functions into FOrmat.h, as they are closely coupled to clang-format, and we otherwise introduce a cyclic dependency between libFormat and libTooling. Patch by Eric Liu. llvm-svn: 262323
Diffstat (limited to 'clang/lib/Format')
-rw-r--r--clang/lib/Format/Format.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 85a0152e295..9d5c0bca335 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1884,6 +1884,34 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
return Replaces;
}
+tooling::Replacements formatReplacements(StringRef Code,
+ const tooling::Replacements &Replaces,
+ const FormatStyle &Style) {
+ if (Replaces.empty())
+ return tooling::Replacements();
+
+ std::string NewCode = applyAllReplacements(Code, Replaces);
+ std::vector<tooling::Range> ChangedRanges =
+ tooling::calculateChangedRangesInFile(Replaces);
+ StringRef FileName = Replaces.begin()->getFilePath();
+ tooling::Replacements FormatReplaces =
+ reformat(Style, NewCode, ChangedRanges, FileName);
+
+ tooling::Replacements MergedReplacements =
+ mergeReplacements(Replaces, FormatReplaces);
+ return MergedReplacements;
+}
+
+std::string applyAllReplacementsAndFormat(StringRef Code,
+ const tooling::Replacements &Replaces,
+ const FormatStyle &Style) {
+ tooling::Replacements NewReplacements =
+ formatReplacements(Code, Replaces, Style);
+ if (NewReplacements.empty())
+ return Code; // Exit early to avoid overhead in `applyAllReplacements`.
+ return applyAllReplacements(Code, NewReplacements);
+}
+
tooling::Replacements reformat(const FormatStyle &Style,
SourceManager &SourceMgr, FileID ID,
ArrayRef<CharSourceRange> Ranges,
OpenPOWER on IntegriCloud