clang API Documentation

ScratchBuffer.cpp
Go to the documentation of this file.
00001 //===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements the ScratchBuffer interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Lex/ScratchBuffer.h"
00015 #include "clang/Basic/SourceManager.h"
00016 #include "llvm/Support/MemoryBuffer.h"
00017 #include <cstring>
00018 using namespace clang;
00019 
00020 // ScratchBufSize - The size of each chunk of scratch memory.  Slightly less
00021 //than a page, almost certainly enough for anything. :)
00022 static const unsigned ScratchBufSize = 4060;
00023 
00024 ScratchBuffer::ScratchBuffer(SourceManager &SM) : SourceMgr(SM), CurBuffer(0) {
00025   // Set BytesUsed so that the first call to getToken will require an alloc.
00026   BytesUsed = ScratchBufSize;
00027 }
00028 
00029 /// getToken - Splat the specified text into a temporary MemoryBuffer and
00030 /// return a SourceLocation that refers to the token.  This is just like the
00031 /// method below, but returns a location that indicates the physloc of the
00032 /// token.
00033 SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
00034                                        const char *&DestPtr) {
00035   if (BytesUsed+Len+2 > ScratchBufSize)
00036     AllocScratchBuffer(Len+2);
00037 
00038   // Prefix the token with a \n, so that it looks like it is the first thing on
00039   // its own virtual line in caret diagnostics.
00040   CurBuffer[BytesUsed++] = '\n';
00041 
00042   // Return a pointer to the character data.
00043   DestPtr = CurBuffer+BytesUsed;
00044 
00045   // Copy the token data into the buffer.
00046   memcpy(CurBuffer+BytesUsed, Buf, Len);
00047 
00048   // Remember that we used these bytes.
00049   BytesUsed += Len+1;
00050 
00051   // Add a NUL terminator to the token.  This keeps the tokens separated, in
00052   // case they get relexed, and puts them on their own virtual lines in case a
00053   // diagnostic points to one.
00054   CurBuffer[BytesUsed-1] = '\0';
00055 
00056   return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1);
00057 }
00058 
00059 void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
00060   // Only pay attention to the requested length if it is larger than our default
00061   // page size.  If it is, we allocate an entire chunk for it.  This is to
00062   // support gigantic tokens, which almost certainly won't happen. :)
00063   if (RequestLen < ScratchBufSize)
00064     RequestLen = ScratchBufSize;
00065 
00066   llvm::MemoryBuffer *Buf =
00067     llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
00068   FileID FID = SourceMgr.createFileIDForMemBuffer(Buf);
00069   BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
00070   CurBuffer = const_cast<char*>(Buf->getBufferStart());
00071   BytesUsed = 1;
00072   CurBuffer[0] = '0';  // Start out with a \0 for cleanliness.
00073 }