summaryrefslogtreecommitdiffstats
path: root/lib/Tooling
diff options
context:
space:
mode:
authorEric Liu <ioeric@google.com>2016-10-14 09:32:06 +0000
committerEric Liu <ioeric@google.com>2016-10-14 09:32:06 +0000
commit3c90e57f06bed57e1587e4b4fb06b7b7fae94119 (patch)
tree8c4a9f7bdcf4a1ba075dd8a36037e8d19985d484 /lib/Tooling
parentfce476b6e965594e2bf2ea2f03a3d1021e2e227a (diff)
Deduplicate sets of replacements by file names.
Summary: If there are multiple <File, Replacements> pairs with the same file path after removing dots, we only keep one pair (with path after dots being removed) and discard the rest. Reviewers: djasper Subscribers: klimek, hokein, bkramer, cfe-commits Differential Revision: https://reviews.llvm.org/D25565 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@284219 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Tooling')
-rw-r--r--lib/Tooling/Core/Replacement.cpp17
-rw-r--r--lib/Tooling/Refactoring.cpp4
2 files changed, 12 insertions, 9 deletions
diff --git a/lib/Tooling/Core/Replacement.cpp b/lib/Tooling/Core/Replacement.cpp
index bdca474015..088b320916 100644
--- a/lib/Tooling/Core/Replacement.cpp
+++ b/lib/Tooling/Core/Replacement.cpp
@@ -21,6 +21,7 @@
#include "clang/Lex/Lexer.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
#include "llvm/Support/raw_os_ostream.h"
namespace clang {
@@ -564,13 +565,15 @@ llvm::Expected<std::string> applyAllReplacements(StringRef Code,
return Result;
}
-std::map<std::string, Replacements>
-groupReplacementsByFile(const Replacements &Replaces) {
- std::map<std::string, Replacements> FileToReplaces;
- for (const auto &Replace : Replaces)
- // We can ignore the Error here since \p Replaces is already conflict-free.
- FileToReplaces[Replace.getFilePath()].add(Replace);
- return FileToReplaces;
+std::map<std::string, Replacements> groupReplacementsByFile(
+ const std::map<std::string, Replacements> &FileToReplaces) {
+ std::map<std::string, Replacements> Result;
+ for (const auto &Entry : FileToReplaces) {
+ llvm::SmallString<256> CleanPath(Entry.first.data());
+ llvm::sys::path::remove_dots(CleanPath, /*remove_dot_dot=*/true);
+ Result[CleanPath.str()] = std::move(Entry.second);
+ }
+ return Result;
}
} // end namespace tooling
diff --git a/lib/Tooling/Refactoring.cpp b/lib/Tooling/Refactoring.cpp
index 5565b5499c..241557d8c4 100644
--- a/lib/Tooling/Refactoring.cpp
+++ b/lib/Tooling/Refactoring.cpp
@@ -57,7 +57,7 @@ int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
bool Result = true;
- for (const auto &Entry : FileToReplaces)
+ for (const auto &Entry : groupReplacementsByFile(FileToReplaces))
Result = tooling::applyAllReplacements(Entry.second, Rewrite) && Result;
return Result;
}
@@ -73,7 +73,7 @@ bool formatAndApplyAllReplacements(
FileManager &Files = SM.getFileManager();
bool Result = true;
- for (const auto &FileAndReplaces : FileToReplaces) {
+ for (const auto &FileAndReplaces : groupReplacementsByFile(FileToReplaces)) {
const std::string &FilePath = FileAndReplaces.first;
auto &CurReplaces = FileAndReplaces.second;