blob: acfefa505edbc70216cdad250e8dc965e00d2b21 [file] [log] [blame]
Alexander Kornienko2745e172017-07-14 10:37:44 +00001//===- unittests/Tooling/DiagnosticsYamlTest.cpp - Serialization tests ---===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Kornienko2745e172017-07-14 10:37:44 +00006//
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 Turnerb26c88e2020-02-25 14:56:57 +010016#include "llvm/ADT/SmallVector.h"
Alexander Kornienko2745e172017-07-14 10:37:44 +000017#include "gtest/gtest.h"
18
19using namespace llvm;
20using namespace clang::tooling;
Matthias Braun0bc7d632017-07-15 00:29:25 +000021using clang::tooling::Diagnostic;
Alexander Kornienko2745e172017-07-14 10:37:44 +000022
Alexander Kornienkoaf17a382018-11-21 01:06:32 +000023static DiagnosticMessage makeMessage(const std::string &Message, int FileOffset,
Haojian Wuf2879d82019-04-17 12:53:59 +000024 const std::string &FilePath,
25 const StringMap<Replacements> &Fix) {
Alexander Kornienko2745e172017-07-14 10:37:44 +000026 DiagnosticMessage DiagMessage;
27 DiagMessage.Message = Message;
28 DiagMessage.FileOffset = FileOffset;
29 DiagMessage.FilePath = FilePath;
Haojian Wuf2879d82019-04-17 12:53:59 +000030 DiagMessage.Fix = Fix;
Alexander Kornienkoaf17a382018-11-21 01:06:32 +000031 return DiagMessage;
32}
33
Joe Turnerb26c88e2020-02-25 14:56:57 +010034static 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 Kornienkoaf17a382018-11-21 01:06:32 +000044static Diagnostic makeDiagnostic(StringRef DiagnosticName,
45 const std::string &Message, int FileOffset,
46 const std::string &FilePath,
Joe Turnerb26c88e2020-02-25 14:56:57 +010047 const StringMap<Replacements> &Fix,
48 const SmallVector<FileByteRange, 1> &Ranges) {
Haojian Wuf2879d82019-04-17 12:53:59 +000049 return Diagnostic(DiagnosticName,
50 makeMessage(Message, FileOffset, FilePath, Fix), {},
Joe Turnerb26c88e2020-02-25 14:56:57 +010051 Diagnostic::Warning, "path/to/build/directory", Ranges);
Alexander Kornienko2745e172017-07-14 10:37:44 +000052}
53
Haojian Wuf2879d82019-04-17 12:53:59 +000054static const char *YAMLContent =
55 "---\n"
56 "MainSourceFile: 'path/to/source.cpp'\n"
Fangrui Songff6836f2019-07-12 05:59:28 +000057 "Diagnostics:\n"
Haojian Wuf2879d82019-04-17 12:53:59 +000058 " - DiagnosticName: 'diagnostic#1\'\n"
Fangrui Songff6836f2019-07-12 05:59:28 +000059 " DiagnosticMessage:\n"
Haojian Wuf2879d82019-04-17 12:53:59 +000060 " Message: 'message #1'\n"
61 " FilePath: 'path/to/source.cpp'\n"
62 " FileOffset: 55\n"
Fangrui Songff6836f2019-07-12 05:59:28 +000063 " Replacements:\n"
Haojian Wuf2879d82019-04-17 12:53:59 +000064 " - FilePath: 'path/to/source.cpp'\n"
65 " Offset: 100\n"
66 " Length: 12\n"
67 " ReplacementText: 'replacement #1'\n"
Dmitry Polukhinc98c94d2020-05-02 02:35:41 -070068 " Level: Warning\n"
69 " BuildDirectory: 'path/to/build/directory'\n"
Haojian Wuf2879d82019-04-17 12:53:59 +000070 " - DiagnosticName: 'diagnostic#2'\n"
Fangrui Songff6836f2019-07-12 05:59:28 +000071 " DiagnosticMessage:\n"
Haojian Wuf2879d82019-04-17 12:53:59 +000072 " Message: 'message #2'\n"
73 " FilePath: 'path/to/header.h'\n"
74 " FileOffset: 60\n"
Fangrui Songff6836f2019-07-12 05:59:28 +000075 " Replacements:\n"
Haojian Wuf2879d82019-04-17 12:53:59 +000076 " - FilePath: 'path/to/header.h'\n"
77 " Offset: 62\n"
78 " Length: 2\n"
79 " ReplacementText: 'replacement #2'\n"
Dmitry Polukhinc98c94d2020-05-02 02:35:41 -070080 " Level: Warning\n"
81 " BuildDirectory: 'path/to/build/directory'\n"
Joe Turnerb26c88e2020-02-25 14:56:57 +010082 " Ranges:\n"
83 " - FilePath: 'path/to/source.cpp'\n"
84 " FileOffset: 10\n"
85 " Length: 10\n"
Haojian Wuf2879d82019-04-17 12:53:59 +000086 " - DiagnosticName: 'diagnostic#3'\n"
Fangrui Songff6836f2019-07-12 05:59:28 +000087 " DiagnosticMessage:\n"
Haojian Wuf2879d82019-04-17 12:53:59 +000088 " Message: 'message #3'\n"
89 " FilePath: 'path/to/source2.cpp'\n"
90 " FileOffset: 72\n"
91 " Replacements: []\n"
Fangrui Songff6836f2019-07-12 05:59:28 +000092 " Notes:\n"
Haojian Wuf2879d82019-04-17 12:53:59 +000093 " - 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 Polukhinc98c94d2020-05-02 02:35:41 -0700101 " Level: Warning\n"
102 " BuildDirectory: 'path/to/build/directory'\n"
Haojian Wuf2879d82019-04-17 12:53:59 +0000103 "...\n";
104
Alexander Kornienko2745e172017-07-14 10:37:44 +0000105TEST(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 Turnerb26c88e2020-02-25 14:56:57 +0100113 "path/to/source.cpp", Fix1, {}));
Alexander Kornienko2745e172017-07-14 10:37:44 +0000114
115 StringMap<Replacements> Fix2 = {
116 {"path/to/header.h",
117 Replacements({"path/to/header.h", 62, 2, "replacement #2"})}};
Joe Turnerb26c88e2020-02-25 14:56:57 +0100118 SmallVector<FileByteRange, 1> Ranges2 =
119 {makeByteRange(10, 10, "path/to/source.cpp")};
Alexander Kornienko2745e172017-07-14 10:37:44 +0000120 TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#2", "message #2", 60,
Joe Turnerb26c88e2020-02-25 14:56:57 +0100121 "path/to/header.h", Fix2, Ranges2));
Alexander Kornienko2745e172017-07-14 10:37:44 +0000122
123 TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#3", "message #3", 72,
Joe Turnerb26c88e2020-02-25 14:56:57 +0100124 "path/to/source2.cpp", {}, {}));
Alexander Kornienkoaf17a382018-11-21 01:06:32 +0000125 TUD.Diagnostics.back().Notes.push_back(
Haojian Wuf2879d82019-04-17 12:53:59 +0000126 makeMessage("Note1", 88, "path/to/note1.cpp", {}));
Alexander Kornienkoaf17a382018-11-21 01:06:32 +0000127 TUD.Diagnostics.back().Notes.push_back(
Haojian Wuf2879d82019-04-17 12:53:59 +0000128 makeMessage("Note2", 99, "path/to/note2.cpp", {}));
Alexander Kornienko2745e172017-07-14 10:37:44 +0000129
130 std::string YamlContent;
131 raw_string_ostream YamlContentStream(YamlContent);
132
133 yaml::Output YAML(YamlContentStream);
134 YAML << TUD;
135
Haojian Wuf2879d82019-04-17 12:53:59 +0000136 EXPECT_EQ(YAMLContent, YamlContentStream.str());
Alexander Kornienko2745e172017-07-14 10:37:44 +0000137}
138
139TEST(DiagnosticsYamlTest, deserializesDiagnostics) {
Alexander Kornienko2745e172017-07-14 10:37:44 +0000140 TranslationUnitDiagnostics TUDActual;
Haojian Wuf2879d82019-04-17 12:53:59 +0000141 yaml::Input YAML(YAMLContent);
Alexander Kornienko2745e172017-07-14 10:37:44 +0000142 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 Wuf2879d82019-04-17 12:53:59 +0000163 std::vector<Replacement> Fixes1 = getFixes(D1.Message.Fix);
Alexander Kornienko2745e172017-07-14 10:37:44 +0000164 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 Turnerb26c88e2020-02-25 14:56:57 +0100169 EXPECT_TRUE(D1.Ranges.empty());
Alexander Kornienko2745e172017-07-14 10:37:44 +0000170
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 Wuf2879d82019-04-17 12:53:59 +0000176 std::vector<Replacement> Fixes2 = getFixes(D2.Message.Fix);
Alexander Kornienko2745e172017-07-14 10:37:44 +0000177 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 Turnerb26c88e2020-02-25 14:56:57 +0100182 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 Kornienko2745e172017-07-14 10:37:44 +0000186
187 Diagnostic D3 = TUDActual.Diagnostics[2];
188 EXPECT_EQ("diagnostic#3", D3.DiagnosticName);
189 EXPECT_EQ("message #3", D3.Message.Message);
Haojian Wuf2879d82019-04-17 12:53:59 +0000190 EXPECT_EQ(72u, D3.Message.FileOffset);
191 EXPECT_EQ("path/to/source2.cpp", D3.Message.FilePath);
Alexander Kornienkoaf17a382018-11-21 01:06:32 +0000192 EXPECT_EQ(2u, D3.Notes.size());
193 EXPECT_EQ("Note1", D3.Notes[0].Message);
Haojian Wuf2879d82019-04-17 12:53:59 +0000194 EXPECT_EQ(88u, D3.Notes[0].FileOffset);
Alexander Kornienkoaf17a382018-11-21 01:06:32 +0000195 EXPECT_EQ("path/to/note1.cpp", D3.Notes[0].FilePath);
196 EXPECT_EQ("Note2", D3.Notes[1].Message);
Haojian Wuf2879d82019-04-17 12:53:59 +0000197 EXPECT_EQ(99u, D3.Notes[1].FileOffset);
Alexander Kornienkoaf17a382018-11-21 01:06:32 +0000198 EXPECT_EQ("path/to/note2.cpp", D3.Notes[1].FilePath);
Haojian Wuf2879d82019-04-17 12:53:59 +0000199 std::vector<Replacement> Fixes3 = getFixes(D3.Message.Fix);
Alexander Kornienko2745e172017-07-14 10:37:44 +0000200 EXPECT_TRUE(Fixes3.empty());
Joe Turnerb26c88e2020-02-25 14:56:57 +0100201 EXPECT_TRUE(D3.Ranges.empty());
Alexander Kornienko2745e172017-07-14 10:37:44 +0000202}