ScratchBuffer.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the ScratchBuffer interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Lex/ScratchBuffer.h"
  13. #include "clang/Basic/SourceManager.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #include <cstring>
  16. using namespace clang;
  17. // ScratchBufSize - The size of each chunk of scratch memory. Slightly less
  18. //than a page, almost certainly enough for anything. :)
  19. static const unsigned ScratchBufSize = 4060;
  20. ScratchBuffer::ScratchBuffer(SourceManager &SM)
  21. : SourceMgr(SM), CurBuffer(nullptr) {
  22. // Set BytesUsed so that the first call to getToken will require an alloc.
  23. BytesUsed = ScratchBufSize;
  24. }
  25. /// getToken - Splat the specified text into a temporary MemoryBuffer and
  26. /// return a SourceLocation that refers to the token. This is just like the
  27. /// method below, but returns a location that indicates the physloc of the
  28. /// token.
  29. SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
  30. const char *&DestPtr) {
  31. if (BytesUsed+Len+2 > ScratchBufSize)
  32. AllocScratchBuffer(Len+2);
  33. else {
  34. // Clear out the source line cache if it's already been computed.
  35. // FIXME: Allow this to be incrementally extended.
  36. SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc))
  37. .getFile()
  38. .getContentCache()
  39. .SourceLineCache = SrcMgr::LineOffsetMapping();
  40. }
  41. // Prefix the token with a \n, so that it looks like it is the first thing on
  42. // its own virtual line in caret diagnostics.
  43. CurBuffer[BytesUsed++] = '\n';
  44. // Return a pointer to the character data.
  45. DestPtr = CurBuffer+BytesUsed;
  46. // Copy the token data into the buffer.
  47. memcpy(CurBuffer+BytesUsed, Buf, Len);
  48. // Remember that we used these bytes.
  49. BytesUsed += Len+1;
  50. // Add a NUL terminator to the token. This keeps the tokens separated, in
  51. // case they get relexed, and puts them on their own virtual lines in case a
  52. // diagnostic points to one.
  53. CurBuffer[BytesUsed-1] = '\0';
  54. return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1);
  55. }
  56. void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
  57. // Only pay attention to the requested length if it is larger than our default
  58. // page size. If it is, we allocate an entire chunk for it. This is to
  59. // support gigantic tokens, which almost certainly won't happen. :)
  60. if (RequestLen < ScratchBufSize)
  61. RequestLen = ScratchBufSize;
  62. // Get scratch buffer. Zero-initialize it so it can be dumped into a PCH file
  63. // deterministically.
  64. std::unique_ptr<llvm::WritableMemoryBuffer> OwnBuf =
  65. llvm::WritableMemoryBuffer::getNewMemBuffer(RequestLen,
  66. "<scratch space>");
  67. CurBuffer = OwnBuf->getBufferStart();
  68. FileID FID = SourceMgr.createFileID(std::move(OwnBuf));
  69. BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
  70. BytesUsed = 0;
  71. }