/// Add a string to the table. It returns an unique ID of the string.
std::pair<unsigned, StringRef> add(StringRef Str);
+ /// Modify \p R to use strings from this string table. If the string table
+ /// does not contain the strings, it adds them.
+ void internalize(Remark &R);
/// Serialize the string table to a stream. It is serialized as a little
/// endian uint64 (the size of the table in bytes) followed by a sequence of
/// NULL-terminated strings, where the N-th string is the string with the ID N
//===----------------------------------------------------------------------===//
#include "llvm/Remarks/RemarkStringTable.h"
+#include "llvm/Remarks/Remark.h"
#include "llvm/Remarks/RemarkParser.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/Error.h"
return {KV.first->second, KV.first->first()};
}
+void StringTable::internalize(Remark &R) {
+ auto Impl = [&](StringRef &S) { S = add(S).second; };
+ Impl(R.PassName);
+ Impl(R.RemarkName);
+ Impl(R.FunctionName);
+ if (R.Loc)
+ Impl(R.Loc->SourceFilePath);
+ for (Argument &Arg : R.Args) {
+ // We need to mutate elements from an ArrayRef here.
+ Impl(Arg.Key);
+ Impl(Arg.Val);
+ if (Arg.Loc)
+ Impl(Arg.Loc->SourceFilePath);
+ }
+}
+
void StringTable::serialize(raw_ostream &OS) const {
// Emit the sequence of strings.
for (StringRef Str : serialize()) {
//===----------------------------------------------------------------------===//
#include "llvm/Remarks/Remark.h"
+#include "llvm/Remarks/RemarkStringTable.h"
#include "gtest/gtest.h"
using namespace llvm;
EXPECT_EQ(R.getArgsAsMsg(), "can not do this because of that.");
}
+
+TEST(RemarksAPI, StringTableInternalize) {
+ remarks::StringTable StrTab;
+
+ // Empty table.
+ EXPECT_EQ(StrTab.SerializedSize, 0UL);
+
+ remarks::Remark R;
+ R.RemarkType = remarks::Type::Missed;
+ R.PassName = "pass";
+ R.RemarkName = "name";
+ R.FunctionName = "func";
+ R.Loc = remarks::RemarkLocation{"path", 3, 4};
+ R.Args.emplace_back();
+ R.Args.back().Key = "keydebug";
+ R.Args.back().Val = "valuedebug";
+ R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
+
+ // Check that internalize starts using the strings from the string table.
+ remarks::Remark R2 = R.clone();
+ StrTab.internalize(R2);
+
+ // Check that the pointers in the remarks are different.
+ EXPECT_NE(R.PassName.data(), R2.PassName.data());
+ EXPECT_NE(R.RemarkName.data(), R2.RemarkName.data());
+ EXPECT_NE(R.FunctionName.data(), R2.FunctionName.data());
+ EXPECT_NE(R.Loc->SourceFilePath.data(), R2.Loc->SourceFilePath.data());
+ EXPECT_NE(R.Args.back().Key.data(), R2.Args.back().Key.data());
+ EXPECT_NE(R.Args.back().Val.data(), R2.Args.back().Val.data());
+ EXPECT_NE(R.Args.back().Loc->SourceFilePath.data(),
+ R2.Args.back().Loc->SourceFilePath.data());
+
+ // Check that the internalized remark is using the pointers from the string table.
+ EXPECT_EQ(StrTab.add(R.PassName).second.data(), R2.PassName.data());
+ EXPECT_EQ(StrTab.add(R.RemarkName).second.data(), R2.RemarkName.data());
+ EXPECT_EQ(StrTab.add(R.FunctionName).second.data(), R2.FunctionName.data());
+ EXPECT_EQ(StrTab.add(R.Loc->SourceFilePath).second.data(),
+ R2.Loc->SourceFilePath.data());
+ EXPECT_EQ(StrTab.add(R.Args.back().Key).second.data(),
+ R2.Args.back().Key.data());
+ EXPECT_EQ(StrTab.add(R.Args.back().Val).second.data(),
+ R2.Args.back().Val.data());
+ EXPECT_EQ(StrTab.add(R.Args.back().Loc->SourceFilePath).second.data(),
+ R2.Args.back().Loc->SourceFilePath.data());
+}