annotate clang/lib/Lex/ScratchBuffer.cpp @ 176:de4ac79aef9d

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