Alpha updated this revision to Diff 81779.
Alpha added a comment.

It was tested against the clang extra unit tests, but not tests run with 
check-clang-tools.
Updated Yaml test files to match the new format, this might need further review.


Repository:
  rL LLVM

https://reviews.llvm.org/D26137

Files:
  include/clang/Tooling/Core/Diagnostic.h
  include/clang/Tooling/Core/Replacement.h
  include/clang/Tooling/DiagnosticsYaml.h
  include/clang/Tooling/ReplacementsYaml.h
  lib/Tooling/Core/CMakeLists.txt
  lib/Tooling/Core/Diagnostic.cpp
  
tools/extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
  tools/extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
  tools/extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
  tools/extra/clang-tidy/ClangTidy.cpp
  tools/extra/clang-tidy/ClangTidy.h
  tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
  tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.h
  tools/extra/clang-tidy/tool/ClangTidyMain.cpp
  tools/extra/test/clang-apply-replacements/Inputs/basic/file1.yaml
  tools/extra/test/clang-apply-replacements/Inputs/basic/file2.yaml
  tools/extra/test/clang-apply-replacements/Inputs/conflict/file1.yaml
  tools/extra/test/clang-apply-replacements/Inputs/conflict/file2.yaml
  tools/extra/test/clang-apply-replacements/Inputs/conflict/file3.yaml
  tools/extra/test/clang-apply-replacements/Inputs/crlf/file1.yaml
  tools/extra/test/clang-apply-replacements/Inputs/format/no.yaml
  tools/extra/test/clang-apply-replacements/Inputs/format/yes.yaml
  unittests/Tooling/ReplacementsYamlTest.cpp

Index: unittests/Tooling/ReplacementsYamlTest.cpp
===================================================================
--- unittests/Tooling/ReplacementsYamlTest.cpp
+++ unittests/Tooling/ReplacementsYamlTest.cpp
@@ -22,11 +22,10 @@
   TranslationUnitReplacements Doc;
 
   Doc.MainSourceFile = "/path/to/source.cpp";
-  Doc.Context = "some context";
-  Doc.Replacements
-      .push_back(Replacement("/path/to/file1.h", 232, 56, "replacement #1"));
-  Doc.Replacements
-      .push_back(Replacement("/path/to/file2.h", 301, 2, "replacement #2"));
+  Doc.Replacements.push_back(
+      Replacement("/path/to/file1.h", 232, 56, "replacement #1"));
+  Doc.Replacements.push_back(
+      Replacement("/path/to/file2.h", 301, 2, "replacement #2"));
 
   std::string YamlContent;
   llvm::raw_string_ostream YamlContentStream(YamlContent);
@@ -37,7 +36,6 @@
   // NOTE: If this test starts to fail for no obvious reason, check whitespace.
   ASSERT_STREQ("---\n"
                "MainSourceFile:  /path/to/source.cpp\n"
-               "Context:         some context\n"
                "Replacements:    \n" // Extra whitespace here!
                "  - FilePath:        /path/to/file1.h\n"
                "    Offset:          232\n"
@@ -54,7 +52,6 @@
 TEST(ReplacementsYamlTest, deserializesReplacements) {
   std::string YamlContent = "---\n"
                             "MainSourceFile:      /path/to/source.cpp\n"
-                            "Context:             some context\n"
                             "Replacements:\n"
                             "  - FilePath:        /path/to/file1.h\n"
                             "    Offset:          232\n"
@@ -71,7 +68,6 @@
   ASSERT_FALSE(YAML.error());
   ASSERT_EQ(2u, DocActual.Replacements.size());
   ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
-  ASSERT_EQ("some context", DocActual.Context);
   ASSERT_EQ("/path/to/file1.h", DocActual.Replacements[0].getFilePath());
   ASSERT_EQ(232u, DocActual.Replacements[0].getOffset());
   ASSERT_EQ(56u, DocActual.Replacements[0].getLength());
@@ -98,7 +94,6 @@
   ASSERT_FALSE(YAML.error());
   ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
   ASSERT_EQ(1u, DocActual.Replacements.size());
-  ASSERT_EQ(std::string(), DocActual.Context);
   ASSERT_EQ("target_file.h", DocActual.Replacements[0].getFilePath());
   ASSERT_EQ(1u, DocActual.Replacements[0].getOffset());
   ASSERT_EQ(10u, DocActual.Replacements[0].getLength());
Index: lib/Tooling/Core/Diagnostic.cpp
===================================================================
--- lib/Tooling/Core/Diagnostic.cpp
+++ lib/Tooling/Core/Diagnostic.cpp
@@ -0,0 +1,46 @@
+//===--- Diagnostic.cpp - Framework for clang diagnostics tools ----------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  Implements classes to support/store diagnostics refactoring.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Tooling/Core/Diagnostic.h"
+#include "clang/Basic/SourceManager.h"
+
+namespace clang {
+namespace tooling {
+
+DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message)
+    : Message(Message), FileOffset(0) {}
+
+DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message,
+                                     const SourceManager &Sources,
+                                     SourceLocation Loc)
+    : Message(Message) {
+  assert(Loc.isValid() && Loc.isFileID());
+  FilePath = Sources.getFilename(Loc);
+  FileOffset = Sources.getFileOffset(Loc);
+}
+
+Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
+                       Diagnostic::Level DiagLevel, StringRef BuildDirectory)
+    : DiagnosticName(DiagnosticName), DiagLevel(DiagLevel),
+      BuildDirectory(BuildDirectory) {}
+
+Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
+                       DiagnosticMessage &Message,
+                       llvm::StringMap<tooling::Replacements> &Fix,
+                       SmallVector<DiagnosticMessage, 1> &Notes,
+                       Level DiagLevel, llvm::StringRef BuildDirectory)
+    : DiagnosticName(DiagnosticName), Message(Message), Fix(Fix), Notes(Notes),
+      DiagLevel(DiagLevel), BuildDirectory(BuildDirectory) {}
+
+} // end namespace tooling
+} // end namespace clang
Index: lib/Tooling/Core/CMakeLists.txt
===================================================================
--- lib/Tooling/Core/CMakeLists.txt
+++ lib/Tooling/Core/CMakeLists.txt
@@ -4,6 +4,7 @@
   Lookup.cpp
   Replacement.cpp
   QualTypeNames.cpp
