Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 1 | //===- unittests/Tooling/DiagnosticsYamlTest.cpp - Serialization tests ---===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 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" |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | using namespace clang::tooling; |
Matthias Braun | 0bc7d63 | 2017-07-15 00:29:25 +0000 | [diff] [blame] | 21 | using clang::tooling::Diagnostic; |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 22 | |
Alexander Kornienko | af17a38 | 2018-11-21 01:06:32 +0000 | [diff] [blame] | 23 | static DiagnosticMessage makeMessage(const std::string &Message, int FileOffset, |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 24 | const std::string &FilePath, |
| 25 | const StringMap<Replacements> &Fix) { |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 26 | DiagnosticMessage DiagMessage; |
| 27 | DiagMessage.Message = Message; |
| 28 | DiagMessage.FileOffset = FileOffset; |
| 29 | DiagMessage.FilePath = FilePath; |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 30 | DiagMessage.Fix = Fix; |
Alexander Kornienko | af17a38 | 2018-11-21 01:06:32 +0000 | [diff] [blame] | 31 | return DiagMessage; |
| 32 | } |
| 33 | |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 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 | |
Alexander Kornienko | af17a38 | 2018-11-21 01:06:32 +0000 | [diff] [blame] | 44 | static Diagnostic makeDiagnostic(StringRef DiagnosticName, |
| 45 | const std::string &Message, int FileOffset, |
| 46 | const std::string &FilePath, |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 47 | const StringMap<Replacements> &Fix, |
| 48 | const SmallVector<FileByteRange, 1> &Ranges) { |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 49 | return Diagnostic(DiagnosticName, |
| 50 | makeMessage(Message, FileOffset, FilePath, Fix), {}, |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 51 | Diagnostic::Warning, "path/to/build/directory", Ranges); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 54 | static const char *YAMLContent = |
| 55 | "---\n" |
| 56 | "MainSourceFile: 'path/to/source.cpp'\n" |
Fangrui Song | ff6836f | 2019-07-12 05:59:28 +0000 | [diff] [blame] | 57 | "Diagnostics:\n" |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 58 | " - DiagnosticName: 'diagnostic#1\'\n" |
Fangrui Song | ff6836f | 2019-07-12 05:59:28 +0000 | [diff] [blame] | 59 | " DiagnosticMessage:\n" |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 60 | " Message: 'message #1'\n" |
| 61 | " FilePath: 'path/to/source.cpp'\n" |
| 62 | " FileOffset: 55\n" |
Fangrui Song | ff6836f | 2019-07-12 05:59:28 +0000 | [diff] [blame] | 63 | " Replacements:\n" |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 64 | " - FilePath: 'path/to/source.cpp'\n" |
| 65 | " Offset: 100\n" |
| 66 | " Length: 12\n" |
| 67 | " ReplacementText: 'replacement #1'\n" |
Dmitry Polukhin | c98c94d | 2020-05-02 02:35:41 -0700 | [diff] [blame] | 68 | " Level: Warning\n" |
| 69 | " BuildDirectory: 'path/to/build/directory'\n" |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 70 | " - DiagnosticName: 'diagnostic#2'\n" |
Fangrui Song | ff6836f | 2019-07-12 05:59:28 +0000 | [diff] [blame] | 71 | " DiagnosticMessage:\n" |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 72 | " Message: 'message #2'\n" |
| 73 | " FilePath: 'path/to/header.h'\n" |
| 74 | " FileOffset: 60\n" |
Fangrui Song | ff6836f | 2019-07-12 05:59:28 +0000 | [diff] [blame] | 75 | " Replacements:\n" |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 76 | " - FilePath: 'path/to/header.h'\n" |
| 77 | " Offset: 62\n" |
| 78 | " Length: 2\n" |
| 79 | " ReplacementText: 'replacement #2'\n" |
Dmitry Polukhin | c98c94d | 2020-05-02 02:35:41 -0700 | [diff] [blame] | 80 | " Level: Warning\n" |
| 81 | " BuildDirectory: 'path/to/build/directory'\n" |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 82 | " Ranges:\n" |
| 83 | " - FilePath: 'path/to/source.cpp'\n" |
| 84 | " FileOffset: 10\n" |
| 85 | " Length: 10\n" |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 86 | " - DiagnosticName: 'diagnostic#3'\n" |
Fangrui Song | ff6836f | 2019-07-12 05:59:28 +0000 | [diff] [blame] | 87 | " DiagnosticMessage:\n" |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 88 | " Message: 'message #3'\n" |
| 89 | " FilePath: 'path/to/source2.cpp'\n" |
| 90 | " FileOffset: 72\n" |
| 91 | " Replacements: []\n" |
Fangrui Song | ff6836f | 2019-07-12 05:59:28 +0000 | [diff] [blame] | 92 | " Notes:\n" |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 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" |
Dmitry Polukhin | c98c94d | 2020-05-02 02:35:41 -0700 | [diff] [blame] | 101 | " Level: Warning\n" |
| 102 | " BuildDirectory: 'path/to/build/directory'\n" |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 103 | "...\n"; |
| 104 | |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 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, |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 113 | "path/to/source.cpp", Fix1, {})); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 114 | |
| 115 | StringMap<Replacements> Fix2 = { |
| 116 | {"path/to/header.h", |
| 117 | Replacements({"path/to/header.h", 62, 2, "replacement #2"})}}; |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 118 | SmallVector<FileByteRange, 1> Ranges2 = |
| 119 | {makeByteRange(10, 10, "path/to/source.cpp")}; |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 120 | TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#2", "message #2", 60, |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 121 | "path/to/header.h", Fix2, Ranges2)); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 122 | |
| 123 | TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#3", "message #3", 72, |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 124 | "path/to/source2.cpp", {}, {})); |
Alexander Kornienko | af17a38 | 2018-11-21 01:06:32 +0000 | [diff] [blame] | 125 | TUD.Diagnostics.back().Notes.push_back( |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 126 | makeMessage("Note1", 88, "path/to/note1.cpp", {})); |
Alexander Kornienko | af17a38 | 2018-11-21 01:06:32 +0000 | [diff] [blame] | 127 | TUD.Diagnostics.back().Notes.push_back( |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 128 | makeMessage("Note2", 99, "path/to/note2.cpp", {})); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 129 | |
| 130 | std::string YamlContent; |
| 131 | raw_string_ostream YamlContentStream(YamlContent); |
| 132 | |
| 133 | yaml::Output YAML(YamlContentStream); |
| 134 | YAML << TUD; |
| 135 | |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 136 | EXPECT_EQ(YAMLContent, YamlContentStream.str()); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST(DiagnosticsYamlTest, deserializesDiagnostics) { |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 140 | TranslationUnitDiagnostics TUDActual; |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 141 | yaml::Input YAML(YAMLContent); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 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); |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 163 | std::vector<Replacement> Fixes1 = getFixes(D1.Message.Fix); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 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()); |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 169 | EXPECT_TRUE(D1.Ranges.empty()); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 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); |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 176 | std::vector<Replacement> Fixes2 = getFixes(D2.Message.Fix); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 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()); |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 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); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 186 | |
| 187 | Diagnostic D3 = TUDActual.Diagnostics[2]; |
| 188 | EXPECT_EQ("diagnostic#3", D3.DiagnosticName); |
| 189 | EXPECT_EQ("message #3", D3.Message.Message); |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 190 | EXPECT_EQ(72u, D3.Message.FileOffset); |
| 191 | EXPECT_EQ("path/to/source2.cpp", D3.Message.FilePath); |
Alexander Kornienko | af17a38 | 2018-11-21 01:06:32 +0000 | [diff] [blame] | 192 | EXPECT_EQ(2u, D3.Notes.size()); |
| 193 | EXPECT_EQ("Note1", D3.Notes[0].Message); |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 194 | EXPECT_EQ(88u, D3.Notes[0].FileOffset); |
Alexander Kornienko | af17a38 | 2018-11-21 01:06:32 +0000 | [diff] [blame] | 195 | EXPECT_EQ("path/to/note1.cpp", D3.Notes[0].FilePath); |
| 196 | EXPECT_EQ("Note2", D3.Notes[1].Message); |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 197 | EXPECT_EQ(99u, D3.Notes[1].FileOffset); |
Alexander Kornienko | af17a38 | 2018-11-21 01:06:32 +0000 | [diff] [blame] | 198 | EXPECT_EQ("path/to/note2.cpp", D3.Notes[1].FilePath); |
Haojian Wu | f2879d8 | 2019-04-17 12:53:59 +0000 | [diff] [blame] | 199 | std::vector<Replacement> Fixes3 = getFixes(D3.Message.Fix); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 200 | EXPECT_TRUE(Fixes3.empty()); |
Joe Turner | b26c88e | 2020-02-25 14:56:57 +0100 | [diff] [blame] | 201 | EXPECT_TRUE(D3.Ranges.empty()); |
Alexander Kornienko | 2745e17 | 2017-07-14 10:37:44 +0000 | [diff] [blame] | 202 | } |