#include "llvm-c/Remarks.h"
#include "llvm/Remarks/Remark.h"
#include "llvm/Remarks/RemarkParser.h"
#include "llvm/Remarks/RemarkSerializer.h"
#include "gtest/gtest.h"
using namespace llvm;
template <size_t N> void parseGood(const char (&Buf)[N]) {
Expected<std::unique_ptr<remarks::RemarkParser>> MaybeParser =
remarks::createRemarkParser(remarks::Format::YAML, {Buf, N - 1});
EXPECT_FALSE(errorToBool(MaybeParser.takeError()));
EXPECT_TRUE(*MaybeParser != nullptr);
std::unique_ptr<remarks::Remark> FromYAMLRemark = nullptr;
remarks::RemarkParser &Parser = **MaybeParser;
Expected<std::unique_ptr<remarks::Remark>> Remark = Parser.next();
EXPECT_FALSE(errorToBool(Remark.takeError())); EXPECT_TRUE(*Remark != nullptr); FromYAMLRemark = std::move(*Remark);
Remark = Parser.next();
Error E = Remark.takeError();
EXPECT_TRUE(E.isA<remarks::EndOfFileError>());
EXPECT_TRUE(errorToBool(std::move(E)));
remarks::StringTable BSStrTab;
BSStrTab.internalize(*FromYAMLRemark);
std::string BSBuf;
raw_string_ostream BSStream(BSBuf);
Expected<std::unique_ptr<remarks::RemarkSerializer>> BSSerializer =
remarks::createRemarkSerializer(remarks::Format::Bitstream,
remarks::SerializerMode::Standalone,
BSStream, std::move(BSStrTab));
EXPECT_FALSE(errorToBool(BSSerializer.takeError()));
(*BSSerializer)->emit(*FromYAMLRemark);
Expected<std::unique_ptr<remarks::RemarkParser>> MaybeBSParser =
remarks::createRemarkParser(remarks::Format::Bitstream, BSStream.str());
EXPECT_FALSE(errorToBool(MaybeBSParser.takeError()));
EXPECT_TRUE(*MaybeBSParser != nullptr);
std::unique_ptr<remarks::Remark> FromBSRemark = nullptr;
remarks::RemarkParser &BSParser = **MaybeBSParser;
Expected<std::unique_ptr<remarks::Remark>> BSRemark = BSParser.next();
EXPECT_FALSE(errorToBool(BSRemark.takeError())); EXPECT_TRUE(*BSRemark != nullptr); FromBSRemark = std::move(*BSRemark);
BSRemark = BSParser.next();
Error BSE = BSRemark.takeError();
EXPECT_TRUE(BSE.isA<remarks::EndOfFileError>());
EXPECT_TRUE(errorToBool(std::move(BSE)));
EXPECT_EQ(*FromYAMLRemark, *FromBSRemark);
}
TEST(BitstreamRemarks, ParsingGood) {
parseGood("\n"
"--- !Missed\n"
"Pass: inline\n"
"Name: NoDefinition\n"
"DebugLoc: { File: file.c, Line: 3, Column: 12 }\n"
"Function: foo\n"
"Args:\n"
" - Callee: bar\n"
" - String: ' will not be inlined into '\n"
" - Caller: foo\n"
" DebugLoc: { File: file.c, Line: 2, Column: 0 }\n"
" - String: ' because its definition is unavailable'\n"
"");
parseGood("\n"
"--- !Missed\n"
"Pass: inline\n"
"Name: NoDefinition\n"
"Function: foo\n"
"Args:\n"
" - Callee: bar\n"
" - String: ' will not be inlined into '\n"
" - Caller: foo\n"
" DebugLoc: { File: file.c, Line: 2, Column: 0 }\n"
" - String: ' because its definition is unavailable'\n"
"");
parseGood("\n"
"--- !Missed\n"
"Pass: inline\n"
"Name: NoDefinition\n"
"DebugLoc: { File: file.c, Line: 3, Column: 12 }\n"
"Function: foo\n"
"");
}
#define COMMON_REMARK "\nPass: inline\nName: NoDefinition\nFunction: foo\n\n"
TEST(BitstreamRemarks, ParsingTypes) {
parseGood("--- !Passed" COMMON_REMARK);
parseGood("--- !Missed" COMMON_REMARK);
parseGood("--- !Analysis" COMMON_REMARK);
parseGood("--- !AnalysisFPCommute" COMMON_REMARK);
parseGood("--- !AnalysisAliasing" COMMON_REMARK);
parseGood("--- !Failure" COMMON_REMARK);
}
#undef COMMON_REMARK
static inline StringRef checkStr(StringRef Str, unsigned ExpectedLen) {
const char *StrData = Str.data();
unsigned StrLen = Str.size();
EXPECT_EQ(StrLen, ExpectedLen);
return StringRef(StrData, StrLen);
}
TEST(BitstreamRemarks, Contents) {
StringRef Buf = "\n"
"--- !Missed\n"
"Pass: inline\n"
"Name: NoDefinition\n"
"DebugLoc: { File: file.c, Line: 3, Column: 12 }\n"
"Function: foo\n"
"Hotness: 4\n"
"Args:\n"
" - Callee: bar\n"
" - String: ' will not be inlined into '\n"
" - Caller: foo\n"
" DebugLoc: { File: file.c, Line: 2, Column: 0 }\n"
" - String: ' because its definition is unavailable'\n"
"\n";
Expected<std::unique_ptr<remarks::RemarkParser>> MaybeParser =
remarks::createRemarkParser(remarks::Format::YAML, Buf);
EXPECT_FALSE(errorToBool(MaybeParser.takeError()));
EXPECT_TRUE(*MaybeParser != nullptr);
remarks::RemarkParser &Parser = **MaybeParser;
Expected<std::unique_ptr<remarks::Remark>> MaybeRemark = Parser.next();
EXPECT_FALSE(
errorToBool(MaybeRemark.takeError())); EXPECT_TRUE(*MaybeRemark != nullptr);
const remarks::Remark &Remark = **MaybeRemark;
EXPECT_EQ(Remark.RemarkType, remarks::Type::Missed);
EXPECT_EQ(checkStr(Remark.PassName, 6), "inline");
EXPECT_EQ(checkStr(Remark.RemarkName, 12), "NoDefinition");
EXPECT_EQ(checkStr(Remark.FunctionName, 3), "foo");
EXPECT_TRUE(Remark.Loc);
const remarks::RemarkLocation &RL = *Remark.Loc;
EXPECT_EQ(checkStr(RL.SourceFilePath, 6), "file.c");
EXPECT_EQ(RL.SourceLine, 3U);
EXPECT_EQ(RL.SourceColumn, 12U);
EXPECT_TRUE(Remark.Hotness);
EXPECT_EQ(*Remark.Hotness, 4U);
EXPECT_EQ(Remark.Args.size(), 4U);
unsigned ArgID = 0;
for (const remarks::Argument &Arg : Remark.Args) {
switch (ArgID) {
case 0:
EXPECT_EQ(checkStr(Arg.Key, 6), "Callee");
EXPECT_EQ(checkStr(Arg.Val, 3), "bar");
EXPECT_FALSE(Arg.Loc);
break;
case 1:
EXPECT_EQ(checkStr(Arg.Key, 6), "String");
EXPECT_EQ(checkStr(Arg.Val, 26), " will not be inlined into ");
EXPECT_FALSE(Arg.Loc);
break;
case 2: {
EXPECT_EQ(checkStr(Arg.Key, 6), "Caller");
EXPECT_EQ(checkStr(Arg.Val, 3), "foo");
EXPECT_TRUE(Arg.Loc);
const remarks::RemarkLocation &RL = *Arg.Loc;
EXPECT_EQ(checkStr(RL.SourceFilePath, 6), "file.c");
EXPECT_EQ(RL.SourceLine, 2U);
EXPECT_EQ(RL.SourceColumn, 0U);
break;
}
case 3:
EXPECT_EQ(checkStr(Arg.Key, 6), "String");
EXPECT_EQ(checkStr(Arg.Val, 38),
" because its definition is unavailable");
EXPECT_FALSE(Arg.Loc);
break;
default:
break;
}
++ArgID;
}
MaybeRemark = Parser.next();
Error E = MaybeRemark.takeError();
EXPECT_TRUE(E.isA<remarks::EndOfFileError>());
EXPECT_TRUE(errorToBool(std::move(E))); }
static inline StringRef checkStr(LLVMRemarkStringRef Str,
unsigned ExpectedLen) {
const char *StrData = LLVMRemarkStringGetData(Str);
unsigned StrLen = LLVMRemarkStringGetLen(Str);
EXPECT_EQ(StrLen, ExpectedLen);
return StringRef(StrData, StrLen);
}
TEST(BitstreamRemarks, ContentsCAPI) {
remarks::StringTable BSStrTab;
remarks::Remark ToSerializeRemark;
ToSerializeRemark.RemarkType = remarks::Type::Missed;
ToSerializeRemark.PassName = "inline";
ToSerializeRemark.RemarkName = "NoDefinition";
ToSerializeRemark.FunctionName = "foo";
ToSerializeRemark.Loc = remarks::RemarkLocation{"file.c", 3, 12};
ToSerializeRemark.Hotness = 0;
ToSerializeRemark.Args.emplace_back();
ToSerializeRemark.Args.back().Key = "Callee";
ToSerializeRemark.Args.back().Val = "bar";
ToSerializeRemark.Args.emplace_back();
ToSerializeRemark.Args.back().Key = "String";
ToSerializeRemark.Args.back().Val = " will not be inlined into ";
ToSerializeRemark.Args.emplace_back();
ToSerializeRemark.Args.back().Key = "Caller";
ToSerializeRemark.Args.back().Val = "foo";
ToSerializeRemark.Args.back().Loc = remarks::RemarkLocation{"file.c", 2, 0};
ToSerializeRemark.Args.emplace_back();
ToSerializeRemark.Args.back().Key = "String";
ToSerializeRemark.Args.back().Val = " because its definition is unavailable";
BSStrTab.internalize(ToSerializeRemark);
std::string BSBuf;
raw_string_ostream BSStream(BSBuf);
Expected<std::unique_ptr<remarks::RemarkSerializer>> BSSerializer =
remarks::createRemarkSerializer(remarks::Format::Bitstream,
remarks::SerializerMode::Standalone,
BSStream, std::move(BSStrTab));
EXPECT_FALSE(errorToBool(BSSerializer.takeError()));
(*BSSerializer)->emit(ToSerializeRemark);
StringRef Buf = BSStream.str();
LLVMRemarkParserRef Parser =
LLVMRemarkParserCreateBitstream(Buf.data(), Buf.size());
LLVMRemarkEntryRef Remark = LLVMRemarkParserGetNext(Parser);
EXPECT_FALSE(Remark == nullptr);
EXPECT_EQ(LLVMRemarkEntryGetType(Remark), LLVMRemarkTypeMissed);
EXPECT_EQ(checkStr(LLVMRemarkEntryGetPassName(Remark), 6), "inline");
EXPECT_EQ(checkStr(LLVMRemarkEntryGetRemarkName(Remark), 12), "NoDefinition");
EXPECT_EQ(checkStr(LLVMRemarkEntryGetFunctionName(Remark), 3), "foo");
LLVMRemarkDebugLocRef DL = LLVMRemarkEntryGetDebugLoc(Remark);
EXPECT_EQ(checkStr(LLVMRemarkDebugLocGetSourceFilePath(DL), 6), "file.c");
EXPECT_EQ(LLVMRemarkDebugLocGetSourceLine(DL), 3U);
EXPECT_EQ(LLVMRemarkDebugLocGetSourceColumn(DL), 12U);
EXPECT_EQ(LLVMRemarkEntryGetHotness(Remark), 0U);
EXPECT_EQ(LLVMRemarkEntryGetNumArgs(Remark), 4U);
unsigned ArgID = 0;
LLVMRemarkArgRef Arg = LLVMRemarkEntryGetFirstArg(Remark);
do {
switch (ArgID) {
case 0:
EXPECT_EQ(checkStr(LLVMRemarkArgGetKey(Arg), 6), "Callee");
EXPECT_EQ(checkStr(LLVMRemarkArgGetValue(Arg), 3), "bar");
EXPECT_EQ(LLVMRemarkArgGetDebugLoc(Arg), nullptr);
break;
case 1:
EXPECT_EQ(checkStr(LLVMRemarkArgGetKey(Arg), 6), "String");
EXPECT_EQ(checkStr(LLVMRemarkArgGetValue(Arg), 26),
" will not be inlined into ");
EXPECT_EQ(LLVMRemarkArgGetDebugLoc(Arg), nullptr);
break;
case 2: {
EXPECT_EQ(checkStr(LLVMRemarkArgGetKey(Arg), 6), "Caller");
EXPECT_EQ(checkStr(LLVMRemarkArgGetValue(Arg), 3), "foo");
LLVMRemarkDebugLocRef DL = LLVMRemarkArgGetDebugLoc(Arg);
EXPECT_EQ(checkStr(LLVMRemarkDebugLocGetSourceFilePath(DL), 6), "file.c");
EXPECT_EQ(LLVMRemarkDebugLocGetSourceLine(DL), 2U);
EXPECT_EQ(LLVMRemarkDebugLocGetSourceColumn(DL), 0U);
break;
}
case 3:
EXPECT_EQ(checkStr(LLVMRemarkArgGetKey(Arg), 6), "String");
EXPECT_EQ(checkStr(LLVMRemarkArgGetValue(Arg), 38),
" because its definition is unavailable");
EXPECT_EQ(LLVMRemarkArgGetDebugLoc(Arg), nullptr);
break;
default:
break;
}
++ArgID;
} while ((Arg = LLVMRemarkEntryGetNextArg(Arg, Remark)));
LLVMRemarkEntryDispose(Remark);
EXPECT_EQ(LLVMRemarkParserGetNext(Parser), nullptr);
EXPECT_FALSE(LLVMRemarkParserHasError(Parser));
LLVMRemarkParserDispose(Parser);
}
static void parseBad(StringRef Input, const char *ErrorMsg) {
Expected<std::unique_ptr<remarks::RemarkParser>> MaybeBSParser =
remarks::createRemarkParser(remarks::Format::Bitstream, Input);
EXPECT_FALSE(errorToBool(MaybeBSParser.takeError()));
EXPECT_TRUE(*MaybeBSParser != nullptr);
remarks::RemarkParser &BSParser = **MaybeBSParser;
Expected<std::unique_ptr<remarks::Remark>> BSRemark = BSParser.next();
EXPECT_EQ(ErrorMsg, toString(BSRemark.takeError())); }
TEST(BitstreamRemarks, ParsingEmpty) {
parseBad(StringRef(), "End of file reached.");
}
TEST(BitstreamRemarks, ParsingBadMagic) {
parseBad("KRMR", "Unknown magic number: expecting RMRK, got KRMR.");
}