ScratchBuffer.cpp revision 2b2453a7d8fe732561795431f39ceb2b2a832d84
1//===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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"
17#include <cstring>
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}
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 BufferStartLoc.getFileLocWithOffset(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 FID = SourceMgr.createFileIDForMemBuffer(Buf);
69  BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
70  CurBuffer = const_cast<char*>(Buf->getBufferStart());
71  BytesUsed = 0;
72}
73