blob: e1340b4f2e05ed6ee743a0d8a544fd71fa444cfe [file] [log] [blame]
Francis Visoiu Mistrih78c92d22019-07-23 18:09:12 +00001//===- 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"
Francis Visoiu Mistrihcbbdc412019-07-23 19:28:03 +000010#include "llvm/Remarks/YAMLRemarkSerializer.h"
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +000011#include "llvm/Support/Error.h"
Francis Visoiu Mistrih78c92d22019-07-23 18:09:12 +000012#include "gtest/gtest.h"
13
14using namespace llvm;
15
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +000016static void check(const remarks::Remark &R, StringRef ExpectedR,
17 Optional<StringRef> ExpectedStrTab = None,
18 Optional<remarks::StringTable> StrTab = None) {
Francis Visoiu Mistrih78c92d22019-07-23 18:09:12 +000019 std::string Buf;
20 raw_string_ostream OS(Buf);
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000021 bool UseStrTab = ExpectedStrTab.hasValue();
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +000022 Expected<std::unique_ptr<remarks::Serializer>> MaybeS = [&] {
23 if (UseStrTab) {
24 if (StrTab)
25 return createRemarkSerializer(remarks::Format::YAMLStrTab, OS,
26 std::move(*StrTab));
27 else
28 return createRemarkSerializer(remarks::Format::YAMLStrTab, OS);
29 } else
30 return createRemarkSerializer(remarks::Format::YAML, OS);
31 }();
32 EXPECT_FALSE(errorToBool(MaybeS.takeError()));
33 std::unique_ptr<remarks::Serializer> S = std::move(*MaybeS);
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000034
35 S->emit(R);
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +000036 EXPECT_EQ(OS.str(), ExpectedR);
Francis Visoiu Mistrih78c92d22019-07-23 18:09:12 +000037 if (ExpectedStrTab) {
38 Buf.clear();
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000039 EXPECT_TRUE(S->StrTab);
40 S->StrTab->serialize(OS);
Francis Visoiu Mistrih78c92d22019-07-23 18:09:12 +000041 EXPECT_EQ(OS.str(), *ExpectedStrTab);
42 }
43}
44
45TEST(YAMLRemarks, SerializerRemark) {
46 remarks::Remark R;
47 R.RemarkType = remarks::Type::Missed;
48 R.PassName = "pass";
49 R.RemarkName = "name";
50 R.FunctionName = "func";
51 R.Loc = remarks::RemarkLocation{"path", 3, 4};
52 R.Hotness = 5;
53 R.Args.emplace_back();
54 R.Args.back().Key = "key";
55 R.Args.back().Val = "value";
56 R.Args.emplace_back();
57 R.Args.back().Key = "keydebug";
58 R.Args.back().Val = "valuedebug";
59 R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
60 check(R, "--- !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}
72
73TEST(YAMLRemarks, SerializerRemarkStrTab) {
74 remarks::Remark R;
75 R.RemarkType = remarks::Type::Missed;
76 R.PassName = "pass";
77 R.RemarkName = "name";
78 R.FunctionName = "func";
79 R.Loc = remarks::RemarkLocation{"path", 3, 4};
80 R.Hotness = 5;
81 R.Args.emplace_back();
82 R.Args.back().Key = "key";
83 R.Args.back().Val = "value";
84 R.Args.emplace_back();
85 R.Args.back().Key = "keydebug";
86 R.Args.back().Val = "valuedebug";
87 R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
88 check(R,
89 "--- !Missed\n"
90 "Pass: 0\n"
91 "Name: 1\n"
92 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
93 "Function: 2\n"
94 "Hotness: 5\n"
95 "Args:\n"
96 " - key: 4\n"
97 " - keydebug: 5\n"
98 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
99 "...\n",
100 StringRef("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45));
101}
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +0000102
103TEST(YAMLRemarks, SerializerRemarkParsedStrTab) {
104 StringRef StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
105 remarks::Remark R;
106 R.RemarkType = remarks::Type::Missed;
107 R.PassName = "pass";
108 R.RemarkName = "name";
109 R.FunctionName = "func";
110 R.Loc = remarks::RemarkLocation{"path", 3, 4};
111 R.Hotness = 5;
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};
119 check(R,
120 "--- !Missed\n"
121 "Pass: 0\n"
122 "Name: 1\n"
123 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
124 "Function: 2\n"
125 "Hotness: 5\n"
126 "Args:\n"
127 " - key: 4\n"
128 " - keydebug: 5\n"
129 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
130 "...\n",
131 StrTab, remarks::StringTable(remarks::ParsedStringTable(StrTab)));
132}