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