diff options
author | Chris Lattner <sabre@nondot.org> | 2009-03-08 08:08:45 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-03-08 08:08:45 +0000 |
commit | 1fa495304c81e03f07f278a47b5efe9317104aab (patch) | |
tree | c972c7493bcaba9b9cb88f4df32ff6fc6018fd0d /lib/Lex/ScratchBuffer.cpp | |
parent | 6b5e4f0cc5672c7d5449db28d5ead94d32407cb2 (diff) |
simplify some logic by making ScratchBuffer handle the application of trailing
\0's to created tokens instead of making all clients do it. No functionality
change.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66373 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/ScratchBuffer.cpp')
-rw-r--r-- | lib/Lex/ScratchBuffer.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/lib/Lex/ScratchBuffer.cpp b/lib/Lex/ScratchBuffer.cpp index 9253bc0944..c73cd68a4c 100644 --- a/lib/Lex/ScratchBuffer.cpp +++ b/lib/Lex/ScratchBuffer.cpp @@ -32,7 +32,7 @@ ScratchBuffer::ScratchBuffer(SourceManager &SM) : SourceMgr(SM), CurBuffer(0) { /// token. SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len, const char *&DestPtr) { - if (BytesUsed+Len > ScratchBufSize) + if (BytesUsed+Len+1 > ScratchBufSize) AllocScratchBuffer(Len); // Return a pointer to the character data. @@ -42,16 +42,21 @@ SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len, memcpy(CurBuffer+BytesUsed, Buf, Len); // Remember that we used these bytes. - BytesUsed += Len; + BytesUsed += Len+1; + + // Add a NUL terminator to the token. This keeps the tokens separated, in + // case they get relexed, and puts them on their own virtual lines in case a + // diagnostic points to one. + CurBuffer[BytesUsed-1] = '\0'; - return BufferStartLoc.getFileLocWithOffset(BytesUsed-Len); + return BufferStartLoc.getFileLocWithOffset(BytesUsed-Len-1); } void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) { // Only pay attention to the requested length if it is larger than our default // page size. If it is, we allocate an entire chunk for it. This is to // support gigantic tokens, which almost certainly won't happen. :) - if (RequestLen < ScratchBufSize) + if (RequestLen+1 < ScratchBufSize) RequestLen = ScratchBufSize; llvm::MemoryBuffer *Buf = @@ -59,5 +64,6 @@ void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) { FileID FID = SourceMgr.createFileIDForMemBuffer(Buf); BufferStartLoc = SourceMgr.getLocForStartOfFile(FID); CurBuffer = const_cast<char*>(Buf->getBufferStart()); - BytesUsed = 0; + BytesUsed = 1; + CurBuffer[0] = '0'; // Start out with a \0 for cleanliness. } |