2006-06-28 14:48:36 +08:00
|
|
|
//===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-28 14:48:36 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the ScratchBuffer interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/ScratchBuffer.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2007-04-29 15:12:06 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2008-03-11 01:04:53 +08:00
|
|
|
#include <cstring>
|
2006-06-28 14:48:36 +08:00
|
|
|
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;
|
|
|
|
|
2014-05-18 07:10:59 +08:00
|
|
|
ScratchBuffer::ScratchBuffer(SourceManager &SM)
|
|
|
|
: SourceMgr(SM), CurBuffer(nullptr) {
|
2006-06-28 14:48:36 +08:00
|
|
|
// Set BytesUsed so that the first call to getToken will require an alloc.
|
|
|
|
BytesUsed = ScratchBufSize;
|
|
|
|
}
|
|
|
|
|
2007-04-29 15:12:06 +08:00
|
|
|
/// getToken - Splat the specified text into a temporary MemoryBuffer and
|
2006-06-30 14:09:36 +08:00
|
|
|
/// 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.
|
2009-01-27 03:29:26 +08:00
|
|
|
SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
|
|
|
|
const char *&DestPtr) {
|
2009-03-08 16:16:41 +08:00
|
|
|
if (BytesUsed+Len+2 > ScratchBufSize)
|
|
|
|
AllocScratchBuffer(Len+2);
|
2017-07-30 14:31:29 +08:00
|
|
|
else {
|
|
|
|
// Clear out the source line cache if it's already been computed.
|
|
|
|
// FIXME: Allow this to be incrementally extended.
|
|
|
|
auto *ContentCache = const_cast<SrcMgr::ContentCache *>(
|
|
|
|
SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc))
|
|
|
|
.getFile().getContentCache());
|
|
|
|
ContentCache->SourceLineCache = nullptr;
|
|
|
|
}
|
2009-03-08 16:16:41 +08:00
|
|
|
|
|
|
|
// Prefix the token with a \n, so that it looks like it is the first thing on
|
|
|
|
// its own virtual line in caret diagnostics.
|
|
|
|
CurBuffer[BytesUsed++] = '\n';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 03:29:26 +08:00
|
|
|
// Return a pointer to the character data.
|
|
|
|
DestPtr = CurBuffer+BytesUsed;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-28 14:48:36 +08:00
|
|
|
// Copy the token data into the buffer.
|
|
|
|
memcpy(CurBuffer+BytesUsed, Buf, Len);
|
|
|
|
|
|
|
|
// Remember that we used these bytes.
|
2009-03-08 16:08:45 +08:00
|
|
|
BytesUsed += Len+1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-08 16:08:45 +08:00
|
|
|
// Add a NUL terminator to the token. This keeps the tokens separated, in
|
|
|
|
// case they get relexed, and puts them on their own virtual lines in case a
|
|
|
|
// diagnostic points to one.
|
|
|
|
CurBuffer[BytesUsed-1] = '\0';
|
2006-06-30 14:09:36 +08:00
|
|
|
|
2011-09-20 04:40:19 +08:00
|
|
|
return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1);
|
2006-06-30 14:09:36 +08:00
|
|
|
}
|
|
|
|
|
2006-06-28 14:48:36 +08:00
|
|
|
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. :)
|
2009-03-08 16:16:41 +08:00
|
|
|
if (RequestLen < ScratchBufSize)
|
2006-06-28 14:48:36 +08:00
|
|
|
RequestLen = ScratchBufSize;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-04-07 04:01:49 +08:00
|
|
|
// Get scratch buffer. Zero-initialize it so it can be dumped into a PCH file
|
|
|
|
// deterministically.
|
2018-01-11 18:43:45 +08:00
|
|
|
std::unique_ptr<llvm::WritableMemoryBuffer> OwnBuf =
|
|
|
|
llvm::WritableMemoryBuffer::getNewMemBuffer(RequestLen,
|
|
|
|
"<scratch space>");
|
|
|
|
CurBuffer = OwnBuf->getBufferStart();
|
2014-08-29 15:59:55 +08:00
|
|
|
FileID FID = SourceMgr.createFileID(std::move(OwnBuf));
|
2009-01-17 14:22:33 +08:00
|
|
|
BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
|
2015-04-07 04:01:49 +08:00
|
|
|
BytesUsed = 0;
|
2006-06-28 14:48:36 +08:00
|
|
|
}
|