1 //===- unittest/Support/YAMLRemarksSerializerTest.cpp --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Remarks/Remark.h"
10 #include "llvm/Remarks/RemarkParser.h"
11 #include "llvm/Remarks/YAMLRemarkSerializer.h"
12 #include "llvm/Support/Error.h"
13 #include "gtest/gtest.h"
15 // We need to supprt Windows paths as well. In order to have paths with the same
16 // length, use a different path according to the platform.
18 #define EXTERNALFILETESTPATH "C:/externalfi"
20 #define EXTERNALFILETESTPATH "/externalfile"
25 static void check(remarks::Format SerializerFormat
,
26 remarks::SerializerMode Mode
, ArrayRef
<remarks::Remark
> Rs
,
27 StringRef ExpectedR
, std::optional
<StringRef
> ExpectedMeta
,
28 std::optional
<remarks::StringTable
> StrTab
= std::nullopt
) {
30 raw_string_ostream
OS(Buf
);
31 Expected
<std::unique_ptr
<remarks::RemarkSerializer
>> MaybeS
= [&] {
33 return createRemarkSerializer(SerializerFormat
, Mode
, OS
,
36 return createRemarkSerializer(SerializerFormat
, Mode
, OS
);
38 EXPECT_FALSE(errorToBool(MaybeS
.takeError()));
39 std::unique_ptr
<remarks::RemarkSerializer
> S
= std::move(*MaybeS
);
41 for (const remarks::Remark
&R
: Rs
)
43 EXPECT_EQ(OS
.str(), ExpectedR
);
47 std::unique_ptr
<remarks::MetaSerializer
> MS
=
48 S
->metaSerializer(OS
, StringRef(EXTERNALFILETESTPATH
));
50 EXPECT_EQ(OS
.str(), *ExpectedMeta
);
54 static void check(remarks::Format SerializerFormat
, const remarks::Remark
&R
,
55 StringRef ExpectedR
, StringRef ExpectedMeta
,
56 std::optional
<remarks::StringTable
> StrTab
= std::nullopt
) {
57 return check(SerializerFormat
, remarks::SerializerMode::Separate
,
58 ArrayRef(&R
, &R
+ 1), ExpectedR
, ExpectedMeta
,
63 checkStandalone(remarks::Format SerializerFormat
, const remarks::Remark
&R
,
65 std::optional
<remarks::StringTable
> StrTab
= std::nullopt
) {
66 return check(SerializerFormat
, remarks::SerializerMode::Standalone
,
67 ArrayRef(&R
, &R
+ 1), ExpectedR
,
68 /*ExpectedMeta=*/std::nullopt
, std::move(StrTab
));
71 TEST(YAMLRemarks
, SerializerRemark
) {
73 R
.RemarkType
= remarks::Type::Missed
;
75 R
.RemarkName
= "name";
76 R
.FunctionName
= "func";
77 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
79 R
.Args
.emplace_back();
80 R
.Args
.back().Key
= "key";
81 R
.Args
.back().Val
= "value";
82 R
.Args
.emplace_back();
83 R
.Args
.back().Key
= "keydebug";
84 R
.Args
.back().Val
= "valuedebug";
85 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
86 check(remarks::Format::YAML
, R
,
90 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
95 " - keydebug: valuedebug\n"
96 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
100 "\0\0\0\0\0\0\0\0" EXTERNALFILETESTPATH
"\0",
104 TEST(YAMLRemarks
, SerializerRemarkStandalone
) {
106 R
.RemarkType
= remarks::Type::Missed
;
108 R
.RemarkName
= "name";
109 R
.FunctionName
= "func";
110 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
112 R
.Args
.emplace_back();
113 R
.Args
.back().Key
= "key";
114 R
.Args
.back().Val
= "value";
115 R
.Args
.emplace_back();
116 R
.Args
.back().Key
= "keydebug";
117 R
.Args
.back().Val
= "valuedebug";
118 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
120 remarks::Format::YAML
, R
,
121 StringRef("--- !Missed\n"
124 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
129 " - keydebug: valuedebug\n"
130 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
134 TEST(YAMLRemarks
, SerializerRemarkStrTab
) {
136 R
.RemarkType
= remarks::Type::Missed
;
138 R
.RemarkName
= "name";
139 R
.FunctionName
= "func";
140 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
142 R
.Args
.emplace_back();
143 R
.Args
.back().Key
= "key";
144 R
.Args
.back().Val
= "value";
145 R
.Args
.emplace_back();
146 R
.Args
.back().Key
= "keydebug";
147 R
.Args
.back().Val
= "valuedebug";
148 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
149 check(remarks::Format::YAMLStrTab
, R
,
153 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
159 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
161 StringRef("REMARKS\0"
164 "pass\0name\0func\0path\0value\0valuedebug\0argpath"
165 "\0" EXTERNALFILETESTPATH
"\0",
169 TEST(YAMLRemarks
, SerializerRemarkParsedStrTab
) {
170 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
172 R
.RemarkType
= remarks::Type::Missed
;
174 R
.RemarkName
= "name";
175 R
.FunctionName
= "func";
176 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
178 R
.Args
.emplace_back();
179 R
.Args
.back().Key
= "key";
180 R
.Args
.back().Val
= "value";
181 R
.Args
.emplace_back();
182 R
.Args
.back().Key
= "keydebug";
183 R
.Args
.back().Val
= "valuedebug";
184 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
185 check(remarks::Format::YAMLStrTab
, R
,
189 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
195 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
197 StringRef("REMARKS\0"
200 "pass\0name\0func\0path\0value\0valuedebug\0argpath"
201 "\0" EXTERNALFILETESTPATH
"\0",
203 remarks::StringTable(remarks::ParsedStringTable(StrTab
)));
206 TEST(YAMLRemarks
, SerializerRemarkParsedStrTabStandaloneNoStrTab
) {
207 // Check that we don't use the string table even if it was provided.
208 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
209 remarks::ParsedStringTable
ParsedStrTab(StrTab
);
210 remarks::StringTable
PreFilledStrTab(ParsedStrTab
);
212 R
.RemarkType
= remarks::Type::Missed
;
214 R
.RemarkName
= "name";
215 R
.FunctionName
= "func";
216 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
218 R
.Args
.emplace_back();
219 R
.Args
.back().Key
= "key";
220 R
.Args
.back().Val
= "value";
221 R
.Args
.emplace_back();
222 R
.Args
.back().Key
= "keydebug";
223 R
.Args
.back().Val
= "valuedebug";
224 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
226 remarks::Format::YAML
, R
,
227 StringRef("--- !Missed\n"
230 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
235 " - keydebug: valuedebug\n"
236 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
238 std::move(PreFilledStrTab
));
241 TEST(YAMLRemarks
, SerializerRemarkParsedStrTabStandalone
) {
242 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
243 remarks::ParsedStringTable
ParsedStrTab(StrTab
);
244 remarks::StringTable
PreFilledStrTab(ParsedStrTab
);
246 R
.RemarkType
= remarks::Type::Missed
;
248 R
.RemarkName
= "name";
249 R
.FunctionName
= "func";
250 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
252 R
.Args
.emplace_back();
253 R
.Args
.back().Key
= "key";
254 R
.Args
.back().Val
= "value";
255 R
.Args
.emplace_back();
256 R
.Args
.back().Key
= "keydebug";
257 R
.Args
.back().Val
= "valuedebug";
258 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
260 remarks::Format::YAMLStrTab
, R
,
261 StringRef("REMARKS\0"
264 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
268 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
274 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
277 std::move(PreFilledStrTab
));
280 TEST(YAMLRemarks
, SerializerRemarkParsedStrTabStandaloneMultipleRemarks
) {
281 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
282 remarks::ParsedStringTable
ParsedStrTab(StrTab
);
283 remarks::StringTable
PreFilledStrTab(ParsedStrTab
);
284 SmallVector
<remarks::Remark
, 2> Rs
;
286 R
.RemarkType
= remarks::Type::Missed
;
288 R
.RemarkName
= "name";
289 R
.FunctionName
= "func";
290 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
292 R
.Args
.emplace_back();
293 R
.Args
.back().Key
= "key";
294 R
.Args
.back().Val
= "value";
295 R
.Args
.emplace_back();
296 R
.Args
.back().Key
= "keydebug";
297 R
.Args
.back().Val
= "valuedebug";
298 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
299 Rs
.emplace_back(R
.clone());
300 Rs
.emplace_back(std::move(R
));
301 check(remarks::Format::YAMLStrTab
, remarks::SerializerMode::Standalone
, Rs
,
302 StringRef("REMARKS\0"
305 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
309 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
315 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
320 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
326 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
329 /*ExpectedMeta=*/std::nullopt
, std::move(PreFilledStrTab
));