blob: 49d4e00dbbe51e3f4c638ef2bd0fc90e8ecabf10 [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//
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/SourceBuffer.h"
16#include "clang/Basic/SourceManager.h"
17using namespace llvm;
18using 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
Chris Lattner098dfc52006-06-30 06:09:36 +000030/// getToken - Splat the specified text into a temporary SourceBuffer 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
47 return SourceLocation(FileID, BytesUsed-Len);
48}
49
Chris Lattner3690f152006-06-28 06:48:36 +000050
51/// getToken - Splat the specified text into a temporary SourceBuffer 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) {
Chris Lattner098dfc52006-06-30 06:09:36 +000056 SourceLocation PhysLoc = getToken(Buf, Len);
Chris Lattner3690f152006-06-28 06:48:36 +000057
Chris Lattner098dfc52006-06-30 06:09:36 +000058 // Map the physloc to the specified sourceloc.
Chris Lattner4fb517b2006-06-29 06:34:53 +000059 unsigned InstantiationFileID =
Chris Lattner098dfc52006-06-30 06:09:36 +000060 SourceMgr.createFileIDForMacroExp(SourceLoc, PhysLoc.getFileID());
61 return SourceLocation(InstantiationFileID, PhysLoc.getRawFilePos());
Chris Lattner3690f152006-06-28 06:48:36 +000062}
63
64void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
65 // Only pay attention to the requested length if it is larger than our default
66 // page size. If it is, we allocate an entire chunk for it. This is to
67 // support gigantic tokens, which almost certainly won't happen. :)
68 if (RequestLen < ScratchBufSize)
69 RequestLen = ScratchBufSize;
70
71 SourceBuffer *Buf =
72 SourceBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
73 FileID = SourceMgr.createFileIDForMemBuffer(Buf);
74 CurBuffer = const_cast<char*>(Buf->getBufferStart());
75 BytesUsed = 0;
76}