[llvm] r365102 - [Remarks] Require an explicit format to the parser
Francis Visoiu Mistrih via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 3 17:31:03 PDT 2019
Author: thegameg
Date: Wed Jul 3 17:31:03 2019
New Revision: 365102
URL: http://llvm.org/viewvc/llvm-project?rev=365102&view=rev
Log:
[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.
Modified:
llvm/trunk/include/llvm/Remarks/RemarkParser.h
llvm/trunk/lib/Remarks/RemarkParser.cpp
llvm/trunk/lib/Remarks/RemarkParserImpl.h
llvm/trunk/lib/Remarks/YAMLRemarkParser.h
llvm/trunk/tools/llvm-opt-report/OptReport.cpp
llvm/trunk/unittests/Remarks/YAMLRemarksParsingTest.cpp
Modified: llvm/trunk/include/llvm/Remarks/RemarkParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Remarks/RemarkParser.h?rev=365102&r1=365101&r2=365102&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Remarks/RemarkParser.h (original)
+++ llvm/trunk/include/llvm/Remarks/RemarkParser.h Wed Jul 3 17:31:03 2019
@@ -25,19 +25,22 @@ namespace remarks {
struct ParserImpl;
struct ParsedStringTable;
+enum class ParserFormat { YAML };
+
/// Parser used to parse a raw buffer to remarks::Remark objects.
struct Parser {
/// The hidden implementation of the parser.
std::unique_ptr<ParserImpl> Impl;
/// Create a parser parsing \p Buffer to Remark objects.
- /// This constructor should be only used for parsing YAML remarks.
- Parser(StringRef Buffer);
+ /// This constructor should be only used for parsing remarks without a string
+ /// table.
+ Parser(ParserFormat Format, StringRef Buffer);
/// 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, const ParsedStringTable &StrTab);
+ Parser(ParserFormat Format, StringRef Buffer,
+ const ParsedStringTable &StrTab);
// Needed because ParserImpl is an incomplete type.
~Parser();
Modified: llvm/trunk/lib/Remarks/RemarkParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Remarks/RemarkParser.cpp?rev=365102&r1=365101&r2=365102&view=diff
==============================================================================
--- llvm/trunk/lib/Remarks/RemarkParser.cpp (original)
+++ llvm/trunk/lib/Remarks/RemarkParser.cpp Wed Jul 3 17:31:03 2019
@@ -20,10 +20,29 @@
using namespace llvm;
using namespace llvm::remarks;
-Parser::Parser(StringRef Buf) : Impl(llvm::make_unique<YAMLParserImpl>(Buf)) {}
+static std::unique_ptr<ParserImpl> formatToParserImpl(ParserFormat Format,
+ StringRef Buf) {
+ switch (Format) {
+ case ParserFormat::YAML:
+ return llvm::make_unique<YAMLParserImpl>(Buf);
+ };
+}
+
+static std::unique_ptr<ParserImpl>
+formatToParserImpl(ParserFormat Format, StringRef Buf,
+ const ParsedStringTable &StrTab) {
+ switch (Format) {
+ case ParserFormat::YAML:
+ return llvm::make_unique<YAMLParserImpl>(Buf, &StrTab);
+ };
+}
+
+Parser::Parser(ParserFormat Format, StringRef Buf)
+ : Impl(formatToParserImpl(Format, Buf)) {}
-Parser::Parser(StringRef Buf, const ParsedStringTable &StrTab)
- : Impl(llvm::make_unique<YAMLParserImpl>(Buf, &StrTab)) {}
+Parser::Parser(ParserFormat Format, StringRef Buf,
+ const ParsedStringTable &StrTab)
+ : Impl(formatToParserImpl(Format, Buf, StrTab)) {}
Parser::~Parser() = default;
@@ -90,7 +109,8 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(remar
extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
uint64_t Size) {
return wrap(
- new remarks::Parser(StringRef(static_cast<const char *>(Buf), Size)));
+ new remarks::Parser(remarks::ParserFormat::YAML,
+ StringRef(static_cast<const char *>(Buf), Size)));
}
static void handleYAMLError(remarks::YAMLParserImpl &Impl, Error E) {
Modified: llvm/trunk/lib/Remarks/RemarkParserImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Remarks/RemarkParserImpl.h?rev=365102&r1=365101&r2=365102&view=diff
==============================================================================
--- llvm/trunk/lib/Remarks/RemarkParserImpl.h (original)
+++ llvm/trunk/lib/Remarks/RemarkParserImpl.h Wed Jul 3 17:31:03 2019
@@ -13,19 +13,19 @@
#ifndef LLVM_REMARKS_REMARK_PARSER_IMPL_H
#define LLVM_REMARKS_REMARK_PARSER_IMPL_H
+#include "llvm/Remarks/RemarkParser.h"
+
namespace llvm {
namespace remarks {
/// This is used as a base for any parser implementation.
struct ParserImpl {
- enum class Kind { YAML };
-
- explicit ParserImpl(Kind TheParserKind) : ParserKind(TheParserKind) {}
+ explicit ParserImpl(ParserFormat Format) : Format(Format) {}
// Virtual destructor prevents mismatched deletes
virtual ~ParserImpl() {}
- // The parser kind. This is used as a tag to safely cast between
+ // The parser format. This is used as a tag to safely cast between
// implementations.
- Kind ParserKind;
+ ParserFormat Format;
};
} // end namespace remarks
} // end namespace llvm
Modified: llvm/trunk/lib/Remarks/YAMLRemarkParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Remarks/YAMLRemarkParser.h?rev=365102&r1=365101&r2=365102&view=diff
==============================================================================
--- llvm/trunk/lib/Remarks/YAMLRemarkParser.h (original)
+++ llvm/trunk/lib/Remarks/YAMLRemarkParser.h Wed Jul 3 17:31:03 2019
@@ -127,11 +127,11 @@ struct YAMLParserImpl : public ParserImp
YAMLParserImpl(StringRef Buf,
Optional<const ParsedStringTable *> StrTab = None)
- : ParserImpl{ParserImpl::Kind::YAML}, YAMLParser(Buf, StrTab),
+ : ParserImpl{ParserFormat::YAML}, YAMLParser(Buf, StrTab),
YAMLIt(YAMLParser.Stream.begin()), HasErrors(false) {}
static bool classof(const ParserImpl *PI) {
- return PI->ParserKind == ParserImpl::Kind::YAML;
+ return PI->Format == ParserFormat::YAML;
}
};
} // end namespace remarks
Modified: llvm/trunk/tools/llvm-opt-report/OptReport.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-opt-report/OptReport.cpp?rev=365102&r1=365101&r2=365102&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-opt-report/OptReport.cpp (original)
+++ llvm/trunk/tools/llvm-opt-report/OptReport.cpp Wed Jul 3 17:31:03 2019
@@ -150,7 +150,7 @@ static bool readLocationInfo(LocationInf
return false;
}
- remarks::Parser Parser((*Buf)->getBuffer());
+ remarks::Parser Parser(remarks::ParserFormat::YAML, (*Buf)->getBuffer());
while (true) {
Expected<const remarks::Remark *> RemarkOrErr = Parser.getNext();
Modified: llvm/trunk/unittests/Remarks/YAMLRemarksParsingTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Remarks/YAMLRemarksParsingTest.cpp?rev=365102&r1=365101&r2=365102&view=diff
==============================================================================
--- llvm/trunk/unittests/Remarks/YAMLRemarksParsingTest.cpp (original)
+++ llvm/trunk/unittests/Remarks/YAMLRemarksParsingTest.cpp Wed Jul 3 17:31:03 2019
@@ -14,7 +14,7 @@
using namespace llvm;
template <size_t N> void parseGood(const char (&Buf)[N]) {
- remarks::Parser Parser({Buf, N - 1});
+ remarks::Parser Parser(remarks::ParserFormat::YAML, {Buf, N - 1});
Expected<const remarks::Remark *> Remark = Parser.getNext();
EXPECT_FALSE(errorToBool(Remark.takeError())); // Check for parsing errors.
EXPECT_TRUE(*Remark != nullptr); // At least one remark.
@@ -25,7 +25,7 @@ template <size_t N> void parseGood(const
template <size_t N>
bool parseExpectError(const char (&Buf)[N], const char *Error) {
- remarks::Parser Parser({Buf, N - 1});
+ remarks::Parser Parser(remarks::ParserFormat::YAML, {Buf, N - 1});
Expected<const remarks::Remark *> Remark = Parser.getNext();
EXPECT_FALSE(Remark); // Expect an error here.
@@ -354,7 +354,7 @@ TEST(YAMLRemarks, Contents) {
" - String: ' because its definition is unavailable'\n"
"\n";
- remarks::Parser Parser(Buf);
+ remarks::Parser Parser(remarks::ParserFormat::YAML, Buf);
Expected<const remarks::Remark *> RemarkOrErr = Parser.getNext();
EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
EXPECT_TRUE(*RemarkOrErr != nullptr);
@@ -516,7 +516,7 @@ TEST(YAMLRemarks, ContentsStrTab) {
115);
remarks::ParsedStringTable StrTab(StrTabBuf);
- remarks::Parser Parser(Buf, StrTab);
+ remarks::Parser Parser(remarks::ParserFormat::YAML, Buf, StrTab);
Expected<const remarks::Remark *> RemarkOrErr = Parser.getNext();
EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
EXPECT_TRUE(*RemarkOrErr != nullptr);
@@ -584,7 +584,7 @@ TEST(YAMLRemarks, ParsingBadStringTableI
StringRef StrTabBuf = StringRef("inline");
remarks::ParsedStringTable StrTab(StrTabBuf);
- remarks::Parser Parser(Buf, StrTab);
+ remarks::Parser Parser(remarks::ParserFormat::YAML, Buf, StrTab);
Expected<const remarks::Remark *> Remark = Parser.getNext();
EXPECT_FALSE(Remark); // Expect an error here.
More information about the llvm-commits
mailing list