Skip to content

Commit 78c92d2

Browse files
committed
[Remarks] Add unit tests for YAML serialization
Add tests for both the string table and non string table case. llvm-svn: 366832
1 parent 3794413 commit 78c92d2

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

llvm/lib/Remarks/RemarkStringTable.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ std::pair<unsigned, StringRef> StringTable::add(StringRef Str) {
2929
}
3030

3131
void StringTable::serialize(raw_ostream &OS) const {
32-
// Emit the number of strings.
33-
uint64_t StrTabSize = SerializedSize;
34-
support::endian::write(OS, StrTabSize, support::little);
3532
// Emit the sequence of strings.
3633
for (StringRef Str : serialize()) {
3734
OS << Str;

llvm/unittests/Remarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ set(LLVM_LINK_COMPONENTS
66
add_llvm_unittest(RemarksTests
77
RemarksStrTabParsingTest.cpp
88
YAMLRemarksParsingTest.cpp
9+
YAMLRemarksSerializerTest.cpp
910
)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===- unittest/Support/YAMLRemarksSerializerTest.cpp --------------------===//
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+
#include "llvm/Remarks/Remark.h"
10+
#include "llvm/Remarks/RemarkSerializer.h"
11+
#include "gtest/gtest.h"
12+
13+
using namespace llvm;
14+
15+
static void check(const remarks::Remark &R, StringRef Expected,
16+
Optional<StringRef> ExpectedStrTab = None) {
17+
std::string Buf;
18+
raw_string_ostream OS(Buf);
19+
remarks::UseStringTable UseStrTab = ExpectedStrTab.hasValue()
20+
? remarks::UseStringTable::Yes
21+
: remarks::UseStringTable::No;
22+
remarks::YAMLSerializer S(OS, UseStrTab);
23+
S.emit(R);
24+
EXPECT_EQ(OS.str(), Expected);
25+
if (ExpectedStrTab) {
26+
Buf.clear();
27+
EXPECT_TRUE(S.StrTab);
28+
S.StrTab->serialize(OS);
29+
EXPECT_EQ(OS.str(), *ExpectedStrTab);
30+
}
31+
}
32+
33+
TEST(YAMLRemarks, SerializerRemark) {
34+
remarks::Remark R;
35+
R.RemarkType = remarks::Type::Missed;
36+
R.PassName = "pass";
37+
R.RemarkName = "name";
38+
R.FunctionName = "func";
39+
R.Loc = remarks::RemarkLocation{"path", 3, 4};
40+
R.Hotness = 5;
41+
R.Args.emplace_back();
42+
R.Args.back().Key = "key";
43+
R.Args.back().Val = "value";
44+
R.Args.emplace_back();
45+
R.Args.back().Key = "keydebug";
46+
R.Args.back().Val = "valuedebug";
47+
R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
48+
check(R, "--- !Missed\n"
49+
"Pass: pass\n"
50+
"Name: name\n"
51+
"DebugLoc: { File: path, Line: 3, Column: 4 }\n"
52+
"Function: func\n"
53+
"Hotness: 5\n"
54+
"Args:\n"
55+
" - key: value\n"
56+
" - keydebug: valuedebug\n"
57+
" DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
58+
"...\n");
59+
}
60+
61+
TEST(YAMLRemarks, SerializerRemarkStrTab) {
62+
remarks::Remark R;
63+
R.RemarkType = remarks::Type::Missed;
64+
R.PassName = "pass";
65+
R.RemarkName = "name";
66+
R.FunctionName = "func";
67+
R.Loc = remarks::RemarkLocation{"path", 3, 4};
68+
R.Hotness = 5;
69+
R.Args.emplace_back();
70+
R.Args.back().Key = "key";
71+
R.Args.back().Val = "value";
72+
R.Args.emplace_back();
73+
R.Args.back().Key = "keydebug";
74+
R.Args.back().Val = "valuedebug";
75+
R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
76+
check(R,
77+
"--- !Missed\n"
78+
"Pass: 0\n"
79+
"Name: 1\n"
80+
"DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
81+
"Function: 2\n"
82+
"Hotness: 5\n"
83+
"Args:\n"
84+
" - key: 4\n"
85+
" - keydebug: 5\n"
86+
" DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
87+
"...\n",
88+
StringRef("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45));
89+
}

0 commit comments

Comments
 (0)