1 //===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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"
17 #include <cstring>
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 
ScratchBuffer(SourceManager & SM)24 ScratchBuffer::ScratchBuffer(SourceManager &SM)
25     : SourceMgr(SM), CurBuffer(nullptr) {
26   // Set BytesUsed so that the first call to getToken will require an alloc.
27   BytesUsed = ScratchBufSize;
28 }
29 
30 /// getToken - Splat the specified text into a temporary MemoryBuffer and
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.
getToken(const char * Buf,unsigned Len,const char * & DestPtr)34 SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
35                                        const char *&DestPtr) {
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';
42 
43   // Return a pointer to the character data.
44   DestPtr = CurBuffer+BytesUsed;
45 
46   // Copy the token data into the buffer.
47   memcpy(CurBuffer+BytesUsed, Buf, Len);
48 
49   // Remember that we used these bytes.
50   BytesUsed += Len+1;
51 
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';
56 
57   return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1);
58 }
59 
AllocScratchBuffer(unsigned RequestLen)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. :)
64   if (RequestLen < ScratchBufSize)
65     RequestLen = ScratchBufSize;
66 
67   std::unique_ptr<llvm::MemoryBuffer> OwnBuf =
68       llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
69   llvm::MemoryBuffer &Buf = *OwnBuf;
70   FileID FID = SourceMgr.createFileID(std::move(OwnBuf));
71   BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
72   CurBuffer = const_cast<char*>(Buf.getBufferStart());
73   BytesUsed = 1;
74   CurBuffer[0] = '0';  // Start out with a \0 for cleanliness.
75 }
76