summaryrefslogtreecommitdiff
path: root/lib/Tooling/Core/Replacement.cpp
diff options
context:
space:
mode:
authorEric Liu <ioeric@google.com>2016-03-29 16:31:53 +0000
committerEric Liu <ioeric@google.com>2016-03-29 16:31:53 +0000
commit6a4597fa13445b50f309fbf1f3ffaa4319a7fbb4 (patch)
tree1b31ed104cfe78420c978889bf2b5d33851db8d6 /lib/Tooling/Core/Replacement.cpp
parentbdbac4893805e6d92539aef918fff0e18833596e (diff)
downloadclang-6a4597fa13445b50f309fbf1f3ffaa4319a7fbb4.tar.gz
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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@264745 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Tooling/Core/Replacement.cpp')
-rw-r--r--lib/Tooling/Core/Replacement.cpp23
1 files changed, 14 insertions, 9 deletions
diff --git a/lib/Tooling/Core/Replacement.cpp b/lib/Tooling/Core/Replacement.cpp
index 6226cf285b..647f2fcc3d 100644
--- a/lib/Tooling/Core/Replacement.cpp
+++ b/lib/Tooling/Core/Replacement.cpp
@@ -58,14 +58,8 @@ bool Replacement::apply(Rewriter &Rewrite) const {
const FileEntry *Entry = SM.getFileManager().getFile(FilePath);
if (!Entry)
return false;
- FileID ID;
- // FIXME: Use SM.translateFile directly.
- SourceLocation Location = SM.translateFileLineCol(Entry, 1, 1);
- ID = Location.isValid() ?
- SM.getFileID(Location) :
- SM.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
- // FIXME: We cannot check whether Offset + Length is in the file, as
- // the remapping API is not public in the RewriteBuffer.
+
+ FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
const SourceLocation Start =
SM.getLocForStartOfFile(ID).
getLocWithOffset(ReplacementRange.getOffset());
@@ -256,6 +250,8 @@ bool applyAllReplacements(const std::vector<Replacement> &Replaces,
}
std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) {
+ if (Replaces.empty()) return Code;
+
IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
new vfs::InMemoryFileSystem);
FileManager Files(FileSystemOptions(), InMemoryFileSystem);
@@ -282,7 +278,7 @@ std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) {
return Result;
}
-std::vector<Range> calculateChangedRangesInFile(const Replacements &Replaces) {
+std::vector<Range> calculateChangedRanges(const Replacements &Replaces) {
std::vector<Range> ChangedRanges;
int Shift = 0;
for (const Replacement &R : Replaces) {
@@ -390,6 +386,15 @@ private:
};
} // namespace
+std::map<std::string, Replacements>
+groupReplacementsByFile(const Replacements &Replaces) {
+ std::map<std::string, Replacements> FileToReplaces;
+ for (const auto &Replace : Replaces) {
+ FileToReplaces[Replace.getFilePath()].insert(Replace);
+ }
+ return FileToReplaces;
+}
+
Replacements mergeReplacements(const Replacements &First,
const Replacements &Second) {
if (First.empty() || Second.empty())