blob: 99fbdf75654aad0c6db7804f33dc31da3526ef05 [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//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
Chris Lattner87cf5ac2008-03-10 17:04:53 +000017#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +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
24ScratchBuffer::ScratchBuffer(SourceManager &SM) : SourceMgr(SM), CurBuffer(0) {
25 // Set BytesUsed so that the first call to getToken will require an alloc.
26 BytesUsed = ScratchBufSize;
27 FileID = 0;
28}
29
30/// getToken - Splat the specified text into a temporary MemoryBuffer and
31/// 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.
34SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len) {
35 if (BytesUsed+Len > ScratchBufSize)
36 AllocScratchBuffer(Len);
37
38 // Copy the token data into the buffer.
39 memcpy(CurBuffer+BytesUsed, Buf, Len);
40
41 // Remember that we used these bytes.
42 BytesUsed += Len;
43
44 assert(BytesUsed-Len < (1 << SourceLocation::FilePosBits) &&
45 "Out of range file position!");
46
Chris Lattner9dc1f532007-07-20 16:37:10 +000047 return SourceLocation::getFileLoc(FileID, BytesUsed-Len);
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
49
50
51/// getToken - Splat the specified text into a temporary MemoryBuffer and
52/// return a SourceLocation that refers to the token. The SourceLoc value
53/// gives a virtual location that the token will appear to be from.
54SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
55 SourceLocation SourceLoc) {
56 // Map the physloc to the specified sourceloc.
57 return SourceMgr.getInstantiationLoc(getToken(Buf, Len), SourceLoc);
58}
59
60void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
61 // Only pay attention to the requested length if it is larger than our default
62 // page size. If it is, we allocate an entire chunk for it. This is to
63 // support gigantic tokens, which almost certainly won't happen. :)
64 if (RequestLen < ScratchBufSize)
65 RequestLen = ScratchBufSize;
66
67 llvm::MemoryBuffer *Buf =
68 llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
69 FileID = SourceMgr.createFileIDForMemBuffer(Buf);
70 CurBuffer = const_cast<char*>(Buf->getBufferStart());
71 BytesUsed = 0;
72}