+  Diagnostic.cpp
 
   LINK_LIBS
   clangAST
Index: include/clang/Tooling/ReplacementsYaml.h
===================================================================
--- include/clang/Tooling/ReplacementsYaml.h
+++ include/clang/Tooling/ReplacementsYaml.h
@@ -65,7 +65,6 @@
   static void mapping(IO &Io,
                       clang::tooling::TranslationUnitReplacements &Doc) {
     Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
-    Io.mapOptional("Context", Doc.Context, std::string());
     Io.mapRequired("Replacements", Doc.Replacements);
   }
 };
Index: include/clang/Tooling/DiagnosticsYaml.h
===================================================================
--- include/clang/Tooling/DiagnosticsYaml.h
+++ include/clang/Tooling/DiagnosticsYaml.h
@@ -0,0 +1,101 @@
+//===-- DiagnosticsYaml.h -- Serialiazation for Diagnosticss ---*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file defines the structure of a YAML document for serializing
+/// diagnostics.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
+#define LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
+
+#include "clang/Tooling/Core/Diagnostic.h"
+#include "clang/Tooling/ReplacementsYaml.h"
+#include "llvm/Support/YAMLTraits.h"
+#include <string>
+
+LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic)
+
+namespace llvm {
+namespace yaml {
+
+template <> struct MappingTraits<clang::tooling::Diagnostic> {
+  /// \brief Helper to (de)serialize a Diagnostic since we don't have direct
+  /// access to its data members.
+  class NormalizedDiagnostic {
+  public:
+    NormalizedDiagnostic(const IO &)
+        : DiagLevel(clang::tooling::Diagnostic::Level::Warning) {}
+
+    NormalizedDiagnostic(const IO &, const clang::tooling::Diagnostic &D)
+        : DiagnosticName(D.DiagnosticName), Message(D.Message), Fix(D.Fix),
+          Notes(D.Notes), DiagLevel(D.DiagLevel),
+          BuildDirectory(D.BuildDirectory) {}
+
+    clang::tooling::Diagnostic denormalize(const IO &) {
+      return clang::tooling::Diagnostic(DiagnosticName, Message, Fix, Notes,
+                                        DiagLevel, BuildDirectory);
+    }
+
+    std::string DiagnosticName;
+    clang::tooling::DiagnosticMessage Message;
+    llvm::StringMap<clang::tooling::Replacements> Fix;
+    SmallVector<clang::tooling::DiagnosticMessage, 1> Notes;
+    clang::tooling::Diagnostic::Level DiagLevel;
+    std::string BuildDirectory;
+  };
+
+  static void mapping(IO &Io, clang::tooling::Diagnostic &D) {
+    MappingNormalization<NormalizedDiagnostic, clang::tooling::Diagnostic> Keys(
+        Io, D);
+    Io.mapRequired("DiagnosticName", Keys->DiagnosticName);
+
+    // FIXME: Export properly all the different fields.
+
+    std::vector<clang::tooling::Replacement> Fixes;
+    for (auto &Replacements : Keys->Fix) {
+      for (auto &Replacement : Replacements.second) {
+        Fixes.push_back(Replacement);
+      }
+    }
+    Io.mapRequired("Replacements", Fixes);
+    for (auto &Fix : Fixes) {
+      llvm::Error Err = Keys->Fix[Fix.getFilePath()].add(Fix);
+      if (Err) {
+        // FIXME: Implement better conflict handling.
+        llvm::errs() << "Fix conflicts with existing fix: "
+                     << llvm::toString(std::move(Err)) << "\n";
+      }
+    }
+  }
+};
+
+/// \brief Specialized MappingTraits to describe how a
+/// TranslationUnitDiagnostics is (de)serialized.
+template <> struct MappingTraits<clang::tooling::TranslationUnitDiagnostics> {
+  static void mapping(IO &Io, clang::tooling::TranslationUnitDiagnostics &Doc) {
+    Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
+
+    std::vector<clang::tooling::Diagnostic> Diagnostics;
+    for (auto &Diagnostic : Doc.Diagnostics) {
+      // FIXME: Export all diagnostics, not just the ones with fixes.
+      // Update MappingTraits<clang::tooling::Diagnostic>::mapping.
+      if (Diagnostic.Fix.size() > 0) {
+        Diagnostics.push_back(Diagnostic);
+      }
+    }
+    Io.mapRequired("Diagnostics", Diagnostics);
+    Doc.Diagnostics = Diagnostics;
+  }
+};
+} // end namespace yaml
+} // end namespace llvm
+
+#endif // LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
Index: include/clang/Tooling/Core/Replacement.h
===================================================================
--- include/clang/Tooling/Core/Replacement.h
+++ include/clang/Tooling/Core/Replacement.h
@@ -329,12 +329,6 @@
 struct TranslationUnitReplacements {
   /// Name of the main source for the translation unit.
   std::string MainSourceFile;
-
-  /// A freeform chunk of text to describe the context of the replacements.
-  /// Will be printed, for example, when detecting conflicts during replacement
-  /// deduplication.
-  std::string Context;
-
   std::vector<Replacement> Replacements;
 };
 
