blob: f779d3cd26554122038a225f8c94cc749e5459f8 [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
Craig Topperd2d442c2014-05-17 23:10:59 +000024ScratchBuffer::ScratchBuffer(SourceManager &SM)
25 : SourceMgr(SM), CurBuffer(nullptr) {
Chris Lattner3690f152006-06-28 06:48:36 +000026 // Set BytesUsed so that the first call to getToken will require an alloc.
27 BytesUsed = ScratchBufSize;
Chris Lattner3690f152006-06-28 06:48:36 +000028}
29
Chris Lattner739e7392007-04-29 07:12:06 +000030/// getToken - Splat the specified text into a temporary MemoryBuffer and
Chris Lattner098dfc52006-06-30 06:09:36 +000031/// 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.
Chris Lattner5a7971e2009-01-26 19:29:26 +000034SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
35 const char *&DestPtr) {
Chris Lattner72539912009-03-08 08:16:41 +000036 if (BytesUsed+Len+2 > ScratchBufSize)
37 AllocScratchBuffer(Len+2);
38
39 // Prefix the token with a \n, so that it looks like it is the first thing on
40 // its own virtual line in caret diagnostics.
41 CurBuffer[BytesUsed++] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +000042
Chris Lattner5a7971e2009-01-26 19:29:26 +000043 // Return a pointer to the character data.
44 DestPtr = CurBuffer+BytesUsed;
Mike Stump11289f42009-09-09 15:08:12 +000045
Chris Lattner098dfc52006-06-30 06:09:36 +000046 // Copy the token data into the buffer.
47 memcpy(CurBuffer+BytesUsed, Buf, Len);
48
49 // Remember that we used these bytes.
Chris Lattnerfa217bd2009-03-08 08:08:45 +000050 BytesUsed += Len+1;
Mike Stump11289f42009-09-09 15:08:12 +000051
Chris Lattnerfa217bd2009-03-08 08:08:45 +000052 // Add a NUL terminator to the token. This keeps the tokens separated, in
53 // case they get relexed, and puts them on their own virtual lines in case a
54 // diagnostic points to one.
55 CurBuffer[BytesUsed-1] = '\0';
Chris Lattner098dfc52006-06-30 06:09:36 +000056
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000057 return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1);
Chris Lattner098dfc52006-06-30 06:09:36 +000058}
59
Chris Lattner3690f152006-06-28 06:48:36 +000060void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
61 // Only pay attention to the requested length if it is larger than our default
62 // page size. If it is, we allocate an entire chunk for it. This is to
63 // support gigantic tokens, which almost certainly won't happen. :)
Chris Lattner72539912009-03-08 08:16:41 +000064 if (RequestLen < ScratchBufSize)
Chris Lattner3690f152006-06-28 06:48:36 +000065 RequestLen = ScratchBufSize;
Mike Stump11289f42009-09-09 15:08:12 +000066
Rafael Espindolad87f8d72014-08-27 20:03:29 +000067 std::unique_ptr<llvm::MemoryBuffer> OwnBuf =
68 llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
69 llvm::MemoryBuffer &Buf = *OwnBuf;
70 FileID FID = SourceMgr.createFileID(OwnBuf.release());
Chris Lattnerd32480d2009-01-17 06:22:33 +000071 BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
Rafael Espindolad87f8d72014-08-27 20:03:29 +000072 CurBuffer = const_cast<char*>(Buf.getBufferStart());
Chris Lattnerfa217bd2009-03-08 08:08:45 +000073 BytesUsed = 1;
74 CurBuffer[0] = '0'; // Start out with a \0 for cleanliness.
Chris Lattner3690f152006-06-28 06:48:36 +000075}