Record the start of the current token, for use in error reporting.
authorChris Lattner <sabre@nondot.org>
Mon, 19 Nov 2007 07:43:52 +0000 (07:43 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 19 Nov 2007 07:43:52 +0000 (07:43 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44227 91177308-0d34-0410-b5e6-96231b3b80d8

utils/TableGen/TGLexer.cpp
utils/TableGen/TGLexer.h

index dd7ad6ceb101df796e6e2c7b3f040e39dabb0004..c859604b55aa31ea35d25885eec7e2a686dfdc7e 100644 (file)
@@ -27,6 +27,7 @@ using namespace llvm;
 
 TGLexer::TGLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) {
   CurPtr = CurBuf->getBufferStart();
+  TokStart = 0;
 }
 
 TGLexer::~TGLexer() {
@@ -76,8 +77,7 @@ void TGLexer::PrintError(const char *ErrorLoc,  const std::string &Msg) const {
   // Print out the line.
   cerr << std::string(LineStart, LineEnd) << "\n";
   // Print out spaces before the carat.
-  const char *Pos = LineStart;
-  while (Pos != ErrorLoc)
+  for (const char *Pos = LineStart; Pos != ErrorLoc; ++Pos)
     cerr << (*Pos == '\t' ? '\t' : ' ');
   cerr << "^\n";
 }
@@ -122,6 +122,7 @@ int TGLexer::getNextChar() {
 }
 
 int TGLexer::LexToken() {
+  TokStart = CurPtr;
   // This always consumes at least one character.
   int CurChar = getNextChar();
 
@@ -238,11 +239,10 @@ int TGLexer::LexIdentifier() {
 /// comes next and enter the include.
 bool TGLexer::LexInclude() {
   // The token after the include must be a string.
-  const char *TokStart = CurPtr-7;
   int Tok = LexToken();
   if (Tok == YYERROR) return true;
   if (Tok != STRVAL) {
-    PrintError(TokStart, "Expected filename after include");
+    PrintError(getTokenStart(), "Expected filename after include");
     return true;
   }
 
@@ -260,7 +260,8 @@ bool TGLexer::LexInclude() {
   }
     
   if (NewBuf == 0) {
-    PrintError(TokStart, "Could not find include file '" + Filename + "'");
+    PrintError(getTokenStart(),
+               "Could not find include file '" + Filename + "'");
     return true;
   }
   
index dab68c3aabcb8f0f942850cd7f49bd335e3a96a1..ef786b6f28bcb58741f616daa4a479a60e94021c 100644 (file)
@@ -40,6 +40,7 @@ class TGLexer {
   // IncludeDirectories - This is the list of directories we should search for
   // include files in.
   std::vector<std::string> IncludeDirectories;
+  const char *TokStart;
 public:
   TGLexer(MemoryBuffer *StartBuf);
   ~TGLexer();
@@ -50,7 +51,10 @@ public:
   
   int LexToken();
 
-  void PrintError(const char *Loc, const std::string &Msg) const;
+  typedef const char* LocationTy;
+  LocationTy getTokenStart() const { return TokStart; }
+
+  void PrintError(LocationTy Loc, const std::string &Msg) const;
   
   std::ostream &err() const;
   void PrintIncludeStack(std::ostream &OS) const;