blob: dc03e16daa8b66b70e9369b7a661d0f230f77b2e [file] [log] [blame]
Chris Lattner3690f152006-06-28 06:48:36 +00001//===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner3690f152006-06-28 06:48:36 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ScratchBuffer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/ScratchBuffer.h"
Chris Lattner3690f152006-06-28 06:48:36 +000015#include "clang/Basic/SourceManager.h"
Chris Lattner739e7392007-04-29 07:12:06 +000016#include "llvm/Support/MemoryBuffer.h"
Chris Lattner0d799d32008-03-10 17:04:53 +000017#include <cstring>
Chris Lattner3690f152006-06-28 06:48:36 +000018using namespace clang;
19
20// ScratchBufSize - The size of each chunk of scratch memory. Slightly less
21//than a page, almost certainly enough for anything. :)
22static const unsigned ScratchBufSize = 4060;
23
Craig Topperd2d442c2014-05-17 23:10:59 +000024ScratchBuffer::ScratchBuffer(SourceManager &SM)
25 : SourceMgr(SM), CurBuffer(nullptr) {
Chris Lattner3690f152006-06-28 06:48:36 +000026 // Set BytesUsed so that the first call to getToken will require an alloc.
27 BytesUsed = ScratchBufSize;
Chris Lattner3690f152006-06-28 06:48:36 +000028}
29
Chris Lattner739e7392007-04-29 07:12:06 +000030/// getToken - Splat the specified text into a temporary MemoryBuffer and
Chris Lattner098dfc52006-06-30 06:09:36 +000031/// 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 Lattner5a7971e2009-01-26 19:29:26 +000034SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
35 const char *&DestPtr) {
Chris Lattner72539912009-03-08 08:16:41 +000036 if (BytesUsed+Len+2 > ScratchBufSize)
37 AllocScratchBuffer(Len+2);
Richard Smithfb5afbd2017-07-30 06:31:29 +000038 else {
39 // Clear out the source line cache if it's already been computed.
40 // FIXME: Allow this to be incrementally extended.
41 auto *ContentCache = const_cast<SrcMgr::ContentCache *>(
42 SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc))
43 .getFile().getContentCache());
44 ContentCache->SourceLineCache = nullptr;
45 }
Chris Lattner72539912009-03-08 08:16:41 +000046
47 // Prefix the token with a \n, so that it looks like it is the first thing on
48 // its own virtual line in caret diagnostics.
49 CurBuffer[BytesUsed++] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +000050
Chris Lattner5a7971e2009-01-26 19:29:26 +000051 // Return a pointer to the character data.
52 DestPtr = CurBuffer+BytesUsed;
Mike Stump11289f42009-09-09 15:08:12 +000053
Chris Lattner098dfc52006-06-30 06:09:36 +000054 // Copy the token data into the buffer.
55 memcpy(CurBuffer+BytesUsed, Buf, Len);
56
57 // Remember that we used these bytes.
Chris Lattnerfa217bd2009-03-08 08:08:45 +000058 BytesUsed += Len+1;
Mike Stump11289f42009-09-09 15:08:12 +000059
Chris Lattnerfa217bd2009-03-08 08:08:45 +000060 // Add a NUL terminator to the token. This keeps the tokens separated, in
61 // case they get relexed, and puts them on their own virtual lines in case a
62 // diagnostic points to one.
63 CurBuffer[BytesUsed-1] = '\0';
Chris Lattner098dfc52006-06-30 06:09:36 +000064
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000065 return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1);
Chris Lattner098dfc52006-06-30 06:09:36 +000066}
67
Chris Lattner3690f152006-06-28 06:48:36 +000068void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
69 // Only pay attention to the requested length if it is larger than our default
70 // page size. If it is, we allocate an entire chunk for it. This is to
71 // support gigantic tokens, which almost certainly won't happen. :)
Chris Lattner72539912009-03-08 08:16:41 +000072 if (RequestLen < ScratchBufSize)
Chris Lattner3690f152006-06-28 06:48:36 +000073 RequestLen = ScratchBufSize;
Mike Stump11289f42009-09-09 15:08:12 +000074
Benjamin Kramer04072572015-04-06 20:01:49 +000075 // Get scratch buffer. Zero-initialize it so it can be dumped into a PCH file
76 // deterministically.
Pavel Labathda454392018-01-11 10:43:45 +000077 std::unique_ptr<llvm::WritableMemoryBuffer> OwnBuf =
78 llvm::WritableMemoryBuffer::getNewMemBuffer(RequestLen,
79 "<scratch space>");
80 CurBuffer = OwnBuf->getBufferStart();
David Blaikie50a5f972014-08-29 07:59:55 +000081 FileID FID = SourceMgr.createFileID(std::move(OwnBuf));
Chris Lattnerd32480d2009-01-17 06:22:33 +000082 BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
Benjamin Kramer04072572015-04-06 20:01:49 +000083 BytesUsed = 0;
Chris Lattner3690f152006-06-28 06:48:36 +000084}