Index: include/clang/Tooling/Core/Diagnostic.h
===================================================================
--- include/clang/Tooling/Core/Diagnostic.h
+++ include/clang/Tooling/Core/Diagnostic.h
@@ -0,0 +1,100 @@
+//===--- Diagnostic.h - Framework for clang diagnostics tools --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// \file
+//  Structures supporting diagnostics and refactorings that span multiple
+//  translation units. Indicate diagnostics reports and replacements
+//  suggestions for the analyzed sources.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
+#define LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
+
+#include "Replacement.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringRef.h"
+#include <string>
+
+namespace clang {
+namespace tooling {
+
+/// \brief Represents the diagnostic message with the error message associated
+/// and the information on the location of the problem.
+struct DiagnosticMessage {
+  DiagnosticMessage(llvm::StringRef Message = "");
+
+  /// \brief Constructs a diagnostic message with anoffset to the diagnostic
+  /// within the file where the problem occured.
+  ///
+  /// \param Loc Should be a file location, it is not meaningful for a macro
+  /// location.
+  ///
+  DiagnosticMessage(llvm::StringRef Message, const SourceManager &Sources,
+                    SourceLocation Loc);
+  std::string Message;
+  std::string FilePath;
+  unsigned FileOffset;
+};
+
+/// \brief Represents the diagnostic with the level of severity and possible
+/// fixes to be applied.
+struct Diagnostic {
+  enum Level {
+    Warning = DiagnosticsEngine::Warning,
+    Error = DiagnosticsEngine::Error
+  };
+
+  Diagnostic() = default;
+
+  Diagnostic(llvm::StringRef DiagnosticName, Level DiagLevel,
+             StringRef BuildDirectory);
+
+  Diagnostic(llvm::StringRef DiagnosticName, DiagnosticMessage &Message,
+             llvm::StringMap<tooling::Replacements> &Fix,
+             SmallVector<DiagnosticMessage, 1> &Notes, Level DiagLevel,
+             llvm::StringRef BuildDirectory);
+
+  /// \brief Name identifying the Diagnostic.
+  std::string DiagnosticName;
+
+  /// \brief Message associated to the diagnostic.
+  DiagnosticMessage Message;
+
+  /// \brief Fixes to apply, grouped by file path.
+  llvm::StringMap<tooling::Replacements> Fix;
+
+  /// \brief Potential notes about the diagnostic.
+  SmallVector<DiagnosticMessage, 1> Notes;
+
+  /// \brief Diagnostic level. Can indicate either an error or a warning.
+  Level DiagLevel;
+
+  /// \brief A build directory of the diagnostic source file.
+  ///
+  /// It's an absolute path which is `directory` field of the source file in
+  /// compilation database. If users don't specify the compilation database
+  /// directory, it is the current directory where clang-tidy runs.
+  ///
+  /// Note: it is empty in unittest.
+  std::string BuildDirectory;
+};
+
+/// \brief Collection of Diagnostics generated from a single translation unit.
+struct TranslationUnitDiagnostics {
+  /// Name of the main source for the translation unit.
+  std::string MainSourceFile;
+  std::vector<Diagnostic> Diagnostics;
+};
+
+} // end namespace tooling
+} // end namespace clang
+#endif // LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
Index: tools/extra/test/clang-apply-replacements/Inputs/format/yes.yaml
===================================================================
--- tools/extra/test/clang-apply-replacements/Inputs/format/yes.yaml
+++ tools/extra/test/clang-apply-replacements/Inputs/format/yes.yaml
@@ -2,21 +2,23 @@
 # so that formatting happens correctly.
 ---
 MainSourceFile:  yes.cpp
