diff options
author | Sean Callanan <scallanan@apple.com> | 2010-01-21 00:19:58 +0000 |
---|---|---|
committer | Sean Callanan <scallanan@apple.com> | 2010-01-21 00:19:58 +0000 |
commit | fd0b0288e2ee5ccf3f1d47090542710c67a77cf7 (patch) | |
tree | bc4826e2dfb787c6eef093ba692a2163936207a6 /tools/llvm-mc/AsmParser.cpp | |
parent | 05273448533565cf14d70e10f88919ee51755bc8 (diff) |
Moved handling of inclusion from the AsmLexer to
the AsmParser, breaking AsmLexer's dependence on
SourceMgr.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@94054 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-mc/AsmParser.cpp')
-rw-r--r-- | tools/llvm-mc/AsmParser.cpp | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/tools/llvm-mc/AsmParser.cpp b/tools/llvm-mc/AsmParser.cpp index 0e0c1a4de7..068e506be2 100644 --- a/tools/llvm-mc/AsmParser.cpp +++ b/tools/llvm-mc/AsmParser.cpp @@ -40,8 +40,10 @@ typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out, const MCAsmInfo &_MAI) - : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0), - SectionUniquingMap(0) { + : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0), + CurBuffer(0), SectionUniquingMap(0) { + Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); + // Debugging directives. AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile); AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine); @@ -104,14 +106,38 @@ void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const { SrcMgr.PrintMessage(Loc, Msg, Type); } - + +bool AsmParser::EnterIncludeFile(const std::string &Filename) { + int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc()); + if (NewBuf == -1) + return true; + + CurBuffer = NewBuf; + + Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); + + return false; +} + const AsmToken &AsmParser::Lex() { - const AsmToken &tok = Lexer.Lex(); + const AsmToken *tok = &Lexer.Lex(); - if (tok.is(AsmToken::Error)) + if (tok->is(AsmToken::Eof)) { + // If this is the end of an included file, pop the parent file off the + // include stack. + SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); + if (ParentIncludeLoc != SMLoc()) { + CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc); + Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), + ParentIncludeLoc.getPointer()); + tok = &Lexer.Lex(); + } + } + + if (tok->is(AsmToken::Error)) PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error"); - return tok; + return *tok; } bool AsmParser::Run() { @@ -1522,7 +1548,7 @@ bool AsmParser::ParseDirectiveInclude() { // Attempt to switch the lexer to the included file before consuming the end // of statement to avoid losing it when we switch. - if (Lexer.EnterIncludeFile(Filename)) { + if (EnterIncludeFile(Filename)) { PrintMessage(IncludeLoc, "Could not find include file '" + Filename + "'", "error"); |