Recommit "rL366894: [yaml2obj] - Allow custom fields for the SHT_UNDEF sections."
[llvm-complete.git] / unittests / Remarks / YAMLRemarksSerializerTest.cpp
blob2d080483d485b1a3e309cd91fce5bf1e3e44d952
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Remarks/Remark.h"
10 #include "llvm/Remarks/YAMLRemarkSerializer.h"
11 #include "llvm/Support/Error.h"
12 #include "gtest/gtest.h"
14 using namespace llvm;
16 static void check(const remarks::Remark &R, StringRef ExpectedR,
17 StringRef ExpectedMeta, bool UseStrTab = false,
18 Optional<remarks::StringTable> StrTab = None) {
19 std::string Buf;
20 raw_string_ostream OS(Buf);
21 Expected<std::unique_ptr<remarks::RemarkSerializer>> MaybeS = [&] {
22 if (UseStrTab) {
23 if (StrTab)
24 return createRemarkSerializer(remarks::Format::YAMLStrTab, OS,
25 std::move(*StrTab));
26 else
27 return createRemarkSerializer(remarks::Format::YAMLStrTab, OS);
28 } else
29 return createRemarkSerializer(remarks::Format::YAML, OS);
30 }();
31 EXPECT_FALSE(errorToBool(MaybeS.takeError()));
32 std::unique_ptr<remarks::RemarkSerializer> S = std::move(*MaybeS);
34 S->emit(R);
35 EXPECT_EQ(OS.str(), ExpectedR);
37 Buf.clear();
38 std::unique_ptr<remarks::MetaSerializer> MS =
39 S->metaSerializer(OS, StringRef("/externalfile"));
40 MS->emit();
41 EXPECT_EQ(OS.str(), ExpectedMeta);
44 TEST(YAMLRemarks, SerializerRemark) {
45 remarks::Remark R;
46 R.RemarkType = remarks::Type::Missed;
47 R.PassName = "pass";
48 R.RemarkName = "name";
49 R.FunctionName = "func";
50 R.Loc = remarks::RemarkLocation{"path", 3, 4};
51 R.Hotness = 5;
52 R.Args.emplace_back();
53 R.Args.back().Key = "key";
54 R.Args.back().Val = "value";
55 R.Args.emplace_back();
56 R.Args.back().Key = "keydebug";
57 R.Args.back().Val = "valuedebug";
58 R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
59 check(R,
60 "--- !Missed\n"
61 "Pass: pass\n"
62 "Name: name\n"
63 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
64 "Function: func\n"
65 "Hotness: 5\n"
66 "Args:\n"
67 " - key: value\n"
68 " - keydebug: valuedebug\n"
69 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
70 "...\n",
71 StringRef("REMARKS\0"
72 "\0\0\0\0\0\0\0\0"
73 "\0\0\0\0\0\0\0\0"
74 "/externalfile\0",
75 38));
78 TEST(YAMLRemarks, SerializerRemarkStrTab) {
79 remarks::Remark R;
80 R.RemarkType = remarks::Type::Missed;
81 R.PassName = "pass";
82 R.RemarkName = "name";
83 R.FunctionName = "func";
84 R.Loc = remarks::RemarkLocation{"path", 3, 4};
85 R.Hotness = 5;
86 R.Args.emplace_back();
87 R.Args.back().Key = "key";
88 R.Args.back().Val = "value";
89 R.Args.emplace_back();
90 R.Args.back().Key = "keydebug";
91 R.Args.back().Val = "valuedebug";
92 R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
93 check(R,
94 "--- !Missed\n"
95 "Pass: 0\n"
96 "Name: 1\n"
97 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
98 "Function: 2\n"
99 "Hotness: 5\n"
100 "Args:\n"
101 " - key: 4\n"
102 " - keydebug: 5\n"
103 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
104 "...\n",
105 StringRef("REMARKS\0"
106 "\0\0\0\0\0\0\0\0"
107 "\x2d\0\0\0\0\0\0\0"
108 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
109 "/externalfile\0",
110 83),
111 /*UseStrTab=*/true);
114 TEST(YAMLRemarks, SerializerRemarkParsedStrTab) {
115 StringRef StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
116 remarks::Remark R;
117 R.RemarkType = remarks::Type::Missed;
118 R.PassName = "pass";
119 R.RemarkName = "name";
120 R.FunctionName = "func";
121 R.Loc = remarks::RemarkLocation{"path", 3, 4};
122 R.Hotness = 5;
123 R.Args.emplace_back();
124 R.Args.back().Key = "key";
125 R.Args.back().Val = "value";
126 R.Args.emplace_back();
127 R.Args.back().Key = "keydebug";
128 R.Args.back().Val = "valuedebug";
129 R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
130 check(R,
131 "--- !Missed\n"
132 "Pass: 0\n"
133 "Name: 1\n"
134 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
135 "Function: 2\n"
136 "Hotness: 5\n"
137 "Args:\n"
138 " - key: 4\n"
139 " - keydebug: 5\n"
140 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
141 "...\n",
142 StringRef("REMARKS\0"
143 "\0\0\0\0\0\0\0\0"
144 "\x2d\0\0\0\0\0\0\0"
145 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
146 "/externalfile\0",
147 83),
148 /*UseStrTab=*/true,
149 remarks::StringTable(remarks::ParsedStringTable(StrTab)));