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
This commit is contained in:
Sean Callanan 2010-01-21 00:19:58 +00:00
parent 0527344853
commit fd0b0288e2
5 changed files with 61 additions and 56 deletions

View File

@ -12,7 +12,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "AsmLexer.h" #include "AsmLexer.h"
#include "llvm/Support/SourceMgr.h" #include "llvm/Support/SMLoc.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Config/config.h" // for strtoull. #include "llvm/Config/config.h" // for strtoull.
#include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCAsmInfo.h"
@ -21,17 +21,26 @@
#include <cstdlib> #include <cstdlib>
using namespace llvm; using namespace llvm;
AsmLexer::AsmLexer(SourceMgr &SM, const MCAsmInfo &_MAI) : SrcMgr(SM), AsmLexer::AsmLexer(const MCAsmInfo &_MAI) : MAI(_MAI) {
MAI(_MAI) { CurBuf = NULL;
CurBuffer = 0; CurPtr = NULL;
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
CurPtr = CurBuf->getBufferStart();
TokStart = 0; TokStart = 0;
} }
AsmLexer::~AsmLexer() { 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 { SMLoc AsmLexer::getLoc() const {
return SMLoc::getFromPointer(TokStart); 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)); 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() { int AsmLexer::getNextChar() {
char CurChar = *CurPtr++; char CurChar = *CurPtr++;
switch (CurChar) { switch (CurChar) {
default: default:
return (unsigned char)CurChar; return (unsigned char)CurChar;
case 0: { case 0:
// A nul character in the stream is either the end of the current buffer or // A nul character in the stream is either the end of the current buffer or
// a random nul in the file. Disambiguate that here. // a random nul in the file. Disambiguate that here.
if (CurPtr-1 != CurBuf->getBufferEnd()) if (CurPtr-1 != CurBuf->getBufferEnd())
return 0; // Just whitespace. 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. // Otherwise, return end of file.
--CurPtr; // Another call to lex will return EOF again. --CurPtr; // Another call to lex will return EOF again.
return EOF; return EOF;
} }
}
} }
/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]* /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*

View File

@ -23,23 +23,17 @@
namespace llvm { namespace llvm {
class MemoryBuffer; class MemoryBuffer;
class SourceMgr;
class SMLoc; class SMLoc;
class MCAsmInfo; class MCAsmInfo;
/// AsmLexer - Lexer class for assembly files. /// AsmLexer - Lexer class for assembly files.
class AsmLexer : public MCAsmLexer { class AsmLexer : public MCAsmLexer {
SourceMgr &SrcMgr;
const MCAsmInfo &MAI; const MCAsmInfo &MAI;
const char *CurPtr; const char *CurPtr;
const MemoryBuffer *CurBuf; const MemoryBuffer *CurBuf;
const char *TokStart; 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 void operator=(const AsmLexer&); // DO NOT IMPLEMENT
AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT
@ -49,17 +43,16 @@ protected:
virtual AsmToken LexToken(); virtual AsmToken LexToken();
public: public:
AsmLexer(SourceMgr &SrcMgr, const MCAsmInfo &MAI); AsmLexer(const MCAsmInfo &MAI);
~AsmLexer(); ~AsmLexer();
void setBuffer(const MemoryBuffer *buf, const char *ptr = NULL);
SMLoc getLoc() const; SMLoc getLoc() const;
StringRef LexUntilEndOfStatement(); StringRef LexUntilEndOfStatement();
bool isAtStartOfComment(char Char); 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; } const MCAsmInfo &getMAI() const { return MAI; }

View File

@ -40,8 +40,10 @@ typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out, AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
const MCAsmInfo &_MAI) const MCAsmInfo &_MAI)
: Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0), : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
SectionUniquingMap(0) { CurBuffer(0), SectionUniquingMap(0) {
Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
// Debugging directives. // Debugging directives.
AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile); AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine); AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
@ -104,14 +106,38 @@ void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
const char *Type) const { const char *Type) const {
SrcMgr.PrintMessage(Loc, Msg, Type); SrcMgr.PrintMessage(Loc, Msg, Type);
} }
const AsmToken &AsmParser::Lex() { bool AsmParser::EnterIncludeFile(const std::string &Filename) {
const AsmToken &tok = Lexer.Lex(); int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
if (NewBuf == -1)
return true;
if (tok.is(AsmToken::Error)) CurBuffer = NewBuf;
Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
return false;
}
const AsmToken &AsmParser::Lex() {
const AsmToken *tok = &Lexer.Lex();
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"); PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
return tok; return *tok;
} }
bool AsmParser::Run() { bool AsmParser::Run() {
@ -1522,7 +1548,7 @@ bool AsmParser::ParseDirectiveInclude() {
// Attempt to switch the lexer to the included file before consuming the end // Attempt to switch the lexer to the included file before consuming the end
// of statement to avoid losing it when we switch. // of statement to avoid losing it when we switch.
if (Lexer.EnterIncludeFile(Filename)) { if (EnterIncludeFile(Filename)) {
PrintMessage(IncludeLoc, PrintMessage(IncludeLoc,
"Could not find include file '" + Filename + "'", "Could not find include file '" + Filename + "'",
"error"); "error");

View File

@ -43,6 +43,10 @@ private:
MCStreamer &Out; MCStreamer &Out;
SourceMgr &SrcMgr; SourceMgr &SrcMgr;
TargetAsmParser *TargetParser; TargetAsmParser *TargetParser;
/// This is the current buffer index we're lexing from as managed by the
/// SourceMgr object.
int CurBuffer;
AsmCond TheCondState; AsmCond TheCondState;
std::vector<AsmCond> TheCondStack; std::vector<AsmCond> TheCondStack;
@ -106,6 +110,9 @@ private:
bool TokError(const char *Msg); bool TokError(const char *Msg);
void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const; 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, bool ParseConditionalAssemblyDirectives(StringRef Directive,
SMLoc DirectiveLoc); SMLoc DirectiveLoc);

View File

@ -134,7 +134,7 @@ static int AsLexInput(const char *ProgName) {
const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName); const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName);
assert(MAI && "Unable to create target asm info!"); assert(MAI && "Unable to create target asm info!");
AsmLexer Lexer(SrcMgr, *MAI); AsmLexer Lexer(*MAI);
bool Error = false; bool Error = false;