llvm.org GIT mirror llvm / c289f25
[Remarks] Require an explicit format to the parser Make the parser require an explicit format. This allows new formats to be easily added by following YAML as an example. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365102 91177308-0d34-0410-b5e6-96231b3b80d8 Francis Visoiu Mistrih a month ago
6 changed file(s) with 44 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
2424 struct ParserImpl;
2525 struct ParsedStringTable;
2626
27 enum class ParserFormat { YAML };
28
2729 /// Parser used to parse a raw buffer to remarks::Remark objects.
2830 struct Parser {
2931 /// The hidden implementation of the parser.
3032 std::unique_ptr Impl;
3133
3234 /// Create a parser parsing \p Buffer to Remark objects.
33 /// This constructor should be only used for parsing YAML remarks.
34 Parser(StringRef Buffer);
35 /// This constructor should be only used for parsing remarks without a string
36 /// table.
37 Parser(ParserFormat Format, StringRef Buffer);
3538
3639 /// Create a parser parsing \p Buffer to Remark objects, using \p StrTab as a
3740 /// string table.
38 /// This constructor should be only used for parsing YAML remarks.
39 Parser(StringRef Buffer, const ParsedStringTable &StrTab);
41 Parser(ParserFormat Format, StringRef Buffer,
42 const ParsedStringTable &StrTab);
4043
4144 // Needed because ParserImpl is an incomplete type.
4245 ~Parser();
1919 using namespace llvm;
2020 using namespace llvm::remarks;
2121
22 Parser::Parser(StringRef Buf) : Impl(llvm::make_unique(Buf)) {}
22 static std::unique_ptr formatToParserImpl(ParserFormat Format,
23 StringRef Buf) {
24 switch (Format) {
25 case ParserFormat::YAML:
26 return llvm::make_unique(Buf);
27 };
28 }
2329
24 Parser::Parser(StringRef Buf, const ParsedStringTable &StrTab)
25 : Impl(llvm::make_unique(Buf, &StrTab)) {}
30 static std::unique_ptr
31 formatToParserImpl(ParserFormat Format, StringRef Buf,
32 const ParsedStringTable &StrTab) {
33 switch (Format) {
34 case ParserFormat::YAML:
35 return llvm::make_unique(Buf, &StrTab);
36 };
37 }
38
39 Parser::Parser(ParserFormat Format, StringRef Buf)
40 : Impl(formatToParserImpl(Format, Buf)) {}
41
42 Parser::Parser(ParserFormat Format, StringRef Buf,
43 const ParsedStringTable &StrTab)
44 : Impl(formatToParserImpl(Format, Buf, StrTab)) {}
2645
2746 Parser::~Parser() = default;
2847
89108 extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
90109 uint64_t Size) {
91110 return wrap(
92 new remarks::Parser(StringRef(static_cast(Buf), Size)));
111 new remarks::Parser(remarks::ParserFormat::YAML,
112 StringRef(static_cast(Buf), Size)));
93113 }
94114
95115 static void handleYAMLError(remarks::YAMLParserImpl &Impl, Error E) {
1212 #ifndef LLVM_REMARKS_REMARK_PARSER_IMPL_H
1313 #define LLVM_REMARKS_REMARK_PARSER_IMPL_H
1414
15 #include "llvm/Remarks/RemarkParser.h"
16
1517 namespace llvm {
1618 namespace remarks {
1719 /// This is used as a base for any parser implementation.
1820 struct ParserImpl {
19 enum class Kind { YAML };
20
21 explicit ParserImpl(Kind TheParserKind) : ParserKind(TheParserKind) {}
21 explicit ParserImpl(ParserFormat Format) : Format(Format) {}
2222 // Virtual destructor prevents mismatched deletes
2323 virtual ~ParserImpl() {}
2424
25 // The parser kind. This is used as a tag to safely cast between
25 // The parser format. This is used as a tag to safely cast between
2626 // implementations.
27 Kind ParserKind;
27 ParserFormat Format;
2828 };
2929 } // end namespace remarks
3030 } // end namespace llvm
126126
127127 YAMLParserImpl(StringRef Buf,
128128 Optional StrTab = None)
129 : ParserImpl{ParserImpl::Kind::YAML}, YAMLParser(Buf, StrTab),
129 : ParserImpl{ParserFormat::YAML}, YAMLParser(Buf, StrTab),
130130 YAMLIt(YAMLParser.Stream.begin()), HasErrors(false) {}
131131
132132 static bool classof(const ParserImpl *PI) {
133 return PI->ParserKind == ParserImpl::Kind::YAML;
133 return PI->Format == ParserFormat::YAML;
134134 }
135135 };
136136 } // end namespace remarks
149149 return false;
150150 }
151151
152 remarks::Parser Parser((*Buf)->getBuffer());
152 remarks::Parser Parser(remarks::ParserFormat::YAML, (*Buf)->getBuffer());
153153
154154 while (true) {
155155 Expected RemarkOrErr = Parser.getNext();
1313 using namespace llvm;
1414
1515 template void parseGood(const char (&Buf)[N]) {
16 remarks::Parser Parser({Buf, N - 1});
16 remarks::Parser Parser(remarks::ParserFormat::YAML, {Buf, N - 1});
1717 Expected Remark = Parser.getNext();
1818 EXPECT_FALSE(errorToBool(Remark.takeError())); // Check for parsing errors.
1919 EXPECT_TRUE(*Remark != nullptr); // At least one remark.
2424
2525 template
2626 bool parseExpectError(const char (&Buf)[N], const char *Error) {
27 remarks::Parser Parser({Buf, N - 1});
27 remarks::Parser Parser(remarks::ParserFormat::YAML, {Buf, N - 1});
2828 Expected Remark = Parser.getNext();
2929 EXPECT_FALSE(Remark); // Expect an error here.
3030
353353 " - String: ' because its definition is unavailable'\n"
354354 "\n";
355355
356 remarks::Parser Parser(Buf);
356 remarks::Parser Parser(remarks::ParserFormat::YAML, Buf);
357357 Expected RemarkOrErr = Parser.getNext();
358358 EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
359359 EXPECT_TRUE(*RemarkOrErr != nullptr);
515515 115);
516516
517517 remarks::ParsedStringTable StrTab(StrTabBuf);
518 remarks::Parser Parser(Buf, StrTab);
518 remarks::Parser Parser(remarks::ParserFormat::YAML, Buf, StrTab);
519519 Expected RemarkOrErr = Parser.getNext();
520520 EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
521521 EXPECT_TRUE(*RemarkOrErr != nullptr);
583583 StringRef StrTabBuf = StringRef("inline");
584584
585585 remarks::ParsedStringTable StrTab(StrTabBuf);
586 remarks::Parser Parser(Buf, StrTab);
586 remarks::Parser Parser(remarks::ParserFormat::YAML, Buf, StrTab);
587587 Expected Remark = Parser.getNext();
588588 EXPECT_FALSE(Remark); // Expect an error here.
589589