clang API Documentation
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) 00025 : SourceMgr(SM), CurBuffer(nullptr) { 00026 // Set BytesUsed so that the first call to getToken will require an alloc. 00027 BytesUsed = ScratchBufSize; 00028 } 00029 00030 /// getToken - Splat the specified text into a temporary MemoryBuffer and 00031 /// return a SourceLocation that refers to the token. This is just like the 00032 /// method below, but returns a location that indicates the physloc of the 00033 /// token. 00034 SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len, 00035 const char *&DestPtr) { 00036 if (BytesUsed+Len+2 > ScratchBufSize) 00037 AllocScratchBuffer(Len+2); 00038 00039 // Prefix the token with a \n, so that it looks like it is the first thing on 00040 // its own virtual line in caret diagnostics. 00041 CurBuffer[BytesUsed++] = '\n'; 00042 00043 // Return a pointer to the character data. 00044 DestPtr = CurBuffer+BytesUsed; 00045 00046 // Copy the token data into the buffer. 00047 memcpy(CurBuffer+BytesUsed, Buf, Len); 00048 00049 // Remember that we used these bytes. 00050 BytesUsed += Len+1; 00051 00052 // Add a NUL terminator to the token. This keeps the tokens separated, in 00053 // case they get relexed, and puts them on their own virtual lines in case a 00054 // diagnostic points to one. 00055 CurBuffer[BytesUsed-1] = '\0'; 00056 00057 return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1); 00058 } 00059 00060 void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) { 00061 // Only pay attention to the requested length if it is larger than our default 00062 // page size. If it is, we allocate an entire chunk for it. This is to 00063 // support gigantic tokens, which almost certainly won't happen. :) 00064 if (RequestLen < ScratchBufSize) 00065 RequestLen = ScratchBufSize; 00066 00067 std::unique_ptr<llvm::MemoryBuffer> OwnBuf = 00068 llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>"); 00069 llvm::MemoryBuffer &Buf = *OwnBuf; 00070 FileID FID = SourceMgr.createFileID(std::move(OwnBuf)); 00071 BufferStartLoc = SourceMgr.getLocForStartOfFile(FID); 00072 CurBuffer = const_cast<char*>(Buf.getBufferStart()); 00073 BytesUsed = 1; 00074 CurBuffer[0] = '0'; // Start out with a \0 for cleanliness. 00075 }