Skip to content

Commit e6ba313

Browse files
committed
[Remarks][NFC] Move the string table parsing out of the parser constructor
Make the parser take an already-parsed string table. llvm-svn: 365101
1 parent 51d3c4d commit e6ba313

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

llvm/include/llvm/Remarks/RemarkParser.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace llvm {
2323
namespace remarks {
2424

2525
struct ParserImpl;
26+
struct ParsedStringTable;
2627

2728
/// Parser used to parse a raw buffer to remarks::Remark objects.
2829
struct Parser {
@@ -33,10 +34,10 @@ struct Parser {
3334
/// This constructor should be only used for parsing YAML remarks.
3435
Parser(StringRef Buffer);
3536

36-
/// Create a parser parsing \p Buffer to Remark objects, using \p StrTabBuf as
37+
/// Create a parser parsing \p Buffer to Remark objects, using \p StrTab as a
3738
/// string table.
3839
/// This constructor should be only used for parsing YAML remarks.
39-
Parser(StringRef Buffer, StringRef StrTabBuf);
40+
Parser(StringRef Buffer, const ParsedStringTable &StrTab);
4041

4142
// Needed because ParserImpl is an incomplete type.
4243
~Parser();
@@ -54,7 +55,7 @@ struct ParsedStringTable {
5455
/// Collection of offsets in the buffer for each string entry.
5556
SmallVector<size_t, 8> Offsets;
5657

57-
Expected<StringRef> operator[](size_t Index);
58+
Expected<StringRef> operator[](size_t Index) const;
5859
ParsedStringTable(StringRef Buffer);
5960
};
6061

llvm/lib/Remarks/RemarkParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ using namespace llvm::remarks;
2222

2323
Parser::Parser(StringRef Buf) : Impl(llvm::make_unique<YAMLParserImpl>(Buf)) {}
2424

25-
Parser::Parser(StringRef Buf, StringRef StrTabBuf)
26-
: Impl(llvm::make_unique<YAMLParserImpl>(Buf, StrTabBuf)) {}
25+
Parser::Parser(StringRef Buf, const ParsedStringTable &StrTab)
26+
: Impl(llvm::make_unique<YAMLParserImpl>(Buf, &StrTab)) {}
2727

2828
Parser::~Parser() = default;
2929

@@ -69,7 +69,7 @@ ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) {
6969
}
7070
}
7171

72-
Expected<StringRef> ParsedStringTable::operator[](size_t Index) {
72+
Expected<StringRef> ParsedStringTable::operator[](size_t Index) const {
7373
if (Index >= Offsets.size())
7474
return createStringError(
7575
std::make_error_code(std::errc::invalid_argument),

llvm/lib/Remarks/YAMLRemarkParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Error YAMLRemarkParser::parseStr(T &Result, yaml::KeyValueNode &Node) {
4242
unsigned StrID = 0;
4343
if (Error E = parseUnsigned(StrID, Node))
4444
return E;
45-
if (Expected<StringRef> Str = (*StrTab)[StrID])
45+
if (Expected<StringRef> Str = (**StrTab)[StrID])
4646
Tmp = *Str;
4747
else
4848
return Str.takeError();

llvm/lib/Remarks/YAMLRemarkParser.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct YAMLRemarkParser {
4040
/// Temporary parsing buffer for the arguments.
4141
SmallVector<Argument, 8> TmpArgs;
4242
/// The string table used for parsing strings.
43-
Optional<ParsedStringTable> StrTab;
43+
Optional<const ParsedStringTable *> StrTab;
4444
/// The state used by the parser to parse a remark entry. Invalidated with
4545
/// every call to `parseYAMLElement`.
4646
struct ParseState {
@@ -59,13 +59,11 @@ struct YAMLRemarkParser {
5959
/// not be containing any value.
6060
Optional<ParseState> State;
6161

62-
YAMLRemarkParser(StringRef Buf, Optional<StringRef> StrTabBuf = None)
62+
YAMLRemarkParser(StringRef Buf,
63+
Optional<const ParsedStringTable *> StrTab = None)
6364
: SM(), Stream(Buf, SM), ErrorString(), ErrorStream(ErrorString),
64-
TmpArgs(), StrTab() {
65+
TmpArgs(), StrTab(StrTab) {
6566
SM.setDiagHandler(YAMLRemarkParser::HandleDiagnostic, this);
66-
67-
if (StrTabBuf)
68-
StrTab.emplace(*StrTabBuf);
6967
}
7068

7169
/// Parse a YAML element.
@@ -127,8 +125,9 @@ struct YAMLParserImpl : public ParserImpl {
127125
/// Set to `true` if we had any errors during parsing.
128126
bool HasErrors = false;
129127

130-
YAMLParserImpl(StringRef Buf, Optional<StringRef> StrTabBuf = None)
131-
: ParserImpl{ParserImpl::Kind::YAML}, YAMLParser(Buf, StrTabBuf),
128+
YAMLParserImpl(StringRef Buf,
129+
Optional<const ParsedStringTable *> StrTab = None)
130+
: ParserImpl{ParserImpl::Kind::YAML}, YAMLParser(Buf, StrTab),
132131
YAMLIt(YAMLParser.Stream.begin()), HasErrors(false) {}
133132

134133
static bool classof(const ParserImpl *PI) {

llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ TEST(YAMLRemarks, ContentsStrTab) {
515515
"unavailable",
516516
115);
517517

518-
remarks::Parser Parser(Buf, StrTabBuf);
518+
remarks::ParsedStringTable StrTab(StrTabBuf);
519+
remarks::Parser Parser(Buf, StrTab);
519520
Expected<const remarks::Remark *> RemarkOrErr = Parser.getNext();
520521
EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
521522
EXPECT_TRUE(*RemarkOrErr != nullptr);
@@ -582,7 +583,8 @@ TEST(YAMLRemarks, ParsingBadStringTableIndex) {
582583

583584
StringRef StrTabBuf = StringRef("inline");
584585

585-
remarks::Parser Parser(Buf, StrTabBuf);
586+
remarks::ParsedStringTable StrTab(StrTabBuf);
587+
remarks::Parser Parser(Buf, StrTab);
586588
Expected<const remarks::Remark *> Remark = Parser.getNext();
587589
EXPECT_FALSE(Remark); // Expect an error here.
588590

0 commit comments

Comments
 (0)