llvm.org GIT mirror llvm / 95095ee
[Remarks][NFC] Move the string table parsing out of the parser constructor Make the parser take an already-parsed string table. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365101 91177308-0d34-0410-b5e6-96231b3b80d8 Francis Visoiu Mistrih a month ago
5 changed file(s) with 19 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
2222 namespace remarks {
2323
2424 struct ParserImpl;
25 struct ParsedStringTable;
2526
2627 /// Parser used to parse a raw buffer to remarks::Remark objects.
2728 struct Parser {
3233 /// This constructor should be only used for parsing YAML remarks.
3334 Parser(StringRef Buffer);
3435
35 /// Create a parser parsing \p Buffer to Remark objects, using \p StrTabBuf as
36 /// Create a parser parsing \p Buffer to Remark objects, using \p StrTab as a
3637 /// string table.
3738 /// This constructor should be only used for parsing YAML remarks.
38 Parser(StringRef Buffer, StringRef StrTabBuf);
39 Parser(StringRef Buffer, const ParsedStringTable &StrTab);
3940
4041 // Needed because ParserImpl is an incomplete type.
4142 ~Parser();
5354 /// Collection of offsets in the buffer for each string entry.
5455 SmallVector Offsets;
5556
56 Expected operator[](size_t Index);
57 Expected operator[](size_t Index) const;
5758 ParsedStringTable(StringRef Buffer);
5859 };
5960
2121
2222 Parser::Parser(StringRef Buf) : Impl(llvm::make_unique(Buf)) {}
2323
24 Parser::Parser(StringRef Buf, StringRef StrTabBuf)
25 : Impl(llvm::make_unique(Buf, StrTabBuf)) {}
24 Parser::Parser(StringRef Buf, const ParsedStringTable &StrTab)
25 : Impl(llvm::make_unique(Buf, &StrTab)) {}
2626
2727 Parser::~Parser() = default;
2828
6868 }
6969 }
7070
71 Expected ParsedStringTable::operator[](size_t Index) {
71 Expected ParsedStringTable::operator[](size_t Index) const {
7272 if (Index >= Offsets.size())
7373 return createStringError(
7474 std::make_error_code(std::errc::invalid_argument),
4141 unsigned StrID = 0;
4242 if (Error E = parseUnsigned(StrID, Node))
4343 return E;
44 if (Expected Str = (*StrTab)[StrID])
44 if (Expected Str = (**StrTab)[StrID])
4545 Tmp = *Str;
4646 else
4747 return Str.takeError();
3939 /// Temporary parsing buffer for the arguments.
4040 SmallVector TmpArgs;
4141 /// The string table used for parsing strings.
42 Optional<ParsedStringTable> StrTab;
42 Optional<const ParsedStringTable *> StrTab;
4343 /// The state used by the parser to parse a remark entry. Invalidated with
4444 /// every call to `parseYAMLElement`.
4545 struct ParseState {
5858 /// not be containing any value.
5959 Optional State;
6060
61 YAMLRemarkParser(StringRef Buf, Optional StrTabBuf = None)
61 YAMLRemarkParser(StringRef Buf,
62 Optional StrTab = None)
6263 : SM(), Stream(Buf, SM), ErrorString(), ErrorStream(ErrorString),
63 TmpArgs(), StrTab() {
64 TmpArgs(), StrTab(StrTab) {
6465 SM.setDiagHandler(YAMLRemarkParser::HandleDiagnostic, this);
65
66 if (StrTabBuf)
67 StrTab.emplace(*StrTabBuf);
6866 }
6967
7068 /// Parse a YAML element.
126124 /// Set to `true` if we had any errors during parsing.
127125 bool HasErrors = false;
128126
129 YAMLParserImpl(StringRef Buf, Optional StrTabBuf = None)
130 : ParserImpl{ParserImpl::Kind::YAML}, YAMLParser(Buf, StrTabBuf),
127 YAMLParserImpl(StringRef Buf,
128 Optional StrTab = None)
129 : ParserImpl{ParserImpl::Kind::YAML}, YAMLParser(Buf, StrTab),
131130 YAMLIt(YAMLParser.Stream.begin()), HasErrors(false) {}
132131
133132 static bool classof(const ParserImpl *PI) {
514514 "unavailable",
515515 115);
516516
517 remarks::Parser Parser(Buf, StrTabBuf);
517 remarks::ParsedStringTable StrTab(StrTabBuf);
518 remarks::Parser Parser(Buf, StrTab);
518519 Expected RemarkOrErr = Parser.getNext();
519520 EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
520521 EXPECT_TRUE(*RemarkOrErr != nullptr);
581582
582583 StringRef StrTabBuf = StringRef("inline");
583584
584 remarks::Parser Parser(Buf, StrTabBuf);
585 remarks::ParsedStringTable StrTab(StrTabBuf);
586 remarks::Parser Parser(Buf, StrTab);
585587 Expected Remark = Parser.getNext();
586588 EXPECT_FALSE(Remark); // Expect an error here.
587589