-Replacements:    
-  - FilePath:        $(path)/yes.cpp
-    Offset:          494
-    Length:          1
-    ReplacementText: nullptr
-  - FilePath:        $(path)/yes.cpp
-    Offset:          410
-    Length:          1
-    ReplacementText: nullptr
-  - FilePath:        $(path)/yes.cpp
-    Offset:          454
-    Length:          1
-    ReplacementText: nullptr
-  - FilePath:        $(path)/yes.cpp
-    Offset:          108
-    Length:          38
-    ReplacementText: 'auto '
+Diagnostics:     
+  - DiagnosticName:  test-yes
+    Replacements:    
+      - FilePath:        $(path)/yes.cpp
+        Offset:          494
+        Length:          1
+        ReplacementText: nullptr
+      - FilePath:        $(path)/yes.cpp
+        Offset:          410
+        Length:          1
+        ReplacementText: nullptr
+      - FilePath:        $(path)/yes.cpp
+        Offset:          454
+        Length:          1
+        ReplacementText: nullptr
+      - FilePath:        $(path)/yes.cpp
+        Offset:          108
+        Length:          38
+        ReplacementText: 'auto '
 ...
Index: tools/extra/test/clang-apply-replacements/Inputs/format/no.yaml
===================================================================
--- tools/extra/test/clang-apply-replacements/Inputs/format/no.yaml
+++ tools/extra/test/clang-apply-replacements/Inputs/format/no.yaml
@@ -1,8 +1,10 @@
 ---
 MainSourceFile:  no.cpp
-Replacements:    
-  - FilePath:        $(path)/no.cpp
-    Offset:          94
-    Length:          3
-    ReplacementText: 'auto '
+Diagnostics:     
+  - DiagnosticName:  test-no
+    Replacements:    
+      - FilePath:        $(path)/no.cpp
+        Offset:          94
+        Length:          3
+        ReplacementText: 'auto '
 ...
Index: tools/extra/test/clang-apply-replacements/Inputs/crlf/file1.yaml
===================================================================
--- tools/extra/test/clang-apply-replacements/Inputs/crlf/file1.yaml
+++ tools/extra/test/clang-apply-replacements/Inputs/crlf/file1.yaml
@@ -1,8 +1,10 @@
 ---
 MainSourceFile:      source1.cpp
-Replacements:    
-  - FilePath:        $(path)/crlf.cpp
-    Offset:          79
-    Length:          1
-    ReplacementText: nullptr
+Diagnostics:
+  - DiagnosticName:  test-crlf
+    Replacements:    
+      - FilePath:        $(path)/crlf.cpp
+        Offset:          79
+        Length:          1
+        ReplacementText: nullptr
 ...
Index: tools/extra/test/clang-apply-replacements/Inputs/conflict/file3.yaml
===================================================================
--- tools/extra/test/clang-apply-replacements/Inputs/conflict/file3.yaml
+++ tools/extra/test/clang-apply-replacements/Inputs/conflict/file3.yaml
@@ -1,8 +1,10 @@
 ---
 MainSourceFile: source1.cpp
-Replacements:
-  - FilePath:        $(path)/common.h
-    Offset:          169
-    Length:          0
-    ReplacementText: "(int*)"
+Diagnostics:
+  - DiagnosticName:  test-conflict
+    Replacements:
+      - FilePath:        $(path)/common.h
+        Offset:          169
+        Length:          0
+        ReplacementText: "(int*)"
 ...
Index: tools/extra/test/clang-apply-replacements/Inputs/conflict/file2.yaml
===================================================================
--- tools/extra/test/clang-apply-replacements/Inputs/conflict/file2.yaml
+++ tools/extra/test/clang-apply-replacements/Inputs/conflict/file2.yaml
@@ -1,16 +1,18 @@
 ---
 MainSourceFile: source2.cpp
-Replacements:
-  - FilePath:        $(path)/common.h
-    Offset:          106
-    Length:          26
-    ReplacementText: 'int & elem : ints'
-  - FilePath:        $(path)/common.h
-    Offset:          140
-    Length:          7
-    ReplacementText: elem
-  - FilePath:        $(path)/common.h
-    Offset:          169
-    Length:          1
-    ReplacementText: nullptr
+Diagnostics:     
+  - DiagnosticName:  test-conflict
+    Replacements:
+      - FilePath:        $(path)/common.h
+        Offset:          106
+        Length:          26
+        ReplacementText: 'int & elem : ints'
+      - FilePath:        $(path)/common.h
+        Offset:          140
+        Length:          7
+        ReplacementText: elem
+      - FilePath:        $(path)/common.h
+        Offset:          169
+        Length:          1
+        ReplacementText: nullptr
 ...
Index: tools/extra/test/clang-apply-replacements/Inputs/conflict/file1.yaml
===================================================================
--- tools/extra/test/clang-apply-replacements/Inputs/conflict/file1.yaml
+++ tools/extra/test/clang-apply-replacements/Inputs/conflict/file1.yaml
@@ -1,16 +1,18 @@
 ---
 MainSourceFile: source1.cpp
