blob: ec07a71baf6ea6d14b2860f9d18435d7af7fa844 [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
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;
Chris Lattner3690f152006-06-28 06:48:36 +000027}
28
Chris Lattner739e7392007-04-29 07:12:06 +000029/// getToken - Splat the specified text into a temporary MemoryBuffer and
Chris Lattner098dfc52006-06-30 06:09:36 +000030/// 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
Chris Lattnerd32480d2009-01-17 06:22:33 +000046 return BufferStartLoc.getFileLocWithOffset(BytesUsed-Len);
Chris Lattner098dfc52006-06-30 06:09:36 +000047}
48
Chris Lattner3690f152006-06-28 06:48:36 +000049
Chris Lattner739e7392007-04-29 07:12:06 +000050/// getToken - Splat the specified text into a temporary MemoryBuffer and
Chris Lattner3690f152006-06-28 06:48:36 +000051/// 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) {
Chris Lattner098dfc52006-06-30 06:09:36 +000055 // Map the physloc to the specified sourceloc.
Chris Lattner7d1b0062006-06-30 06:14:45 +000056 return SourceMgr.getInstantiationLoc(getToken(Buf, Len), SourceLoc);
Chris Lattner3690f152006-06-28 06:48:36 +000057}
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
Chris Lattner23b7eb62007-06-15 23:05:46 +000066 llvm::MemoryBuffer *Buf =
67 llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
Chris Lattnerd32480d2009-01-17 06:22:33 +000068 FileID FID = SourceMgr.createFileIDForMemBuffer(Buf);
69 BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
Chris Lattner3690f152006-06-28 06:48:36 +000070 CurBuffer = const_cast<char*>(Buf->getBufferStart());
71 BytesUsed = 0;
72}