From: Alexander Kornienko Date: Wed, 21 Nov 2018 01:06:32 +0000 (+0000) Subject: Implement YAML serialization of notes in clang::tooling::Diagnostic. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=af49685e5ecd9293914849c3d413f088323c4643;p=clang Implement YAML serialization of notes in clang::tooling::Diagnostic. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@347371 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Tooling/DiagnosticsYaml.h b/include/clang/Tooling/DiagnosticsYaml.h index bf0732bf00..d869450529 100644 --- a/include/clang/Tooling/DiagnosticsYaml.h +++ b/include/clang/Tooling/DiagnosticsYaml.h @@ -22,10 +22,19 @@ #include LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic) +LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::DiagnosticMessage) namespace llvm { namespace yaml { +template <> struct MappingTraits { + 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 { /// Helper to (de)serialize a Diagnostic since we don't have direct /// access to its data members. @@ -59,6 +68,7 @@ template <> struct MappingTraits { 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. diff --git a/unittests/Tooling/DiagnosticsYamlTest.cpp b/unittests/Tooling/DiagnosticsYamlTest.cpp index 420d7af2ef..f4de53fad2 100644 --- a/unittests/Tooling/DiagnosticsYamlTest.cpp +++ b/unittests/Tooling/DiagnosticsYamlTest.cpp @@ -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 &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 &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 Fixes3 = getFixes(D3.Fix); EXPECT_TRUE(Fixes3.empty()); }