-Replacements:
-  - FilePath:        $(path)/common.h
-    Offset:          106
-    Length:          26
-    ReplacementText: 'auto & i : ints'
-  - FilePath:        $(path)/common.h
-    Offset:          140
-    Length:          7
-    ReplacementText: i
-  - FilePath:        $(path)/common.h
-    Offset:          160
-    Length:          12
-    ReplacementText: ''
+Diagnostics:
+  - DiagnosticName: test-conflict
+    Replacements:
+      - FilePath:        $(path)/common.h
+        Offset:          106
+        Length:          26
+        ReplacementText: 'auto & i : ints'
+      - FilePath:        $(path)/common.h
+        Offset:          140
+        Length:          7
+        ReplacementText: i
+      - FilePath:        $(path)/common.h
+        Offset:          160
+        Length:          12
+        ReplacementText: ''
 ...
Index: tools/extra/test/clang-apply-replacements/Inputs/basic/file2.yaml
===================================================================
--- tools/extra/test/clang-apply-replacements/Inputs/basic/file2.yaml
+++ tools/extra/test/clang-apply-replacements/Inputs/basic/file2.yaml
@@ -1,8 +1,10 @@
 ---
 MainSourceFile:     source2.cpp
-Replacements:
-  - FilePath:        $(path)/basic.h
-    Offset:          148
-    Length:          0
-    ReplacementText: 'override '
+Diagnostics:
+  - DiagnosticName: test-basic
+    Replacements:
+      - FilePath:        $(path)/basic.h
+        Offset:          148
+        Length:          0
+        ReplacementText: 'override '
 ...
Index: tools/extra/test/clang-apply-replacements/Inputs/basic/file1.yaml
===================================================================
--- tools/extra/test/clang-apply-replacements/Inputs/basic/file1.yaml
+++ tools/extra/test/clang-apply-replacements/Inputs/basic/file1.yaml
@@ -1,20 +1,22 @@
 ---
 MainSourceFile:     source1.cpp
-Replacements:
-  - FilePath:        $(path)/basic.h
-    Offset:          242
-    Length:          26
-    ReplacementText: 'auto & elem : ints'
-  - FilePath:        $(path)/basic.h
-    Offset:          276
-    Length:          22
-    ReplacementText: ''
-  - FilePath:        $(path)/basic.h
-    Offset:          298
-    Length:          1
-    ReplacementText: elem
-  - FilePath:        $(path)/../basic/basic.h
-    Offset:          148
-    Length:          0
-    ReplacementText: 'override '
+Diagnostics:
+  - DiagnosticName: test-basic
+    Replacements:
+      - FilePath:        $(path)/basic.h
+        Offset:          242
+        Length:          26
+        ReplacementText: 'auto & elem : ints'
+      - FilePath:        $(path)/basic.h
+        Offset:          276
+        Length:          22
+        ReplacementText: ''
+      - FilePath:        $(path)/basic.h
+        Offset:          298
+        Length:          1
+        ReplacementText: elem
+      - FilePath:        $(path)/../basic/basic.h
+        Offset:          148
+        Length:          0
+        ReplacementText: 'override '
 ...
Index: tools/extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- tools/extra/clang-tidy/tool/ClangTidyMain.cpp
+++ tools/extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -403,7 +403,7 @@
       llvm::errs() << "Error opening output file: " << EC.message() << '\n';
       return 1;
     }
-    exportReplacements(Errors, OS);
+    exportReplacements(FilePath.str(), Errors, OS);
   }
 
   printStats(Stats);
Index: tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.h
===================================================================
--- tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.h
+++ tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.h
@@ -13,6 +13,7 @@
 #include "ClangTidyOptions.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Tooling/Core/Diagnostic.h"
 #include "clang/Tooling/Refactoring.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringMap.h"
@@ -32,50 +33,19 @@
 
 namespace tidy {
 
-/// \brief A message from a clang-tidy check.
-///
-/// Note that this is independent of a \c SourceManager.
-struct ClangTidyMessage {
-  ClangTidyMessage(StringRef Message = "");
-  ClangTidyMessage(StringRef Message, const SourceManager &Sources,
-                   SourceLocation Loc);
-  std::string Message;
-  std::string FilePath;
-  unsigned FileOffset;
-};
+typedef clang::tooling::DiagnosticMessage ClangTidyMessage;
 
 /// \brief A detected error complete with information to display diagnostic and
 /// automatic fix.
 ///
 /// This is used as an intermediate format to transport Diagnostics without a
 /// dependency on a SourceManager.
 ///
 /// FIXME: Make Diagnostics flexible enough to support this directly.
-struct ClangTidyError {
-  enum Level {
-    Warning = DiagnosticsEngine::Warning,
-    Error = DiagnosticsEngine::Error
-  };
-
-  ClangTidyError(StringRef CheckName, Level DiagLevel, bool IsWarningAsError,
-                 StringRef BuildDirectory);
-
-  std::string CheckName;
-  ClangTidyMessage Message;
-  // Fixes grouped by file path.
-  llvm::StringMap<tooling::Replacements> Fix;
-  SmallVector<ClangTidyMessage, 1> Notes;
-
-  // A build directory of the diagnostic source file.
-  //
-  // It's an absolute path which is `directory` field of the source file in
-  // compilation database. If users don't specify the compilation database
-  // directory, it is the current directory where clang-tidy runs.
-  //
-  // Note: it is empty in unittest.
-  std::string BuildDirectory;
+struct ClangTidyError : clang::tooling::Diagnostic {
+  ClangTidyError(StringRef CheckName, Level DiagLevel, StringRef BuildDirectory,
+                 bool IsWarningAsError);
 
-  Level DiagLevel;
   bool IsWarningAsError;
 };
 
Index: tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
===================================================================
--- tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -45,7 +45,7 @@
     // FIXME: Remove this once there's a better way to pass check names than
     // appending the check name to the message in ClangTidyContext::diag and
     // using getCustomDiagID.
-    std::string CheckNameInMessage = " [" + Error.CheckName + "]";
+    std::string CheckNameInMessage = " [" + Error.DiagnosticName + "]";
     if (Message.endswith(CheckNameInMessage))
       Message = Message.substr(0, Message.size() - CheckNameInMessage.size());
 
@@ -110,23 +110,11 @@
 };
 } // end anonymous namespace
 
