blob: 811c1b2a455a0a96c6b5d31b5f3125bfe97dac47 [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
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +000014// 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.
16#ifdef _WIN32
17#define EXTERNALFILETESTPATH "C:/externalfi"
18#else
19#define EXTERNALFILETESTPATH "/externalfile"
20#endif
21
Francis Visoiu Mistrih78c92d22019-07-23 18:09:12 +000022using namespace llvm;
23
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +000024static void check(const remarks::Remark &R, StringRef ExpectedR,
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +000025 StringRef ExpectedMeta, bool UseStrTab = false,
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +000026 Optional<remarks::StringTable> StrTab = None) {
Francis Visoiu Mistrih78c92d22019-07-23 18:09:12 +000027 std::string Buf;
28 raw_string_ostream OS(Buf);
Francis Visoiu Mistrihff4b5152019-07-24 19:47:57 +000029 Expected<std::unique_ptr<remarks::RemarkSerializer>> MaybeS = [&] {
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +000030 if (UseStrTab) {
31 if (StrTab)
32 return createRemarkSerializer(remarks::Format::YAMLStrTab, OS,
33 std::move(*StrTab));
34 else
35 return createRemarkSerializer(remarks::Format::YAMLStrTab, OS);
36 } else
37 return createRemarkSerializer(remarks::Format::YAML, OS);
38 }();
39 EXPECT_FALSE(errorToBool(MaybeS.takeError()));
Francis Visoiu Mistrihff4b5152019-07-24 19:47:57 +000040 std::unique_ptr<remarks::RemarkSerializer> S = std::move(*MaybeS);
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000041
42 S->emit(R);
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +000043 EXPECT_EQ(OS.str(), ExpectedR);
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +000044
45 Buf.clear();
46 std::unique_ptr<remarks::MetaSerializer> MS =
47 S->metaSerializer(OS, StringRef(EXTERNALFILETESTPATH));
48 MS->emit();
49 EXPECT_EQ(OS.str(), ExpectedMeta);
Francis Visoiu Mistrih78c92d22019-07-23 18:09:12 +000050}
51
52TEST(YAMLRemarks, SerializerRemark) {
53 remarks::Remark R;
54 R.RemarkType = remarks::Type::Missed;
55 R.PassName = "pass";
56 R.RemarkName = "name";
57 R.FunctionName = "func";
58 R.Loc = remarks::RemarkLocation{"path", 3, 4};
59 R.Hotness = 5;
60 R.Args.emplace_back();
61 R.Args.back().Key = "key";
62 R.Args.back().Val = "value";
63 R.Args.emplace_back();
64 R.Args.back().Key = "keydebug";
65 R.Args.back().Val = "valuedebug";
66 R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +000067 check(R,
68 "--- !Missed\n"
69 "Pass: pass\n"
70 "Name: name\n"
71 "DebugLoc: { File: path, Line: 3, Column: 4 }\n"
72 "Function: func\n"
73 "Hotness: 5\n"
74 "Args:\n"
75 " - key: value\n"
76 " - keydebug: valuedebug\n"
77 " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n"
78 "...\n",
79 StringRef("REMARKS\0"
80 "\0\0\0\0\0\0\0\0"
81 "\0\0\0\0\0\0\0\0"
82 EXTERNALFILETESTPATH"\0",
83 38));
Francis Visoiu Mistrih78c92d22019-07-23 18:09:12 +000084}
85
86TEST(YAMLRemarks, SerializerRemarkStrTab) {
87 remarks::Remark R;
88 R.RemarkType = remarks::Type::Missed;
89 R.PassName = "pass";
90 R.RemarkName = "name";
91 R.FunctionName = "func";
92 R.Loc = remarks::RemarkLocation{"path", 3, 4};
93 R.Hotness = 5;
94 R.Args.emplace_back();
95 R.Args.back().Key = "key";
96 R.Args.back().Val = "value";
97 R.Args.emplace_back();
98 R.Args.back().Key = "keydebug";
99 R.Args.back().Val = "valuedebug";
100 R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
101 check(R,
102 "--- !Missed\n"
103 "Pass: 0\n"
104 "Name: 1\n"
105 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
106 "Function: 2\n"
107 "Hotness: 5\n"
108 "Args:\n"
109 " - key: 4\n"
110 " - keydebug: 5\n"
111 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
112 "...\n",
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000113 StringRef("REMARKS\0"
114 "\0\0\0\0\0\0\0\0"
115 "\x2d\0\0\0\0\0\0\0"
116 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
117 EXTERNALFILETESTPATH"\0",
118 83),
119 /*UseStrTab=*/true);
Francis Visoiu Mistrih78c92d22019-07-23 18:09:12 +0000120}
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +0000121
122TEST(YAMLRemarks, SerializerRemarkParsedStrTab) {
123 StringRef StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45);
124 remarks::Remark R;
125 R.RemarkType = remarks::Type::Missed;
126 R.PassName = "pass";
127 R.RemarkName = "name";
128 R.FunctionName = "func";
129 R.Loc = remarks::RemarkLocation{"path", 3, 4};
130 R.Hotness = 5;
131 R.Args.emplace_back();
132 R.Args.back().Key = "key";
133 R.Args.back().Val = "value";
134 R.Args.emplace_back();
135 R.Args.back().Key = "keydebug";
136 R.Args.back().Val = "valuedebug";
137 R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
138 check(R,
139 "--- !Missed\n"
140 "Pass: 0\n"
141 "Name: 1\n"
142 "DebugLoc: { File: 3, Line: 3, Column: 4 }\n"
143 "Function: 2\n"
144 "Hotness: 5\n"
145 "Args:\n"
146 " - key: 4\n"
147 " - keydebug: 5\n"
148 " DebugLoc: { File: 6, Line: 6, Column: 7 }\n"
149 "...\n",
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000150 StringRef("REMARKS\0"
151 "\0\0\0\0\0\0\0\0"
152 "\x2d\0\0\0\0\0\0\0"
153 "pass\0name\0func\0path\0value\0valuedebug\0argpath\0"
154 EXTERNALFILETESTPATH"\0",
155 83),
156 /*UseStrTab=*/true,
157 remarks::StringTable(remarks::ParsedStringTable(StrTab)));
Francis Visoiu Mistrihc5cc9ef2019-07-24 16:36:35 +0000158}