ScratchBuffer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // Prefix the token with a \n, so that it looks like it is the first thing on
  35. // its own virtual line in caret diagnostics.
  36. CurBuffer[BytesUsed++] = '\n';
  37. // Return a pointer to the character data.
  38. DestPtr = CurBuffer+BytesUsed;
  39. // Copy the token data into the buffer.
  40. memcpy(CurBuffer+BytesUsed, Buf, Len);
  41. // Remember that we used these bytes.
  42. BytesUsed += Len+1;
  43. // Add a NUL terminator to the token. This keeps the tokens separated, in
  44. // case they get relexed, and puts them on their own virtual lines in case a
  45. // diagnostic points to one.
  46. CurBuffer[BytesUsed-1] = '\0';
  47. return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1);
  48. }
  49. void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
  50. // Only pay attention to the requested length if it is larger than our default
  51. // page size. If it is, we allocate an entire chunk for it. This is to
  52. // support gigantic tokens, which almost certainly won't happen. :)
  53. if (RequestLen < ScratchBufSize)
  54. RequestLen = ScratchBufSize;
  55. std::unique_ptr<llvm::MemoryBuffer> OwnBuf =
  56. llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
  57. llvm::MemoryBuffer &Buf = *OwnBuf;
  58. FileID FID = SourceMgr.createFileID(std::move(OwnBuf));
  59. BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
  60. CurBuffer = const_cast<char*>(Buf.getBufferStart());
  61. BytesUsed = 1;
  62. CurBuffer[0] = '0'; // Start out with a \0 for cleanliness.
  63. }