split buffer management and diagnostic printing out of the tblgen

lexer into its own TGSourceMgr class.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66873 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2009-03-13 07:05:43 +00:00
parent 49c8aa0d8b
commit aa739d26b1
6 changed files with 228 additions and 70 deletions

View File

@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "TGLexer.h"
#include "TGSourceMgr.h"
#include "llvm/Support/Streams.h"
#include "llvm/Support/MemoryBuffer.h"
#include <ostream>
@@ -23,18 +24,13 @@
#include <cerrno>
using namespace llvm;
TGLexer::TGLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) {
TGLexer::TGLexer(TGSourceMgr &SM) : SrcMgr(SM) {
CurBuffer = 0;
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
CurPtr = CurBuf->getBufferStart();
TokStart = 0;
}
TGLexer::~TGLexer() {
while (!IncludeStack.empty()) {
delete IncludeStack.back().Buffer;
IncludeStack.pop_back();
}
delete CurBuf;
}
/// ReturnError - Set the error to the specified string at the specified
/// location. This is defined to always return tgtok::Error.
@@ -43,36 +39,9 @@ tgtok::TokKind TGLexer::ReturnError(const char *Loc, const std::string &Msg) {
return tgtok::Error;
}
void TGLexer::PrintIncludeStack(std::ostream &OS) const {
for (unsigned i = 0, e = IncludeStack.size(); i != e; ++i)
OS << "Included from " << IncludeStack[i].Buffer->getBufferIdentifier()
<< ":" << IncludeStack[i].LineNo << ":\n";
OS << "Parsing " << CurBuf->getBufferIdentifier() << ":"
<< CurLineNo << ": ";
}
/// PrintError - Print the error at the specified location.
void TGLexer::PrintError(const char *ErrorLoc, const std::string &Msg) const {
PrintIncludeStack(*cerr.stream());
cerr << Msg << "\n";
assert(ErrorLoc && "Location not specified!");
// Scan backward to find the start of the line.
const char *LineStart = ErrorLoc;
while (LineStart != CurBuf->getBufferStart() &&
LineStart[-1] != '\n' && LineStart[-1] != '\r')
--LineStart;
// Get the end of the line.
const char *LineEnd = ErrorLoc;
while (LineEnd != CurBuf->getBufferEnd() &&
LineEnd[0] != '\n' && LineEnd[0] != '\r')
++LineEnd;
// Print out the line.
cerr << std::string(LineStart, LineEnd) << "\n";
// Print out spaces before the carat.
for (const char *Pos = LineStart; Pos != ErrorLoc; ++Pos)
cerr << (*Pos == '\t' ? '\t' : ' ');
cerr << "^\n";
void TGLexer::PrintError(LocTy Loc, const std::string &Msg) const {
SrcMgr.PrintError(Loc, Msg);
}
int TGLexer::getNextChar() {
@@ -80,7 +49,7 @@ int TGLexer::getNextChar() {
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())
@@ -88,18 +57,18 @@ int TGLexer::getNextChar() {
// If this is the end of an included file, pop the parent file off the
// include stack.
if (!IncludeStack.empty()) {
delete CurBuf;
CurBuf = IncludeStack.back().Buffer;
CurLineNo = IncludeStack.back().LineNo;
CurPtr = IncludeStack.back().CurPtr;
IncludeStack.pop_back();
TGLocTy ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
if (ParentIncludeLoc != TGLocTy()) {
CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
CurPtr = ParentIncludeLoc;
return getNextChar();
}
// Otherwise, return end of file.
--CurPtr; // Another call to lex will return EOF again.
return EOF;
}
case '\n':
case '\r':
// Handle the newline character by ignoring it and incrementing the line
@@ -108,8 +77,6 @@ int TGLexer::getNextChar() {
if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
*CurPtr != CurChar)
++CurPtr; // Eat the two char newline sequence.
++CurLineNo;
return '\n';
}
}
@@ -272,9 +239,8 @@ bool TGLexer::LexInclude() {
}
// Save the line number and lex buffer of the includer.
IncludeStack.push_back(IncludeRec(CurBuf, CurPtr, CurLineNo));
CurBuffer = SrcMgr.AddNewSourceBuffer(NewBuf, CurPtr);
CurLineNo = 1; // Reset line numbering.
CurBuf = NewBuf;
CurPtr = CurBuf->getBufferStart();
return false;