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
, Optional
<StringRef
> ExpectedMeta
,
28 Optional
<remarks::StringTable
> StrTab
= None
) {
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 Optional
<remarks::StringTable
> StrTab
= None
) {
57 return check(SerializerFormat
, remarks::SerializerMode::Separate
,
58 makeArrayRef(&R
, &R
+ 1), ExpectedR
, ExpectedMeta
,
62 static void checkStandalone(remarks::Format SerializerFormat
,
63 const remarks::Remark
&R
, StringRef ExpectedR
,
64 Optional
<remarks::StringTable
> StrTab
= None
) {
65 return check(SerializerFormat
, remarks::SerializerMode::Standalone
,
66 makeArrayRef(&R
, &R
+ 1), ExpectedR
,
67 /*ExpectedMeta=*/None
, std::move(StrTab
));
70 TEST(YAMLRemarks
, SerializerRemark
) {
72 R
.RemarkType
= remarks::Type::Missed
;
74 R
.RemarkName
= "name";
75 R
.FunctionName
= "func";
76 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
78 R
.Args
.emplace_back();
79 R
.Args
.back().Key
= "key";
80 R
.Args
.back().Val
= "value";
81 R
.Args
.emplace_back();
82 R
.Args
.back().Key
= "keydebug";
83 R
.Args
.back().Val
= "valuedebug";
84 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
85 check(remarks::Format::YAML
, R
,
89 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
94 " - keydebug: valuedebug\n"
95 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
99 "\0\0\0\0\0\0\0\0" EXTERNALFILETESTPATH
"\0",
103 TEST(YAMLRemarks
, SerializerRemarkStandalone
) {
105 R
.RemarkType
= remarks::Type::Missed
;
107 R
.RemarkName
= "name";
108 R
.FunctionName
= "func";
109 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
111 R
.Args
.emplace_back();
112 R
.Args
.back().Key
= "key";
113 R
.Args
.back().Val
= "value";
114 R
.Args
.emplace_back();
115 R
.Args
.back().Key
= "keydebug";
116 R
.Args
.back().Val
= "valuedebug";
117 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
119 remarks::Format::YAML
, R
,
120 StringRef("--- !Missed\n"
123 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
128 " - keydebug: valuedebug\n"
129 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
133 TEST(YAMLRemarks
, SerializerRemarkStrTab
) {
135 R
.RemarkType
= remarks::Type::Missed
;
137 R
.RemarkName
= "name";
138 R
.FunctionName
= "func";
139 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
141 R
.Args
.emplace_back();
142 R
.Args
.back().Key
= "key";
143 R
.Args
.back().Val
= "value";
144 R
.Args
.emplace_back();
145 R
.Args
.back().Key
= "keydebug";
146 R
.Args
.back().Val
= "valuedebug";
147 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
148 check(remarks::Format::YAMLStrTab
, R
,
152 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
158 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
160 StringRef("REMARKS\0"
163 "pass\0name\0func\0path\0value\0valuedebug\0argpath"
164 "\0" EXTERNALFILETESTPATH
"\0",
168 TEST(YAMLRemarks
, SerializerRemarkParsedStrTab
) {
169 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
171 R
.RemarkType
= remarks::Type::Missed
;
173 R
.RemarkName
= "name";
174 R
.FunctionName
= "func";
175 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
177 R
.Args
.emplace_back();
178 R
.Args
.back().Key
= "key";
179 R
.Args
.back().Val
= "value";
180 R
.Args
.emplace_back();
181 R
.Args
.back().Key
= "keydebug";
182 R
.Args
.back().Val
= "valuedebug";
183 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
184 check(remarks::Format::YAMLStrTab
, R
,
188 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
194 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
196 StringRef("REMARKS\0"
199 "pass\0name\0func\0path\0value\0valuedebug\0argpath"
200 "\0" EXTERNALFILETESTPATH
"\0",
202 remarks::StringTable(remarks::ParsedStringTable(StrTab
)));
205 TEST(YAMLRemarks
, SerializerRemarkParsedStrTabStandaloneNoStrTab
) {
206 // Check that we don't use the string table even if it was provided.
207 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
208 remarks::ParsedStringTable
ParsedStrTab(StrTab
);
209 remarks::StringTable
PreFilledStrTab(ParsedStrTab
);
211 R
.RemarkType
= remarks::Type::Missed
;
213 R
.RemarkName
= "name";
214 R
.FunctionName
= "func";
215 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
217 R
.Args
.emplace_back();
218 R
.Args
.back().Key
= "key";
219 R
.Args
.back().Val
= "value";
220 R
.Args
.emplace_back();
221 R
.Args
.back().Key
= "keydebug";
222 R
.Args
.back().Val
= "valuedebug";
223 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
225 remarks::Format::YAML
, R
,
226 StringRef("--- !Missed\n"
229 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
234 " - keydebug: valuedebug\n"
235 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
237 std::move(PreFilledStrTab
));
240 TEST(YAMLRemarks
, SerializerRemarkParsedStrTabStandalone
) {
241 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
242 remarks::ParsedStringTable
ParsedStrTab(StrTab
);
243 remarks::StringTable
PreFilledStrTab(ParsedStrTab
);
245 R
.RemarkType
= remarks::Type::Missed
;
247 R
.RemarkName
= "name";
248 R
.FunctionName
= "func";
249 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
251 R
.Args
.emplace_back();
252 R
.Args
.back().Key
= "key";
253 R
.Args
.back().Val
= "value";
254 R
.Args
.emplace_back();
255 R
.Args
.back().Key
= "keydebug";
256 R
.Args
.back().Val
= "valuedebug";
257 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
259 remarks::Format::YAMLStrTab
, R
,
260 StringRef("REMARKS\0"
263 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
267 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
273 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
276 std::move(PreFilledStrTab
));
279 TEST(YAMLRemarks
, SerializerRemarkParsedStrTabStandaloneMultipleRemarks
) {
280 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
281 remarks::ParsedStringTable
ParsedStrTab(StrTab
);
282 remarks::StringTable
PreFilledStrTab(ParsedStrTab
);
283 SmallVector
<remarks::Remark
, 2> Rs
;
285 R
.RemarkType
= remarks::Type::Missed
;
287 R
.RemarkName
= "name";
288 R
.FunctionName
= "func";
289 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
291 R
.Args
.emplace_back();
292 R
.Args
.back().Key
= "key";
293 R
.Args
.back().Val
= "value";
294 R
.Args
.emplace_back();
295 R
.Args
.back().Key
= "keydebug";
296 R
.Args
.back().Val
= "valuedebug";
297 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
298 Rs
.emplace_back(R
.clone());
299 Rs
.emplace_back(std::move(R
));
300 check(remarks::Format::YAMLStrTab
, remarks::SerializerMode::Standalone
, Rs
,
301 StringRef("REMARKS\0"
304 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
308 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
314 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
319 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
325 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
328 /*ExpectedMeta=*/None
, std::move(PreFilledStrTab
));