| 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/RemarkParser.h" |
| 11 |
#include "llvm/Remarks/YAMLRemarkSerializer.h" |
| 12 |
#include "llvm/Support/Error.h" |
| 13 |
#include "gtest/gtest.h" |
| 14 |
|
| 15 |
// We need to supprt Windows paths as well. In order to have paths with the same |
| 16 |
// length, use a different path according to the platform. |
| 17 |
#ifdef _WIN32 |
| 18 |
#define EXTERNALFILETESTPATH "C:/externalfi" |
| 19 |
#else |
| 20 |
#define EXTERNALFILETESTPATH "/externalfile" |
| 21 |
#endif |
| 22 |
|
| 23 |
using namespace llvm; |
| 24 |
|
| 25 |
static void check(remarks::Format SerializerFormat, |
| 26 |
remarks::SerializerMode Mode, ArrayRef<remarks::Remark> Rs, |
| 27 |
StringRef ExpectedR, std::optional<StringRef> ExpectedMeta, |
| 28 |
std::optional<remarks::StringTable> StrTab = std::nullopt) { |
| 29 |
std::string Buf; |
| 30 |
raw_string_ostream OS(Buf); |
| 31 |
Expected<std::unique_ptr<remarks::RemarkSerializer>> MaybeS = [&] { |
| 32 |
if (StrTab) |
| 33 |
return createRemarkSerializer(SerializerFormat, Mode, OS, |
| 34 |
std::move(*StrTab)); |
| 35 |
else |
| 36 |
return createRemarkSerializer(SerializerFormat, Mode, OS); |
| 37 |
}(); |
| 38 |
EXPECT_FALSE(errorToBool(MaybeS.takeError())); |
| 39 |
std::unique_ptr<remarks::RemarkSerializer> S = std::move(*MaybeS); |
| 40 |
|
| 41 |
for (const remarks::Remark &R : Rs) |
| 42 |
S->emit(R); |
| 43 |
EXPECT_EQ(OS.str(), ExpectedR); |
| 44 |
|
| 45 |
if (ExpectedMeta) { |
| 46 |
Buf.clear(); |
| 47 |
std::unique_ptr<remarks::MetaSerializer> MS = |
| 48 |
S->metaSerializer(OS, StringRef(EXTERNALFILETESTPATH)); |
| 49 |
MS->emit(); |
| 50 |
EXPECT_EQ(OS.str(), *ExpectedMeta); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
static void check(remarks::Format SerializerFormat, const remarks::Remark &R, |
| 55 |
StringRef ExpectedR, StringRef ExpectedMeta, |
| 56 |
std::optional<remarks::StringTable> StrTab = std::nullopt) { |
| 57 |
return check(SerializerFormat, remarks::SerializerMode::Separate, |
| 58 |
ArrayRef(&R, &R + 1), ExpectedR, ExpectedMeta, |
| 59 |
std::move(StrTab)); |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
checkStandalone(remarks::Format SerializerFormat, const remarks::Remark &R, |
| 64 |
StringRef ExpectedR, |
| 65 |
std::optional<remarks::StringTable> StrTab = std::nullopt) { |
| 66 |
return check(SerializerFormat, remarks::SerializerMode::Standalone, |
| 67 |
ArrayRef(&R, &R + 1), ExpectedR, |
| 68 |
/*ExpectedMeta=*/std::nullopt, std::move(StrTab)); |
| 69 |
} |
| 70 |
|
| 71 |
TEST(YAMLRemarks, SerializerRemark) { |
| 72 |
remarks::Remark R; |
| 73 |
R.RemarkType = remarks::Type::Missed; |
| 74 |
R.PassName = "pass"; |
| 75 |
R.RemarkName = "name"; |
| 76 |
R.FunctionName = "func"; |
| 77 |
R.Loc = remarks::RemarkLocation{"path", 3, 4}; |
| 78 |
R.Hotness = 5; |
| 79 |
R.Args.emplace_back(); |
| 80 |
R.Args.back().Key = "key"; |
| 81 |
R.Args.back().Val = "value"; |
| 82 |
R.Args.emplace_back(); |
| 83 |
R.Args.back().Key = "keydebug"; |
| 84 |
R.Args.back().Val = "valuedebug"; |
| 85 |
R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7}; |
| 86 |
check(remarks::Format::YAML, R, |
| 87 |
"--- !Missed\n" |
| 88 |
"Pass: pass\n" |
| 89 |
"Name: name\n" |
| 90 |
"DebugLoc: { File: path, Line: 3, Column: 4 }\n" |
| 91 |
"Function: func\n" |
| 92 |
"Hotness: 5\n" |
| 93 |
"Args:\n" |
| 94 |
" - key: value\n" |
| 95 |
" - keydebug: valuedebug\n" |
| 96 |
" DebugLoc: { File: argpath, Line: 6, Column: 7 }\n" |
| 97 |
"...\n", |
| 98 |
StringRef("REMARKS\0" |
| 99 |
"\0\0\0\0\0\0\0\0" |
| 100 |
"\0\0\0\0\0\0\0\0" EXTERNALFILETESTPATH "\0", |
| 101 |
38)); |
| 102 |
} |
| 103 |
|
| 104 |
TEST(YAMLRemarks, SerializerRemarkStandalone) { |
| 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 |
checkStandalone( |
| 120 |
remarks::Format::YAML, R, |
| 121 |
StringRef("--- !Missed\n" |
| 122 |
"Pass: pass\n" |
| 123 |
"Name: name\n" |
| 124 |
"DebugLoc: { File: path, Line: 3, Column: 4 }\n" |
| 125 |
"Function: func\n" |
| 126 |
"Hotness: 5\n" |
| 127 |
"Args:\n" |
| 128 |
" - key: value\n" |
| 129 |
" - keydebug: valuedebug\n" |
| 130 |
" DebugLoc: { File: argpath, Line: 6, Column: 7 }\n" |
| 131 |
"...\n")); |
| 132 |
} |
| 133 |
|
| 134 |
TEST(YAMLRemarks, SerializerRemarkParsedStrTabStandaloneNoStrTab) { |
| 135 |
// Check that we don't use the string table even if it was provided. |
| 136 |
StringRef StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45); |
| 137 |
remarks::ParsedStringTable ParsedStrTab(StrTab); |
| 138 |
remarks::StringTable PreFilledStrTab(ParsedStrTab); |
| 139 |
remarks::Remark R; |
| 140 |
R.RemarkType = remarks::Type::Missed; |
| 141 |
R.PassName = "pass"; |
| 142 |
R.RemarkName = "name"; |
| 143 |
R.FunctionName = "func"; |
| 144 |
R.Loc = remarks::RemarkLocation{"path", 3, 4}; |
| 145 |
R.Hotness = 5; |
| 146 |
R.Args.emplace_back(); |
| 147 |
R.Args.back().Key = "key"; |
| 148 |
R.Args.back().Val = "value"; |
| 149 |
R.Args.emplace_back(); |
| 150 |
R.Args.back().Key = "keydebug"; |
| 151 |
R.Args.back().Val = "valuedebug"; |
| 152 |
R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7}; |
| 153 |
checkStandalone( |
| 154 |
remarks::Format::YAML, R, |
| 155 |
StringRef("--- !Missed\n" |
| 156 |
"Pass: pass\n" |
| 157 |
"Name: name\n" |
| 158 |
"DebugLoc: { File: path, Line: 3, Column: 4 }\n" |
| 159 |
"Function: func\n" |
| 160 |
"Hotness: 5\n" |
| 161 |
"Args:\n" |
| 162 |
" - key: value\n" |
| 163 |
" - keydebug: valuedebug\n" |
| 164 |
" DebugLoc: { File: argpath, Line: 6, Column: 7 }\n" |
| 165 |
"...\n"), |
| 166 |
std::move(PreFilledStrTab)); |
| 167 |
} |