Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ScratchBuffer interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/ScratchBuffer.h" |
| 15 | #include "clang/Basic/SourceManager.h" |
| 16 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 87cf5ac | 2008-03-10 17:04:53 +0000 | [diff] [blame] | 17 | #include <cstring> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
| 20 | // ScratchBufSize - The size of each chunk of scratch memory. Slightly less |
| 21 | //than a page, almost certainly enough for anything. :) |
| 22 | static const unsigned ScratchBufSize = 4060; |
| 23 | |
| 24 | ScratchBuffer::ScratchBuffer(SourceManager &SM) : SourceMgr(SM), CurBuffer(0) { |
| 25 | // Set BytesUsed so that the first call to getToken will require an alloc. |
| 26 | BytesUsed = ScratchBufSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /// getToken - Splat the specified text into a temporary MemoryBuffer and |
| 30 | /// return a SourceLocation that refers to the token. This is just like the |
| 31 | /// method below, but returns a location that indicates the physloc of the |
| 32 | /// token. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 33 | SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len, |
| 34 | const char *&DestPtr) { |
Chris Lattner | 2bc69bd | 2009-03-08 08:16:41 +0000 | [diff] [blame] | 35 | if (BytesUsed+Len+2 > ScratchBufSize) |
| 36 | AllocScratchBuffer(Len+2); |
| 37 | |
| 38 | // Prefix the token with a \n, so that it looks like it is the first thing on |
| 39 | // its own virtual line in caret diagnostics. |
| 40 | CurBuffer[BytesUsed++] = '\n'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 42 | // Return a pointer to the character data. |
| 43 | DestPtr = CurBuffer+BytesUsed; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | // Copy the token data into the buffer. |
| 46 | memcpy(CurBuffer+BytesUsed, Buf, Len); |
| 47 | |
| 48 | // Remember that we used these bytes. |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 49 | BytesUsed += Len+1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 51 | // Add a NUL terminator to the token. This keeps the tokens separated, in |
| 52 | // case they get relexed, and puts them on their own virtual lines in case a |
| 53 | // diagnostic points to one. |
| 54 | CurBuffer[BytesUsed-1] = '\0'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 56 | return BufferStartLoc.getFileLocWithOffset(BytesUsed-Len-1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) { |
| 60 | // Only pay attention to the requested length if it is larger than our default |
| 61 | // page size. If it is, we allocate an entire chunk for it. This is to |
| 62 | // support gigantic tokens, which almost certainly won't happen. :) |
Chris Lattner | 2bc69bd | 2009-03-08 08:16:41 +0000 | [diff] [blame] | 63 | if (RequestLen < ScratchBufSize) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | RequestLen = ScratchBufSize; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
| 66 | llvm::MemoryBuffer *Buf = |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>"); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 68 | FileID FID = SourceMgr.createFileIDForMemBuffer(Buf); |
| 69 | BufferStartLoc = SourceMgr.getLocForStartOfFile(FID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | CurBuffer = const_cast<char*>(Buf->getBufferStart()); |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 71 | BytesUsed = 1; |
| 72 | CurBuffer[0] = '0'; // Start out with a \0 for cleanliness. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | } |