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/YAMLRemarkSerializer.h"
11 #include "llvm/Support/Error.h"
12 #include "gtest/gtest.h"
14 // We need to supprt Windows paths as well. In order to have paths with the same
15 // length, use a different path according to the platform.
17 #define EXTERNALFILETESTPATH "C:/externalfi"
19 #define EXTERNALFILETESTPATH "/externalfile"
24 static void check(remarks::SerializerMode Mode
, ArrayRef
<remarks::Remark
> Rs
,
25 StringRef ExpectedR
, Optional
<StringRef
> ExpectedMeta
,
26 bool UseStrTab
= false,
27 Optional
<remarks::StringTable
> StrTab
= None
) {
29 raw_string_ostream
OS(Buf
);
30 Expected
<std::unique_ptr
<remarks::RemarkSerializer
>> MaybeS
= [&] {
33 return createRemarkSerializer(remarks::Format::YAMLStrTab
, Mode
, OS
,
36 return createRemarkSerializer(remarks::Format::YAMLStrTab
, Mode
, OS
);
38 return createRemarkSerializer(remarks::Format::YAML
, Mode
, OS
);
40 EXPECT_FALSE(errorToBool(MaybeS
.takeError()));
41 std::unique_ptr
<remarks::RemarkSerializer
> S
= std::move(*MaybeS
);
43 for (const remarks::Remark
&R
: Rs
)
45 EXPECT_EQ(OS
.str(), ExpectedR
);
49 std::unique_ptr
<remarks::MetaSerializer
> MS
=
50 S
->metaSerializer(OS
, StringRef(EXTERNALFILETESTPATH
));
52 EXPECT_EQ(OS
.str(), *ExpectedMeta
);
56 static void check(const remarks::Remark
&R
, StringRef ExpectedR
,
57 StringRef ExpectedMeta
, bool UseStrTab
= false,
58 Optional
<remarks::StringTable
> StrTab
= None
) {
59 return check(remarks::SerializerMode::Separate
, makeArrayRef(&R
, &R
+ 1), ExpectedR
, ExpectedMeta
,
60 UseStrTab
, std::move(StrTab
));
63 static void checkStandalone(const remarks::Remark
&R
, StringRef ExpectedR
,
64 Optional
<remarks::StringTable
> StrTab
= None
) {
65 bool UseStrTab
= StrTab
.hasValue();
66 return check(remarks::SerializerMode::Standalone
, makeArrayRef(&R
, &R
+1), ExpectedR
,
67 /*ExpectedMeta=*/None
, UseStrTab
, 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};
89 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
94 " - keydebug: valuedebug\n"
95 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
100 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};
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};
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\0"
165 EXTERNALFILETESTPATH
"\0",
170 TEST(YAMLRemarks
, SerializerRemarkParsedStrTab
) {
171 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
173 R
.RemarkType
= remarks::Type::Missed
;
175 R
.RemarkName
= "name";
176 R
.FunctionName
= "func";
177 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
179 R
.Args
.emplace_back();
180 R
.Args
.back().Key
= "key";
181 R
.Args
.back().Val
= "value";
182 R
.Args
.emplace_back();
183 R
.Args
.back().Key
= "keydebug";
184 R
.Args
.back().Val
= "valuedebug";
185 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
190 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
196 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
198 StringRef("REMARKS\0"
201 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
202 EXTERNALFILETESTPATH
"\0",
205 remarks::StringTable(remarks::ParsedStringTable(StrTab
)));
208 TEST(YAMLRemarks
, SerializerRemarkParsedStrTabStandalone
) {
209 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
210 remarks::ParsedStringTable
ParsedStrTab(StrTab
);
211 remarks::StringTable
PreFilledStrTab(ParsedStrTab
);
213 R
.RemarkType
= remarks::Type::Missed
;
215 R
.RemarkName
= "name";
216 R
.FunctionName
= "func";
217 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
219 R
.Args
.emplace_back();
220 R
.Args
.back().Key
= "key";
221 R
.Args
.back().Val
= "value";
222 R
.Args
.emplace_back();
223 R
.Args
.back().Key
= "keydebug";
224 R
.Args
.back().Val
= "valuedebug";
225 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
228 StringRef("REMARKS\0"
231 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
235 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
241 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
244 std::move(PreFilledStrTab
));
247 TEST(YAMLRemarks
, SerializerRemarkParsedStrTabStandaloneMultipleRemarks
) {
248 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
249 remarks::ParsedStringTable
ParsedStrTab(StrTab
);
250 remarks::StringTable
PreFilledStrTab(ParsedStrTab
);
251 SmallVector
<remarks::Remark
, 2> Rs
;
253 R
.RemarkType
= remarks::Type::Missed
;
255 R
.RemarkName
= "name";
256 R
.FunctionName
= "func";
257 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
259 R
.Args
.emplace_back();
260 R
.Args
.back().Key
= "key";
261 R
.Args
.back().Val
= "value";
262 R
.Args
.emplace_back();
263 R
.Args
.back().Key
= "keydebug";
264 R
.Args
.back().Val
= "valuedebug";
265 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
266 Rs
.emplace_back(R
.clone());
267 Rs
.emplace_back(std::move(R
));
268 check(remarks::SerializerMode::Standalone
, Rs
,
269 StringRef("REMARKS\0"
272 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
276 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
282 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
287 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
293 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
296 /*ExpectedMeta=*/None
,
297 /*UseStrTab=*/true, std::move(PreFilledStrTab
));