blob: 12cb0965ce8a4fb176da300f3a5fc42d8ee74d89 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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"
17using namespace clang;
18
19// ScratchBufSize - The size of each chunk of scratch memory. Slightly less
20//than a page, almost certainly enough for anything. :)
21static const unsigned ScratchBufSize = 4060;
22
23ScratchBuffer::ScratchBuffer(SourceManager &SM) : SourceMgr(SM), CurBuffer(0) {
24 // Set BytesUsed so that the first call to getToken will require an alloc.
25 BytesUsed = ScratchBufSize;
26 FileID = 0;
27}
28
29/// getToken - Splat the specified text into a temporary MemoryBuffer and
30/// return a SourceLocation that refers to the token. This is just like the
31/// method below, but returns a location that indicates the physloc of the
32/// token.
33SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len) {
34 if (BytesUsed+Len > ScratchBufSize)
35 AllocScratchBuffer(Len);
36
37 // Copy the token data into the buffer.
38 memcpy(CurBuffer+BytesUsed, Buf, Len);
39
40 // Remember that we used these bytes.
41 BytesUsed += Len;
42
43 assert(BytesUsed-Len < (1 << SourceLocation::FilePosBits) &&
44 "Out of range file position!");
45
46 return SourceLocation(FileID, BytesUsed-Len);
47}
48
49
50/// getToken - Splat the specified text into a temporary MemoryBuffer and
51/// return a SourceLocation that refers to the token. The SourceLoc value
52/// gives a virtual location that the token will appear to be from.
53SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
54 SourceLocation SourceLoc) {
55 // Map the physloc to the specified sourceloc.
56 return SourceMgr.getInstantiationLoc(getToken(Buf, Len), SourceLoc);
57}
58
59void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
60 // Only pay attention to the requested length if it is larger than our default
61 // page size. If it is, we allocate an entire chunk for it. This is to
62 // support gigantic tokens, which almost certainly won't happen. :)
63 if (RequestLen < ScratchBufSize)
64 RequestLen = ScratchBufSize;
65
66 llvm::MemoryBuffer *Buf =
67 llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
68 FileID = SourceMgr.createFileIDForMemBuffer(Buf);
69 CurBuffer = const_cast<char*>(Buf->getBufferStart());
70 BytesUsed = 0;
71}