1 //===- unittests/Tooling/DiagnosticsYamlTest.cpp - Serialization tests ---===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Tests for serialization of Diagnostics. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Tooling/DiagnosticsYaml.h" 14 #include "clang/Tooling/Core/Diagnostic.h" 15 #include "clang/Tooling/ReplacementsYaml.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "gtest/gtest.h" 18 19 using namespace llvm; 20 using namespace clang::tooling; 21 using clang::tooling::Diagnostic; 22 23 static DiagnosticMessage makeMessage(const std::string &Message, int FileOffset, 24 const std::string &FilePath, 25 const StringMap<Replacements> &Fix) { 26 DiagnosticMessage DiagMessage; 27 DiagMessage.Message = Message; 28 DiagMessage.FileOffset = FileOffset; 29 DiagMessage.FilePath = FilePath; 30 DiagMessage.Fix = Fix; 31 return DiagMessage; 32 } 33 34 static FileByteRange makeByteRange(int FileOffset, 35 int Length, 36 const std::string &FilePath) { 37 FileByteRange Range; 38 Range.FileOffset = FileOffset; 39 Range.Length = Length; 40 Range.FilePath = FilePath; 41 return Range; 42 } 43 44 static Diagnostic makeDiagnostic(StringRef DiagnosticName, 45 const std::string &Message, int FileOffset, 46 const std::string &FilePath, 47 const StringMap<Replacements> &Fix, 48 const SmallVector<FileByteRange, 1> &Ranges) { 49 return Diagnostic(DiagnosticName, 50 makeMessage(Message, FileOffset, FilePath, Fix), {}, 51 Diagnostic::Warning, "path/to/build/directory", Ranges); 52 } 53 54 static const char *YAMLContent = 55 "---\n" 56 "MainSourceFile: 'path/to/source.cpp'\n" 57 "Diagnostics:\n" 58 " - DiagnosticName: 'diagnostic#1\'\n" 59 " DiagnosticMessage:\n" 60 " Message: 'message #1'\n" 61 " FilePath: 'path/to/source.cpp'\n" 62 " FileOffset: 55\n" 63 " Replacements:\n" 64 " - FilePath: 'path/to/source.cpp'\n" 65 " Offset: 100\n" 66 " Length: 12\n" 67 " ReplacementText: 'replacement #1'\n" 68 " Level: Warning\n" 69 " BuildDirectory: 'path/to/build/directory'\n" 70 " - DiagnosticName: 'diagnostic#2'\n" 71 " DiagnosticMessage:\n" 72 " Message: 'message #2'\n" 73 " FilePath: 'path/to/header.h'\n" 74 " FileOffset: 60\n" 75 " Replacements:\n" 76 " - FilePath: 'path/to/header.h'\n" 77 " Offset: 62\n" 78 " Length: 2\n" 79 " ReplacementText: 'replacement #2'\n" 80 " Level: Warning\n" 81 " BuildDirectory: 'path/to/build/directory'\n" 82 " Ranges:\n" 83 " - FilePath: 'path/to/source.cpp'\n" 84 " FileOffset: 10\n" 85 " Length: 10\n" 86 " - DiagnosticName: 'diagnostic#3'\n" 87 " DiagnosticMessage:\n" 88 " Message: 'message #3'\n" 89 " FilePath: 'path/to/source2.cpp'\n" 90 " FileOffset: 72\n" 91 " Replacements: []\n" 92 " Notes:\n" 93 " - Message: Note1\n" 94 " FilePath: 'path/to/note1.cpp'\n" 95 " FileOffset: 88\n" 96 " Replacements: []\n" 97 " - Message: Note2\n" 98 " FilePath: 'path/to/note2.cpp'\n" 99 " FileOffset: 99\n" 100 " Replacements: []\n" 101 " Level: Warning\n" 102 " BuildDirectory: 'path/to/build/directory'\n" 103 "...\n"; 104 105 TEST(DiagnosticsYamlTest, serializesDiagnostics) { 106 TranslationUnitDiagnostics TUD; 107 TUD.MainSourceFile = "path/to/source.cpp"; 108 109 StringMap<Replacements> Fix1 = { 110 {"path/to/source.cpp", 111 Replacements({"path/to/source.cpp", 100, 12, "replacement #1"})}}; 112 TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#1", "message #1", 55, 113 "path/to/source.cpp", Fix1, {})); 114 115 StringMap<Replacements> Fix2 = { 116 {"path/to/header.h", 117 Replacements({"path/to/header.h", 62, 2, "replacement #2"})}}; 118 SmallVector<FileByteRange, 1> Ranges2 = 119 {makeByteRange(10, 10, "path/to/source.cpp")}; 120 TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#2", "message #2", 60, 121 "path/to/header.h", Fix2, Ranges2)); 122 123 TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#3", "message #3", 72, 124 "path/to/source2.cpp", {}, {})); 125 TUD.Diagnostics.back().Notes.push_back( 126 makeMessage("Note1", 88, "path/to/note1.cpp", {})); 127 TUD.Diagnostics.back().Notes.push_back( 128 makeMessage("Note2", 99, "path/to/note2.cpp", {})); 129 130 std::string YamlContent; 131 raw_string_ostream YamlContentStream(YamlContent); 132 133 yaml::Output YAML(YamlContentStream); 134 YAML << TUD; 135 136 EXPECT_EQ(YAMLContent, YamlContentStream.str()); 137 } 138 139 TEST(DiagnosticsYamlTest, deserializesDiagnostics) { 140 TranslationUnitDiagnostics TUDActual; 141 yaml::Input YAML(YAMLContent); 142 YAML >> TUDActual; 143 144 ASSERT_FALSE(YAML.error()); 145 ASSERT_EQ(3u, TUDActual.Diagnostics.size()); 146 EXPECT_EQ("path/to/source.cpp", TUDActual.MainSourceFile); 147 148 auto getFixes = [](const StringMap<Replacements> &Fix) { 149 std::vector<Replacement> Fixes; 150 for (auto &Replacements : Fix) { 151 for (auto &Replacement : Replacements.second) { 152 Fixes.push_back(Replacement); 153 } 154 } 155 return Fixes; 156 }; 157 158 Diagnostic D1 = TUDActual.Diagnostics[0]; 159 EXPECT_EQ("diagnostic#1", D1.DiagnosticName); 160 EXPECT_EQ("message #1", D1.Message.Message); 161 EXPECT_EQ(55u, D1.Message.FileOffset); 162 EXPECT_EQ("path/to/source.cpp", D1.Message.FilePath); 163 std::vector<Replacement> Fixes1 = getFixes(D1.Message.Fix); 164 ASSERT_EQ(1u, Fixes1.size()); 165 EXPECT_EQ("path/to/source.cpp", Fixes1[0].getFilePath()); 166 EXPECT_EQ(100u, Fixes1[0].getOffset()); 167 EXPECT_EQ(12u, Fixes1[0].getLength()); 168 EXPECT_EQ("replacement #1", Fixes1[0].getReplacementText()); 169 EXPECT_TRUE(D1.Ranges.empty()); 170 171 Diagnostic D2 = TUDActual.Diagnostics[1]; 172 EXPECT_EQ("diagnostic#2", D2.DiagnosticName); 173 EXPECT_EQ("message #2", D2.Message.Message); 174 EXPECT_EQ(60u, D2.Message.FileOffset); 175 EXPECT_EQ("path/to/header.h", D2.Message.FilePath); 176 std::vector<Replacement> Fixes2 = getFixes(D2.Message.Fix); 177 ASSERT_EQ(1u, Fixes2.size()); 178 EXPECT_EQ("path/to/header.h", Fixes2[0].getFilePath()); 179 EXPECT_EQ(62u, Fixes2[0].getOffset()); 180 EXPECT_EQ(2u, Fixes2[0].getLength()); 181 EXPECT_EQ("replacement #2", Fixes2[0].getReplacementText()); 182 EXPECT_EQ(1u, D2.Ranges.size()); 183 EXPECT_EQ("path/to/source.cpp", D2.Ranges[0].FilePath); 184 EXPECT_EQ(10u, D2.Ranges[0].FileOffset); 185 EXPECT_EQ(10u, D2.Ranges[0].Length); 186 187 Diagnostic D3 = TUDActual.Diagnostics[2]; 188 EXPECT_EQ("diagnostic#3", D3.DiagnosticName); 189 EXPECT_EQ("message #3", D3.Message.Message); 190 EXPECT_EQ(72u, D3.Message.FileOffset); 191 EXPECT_EQ("path/to/source2.cpp", D3.Message.FilePath); 192 EXPECT_EQ(2u, D3.Notes.size()); 193 EXPECT_EQ("Note1", D3.Notes[0].Message); 194 EXPECT_EQ(88u, D3.Notes[0].FileOffset); 195 EXPECT_EQ("path/to/note1.cpp", D3.Notes[0].FilePath); 196 EXPECT_EQ("Note2", D3.Notes[1].Message); 197 EXPECT_EQ(99u, D3.Notes[1].FileOffset); 198 EXPECT_EQ("path/to/note2.cpp", D3.Notes[1].FilePath); 199 std::vector<Replacement> Fixes3 = getFixes(D3.Message.Fix); 200 EXPECT_TRUE(Fixes3.empty()); 201 EXPECT_TRUE(D3.Ranges.empty()); 202 } 203