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 "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 <cstdlib>
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_$.@]*

View File

@ -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; }

View File

@ -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);
}
const AsmToken &AsmParser::Lex() {
const AsmToken &tok = Lexer.Lex();
bool AsmParser::EnterIncludeFile(const std::string &Filename) {
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");
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");

View File

@ -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<AsmCond> 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);

View File

@ -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;