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
, const remarks::Remark
&R
,
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
);
44 EXPECT_EQ(OS
.str(), ExpectedR
);
48 std::unique_ptr
<remarks::MetaSerializer
> MS
=
49 S
->metaSerializer(OS
, StringRef(EXTERNALFILETESTPATH
));
51 EXPECT_EQ(OS
.str(), *ExpectedMeta
);
55 static void check(const remarks::Remark
&R
, StringRef ExpectedR
,
56 StringRef ExpectedMeta
, bool UseStrTab
= false,
57 Optional
<remarks::StringTable
> StrTab
= None
) {
58 return check(remarks::SerializerMode::Separate
, R
, ExpectedR
, ExpectedMeta
,
59 UseStrTab
, std::move(StrTab
));
62 static void checkStandalone(const remarks::Remark
&R
, StringRef ExpectedR
,
63 Optional
<remarks::StringTable
> StrTab
= None
) {
64 bool UseStrTab
= StrTab
.hasValue();
65 return check(remarks::SerializerMode::Standalone
, R
, ExpectedR
,
66 /*ExpectedMeta=*/None
, UseStrTab
, std::move(StrTab
));
69 TEST(YAMLRemarks
, SerializerRemark
) {
71 R
.RemarkType
= remarks::Type::Missed
;
73 R
.RemarkName
= "name";
74 R
.FunctionName
= "func";
75 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
77 R
.Args
.emplace_back();
78 R
.Args
.back().Key
= "key";
79 R
.Args
.back().Val
= "value";
80 R
.Args
.emplace_back();
81 R
.Args
.back().Key
= "keydebug";
82 R
.Args
.back().Val
= "valuedebug";
83 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
88 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
93 " - keydebug: valuedebug\n"
94 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
99 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};
120 StringRef("REMARKS\0"
126 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
131 " - keydebug: valuedebug\n"
132 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
137 TEST(YAMLRemarks
, SerializerRemarkStrTab
) {
139 R
.RemarkType
= remarks::Type::Missed
;
141 R
.RemarkName
= "name";
142 R
.FunctionName
= "func";
143 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
145 R
.Args
.emplace_back();
146 R
.Args
.back().Key
= "key";
147 R
.Args
.back().Val
= "value";
148 R
.Args
.emplace_back();
149 R
.Args
.back().Key
= "keydebug";
150 R
.Args
.back().Val
= "valuedebug";
151 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
156 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
162 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
164 StringRef("REMARKS\0"
167 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
168 EXTERNALFILETESTPATH
"\0",
173 TEST(YAMLRemarks
, SerializerRemarkParsedStrTab
) {
174 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
176 R
.RemarkType
= remarks::Type::Missed
;
178 R
.RemarkName
= "name";
179 R
.FunctionName
= "func";
180 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
182 R
.Args
.emplace_back();
183 R
.Args
.back().Key
= "key";
184 R
.Args
.back().Val
= "value";
185 R
.Args
.emplace_back();
186 R
.Args
.back().Key
= "keydebug";
187 R
.Args
.back().Val
= "valuedebug";
188 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
193 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
199 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
201 StringRef("REMARKS\0"
204 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
205 EXTERNALFILETESTPATH
"\0",
208 remarks::StringTable(remarks::ParsedStringTable(StrTab
)));
211 TEST(YAMLRemarks
, SerializerRemarkParsedStrTabStandalone
) {
212 StringRef
StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
213 remarks::ParsedStringTable
ParsedStrTab(StrTab
);
214 remarks::StringTable
PreFilledStrTab(ParsedStrTab
);
216 R
.RemarkType
= remarks::Type::Missed
;
218 R
.RemarkName
= "name";
219 R
.FunctionName
= "func";
220 R
.Loc
= remarks::RemarkLocation
{"path", 3, 4};
222 R
.Args
.emplace_back();
223 R
.Args
.back().Key
= "key";
224 R
.Args
.back().Val
= "value";
225 R
.Args
.emplace_back();
226 R
.Args
.back().Key
= "keydebug";
227 R
.Args
.back().Val
= "valuedebug";
228 R
.Args
.back().Loc
= remarks::RemarkLocation
{"argpath", 6, 7};
231 StringRef("REMARKS\0"
234 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
238 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
244 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
247 std::move(PreFilledStrTab
));