Implement YAML serialization of notes in clang::tooling::Diagnostic.
authorAlexander Kornienko <alexfh@google.com>
Wed, 21 Nov 2018 01:06:32 +0000 (01:06 +0000)
committerAlexander Kornienko <alexfh@google.com>
Wed, 21 Nov 2018 01:06:32 +0000 (01:06 +0000)
llvm-svn: 347371

clang/include/clang/Tooling/DiagnosticsYaml.h
clang/unittests/Tooling/DiagnosticsYamlTest.cpp

index bf0732b..d869450 100644 (file)
 #include <string>
 
 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic)
+LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::DiagnosticMessage)
 
 namespace llvm {
 namespace yaml {
 
+template <> struct MappingTraits<clang::tooling::DiagnosticMessage> {
+  static void mapping(IO &Io, clang::tooling::DiagnosticMessage &M) {
+    Io.mapRequired("Message", M.Message);
+    Io.mapOptional("FilePath", M.FilePath);
+    Io.mapOptional("FileOffset", M.FileOffset);
+  }
+};
+
 template <> struct MappingTraits<clang::tooling::Diagnostic> {
   /// Helper to (de)serialize a Diagnostic since we don't have direct
   /// access to its data members.
@@ -59,6 +68,7 @@ template <> struct MappingTraits<clang::tooling::Diagnostic> {
     Io.mapRequired("Message", Keys->Message.Message);
     Io.mapRequired("FileOffset", Keys->Message.FileOffset);
     Io.mapRequired("FilePath", Keys->Message.FilePath);
+    Io.mapOptional("Notes", Keys->Notes);
 
     // FIXME: Export properly all the different fields.
 
index 420d7af..f4de53f 100644 (file)
@@ -20,16 +20,21 @@ using namespace llvm;
 using namespace clang::tooling;
 using clang::tooling::Diagnostic;
 
-static Diagnostic makeDiagnostic(StringRef DiagnosticName,
-                                 const std::string &Message, int FileOffset,
-                                 const std::string &FilePath,
-                                 const StringMap<Replacements> &Fix) {
+static DiagnosticMessage makeMessage(const std::string &Message, int FileOffset,
+                                     const std::string &FilePath) {
   DiagnosticMessage DiagMessage;
   DiagMessage.Message = Message;
   DiagMessage.FileOffset = FileOffset;
   DiagMessage.FilePath = FilePath;
-  return Diagnostic(DiagnosticName, DiagMessage, Fix, {}, Diagnostic::Warning,
-                    "path/to/build/directory");
+  return DiagMessage;
+}
+
+static Diagnostic makeDiagnostic(StringRef DiagnosticName,
+                                 const std::string &Message, int FileOffset,
+                                 const std::string &FilePath,
+                                 const StringMap<Replacements> &Fix) {
+  return Diagnostic(DiagnosticName, makeMessage(Message, FileOffset, FilePath),
+                    Fix, {}, Diagnostic::Warning, "path/to/build/directory");
 }
 
 TEST(DiagnosticsYamlTest, serializesDiagnostics) {
@@ -50,6 +55,10 @@ TEST(DiagnosticsYamlTest, serializesDiagnostics) {
 
   TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#3", "message #3", 72,
                                            "path/to/source2.cpp", {}));
+  TUD.Diagnostics.back().Notes.push_back(
+      makeMessage("Note1", 88, "path/to/note1.cpp"));
+  TUD.Diagnostics.back().Notes.push_back(
+      makeMessage("Note2", 99, "path/to/note2.cpp"));
 
   std::string YamlContent;
   raw_string_ostream YamlContentStream(YamlContent);
@@ -82,6 +91,13 @@ TEST(DiagnosticsYamlTest, serializesDiagnostics) {
             "    Message:         'message #3'\n"
             "    FileOffset:      72\n"
             "    FilePath:        'path/to/source2.cpp'\n"
+            "    Notes:           \n"
+            "      - Message:         Note1\n"
+            "        FilePath:        'path/to/note1.cpp'\n"
+            "        FileOffset:      88\n"
+            "      - Message:         Note2\n"
+            "        FilePath:        'path/to/note2.cpp'\n"
+            "        FileOffset:      99\n"
             "    Replacements:    []\n"
             "...\n",
             YamlContentStream.str());
@@ -113,6 +129,13 @@ TEST(DiagnosticsYamlTest, deserializesDiagnostics) {
                             "    Message:         'message #3'\n"
                             "    FileOffset:      98\n"
                             "    FilePath:        path/to/source.cpp\n"
+                            "    Notes:\n"
+                            "      - Message:         Note1\n"
+                            "        FilePath:        'path/to/note1.cpp'\n"
+                            "        FileOffset:      66\n"
+                            "      - Message:         Note2\n"
+                            "        FilePath:        'path/to/note2.cpp'\n"
+                            "        FileOffset:      77\n"
                             "    Replacements:    []\n"
                             "...\n";
   TranslationUnitDiagnostics TUDActual;
@@ -162,6 +185,13 @@ TEST(DiagnosticsYamlTest, deserializesDiagnostics) {
   EXPECT_EQ("message #3", D3.Message.Message);
   EXPECT_EQ(98u, D3.Message.FileOffset);
   EXPECT_EQ("path/to/source.cpp", D3.Message.FilePath);
+  EXPECT_EQ(2u, D3.Notes.size());
+  EXPECT_EQ("Note1", D3.Notes[0].Message);
+  EXPECT_EQ(66u, D3.Notes[0].FileOffset);
+  EXPECT_EQ("path/to/note1.cpp", D3.Notes[0].FilePath);
+  EXPECT_EQ("Note2", D3.Notes[1].Message);
+  EXPECT_EQ(77u, D3.Notes[1].FileOffset);
+  EXPECT_EQ("path/to/note2.cpp", D3.Notes[1].FilePath);
   std::vector<Replacement> Fixes3 = getFixes(D3.Fix);
   EXPECT_TRUE(Fixes3.empty());
 }