xref: /freebsd-src/contrib/llvm-project/clang/lib/Lex/ScratchBuffer.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
10b57cec5SDimitry Andric //===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric //  This file implements the ScratchBuffer interface.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "clang/Lex/ScratchBuffer.h"
140b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h"
150b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
160b57cec5SDimitry Andric #include <cstring>
170b57cec5SDimitry Andric using namespace clang;
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric // ScratchBufSize - The size of each chunk of scratch memory.  Slightly less
200b57cec5SDimitry Andric //than a page, almost certainly enough for anything. :)
210b57cec5SDimitry Andric static const unsigned ScratchBufSize = 4060;
220b57cec5SDimitry Andric 
ScratchBuffer(SourceManager & SM)230b57cec5SDimitry Andric ScratchBuffer::ScratchBuffer(SourceManager &SM)
240b57cec5SDimitry Andric     : SourceMgr(SM), CurBuffer(nullptr) {
250b57cec5SDimitry Andric   // Set BytesUsed so that the first call to getToken will require an alloc.
260b57cec5SDimitry Andric   BytesUsed = ScratchBufSize;
270b57cec5SDimitry Andric }
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric /// getToken - Splat the specified text into a temporary MemoryBuffer and
300b57cec5SDimitry Andric /// return a SourceLocation that refers to the token.  This is just like the
310b57cec5SDimitry Andric /// method below, but returns a location that indicates the physloc of the
320b57cec5SDimitry Andric /// token.
getToken(const char * Buf,unsigned Len,const char * & DestPtr)330b57cec5SDimitry Andric SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
340b57cec5SDimitry Andric                                        const char *&DestPtr) {
350b57cec5SDimitry Andric   if (BytesUsed+Len+2 > ScratchBufSize)
360b57cec5SDimitry Andric     AllocScratchBuffer(Len+2);
370b57cec5SDimitry Andric   else {
380b57cec5SDimitry Andric     // Clear out the source line cache if it's already been computed.
390b57cec5SDimitry Andric     // FIXME: Allow this to be incrementally extended.
400b57cec5SDimitry Andric     SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc))
41*e8d8bef9SDimitry Andric         .getFile()
42*e8d8bef9SDimitry Andric         .getContentCache()
43*e8d8bef9SDimitry Andric         .SourceLineCache = SrcMgr::LineOffsetMapping();
440b57cec5SDimitry Andric   }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   // Prefix the token with a \n, so that it looks like it is the first thing on
470b57cec5SDimitry Andric   // its own virtual line in caret diagnostics.
480b57cec5SDimitry Andric   CurBuffer[BytesUsed++] = '\n';
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   // Return a pointer to the character data.
510b57cec5SDimitry Andric   DestPtr = CurBuffer+BytesUsed;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   // Copy the token data into the buffer.
540b57cec5SDimitry Andric   memcpy(CurBuffer+BytesUsed, Buf, Len);
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   // Remember that we used these bytes.
570b57cec5SDimitry Andric   BytesUsed += Len+1;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   // Add a NUL terminator to the token.  This keeps the tokens separated, in
600b57cec5SDimitry Andric   // case they get relexed, and puts them on their own virtual lines in case a
610b57cec5SDimitry Andric   // diagnostic points to one.
620b57cec5SDimitry Andric   CurBuffer[BytesUsed-1] = '\0';
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1);
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
AllocScratchBuffer(unsigned RequestLen)670b57cec5SDimitry Andric void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
680b57cec5SDimitry Andric   // Only pay attention to the requested length if it is larger than our default
690b57cec5SDimitry Andric   // page size.  If it is, we allocate an entire chunk for it.  This is to
700b57cec5SDimitry Andric   // support gigantic tokens, which almost certainly won't happen. :)
710b57cec5SDimitry Andric   if (RequestLen < ScratchBufSize)
720b57cec5SDimitry Andric     RequestLen = ScratchBufSize;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   // Get scratch buffer. Zero-initialize it so it can be dumped into a PCH file
750b57cec5SDimitry Andric   // deterministically.
760b57cec5SDimitry Andric   std::unique_ptr<llvm::WritableMemoryBuffer> OwnBuf =
770b57cec5SDimitry Andric       llvm::WritableMemoryBuffer::getNewMemBuffer(RequestLen,
780b57cec5SDimitry Andric                                                   "<scratch space>");
790b57cec5SDimitry Andric   CurBuffer = OwnBuf->getBufferStart();
800b57cec5SDimitry Andric   FileID FID = SourceMgr.createFileID(std::move(OwnBuf));
810b57cec5SDimitry Andric   BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
820b57cec5SDimitry Andric   BytesUsed = 0;
830b57cec5SDimitry Andric }
84