[Remarks][NFC] Move the string table parsing out of the parser constructor

Make the parser take an already-parsed string table.

llvm-svn: 365101
This commit is contained in:
Francis Visoiu Mistrih 2019-07-04 00:30:58 +00:00
parent f8c876ff3b
commit 386dd80250
5 changed files with 19 additions and 17 deletions

View File

@ -23,6 +23,7 @@ namespace llvm {
namespace remarks {
struct ParserImpl;
struct ParsedStringTable;
/// Parser used to parse a raw buffer to remarks::Remark objects.
struct Parser {
@ -33,10 +34,10 @@ struct Parser {
/// This constructor should be only used for parsing YAML remarks.
Parser(StringRef Buffer);
/// Create a parser parsing \p Buffer to Remark objects, using \p StrTabBuf as
/// Create a parser parsing \p Buffer to Remark objects, using \p StrTab as a
/// string table.
/// This constructor should be only used for parsing YAML remarks.
Parser(StringRef Buffer, StringRef StrTabBuf);
Parser(StringRef Buffer, const ParsedStringTable &StrTab);
// Needed because ParserImpl is an incomplete type.
~Parser();
@ -54,7 +55,7 @@ struct ParsedStringTable {
/// Collection of offsets in the buffer for each string entry.
SmallVector<size_t, 8> Offsets;
Expected<StringRef> operator[](size_t Index);
Expected<StringRef> operator[](size_t Index) const;
ParsedStringTable(StringRef Buffer);
};

View File

@ -22,8 +22,8 @@ using namespace llvm::remarks;
Parser::Parser(StringRef Buf) : Impl(llvm::make_unique<YAMLParserImpl>(Buf)) {}
Parser::Parser(StringRef Buf, StringRef StrTabBuf)
: Impl(llvm::make_unique<YAMLParserImpl>(Buf, StrTabBuf)) {}
Parser::Parser(StringRef Buf, const ParsedStringTable &StrTab)
: Impl(llvm::make_unique<YAMLParserImpl>(Buf, &StrTab)) {}
Parser::~Parser() = default;
@ -69,7 +69,7 @@ ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) {
}
}
Expected<StringRef> ParsedStringTable::operator[](size_t Index) {
Expected<StringRef> ParsedStringTable::operator[](size_t Index) const {
if (Index >= Offsets.size())
return createStringError(
std::make_error_code(std::errc::invalid_argument),

View File

@ -42,7 +42,7 @@ Error YAMLRemarkParser::parseStr(T &Result, yaml::KeyValueNode &Node) {
unsigned StrID = 0;
if (Error E = parseUnsigned(StrID, Node))
return E;
if (Expected<StringRef> Str = (*StrTab)[StrID])
if (Expected<StringRef> Str = (**StrTab)[StrID])
Tmp = *Str;
else
return Str.takeError();

View File

@ -40,7 +40,7 @@ struct YAMLRemarkParser {
/// Temporary parsing buffer for the arguments.
SmallVector<Argument, 8> TmpArgs;
/// The string table used for parsing strings.
Optional<ParsedStringTable> StrTab;
Optional<const ParsedStringTable *> StrTab;
/// The state used by the parser to parse a remark entry. Invalidated with
/// every call to `parseYAMLElement`.
struct ParseState {
@ -59,13 +59,11 @@ struct YAMLRemarkParser {
/// not be containing any value.
Optional<ParseState> State;
YAMLRemarkParser(StringRef Buf, Optional<StringRef> StrTabBuf = None)
YAMLRemarkParser(StringRef Buf,
Optional<const ParsedStringTable *> StrTab = None)
: SM(), Stream(Buf, SM), ErrorString(), ErrorStream(ErrorString),
TmpArgs(), StrTab() {
TmpArgs(), StrTab(StrTab) {
SM.setDiagHandler(YAMLRemarkParser::HandleDiagnostic, this);
if (StrTabBuf)
StrTab.emplace(*StrTabBuf);
}
/// Parse a YAML element.
@ -127,8 +125,9 @@ struct YAMLParserImpl : public ParserImpl {
/// Set to `true` if we had any errors during parsing.
bool HasErrors = false;
YAMLParserImpl(StringRef Buf, Optional<StringRef> StrTabBuf = None)
: ParserImpl{ParserImpl::Kind::YAML}, YAMLParser(Buf, StrTabBuf),
YAMLParserImpl(StringRef Buf,
Optional<const ParsedStringTable *> StrTab = None)
: ParserImpl{ParserImpl::Kind::YAML}, YAMLParser(Buf, StrTab),
YAMLIt(YAMLParser.Stream.begin()), HasErrors(false) {}
static bool classof(const ParserImpl *PI) {

View File

@ -515,7 +515,8 @@ TEST(YAMLRemarks, ContentsStrTab) {
"unavailable",
115);
remarks::Parser Parser(Buf, StrTabBuf);
remarks::ParsedStringTable StrTab(StrTabBuf);
remarks::Parser Parser(Buf, StrTab);
Expected<const remarks::Remark *> RemarkOrErr = Parser.getNext();
EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
EXPECT_TRUE(*RemarkOrErr != nullptr);
@ -582,7 +583,8 @@ TEST(YAMLRemarks, ParsingBadStringTableIndex) {
StringRef StrTabBuf = StringRef("inline");
remarks::Parser Parser(Buf, StrTabBuf);
remarks::ParsedStringTable StrTab(StrTabBuf);
remarks::Parser Parser(Buf, StrTab);
Expected<const remarks::Remark *> Remark = Parser.getNext();
EXPECT_FALSE(Remark); // Expect an error here.