1e5dd7070Spatrick //===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This file implements the ScratchBuffer interface.
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12e5dd7070Spatrick
13e5dd7070Spatrick #include "clang/Lex/ScratchBuffer.h"
14e5dd7070Spatrick #include "clang/Basic/SourceManager.h"
15e5dd7070Spatrick #include "llvm/Support/MemoryBuffer.h"
16e5dd7070Spatrick #include <cstring>
17e5dd7070Spatrick using namespace clang;
18e5dd7070Spatrick
19e5dd7070Spatrick // ScratchBufSize - The size of each chunk of scratch memory. Slightly less
20e5dd7070Spatrick //than a page, almost certainly enough for anything. :)
21e5dd7070Spatrick static const unsigned ScratchBufSize = 4060;
22e5dd7070Spatrick
ScratchBuffer(SourceManager & SM)23e5dd7070Spatrick ScratchBuffer::ScratchBuffer(SourceManager &SM)
24e5dd7070Spatrick : SourceMgr(SM), CurBuffer(nullptr) {
25e5dd7070Spatrick // Set BytesUsed so that the first call to getToken will require an alloc.
26e5dd7070Spatrick BytesUsed = ScratchBufSize;
27e5dd7070Spatrick }
28e5dd7070Spatrick
29e5dd7070Spatrick /// getToken - Splat the specified text into a temporary MemoryBuffer and
30e5dd7070Spatrick /// return a SourceLocation that refers to the token. This is just like the
31e5dd7070Spatrick /// method below, but returns a location that indicates the physloc of the
32e5dd7070Spatrick /// token.
getToken(const char * Buf,unsigned Len,const char * & DestPtr)33e5dd7070Spatrick SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
34e5dd7070Spatrick const char *&DestPtr) {
35e5dd7070Spatrick if (BytesUsed+Len+2 > ScratchBufSize)
36e5dd7070Spatrick AllocScratchBuffer(Len+2);
37e5dd7070Spatrick else {
38e5dd7070Spatrick // Clear out the source line cache if it's already been computed.
39e5dd7070Spatrick // FIXME: Allow this to be incrementally extended.
40e5dd7070Spatrick SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc))
41*a9ac8606Spatrick .getFile()
42*a9ac8606Spatrick .getContentCache()
43*a9ac8606Spatrick .SourceLineCache = SrcMgr::LineOffsetMapping();
44e5dd7070Spatrick }
45e5dd7070Spatrick
46e5dd7070Spatrick // Prefix the token with a \n, so that it looks like it is the first thing on
47e5dd7070Spatrick // its own virtual line in caret diagnostics.
48e5dd7070Spatrick CurBuffer[BytesUsed++] = '\n';
49e5dd7070Spatrick
50e5dd7070Spatrick // Return a pointer to the character data.
51e5dd7070Spatrick DestPtr = CurBuffer+BytesUsed;
52e5dd7070Spatrick
53e5dd7070Spatrick // Copy the token data into the buffer.
54e5dd7070Spatrick memcpy(CurBuffer+BytesUsed, Buf, Len);
55e5dd7070Spatrick
56e5dd7070Spatrick // Remember that we used these bytes.
57e5dd7070Spatrick BytesUsed += Len+1;
58e5dd7070Spatrick
59e5dd7070Spatrick // Add a NUL terminator to the token. This keeps the tokens separated, in
60e5dd7070Spatrick // case they get relexed, and puts them on their own virtual lines in case a
61e5dd7070Spatrick // diagnostic points to one.
62e5dd7070Spatrick CurBuffer[BytesUsed-1] = '\0';
63e5dd7070Spatrick
64e5dd7070Spatrick return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1);
65e5dd7070Spatrick }
66e5dd7070Spatrick
AllocScratchBuffer(unsigned RequestLen)67e5dd7070Spatrick void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
68e5dd7070Spatrick // Only pay attention to the requested length if it is larger than our default
69e5dd7070Spatrick // page size. If it is, we allocate an entire chunk for it. This is to
70e5dd7070Spatrick // support gigantic tokens, which almost certainly won't happen. :)
71e5dd7070Spatrick if (RequestLen < ScratchBufSize)
72e5dd7070Spatrick RequestLen = ScratchBufSize;
73e5dd7070Spatrick
74e5dd7070Spatrick // Get scratch buffer. Zero-initialize it so it can be dumped into a PCH file
75e5dd7070Spatrick // deterministically.
76e5dd7070Spatrick std::unique_ptr<llvm::WritableMemoryBuffer> OwnBuf =
77e5dd7070Spatrick llvm::WritableMemoryBuffer::getNewMemBuffer(RequestLen,
78e5dd7070Spatrick "<scratch space>");
79e5dd7070Spatrick CurBuffer = OwnBuf->getBufferStart();
80e5dd7070Spatrick FileID FID = SourceMgr.createFileID(std::move(OwnBuf));
81e5dd7070Spatrick BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
82e5dd7070Spatrick BytesUsed = 0;
83e5dd7070Spatrick }
84