-ClangTidyMessage::ClangTidyMessage(StringRef Message)
-    : Message(Message), FileOffset(0) {}
-
-ClangTidyMessage::ClangTidyMessage(StringRef Message,
-                                   const SourceManager &Sources,
-                                   SourceLocation Loc)
-    : Message(Message) {
-  assert(Loc.isValid() && Loc.isFileID());
-  FilePath = Sources.getFilename(Loc);
-  FileOffset = Sources.getFileOffset(Loc);
-}
-
 ClangTidyError::ClangTidyError(StringRef CheckName,
                                ClangTidyError::Level DiagLevel,
-                               bool IsWarningAsError, StringRef BuildDirectory)
-    : CheckName(CheckName), BuildDirectory(BuildDirectory),
-      DiagLevel(DiagLevel), IsWarningAsError(IsWarningAsError) {}
+                               StringRef BuildDirectory, bool IsWarningAsError)
+    : tooling::Diagnostic(CheckName, DiagLevel, BuildDirectory),
+      IsWarningAsError(IsWarningAsError) {}
 
 // Returns true if GlobList starts with the negative indicator ('-'), removes it
 // from the GlobList.
@@ -260,7 +248,7 @@
 void ClangTidyDiagnosticConsumer::finalizeLastError() {
   if (!Errors.empty()) {
     ClangTidyError &Error = Errors.back();
-    if (!Context.getChecksFilter().contains(Error.CheckName) &&
+    if (!Context.getChecksFilter().contains(Error.DiagnosticName) &&
         Error.DiagLevel != ClangTidyError::Error) {
       ++Context.Stats.ErrorsIgnoredCheckFilter;
       Errors.pop_back();
@@ -366,8 +354,9 @@
     bool IsWarningAsError =
         DiagLevel == DiagnosticsEngine::Warning &&
         Context.getWarningAsErrorFilter().contains(CheckName);
-    Errors.push_back(ClangTidyError(CheckName, Level, IsWarningAsError,
-                                    Context.getCurrentBuildDirectory()));
+    Errors.push_back(ClangTidyError(CheckName, Level,
+                                    Context.getCurrentBuildDirectory(),
+                                    IsWarningAsError));
   }
 
   ClangTidyDiagnosticRenderer Converter(
Index: tools/extra/clang-tidy/ClangTidy.h
===================================================================
--- tools/extra/clang-tidy/ClangTidy.h
+++ tools/extra/clang-tidy/ClangTidy.h
@@ -242,7 +242,8 @@
 
 /// \brief Serializes replacements into YAML and writes them to the specified
 /// output stream.
-void exportReplacements(const std::vector<ClangTidyError> &Errors,
+void exportReplacements(const StringRef MainFilePath,
+                        const std::vector<ClangTidyError> &Errors,
                         raw_ostream &OS);
 
 } // end namespace tidy
Index: tools/extra/clang-tidy/ClangTidy.cpp
===================================================================
--- tools/extra/clang-tidy/ClangTidy.cpp
+++ tools/extra/clang-tidy/ClangTidy.cpp
@@ -35,6 +35,7 @@
 #include "clang/Rewrite/Frontend/FrontendActions.h"
 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
+#include "clang/Tooling/DiagnosticsYaml.h"
 #include "clang/Tooling/Refactoring.h"
 #include "clang/Tooling/ReplacementsYaml.h"
 #include "clang/Tooling/Tooling.h"
@@ -102,14 +103,14 @@
   SourceManager &getSourceManager() { return SourceMgr; }
 
   void reportDiagnostic(const ClangTidyError &Error) {
-    const ClangTidyMessage &Message = Error.Message;
+    const ClangTidyMessage Message = Error.Message;
     SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
     // Contains a pair for each attempted fix: location and whether the fix was
     // applied successfully.
     SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
     {
       auto Level = static_cast<DiagnosticsEngine::Level>(Error.DiagLevel);
-      std::string Name = Error.CheckName;
+      std::string Name = Error.DiagnosticName;
       if (Error.IsWarningAsError) {
         Name += ",-warnings-as-errors";
         Level = DiagnosticsEngine::Error;
@@ -562,18 +563,18 @@
   WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
 }
 
-void exportReplacements(const std::vector<ClangTidyError> &Errors,
+void exportReplacements(const llvm::StringRef MainFilePath,
+                        const std::vector<ClangTidyError> &Errors,
                         raw_ostream &OS) {
-  TranslationUnitReplacements TUR;
-  for (const ClangTidyError &Error : Errors) {
-    for (const auto &FileAndFixes : Error.Fix)
-      TUR.Replacements.insert(TUR.Replacements.end(),
-                              FileAndFixes.second.begin(),
-                              FileAndFixes.second.end());
+  TranslationUnitDiagnostics TUD;
+  TUD.MainSourceFile = MainFilePath;
+  for (const auto &Error : Errors) {
+    tooling::Diagnostic Diag = Error;
+    TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
   }
 
   yaml::Output YAML(OS);
-  YAML << TUR;
+  YAML << TUD;
 }
 
 } // namespace tidy
Index: tools/extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
===================================================================
--- tools/extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
+++ tools/extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
@@ -66,7 +66,7 @@
                    cl::init("LLVM"), cl::cat(FormattingCategory));
 
 namespace {
-// Helper object to remove the TUReplacement files (triggered by
+// Helper object to remove the TUReplacement and TUDiagnostic (triggered by
 // "remove-change-desc-files" command line option) when exiting current scope.
 class ScopedFileRemover {
 public:
@@ -211,11 +211,16 @@
   if (DoFormat)
     FormatStyle = format::getStyle(FormatStyleOpt, FormatStyleConfig, "LLVM");
 
-  TUReplacements TUs;
-  TUReplacementFiles TURFiles;
+  TUReplacements TURs;
+  TUReplacementFiles TUFiles;
 
   std::error_code ErrorCode =
-      collectReplacementsFromDirectory(Directory, TUs, TURFiles, Diagnostics);
+      collectReplacementsFromDirectory(Directory, TURs, TUFiles, Diagnostics);
+
+  TUDiagnostics TUDs;
+  TUFiles.clear();
+  ErrorCode =
+      collectReplacementsFromDirectory(Directory, TUDs, TUFiles, Diagnostics);
 
   if (ErrorCode) {
     errs() << "Trouble iterating over directory '" << Directory
@@ -227,13 +232,15 @@
   // command line option) when exiting main().
   std::unique_ptr<ScopedFileRemover> Remover;
   if (RemoveTUReplacementFiles)
-    Remover.reset(new ScopedFileRemover(TURFiles, Diagnostics));
+    Remover.reset(new ScopedFileRemover(TUFiles, Diagnostics));
 
   FileManager Files((FileSystemOptions()));
   SourceManager SM(Diagnostics, Files);
 
   FileToReplacementsMap GroupedReplacements;
-  if (!mergeAndDeduplicate(TUs, GroupedReplacements, SM))
+  if (!mergeAndDeduplicate(TURs, GroupedReplacements, SM))
+    return 1;
+  if (!mergeAndDeduplicate(TUDs, GroupedReplacements, SM))
     return 1;
 
   Rewriter ReplacementsRewriter(SM, LangOptions());
Index: tools/extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
===================================================================
--- tools/extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
+++ tools/extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
@@ -20,6 +20,7 @@
 #include "clang/Format/Format.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Rewrite/Core/Rewriter.h"
+#include "clang/Tooling/DiagnosticsYaml.h"
 #include "clang/Tooling/ReplacementsYaml.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/Support/FileSystem.h"
@@ -37,7 +38,7 @@
 
 std::error_code collectReplacementsFromDirectory(
     const llvm::StringRef Directory, TUReplacements &TUs,
-    TUReplacementFiles &TURFiles, clang::DiagnosticsEngine &Diagnostics) {
+    TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
   using namespace llvm::sys::fs;
   using namespace llvm::sys::path;
 
@@ -54,7 +55,7 @@
     if (extension(I->path()) != ".yaml")
       continue;
 
-    TURFiles.push_back(I->path());
+    TUFiles.push_back(I->path());
 
     ErrorOr<std::unique_ptr<MemoryBuffer>> Out =
         MemoryBuffer::getFile(I->path());
@@ -79,6 +80,51 @@
   return ErrorCode;
 }
 
+std::error_code
+collectReplacementsFromDirectory(const llvm::StringRef Directory,
+                                 TUDiagnostics &TUs, TUReplacementFiles &TUFiles,
+                                 clang::DiagnosticsEngine &Diagnostics) {
+  using namespace llvm::sys::fs;
+  using namespace llvm::sys::path;
+
+  std::error_code ErrorCode;
+
+  for (recursive_directory_iterator I(Directory, ErrorCode), E;
+       I != E && !ErrorCode; I.increment(ErrorCode)) {
+    if (filename(I->path())[0] == '.') {
+      // Indicate not to descend into directories beginning with '.'
+      I.no_push();
+      continue;
+    }
+
+    if (extension(I->path()) != ".yaml")
+      continue;
+
+    TUFiles.push_back(I->path());
+
+    ErrorOr<std::unique_ptr<MemoryBuffer>> Out =
+        MemoryBuffer::getFile(I->path());
+    if (std::error_code BufferError = Out.getError()) {
+      errs() << "Error reading " << I->path() << ": " << BufferError.message()
+             << "\n";
+      continue;
+    }
+
+    yaml::Input YIn(Out.get()->getBuffer(), nullptr, &eatDiagnostics);
+    tooling::TranslationUnitDiagnostics TU;
+    YIn >> TU;
+    if (YIn.error()) {
+      // File doesn't appear to be a header change description. Ignore it.
+      continue;
+    }
+
+    // Only keep files that properly parse.
+    TUs.push_back(TU);
+  }
+
+  return ErrorCode;
+}
+
 /// \brief Dumps information for a sequence of conflicting Replacements.
 ///
 /// \param[in] File FileEntry for the file the conflicting Replacements are
@@ -256,6 +302,34 @@
   return !deduplicateAndDetectConflicts(GroupedReplacements, SM);
 }
 
+bool mergeAndDeduplicate(const TUDiagnostics &TUs,
+                         FileToReplacementsMap &GroupedReplacements,
+                         clang::SourceManager &SM) {
+
+  // Group all replacements by target file.
+  std::set<StringRef> Warned;
+  for (const auto &TU : TUs) {
+    for (const auto &D : TU.Diagnostics) {
+      for (const auto &Fix : D.Fix) {
+        for (const tooling::Replacement &R : Fix.second) {
+          // Use the file manager to deduplicate paths. FileEntries are
+          // automatically canonicalized.
+          const FileEntry *Entry = SM.getFileManager().getFile(R.getFilePath());
+          if (!Entry && Warned.insert(R.getFilePath()).second) {
+            errs() << "Described file '" << R.getFilePath()
+                   << "' doesn't exist. Ignoring...\n";
+            continue;
+          }
+          GroupedReplacements[Entry].push_back(R);
+        }
+      }
+    }
+  }
+
+  // Ask clang to deduplicate and report conflicts.
+  return !deduplicateAndDetectConflicts(GroupedReplacements, SM);
+}
+
 bool applyReplacements(const FileToReplacementsMap &GroupedReplacements,
                        clang::Rewriter &Rewrites) {
 
Index: tools/extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
===================================================================
--- tools/extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
+++ tools/extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
@@ -16,6 +16,7 @@
 #ifndef LLVM_CLANG_APPLYREPLACEMENTS_H
 #define LLVM_CLANG_APPLYREPLACEMENTS_H
 
+#include "clang/Tooling/Core/Diagnostic.h"
 #include "clang/Tooling/Refactoring.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
@@ -43,6 +44,9 @@
 /// \brief Collection of TranslationUnitReplacement files.
 typedef std::vector<std::string> TUReplacementFiles;
 
+/// \brief Collection of TranslationUniDiagnostics.
+typedef std::vector<clang::tooling::TranslationUnitDiagnostics> TUDiagnostics;
+
 /// \brief Map mapping file name to Replacements targeting that file.
 typedef llvm::DenseMap<const clang::FileEntry *,
                        std::vector<clang::tooling::Replacement>>
@@ -58,24 +62,29 @@
 /// \param[in] Directory Directory to begin search for serialized
 /// TranslationUnitReplacements.
 /// \param[out] TUs Collection of all found and deserialized
-/// TranslationUnitReplacements.
-/// \param[out] TURFiles Collection of all TranslationUnitReplacement files
+/// TranslationUnitReplacements or TranslationUnitDiagnostics.
+/// \param[out] TUFiles Collection of all TranslationUnitReplacement files
 /// found in \c Directory.
 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
 ///
 /// \returns An error_code indicating success or failure in navigating the
 /// directory structure.
 std::error_code collectReplacementsFromDirectory(
     const llvm::StringRef Directory, TUReplacements &TUs,
-    TUReplacementFiles &TURFiles, clang::DiagnosticsEngine &Diagnostics);
+    TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
+
+std::error_code collectReplacementsFromDirectory(
+    const llvm::StringRef Directory, TUDiagnostics &TUs,
+    TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
 
 /// \brief Deduplicate, check for conflicts, and apply all Replacements stored
 /// in \c TUs. If conflicts occur, no Replacements are applied.
 ///
 /// \post For all (key,value) in GroupedReplacements, value[i].getOffset() <=
 /// value[i+1].getOffset().
 ///
-/// \param[in] TUs Collection of TranslationUnitReplacements to merge,
+/// \param[in] TUs Collection of TranslationUnitReplacements or
+/// TranslationUnitDiagnostics to merge,
 /// deduplicate, and test for conflicts.
 /// \param[out] GroupedReplacements Container grouping all Replacements by the
 /// file they target.
@@ -88,6 +97,10 @@
                          FileToReplacementsMap &GroupedReplacements,
                          clang::SourceManager &SM);
 
+bool mergeAndDeduplicate(const TUDiagnostics &TUs,
+                         FileToReplacementsMap &GroupedReplacements,
+                         clang::SourceManager &SM);
+
 // FIXME: Remove this function after changing clang-apply-replacements to use
 // Replacements class.
 bool applyAllReplacements(const std::vector<tooling::Replacement> &Replaces,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to