Chris Lattner | 3690f15 | 2006-06-28 06:48:36 +0000 | [diff] [blame] | 1 | //===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | 3690f15 | 2006-06-28 06:48:36 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ScratchBuffer interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/ScratchBuffer.h" |
Chris Lattner | 3690f15 | 2006-06-28 06:48:36 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 16 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 0d799d3 | 2008-03-10 17:04:53 +0000 | [diff] [blame] | 17 | #include <cstring> |
Chris Lattner | 3690f15 | 2006-06-28 06:48:36 +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 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 24 | ScratchBuffer::ScratchBuffer(SourceManager &SM) |
| 25 | : SourceMgr(SM), CurBuffer(nullptr) { |
Chris Lattner | 3690f15 | 2006-06-28 06:48:36 +0000 | [diff] [blame] | 26 | // Set BytesUsed so that the first call to getToken will require an alloc. |
| 27 | BytesUsed = ScratchBufSize; |
Chris Lattner | 3690f15 | 2006-06-28 06:48:36 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 30 | /// getToken - Splat the specified text into a temporary MemoryBuffer and |
Chris Lattner | 098dfc5 | 2006-06-30 06:09:36 +0000 | [diff] [blame] | 31 | /// return a SourceLocation that refers to the token. This is just like the |
| 32 | /// method below, but returns a location that indicates the physloc of the |
| 33 | /// token. |
Chris Lattner | 5a7971e | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 34 | SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len, |
| 35 | const char *&DestPtr) { |
Chris Lattner | 7253991 | 2009-03-08 08:16:41 +0000 | [diff] [blame] | 36 | if (BytesUsed+Len+2 > ScratchBufSize) |
| 37 | AllocScratchBuffer(Len+2); |
| 38 | |
| 39 | // Prefix the token with a \n, so that it looks like it is the first thing on |
| 40 | // its own virtual line in caret diagnostics. |
| 41 | CurBuffer[BytesUsed++] = '\n'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 5a7971e | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 43 | // Return a pointer to the character data. |
| 44 | DestPtr = CurBuffer+BytesUsed; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 098dfc5 | 2006-06-30 06:09:36 +0000 | [diff] [blame] | 46 | // Copy the token data into the buffer. |
| 47 | memcpy(CurBuffer+BytesUsed, Buf, Len); |
| 48 | |
| 49 | // Remember that we used these bytes. |
Chris Lattner | fa217bd | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 50 | BytesUsed += Len+1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Chris Lattner | fa217bd | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 52 | // Add a NUL terminator to the token. This keeps the tokens separated, in |
| 53 | // case they get relexed, and puts them on their own virtual lines in case a |
| 54 | // diagnostic points to one. |
| 55 | CurBuffer[BytesUsed-1] = '\0'; |
Chris Lattner | 098dfc5 | 2006-06-30 06:09:36 +0000 | [diff] [blame] | 56 | |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 57 | return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1); |
Chris Lattner | 098dfc5 | 2006-06-30 06:09:36 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | 3690f15 | 2006-06-28 06:48:36 +0000 | [diff] [blame] | 60 | void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) { |
| 61 | // Only pay attention to the requested length if it is larger than our default |
| 62 | // page size. If it is, we allocate an entire chunk for it. This is to |
| 63 | // support gigantic tokens, which almost certainly won't happen. :) |
Chris Lattner | 7253991 | 2009-03-08 08:16:41 +0000 | [diff] [blame] | 64 | if (RequestLen < ScratchBufSize) |
Chris Lattner | 3690f15 | 2006-06-28 06:48:36 +0000 | [diff] [blame] | 65 | RequestLen = ScratchBufSize; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Benjamin Kramer | 0407257 | 2015-04-06 20:01:49 +0000 | [diff] [blame] | 67 | // Get scratch buffer. Zero-initialize it so it can be dumped into a PCH file |
| 68 | // deterministically. |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 69 | std::unique_ptr<llvm::MemoryBuffer> OwnBuf = |
Benjamin Kramer | 0407257 | 2015-04-06 20:01:49 +0000 | [diff] [blame] | 70 | llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>"); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 71 | llvm::MemoryBuffer &Buf = *OwnBuf; |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 72 | FileID FID = SourceMgr.createFileID(std::move(OwnBuf)); |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 73 | BufferStartLoc = SourceMgr.getLocForStartOfFile(FID); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 74 | CurBuffer = const_cast<char*>(Buf.getBufferStart()); |
Benjamin Kramer | 0407257 | 2015-04-06 20:01:49 +0000 | [diff] [blame] | 75 | BytesUsed = 0; |
Chris Lattner | 3690f15 | 2006-06-28 06:48:36 +0000 | [diff] [blame] | 76 | } |