From fd0b0288e2ee5ccf3f1d47090542710c67a77cf7 Mon Sep 17 00:00:00 2001 From: Sean Callanan Date: Thu, 21 Jan 2010 00:19:58 +0000 Subject: 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 --- tools/llvm-mc/AsmLexer.cpp | 53 ++++++++++++++------------------------------- tools/llvm-mc/AsmLexer.h | 13 +++-------- tools/llvm-mc/AsmParser.cpp | 40 ++++++++++++++++++++++++++++------ tools/llvm-mc/AsmParser.h | 7 ++++++ tools/llvm-mc/llvm-mc.cpp | 2 +- 5 files changed, 60 insertions(+), 55 deletions(-) (limited to 'tools/llvm-mc') diff --git a/tools/llvm-mc/AsmLexer.cpp b/tools/llvm-mc/AsmLexer.cpp index 19f5247a45..234b8f33ad 100644 --- a/tools/llvm-mc/AsmLexer.cpp +++ b/tools/llvm-mc/AsmLexer.cpp @@ -12,7 +12,7 @@ //===----------------------------------------------------------------------===// #include "AsmLexer.h" -#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/SMLoc.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Config/config.h" // for strtoull. #include "llvm/MC/MCAsmInfo.h" @@ -21,17 +21,26 @@ #include using namespace llvm; -AsmLexer::AsmLexer(SourceMgr &SM, const MCAsmInfo &_MAI) : SrcMgr(SM), - MAI(_MAI) { - CurBuffer = 0; - CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); - CurPtr = CurBuf->getBufferStart(); +AsmLexer::AsmLexer(const MCAsmInfo &_MAI) : MAI(_MAI) { + CurBuf = NULL; + CurPtr = NULL; TokStart = 0; } AsmLexer::~AsmLexer() { } +void AsmLexer::setBuffer(const MemoryBuffer *buf, const char *ptr) { + CurBuf = buf; + + if (ptr) + CurPtr = ptr; + else + CurPtr = CurBuf->getBufferStart(); + + TokStart = 0; +} + SMLoc AsmLexer::getLoc() const { return SMLoc::getFromPointer(TokStart); } @@ -44,51 +53,21 @@ AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) { return AsmToken(AsmToken::Error, StringRef(Loc, 0)); } -/// EnterIncludeFile - Enter the specified file. This prints an error and -/// returns true on failure. -bool AsmLexer::EnterIncludeFile(const std::string &Filename) { - int NewBuf = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr)); - if (NewBuf == -1) - return true; - - // Save the line number and lex buffer of the includer. - CurBuffer = NewBuf; - CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); - CurPtr = CurBuf->getBufferStart(); - return false; -} - - int AsmLexer::getNextChar() { char CurChar = *CurPtr++; switch (CurChar) { default: return (unsigned char)CurChar; - case 0: { + case 0: // A nul character in the stream is either the end of the current buffer or // a random nul in the file. Disambiguate that here. if (CurPtr-1 != CurBuf->getBufferEnd()) return 0; // Just whitespace. - // 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); - CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); - CurPtr = ParentIncludeLoc.getPointer(); - - // Reset the token start pointer to the start of the new file. - TokStart = CurPtr; - - return getNextChar(); - } - // Otherwise, return end of file. --CurPtr; // Another call to lex will return EOF again. return EOF; } - } } /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]* diff --git a/tools/llvm-mc/AsmLexer.h b/tools/llvm-mc/AsmLexer.h index cc40d3516e..1d49e4b766 100644 --- a/tools/llvm-mc/AsmLexer.h +++ b/tools/llvm-mc/AsmLexer.h @@ -23,23 +23,17 @@ namespace llvm { class MemoryBuffer; -class SourceMgr; class SMLoc; class MCAsmInfo; /// AsmLexer - Lexer class for assembly files. class AsmLexer : public MCAsmLexer { - SourceMgr &SrcMgr; const MCAsmInfo &MAI; const char *CurPtr; const MemoryBuffer *CurBuf; const char *TokStart; - - /// This is the current buffer index we're lexing from as managed by the - /// SourceMgr object. - int CurBuffer; void operator=(const AsmLexer&); // DO NOT IMPLEMENT AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT @@ -49,17 +43,16 @@ protected: virtual AsmToken LexToken(); public: - AsmLexer(SourceMgr &SrcMgr, const MCAsmInfo &MAI); + AsmLexer(const MCAsmInfo &MAI); ~AsmLexer(); + void setBuffer(const MemoryBuffer *buf, const char *ptr = NULL); + SMLoc getLoc() const; StringRef LexUntilEndOfStatement(); bool isAtStartOfComment(char Char); - - /// EnterIncludeFile - Enter the specified file. This returns true on failure. - bool EnterIncludeFile(const std::string &Filename); const MCAsmInfo &getMAI() const { return MAI; } 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 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"); diff --git a/tools/llvm-mc/AsmParser.h b/tools/llvm-mc/AsmParser.h index 21792eb0e9..9336d35806 100644 --- a/tools/llvm-mc/AsmParser.h +++ b/tools/llvm-mc/AsmParser.h @@ -43,6 +43,10 @@ private: MCStreamer &Out; SourceMgr &SrcMgr; TargetAsmParser *TargetParser; + + /// This is the current buffer index we're lexing from as managed by the + /// SourceMgr object. + int CurBuffer; AsmCond TheCondState; std::vector TheCondStack; @@ -106,6 +110,9 @@ private: bool TokError(const char *Msg); void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const; + + /// EnterIncludeFile - Enter the specified file. This returns true on failure. + bool EnterIncludeFile(const std::string &Filename); bool ParseConditionalAssemblyDirectives(StringRef Directive, SMLoc DirectiveLoc); diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp index c84c684f19..fe545ab6e9 100644 --- a/tools/llvm-mc/llvm-mc.cpp +++ b/tools/llvm-mc/llvm-mc.cpp @@ -134,7 +134,7 @@ static int AsLexInput(const char *ProgName) { const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName); assert(MAI && "Unable to create target asm info!"); - AsmLexer Lexer(SrcMgr, *MAI); + AsmLexer Lexer(*MAI); bool Error = false; -- cgit v1.2.3