blob: 695a5365faf7f4eb2ab8150411e0952bece19145 [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;
Reid Spencer5f016e22007-07-11 17:01:13 +000027}
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
Chris Lattner2b2453a2009-01-17 06:22:33 +000043 return BufferStartLoc.getFileLocWithOffset(BytesUsed-Len);
Reid Spencer5f016e22007-07-11 17:01:13 +000044}
45
46
47/// getToken - Splat the specified text into a temporary MemoryBuffer and
48/// return a SourceLocation that refers to the token. The SourceLoc value
49/// gives a virtual location that the token will appear to be from.
50SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
51 SourceLocation SourceLoc) {
52 // Map the physloc to the specified sourceloc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +000053 return SourceMgr.createInstantiationLoc(getToken(Buf, Len), SourceLoc, Len);
Reid Spencer5f016e22007-07-11 17:01:13 +000054}
55
56void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
57 // Only pay attention to the requested length if it is larger than our default
58 // page size. If it is, we allocate an entire chunk for it. This is to
59 // support gigantic tokens, which almost certainly won't happen. :)
60 if (RequestLen < ScratchBufSize)
61 RequestLen = ScratchBufSize;
62
63 llvm::MemoryBuffer *Buf =
64 llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
Chris Lattner2b2453a2009-01-17 06:22:33 +000065 FileID FID = SourceMgr.createFileIDForMemBuffer(Buf);
66 BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
Reid Spencer5f016e22007-07-11 17:01:13 +000067 CurBuffer = const_cast<char*>(Buf->getBufferStart());
68 BytesUsed = 0;
69}