[llvm-commits] [llvm] r94054 - in /llvm/trunk/tools/llvm-mc: AsmLexer.cpp AsmLexer.h AsmParser.cpp AsmParser.h llvm-mc.cpp

Sean Callanan scallanan at apple.com
Wed Jan 20 16:19:58 PST 2010


Author: spyffe
Date: Wed Jan 20 18:19:58 2010
New Revision: 94054

URL: http://llvm.org/viewvc/llvm-project?rev=94054&view=rev
Log:
Moved handling of inclusion from the AsmLexer to
the AsmParser, breaking AsmLexer's dependence on
SourceMgr.

Modified:
    llvm/trunk/tools/llvm-mc/AsmLexer.cpp
    llvm/trunk/tools/llvm-mc/AsmLexer.h
    llvm/trunk/tools/llvm-mc/AsmParser.cpp
    llvm/trunk/tools/llvm-mc/AsmParser.h
    llvm/trunk/tools/llvm-mc/llvm-mc.cpp

Modified: llvm/trunk/tools/llvm-mc/AsmLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmLexer.cpp?rev=94054&r1=94053&r2=94054&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmLexer.cpp (original)
+++ llvm/trunk/tools/llvm-mc/AsmLexer.cpp Wed Jan 20 18:19:58 2010
@@ -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 @@
   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_$.@]*

Modified: llvm/trunk/tools/llvm-mc/AsmLexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmLexer.h?rev=94054&r1=94053&r2=94054&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmLexer.h (original)
+++ llvm/trunk/tools/llvm-mc/AsmLexer.h Wed Jan 20 18:19:58 2010
@@ -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 @@
   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; }
 

Modified: llvm/trunk/tools/llvm-mc/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmParser.cpp?rev=94054&r1=94053&r2=94054&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmParser.cpp (original)
+++ llvm/trunk/tools/llvm-mc/AsmParser.cpp Wed Jan 20 18:19:58 2010
@@ -40,8 +40,10 @@
 
 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 @@
                              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 @@
   
   // 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");

Modified: llvm/trunk/tools/llvm-mc/AsmParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmParser.h?rev=94054&r1=94053&r2=94054&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmParser.h (original)
+++ llvm/trunk/tools/llvm-mc/AsmParser.h Wed Jan 20 18:19:58 2010
@@ -43,6 +43,10 @@
   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 @@
   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);

Modified: llvm/trunk/tools/llvm-mc/llvm-mc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/llvm-mc.cpp?rev=94054&r1=94053&r2=94054&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/llvm-mc.cpp (original)
+++ llvm/trunk/tools/llvm-mc/llvm-mc.cpp Wed Jan 20 18:19:58 2010
@@ -134,7 +134,7 @@
   const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName);
   assert(MAI && "Unable to create target asm info!");
 
-  AsmLexer Lexer(SrcMgr, *MAI);
+  AsmLexer Lexer(*MAI);
   
   bool Error = false;
   





More information about the llvm-commits mailing list