xref: /llvm-project/clang/unittests/Tooling/DiagnosticsYamlTest.cpp (revision f2879d8a4877eafcdb12c852030746d175f8abbd)
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 "gtest/gtest.h"
17 
18 using namespace llvm;
19 using namespace clang::tooling;
20 using clang::tooling::Diagnostic;
21 
22 static DiagnosticMessage makeMessage(const std::string &Message, int FileOffset,
23                                      const std::string &FilePath,
24                                      const StringMap<Replacements> &Fix) {
25   DiagnosticMessage DiagMessage;
26   DiagMessage.Message = Message;
27   DiagMessage.FileOffset = FileOffset;
28   DiagMessage.FilePath = FilePath;
29   DiagMessage.Fix = Fix;
30   return DiagMessage;
31 }
32 
33 static Diagnostic makeDiagnostic(StringRef DiagnosticName,
34                                  const std::string &Message, int FileOffset,
35                                  const std::string &FilePath,
36                                  const StringMap<Replacements> &Fix) {
37   return Diagnostic(DiagnosticName,
38                     makeMessage(Message, FileOffset, FilePath, Fix), {},
39                     Diagnostic::Warning, "path/to/build/directory");
40 }
41 
42 static const char *YAMLContent =
43     "---\n"
44     "MainSourceFile:  'path/to/source.cpp'\n"
45     "Diagnostics:     \n"
46     "  - DiagnosticName:  'diagnostic#1\'\n"
47     "    DiagnosticMessage: \n"
48     "      Message:         'message #1'\n"
49     "      FilePath:        'path/to/source.cpp'\n"
50     "      FileOffset:      55\n"
51     "      Replacements:    \n"
52     "        - FilePath:        'path/to/source.cpp'\n"
53     "          Offset:          100\n"
54     "          Length:          12\n"
55     "          ReplacementText: 'replacement #1'\n"
56     "  - DiagnosticName:  'diagnostic#2'\n"
57     "    DiagnosticMessage: \n"
58     "      Message:         'message #2'\n"
59     "      FilePath:        'path/to/header.h'\n"
60     "      FileOffset:      60\n"
61     "      Replacements:    \n"
62     "        - FilePath:        'path/to/header.h'\n"
63     "          Offset:          62\n"
64     "          Length:          2\n"
65     "          ReplacementText: 'replacement #2'\n"
66     "  - DiagnosticName:  'diagnostic#3'\n"
67     "    DiagnosticMessage: \n"
68     "      Message:         'message #3'\n"
69     "      FilePath:        'path/to/source2.cpp'\n"
70     "      FileOffset:      72\n"
71     "      Replacements:    []\n"
72     "    Notes:           \n"
73     "      - Message:         Note1\n"
74     "        FilePath:        'path/to/note1.cpp'\n"
75     "        FileOffset:      88\n"
76     "        Replacements:    []\n"
77     "      - Message:         Note2\n"
78     "        FilePath:        'path/to/note2.cpp'\n"
79     "        FileOffset:      99\n"
80     "        Replacements:    []\n"
81     "...\n";
82 
83 TEST(DiagnosticsYamlTest, serializesDiagnostics) {
84   TranslationUnitDiagnostics TUD;
85   TUD.MainSourceFile = "path/to/source.cpp";
86 
87   StringMap<Replacements> Fix1 = {
88       {"path/to/source.cpp",
89        Replacements({"path/to/source.cpp", 100, 12, "replacement #1"})}};
90   TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#1", "message #1", 55,
91                                            "path/to/source.cpp", Fix1));
92 
93   StringMap<Replacements> Fix2 = {
94       {"path/to/header.h",
95        Replacements({"path/to/header.h", 62, 2, "replacement #2"})}};
96   TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#2", "message #2", 60,
97                                            "path/to/header.h", Fix2));
98 
99   TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#3", "message #3", 72,
100                                            "path/to/source2.cpp", {}));
101   TUD.Diagnostics.back().Notes.push_back(
102       makeMessage("Note1", 88, "path/to/note1.cpp", {}));
103   TUD.Diagnostics.back().Notes.push_back(
104       makeMessage("Note2", 99, "path/to/note2.cpp", {}));
105 
106   std::string YamlContent;
107   raw_string_ostream YamlContentStream(YamlContent);
108 
109   yaml::Output YAML(YamlContentStream);
110   YAML << TUD;
111 
112   EXPECT_EQ(YAMLContent, YamlContentStream.str());
113 }
114 
115 TEST(DiagnosticsYamlTest, deserializesDiagnostics) {
116   TranslationUnitDiagnostics TUDActual;
117   yaml::Input YAML(YAMLContent);
118   YAML >> TUDActual;
119 
120   ASSERT_FALSE(YAML.error());
121   ASSERT_EQ(3u, TUDActual.Diagnostics.size());
122   EXPECT_EQ("path/to/source.cpp", TUDActual.MainSourceFile);
123 
124   auto getFixes = [](const StringMap<Replacements> &Fix) {
125     std::vector<Replacement> Fixes;
126     for (auto &Replacements : Fix) {
127       for (auto &Replacement : Replacements.second) {
128         Fixes.push_back(Replacement);
129       }
130     }
131     return Fixes;
132   };
133 
134   Diagnostic D1 = TUDActual.Diagnostics[0];
135   EXPECT_EQ("diagnostic#1", D1.DiagnosticName);
136   EXPECT_EQ("message #1", D1.Message.Message);
137   EXPECT_EQ(55u, D1.Message.FileOffset);
138   EXPECT_EQ("path/to/source.cpp", D1.Message.FilePath);
139   std::vector<Replacement> Fixes1 = getFixes(D1.Message.Fix);
140   ASSERT_EQ(1u, Fixes1.size());
141   EXPECT_EQ("path/to/source.cpp", Fixes1[0].getFilePath());
142   EXPECT_EQ(100u, Fixes1[0].getOffset());
143   EXPECT_EQ(12u, Fixes1[0].getLength());
144   EXPECT_EQ("replacement #1", Fixes1[0].getReplacementText());
145 
146   Diagnostic D2 = TUDActual.Diagnostics[1];
147   EXPECT_EQ("diagnostic#2", D2.DiagnosticName);
148   EXPECT_EQ("message #2", D2.Message.Message);
149   EXPECT_EQ(60u, D2.Message.FileOffset);
150   EXPECT_EQ("path/to/header.h", D2.Message.FilePath);
151   std::vector<Replacement> Fixes2 = getFixes(D2.Message.Fix);
152   ASSERT_EQ(1u, Fixes2.size());
153   EXPECT_EQ("path/to/header.h", Fixes2[0].getFilePath());
154   EXPECT_EQ(62u, Fixes2[0].getOffset());
155   EXPECT_EQ(2u, Fixes2[0].getLength());
156   EXPECT_EQ("replacement #2", Fixes2[0].getReplacementText());
157 
158   Diagnostic D3 = TUDActual.Diagnostics[2];
159   EXPECT_EQ("diagnostic#3", D3.DiagnosticName);
160   EXPECT_EQ("message #3", D3.Message.Message);
161   EXPECT_EQ(72u, D3.Message.FileOffset);
162   EXPECT_EQ("path/to/source2.cpp", D3.Message.FilePath);
163   EXPECT_EQ(2u, D3.Notes.size());
164   EXPECT_EQ("Note1", D3.Notes[0].Message);
165   EXPECT_EQ(88u, D3.Notes[0].FileOffset);
166   EXPECT_EQ("path/to/note1.cpp", D3.Notes[0].FilePath);
167   EXPECT_EQ("Note2", D3.Notes[1].Message);
168   EXPECT_EQ(99u, D3.Notes[1].FileOffset);
169   EXPECT_EQ("path/to/note2.cpp", D3.Notes[1].FilePath);
170   std::vector<Replacement> Fixes3 = getFixes(D3.Message.Fix);
171   EXPECT_TRUE(Fixes3.empty());
172 }
173