[Remarks] Introduce a new format: yaml-strtab
This exposes better support to use a string table with a format through
an actual new remark::Format, called yaml-strtab.
This can now be used with -fsave-optimization-record=yaml-strtab.
llvm-svn: 366849
diff --git a/llvm/lib/Remarks/YAMLRemarkParser.cpp b/llvm/lib/Remarks/YAMLRemarkParser.cpp
index ed78b7b..3cbb4932 100644
--- a/llvm/lib/Remarks/YAMLRemarkParser.cpp
+++ b/llvm/lib/Remarks/YAMLRemarkParser.cpp
@@ -54,6 +54,9 @@
return SM;
}
+YAMLRemarkParser::YAMLRemarkParser(StringRef Buf)
+ : YAMLRemarkParser(Buf, None) {}
+
YAMLRemarkParser::YAMLRemarkParser(StringRef Buf,
Optional<const ParsedStringTable *> StrTab)
: Parser{Format::YAML}, StrTab(StrTab), LastErrorMessage(),
@@ -179,22 +182,7 @@
auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
if (!Value)
return error("expected a value of scalar type.", Node);
- StringRef Result;
- if (!StrTab) {
- Result = Value->getRawValue();
- } else {
- // If we have a string table, parse it as an unsigned.
- unsigned StrID = 0;
- if (Expected<unsigned> MaybeStrID = parseUnsigned(Node))
- StrID = *MaybeStrID;
- else
- return MaybeStrID.takeError();
-
- if (Expected<StringRef> Str = (**StrTab)[StrID])
- Result = *Str;
- else
- return Str.takeError();
- }
+ StringRef Result = Value->getRawValue();
if (Result.front() == '\'')
Result = Result.drop_front();
@@ -325,3 +313,29 @@
return std::move(*MaybeResult);
}
+
+Expected<StringRef> YAMLStrTabRemarkParser::parseStr(yaml::KeyValueNode &Node) {
+ auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
+ if (!Value)
+ return error("expected a value of scalar type.", Node);
+ StringRef Result;
+ // If we have a string table, parse it as an unsigned.
+ unsigned StrID = 0;
+ if (Expected<unsigned> MaybeStrID = parseUnsigned(Node))
+ StrID = *MaybeStrID;
+ else
+ return MaybeStrID.takeError();
+
+ if (Expected<StringRef> Str = (**StrTab)[StrID])
+ Result = *Str;
+ else
+ return Str.takeError();
+
+ if (Result.front() == '\'')
+ Result = Result.drop_front();
+
+ if (Result.back() == '\'')
+ Result = Result.drop_back();
+
+ return Result;
+}