Deduplicate replacements from diagnostics.
authorEric Liu <ioeric@google.com>
Tue, 25 Sep 2018 08:24:07 +0000 (08:24 +0000)
committerEric Liu <ioeric@google.com>
Tue, 25 Sep 2018 08:24:07 +0000 (08:24 +0000)
Summary:
After r329813, clang-apply-replacements stopped deduplicating identical
replacements; however, tools like clang-tidy relies on the deduplication of
identical dignostics replacements from different TUs to apply fixes correctly.

This change partially roll back the behavior by deduplicating changes from
diagnostics. Ideally, we should deduplicate on diagnostics level, but we need to
figure out an effecient way.

Reviewers: bkramer

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D52264

llvm-svn: 342951

clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
clang-tools-extra/test/clang-apply-replacements/Inputs/identical/file1.yaml
clang-tools-extra/test/clang-apply-replacements/Inputs/identical/file2.yaml [new file with mode: 0644]
clang-tools-extra/test/clang-apply-replacements/Inputs/identical/identical.cpp
clang-tools-extra/test/clang-apply-replacements/identical.cpp

index 1970d05..b479922 100644 (file)
@@ -125,7 +125,8 @@ std::error_code collectReplacementsFromDirectory(
 }
 
 /// \brief Extract replacements from collected TranslationUnitReplacements and
-/// TranslationUnitDiagnostics and group them per file.
+/// TranslationUnitDiagnostics and group them per file. Identical replacements
+/// from diagnostics are deduplicated.
 ///
 /// \param[in] TUs Collection of all found and deserialized
 /// TranslationUnitReplacements.
@@ -142,10 +143,20 @@ groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
   llvm::DenseMap<const FileEntry *, std::vector<tooling::Replacement>>
       GroupedReplacements;
 
-  auto AddToGroup = [&](const tooling::Replacement &R) {
+  // Deduplicate identical replacements in diagnostics.
+  // FIXME: Find an efficient way to deduplicate on diagnostics level.
+  llvm::DenseMap<const FileEntry *, std::set<tooling::Replacement>>
+      DiagReplacements;
+
+  auto AddToGroup = [&](const tooling::Replacement &R, bool FromDiag) {
     // Use the file manager to deduplicate paths. FileEntries are
     // automatically canonicalized.
     if (const FileEntry *Entry = SM.getFileManager().getFile(R.getFilePath())) {
+      if (FromDiag) {
+        auto &Replaces = DiagReplacements[Entry];
+        if (!Replaces.insert(R).second)
+          return;
+      }
       GroupedReplacements[Entry].push_back(R);
     } else if (Warned.insert(R.getFilePath()).second) {
       errs() << "Described file '" << R.getFilePath()
@@ -155,13 +166,13 @@ groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
 
   for (const auto &TU : TUs)
     for (const tooling::Replacement &R : TU.Replacements)
-      AddToGroup(R);
+      AddToGroup(R, false);
 
   for (const auto &TU : TUDs)
     for (const auto &D : TU.Diagnostics)
       for (const auto &Fix : D.Fix)
         for (const tooling::Replacement &R : Fix.second)
-          AddToGroup(R);
+          AddToGroup(R, true);
 
   // Sort replacements per file to keep consistent behavior when
   // clang-apply-replacements run on differents machine.
index 1ef5651..cf273c4 100644 (file)
@@ -10,9 +10,5 @@ Diagnostics:
         Offset:          12
         Length:          0
         ReplacementText: '0'
-      - FilePath:        $(path)/identical.cpp
-        Offset:          12
-        Length:          0
-        ReplacementText: '0'
 ...
 
diff --git a/clang-tools-extra/test/clang-apply-replacements/Inputs/identical/file2.yaml b/clang-tools-extra/test/clang-apply-replacements/Inputs/identical/file2.yaml
new file mode 100644 (file)
index 0000000..cf273c4
--- /dev/null
@@ -0,0 +1,14 @@
+---
+MainSourceFile:     identical.cpp
+Diagnostics:
+  - DiagnosticName: test-identical-insertion
+    Message: Fix
+    FilePath: $(path)/identical.cpp
+    FileOffset: 12
+    Replacements:
+      - FilePath:        $(path)/identical.cpp
+        Offset:          12
+        Length:          0
+        ReplacementText: '0'
+...
+
index b513f3e..ffbf2e3 100644 (file)
@@ -1,5 +1,6 @@
 // RUN: mkdir -p %T/Inputs/identical
 // RUN: grep -Ev "// *[A-Z-]+:" %S/Inputs/identical/identical.cpp > %T/Inputs/identical/identical.cpp
 // RUN: sed "s#\$(path)#%/T/Inputs/identical#" %S/Inputs/identical/file1.yaml > %T/Inputs/identical/file1.yaml
+// RUN: sed "s#\$(path)#%/T/Inputs/identical#" %S/Inputs/identical/file2.yaml > %T/Inputs/identical/file2.yaml
 // RUN: clang-apply-replacements %T/Inputs/identical
 // RUN: FileCheck -input-file=%T/Inputs/identical/identical.cpp %S/Inputs/identical/identical.cpp