ScratchBuffer.cpp 3.2 KB

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