mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-02-04 00:06:50 +00:00
[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. llvm-svn: 365102
This commit is contained in:
parent
e6ba313a86
commit
312f1d7d7c
@ -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();
|
||||
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
Parser::Parser(StringRef Buf, const ParsedStringTable &StrTab)
|
||||
: Impl(llvm::make_unique<YAMLParserImpl>(Buf, &StrTab)) {}
|
||||
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(ParserFormat Format, StringRef Buf,
|
||||
const ParsedStringTable &StrTab)
|
||||
: Impl(formatToParserImpl(Format, Buf, StrTab)) {}
|
||||
|
||||
Parser::~Parser() = default;
|
||||
|
||||
@ -90,7 +109,8 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(remarks::Parser, LLVMRemarkParserRef)
|
||||
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) {
|
||||
|
@ -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
|
||||
|
@ -127,11 +127,11 @@ struct YAMLParserImpl : public ParserImpl {
|
||||
|
||||
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
|
||||
|
@ -150,7 +150,7 @@ static bool readLocationInfo(LocationInfoTy &LocationInfo) {
|
||||
return false;
|
||||
}
|
||||
|
||||
remarks::Parser Parser((*Buf)->getBuffer());
|
||||
remarks::Parser Parser(remarks::ParserFormat::YAML, (*Buf)->getBuffer());
|
||||
|
||||
while (true) {
|
||||
Expected<const remarks::Remark *> RemarkOrErr = Parser.getNext();
|
||||
|
@ -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 char (&Buf)[N]) {
|
||||
|
||||
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, ParsingBadStringTableIndex) {
|
||||
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.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user