[PECOFF] Refactor module-defintion file parser.

Refactor the parser so that the parser can return arbitrary type of parse
result other than a vector of ExportDesc. Parsers for non-EXPORTS directives
will be implemented in different patches. No functionality change.

llvm-svn: 198993
This commit is contained in:
Rui Ueyama
2014-01-11 01:33:42 +00:00
parent 454b4af0b0
commit f98a18acb7
4 changed files with 88 additions and 34 deletions

View File

@@ -18,25 +18,33 @@ using namespace lld;
class ParserTest : public testing::Test {
protected:
bool parse(const char *contents,
std::vector<PECOFFLinkingContext::ExportDesc> &ret) {
llvm::Optional<moduledef::Directive *> parse(const char *contents,
llvm::BumpPtrAllocator &alloc) {
auto membuf =
std::unique_ptr<MemoryBuffer>(MemoryBuffer::getMemBuffer(contents));
moduledef::Lexer lexer(std::move(membuf));
moduledef::Parser parser(lexer);
return parser.parse(ret);
moduledef::Parser parser(lexer, alloc);
return parser.parse();
}
};
TEST_F(ParserTest, Exports) {
std::vector<PECOFFLinkingContext::ExportDesc> exports;
EXPECT_TRUE(parse("EXPORTS\n"
" sym1\n"
" sym2 @5\n"
" sym3 @8 NONAME\n"
" sym4 DATA\n"
" sym5 @10 NONAME DATA\n",
exports));
llvm::BumpPtrAllocator alloc;
llvm::Optional<moduledef::Directive *> dir = parse("EXPORTS\n"
" sym1\n"
" sym2 @5\n"
" sym3 @8 NONAME\n"
" sym4 DATA\n"
" sym5 @10 NONAME DATA\n",
alloc);
EXPECT_TRUE(dir.hasValue());
EXPECT_EQ(moduledef::Directive::Kind::exports, dir.getValue()->getKind());
auto *exportsDir = dyn_cast<moduledef::Exports>(dir.getValue());
EXPECT_TRUE(exportsDir != nullptr);
const std::vector<PECOFFLinkingContext::ExportDesc> &exports =
exportsDir->getExports();
EXPECT_EQ(5U, exports.size());
EXPECT_EQ(exports[0].name, "sym1");
EXPECT_EQ(exports[0].ordinal, -1);