Moved handling of inclusion from the AsmLexer to
authorSean Callanan <scallanan@apple.com>
Thu, 21 Jan 2010 00:19:58 +0000 (00:19 +0000)
committerSean Callanan <scallanan@apple.com>
Thu, 21 Jan 2010 00:19:58 +0000 (00:19 +0000)
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
tools/llvm-mc/AsmLexer.h
tools/llvm-mc/AsmParser.cpp
tools/llvm-mc/AsmParser.h
tools/llvm-mc/llvm-mc.cpp

index 19f5247a4567108e278fb4b204532dd4d14cd264..234b8f33ad08b3e21708cd59eeff8b36d08eb8d3 100644 (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"
 #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_$.@]*
index cc40d3516e96d1133ae0084c2b814277eb2c7e56..1d49e4b766dd8d2ea8d5a0130a187356a6aefb14 100644 (file)
 
 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; }
 
index 0e0c1a4de7a5fddd565e4764b4a8d4e28bb28984..068e506be26642ffcdc25ce35660bd802439cd16 100644 (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);
 }
-
+                  
+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");
index 21792eb0e9527167fa0e8e26761202ff68921a41..9336d358063ba597588b17376fa7a39375d2c4cb 100644 (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);
index c84c684f19a4bacfb31fe49f3ba58845f509e554..fe545ab6e90d7c2a210c0bf76bae9e44e551c680 100644 (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;