summaryrefslogtreecommitdiffstats
path: root/clang/Lex/ScratchBuffer.cpp
blob: 49d4e00dbbe51e3f4c638ef2bd0fc90e8ecabf10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file implements the ScratchBuffer interface.
//
//===----------------------------------------------------------------------===//

#include "clang/Lex/ScratchBuffer.h"
#include "clang/Basic/SourceBuffer.h"
#include "clang/Basic/SourceManager.h"
using namespace llvm;
using namespace clang;

// ScratchBufSize - The size of each chunk of scratch memory.  Slightly less
//than a page, almost certainly enough for anything. :)
static const unsigned ScratchBufSize = 4060;

ScratchBuffer::ScratchBuffer(SourceManager &SM) : SourceMgr(SM), CurBuffer(0) {
  // Set BytesUsed so that the first call to getToken will require an alloc.
  BytesUsed = ScratchBufSize;
  FileID = 0;
}

/// getToken - Splat the specified text into a temporary SourceBuffer and
/// return a SourceLocation that refers to the token.  This is just like the
/// method below, but returns a location that indicates the physloc of the
/// token.
SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len) {
  if (BytesUsed+Len > ScratchBufSize)
    AllocScratchBuffer(Len);
  
  // Copy the token data into the buffer.
  memcpy(CurBuffer+BytesUsed, Buf, Len);

  // Remember that we used these bytes.
  BytesUsed += Len;

  assert(BytesUsed-Len < (1 << SourceLocation::FilePosBits) &&
         "Out of range file position!");
  
  return SourceLocation(FileID, BytesUsed-Len);
}


/// getToken - Splat the specified text into a temporary SourceBuffer and
/// return a SourceLocation that refers to the token.  The SourceLoc value
/// gives a virtual location that the token will appear to be from.
SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
                                       SourceLocation SourceLoc) {
  SourceLocation PhysLoc = getToken(Buf, Len);

  // Map the physloc to the specified sourceloc.
  unsigned InstantiationFileID =
    SourceMgr.createFileIDForMacroExp(SourceLoc, PhysLoc.getFileID());
  return SourceLocation(InstantiationFileID, PhysLoc.getRawFilePos());
}

void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
  // Only pay attention to the requested length if it is larger than our default
  // page size.  If it is, we allocate an entire chunk for it.  This is to
  // support gigantic tokens, which almost certainly won't happen. :)
  if (RequestLen < ScratchBufSize)
    RequestLen = ScratchBufSize;
  
  SourceBuffer *Buf = 
    SourceBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
  FileID = SourceMgr.createFileIDForMemBuffer(Buf);
  CurBuffer = const_cast<char*>(Buf->getBufferStart());
  BytesUsed = 0;
}
OpenPOWER on IntegriCloud