Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Lex/ScratchBuffer.cpp
Line
Count
Source
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
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
19
// ScratchBufSize - The size of each chunk of scratch memory.  Slightly less
20
//than a page, almost certainly enough for anything. :)
21
static const unsigned ScratchBufSize = 4060;
22
23
ScratchBuffer::ScratchBuffer(SourceManager &SM)
24
44.2k
    : SourceMgr(SM), CurBuffer(nullptr) {
25
44.2k
  // Set BytesUsed so that the first call to getToken will require an alloc.
26
44.2k
  BytesUsed = ScratchBufSize;
27
44.2k
}
28
29
/// getToken - Splat the specified text into a temporary MemoryBuffer and
30
/// return a SourceLocation that refers to the token.  This is just like the
31
/// method below, but returns a location that indicates the physloc of the
32
/// token.
33
SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
34
3.89M
                                       const char *&DestPtr) {
35
3.89M
  if (BytesUsed+Len+2 > ScratchBufSize)
36
20.5k
    AllocScratchBuffer(Len+2);
37
3.87M
  else {
38
3.87M
    // Clear out the source line cache if it's already been computed.
39
3.87M
    // FIXME: Allow this to be incrementally extended.
40
3.87M
    auto *ContentCache = const_cast<SrcMgr::ContentCache *>(
41
3.87M
        SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc))
42
3.87M
                 .getFile().getContentCache());
43
3.87M
    ContentCache->SourceLineCache = nullptr;
44
3.87M
  }
45
3.89M
46
3.89M
  // Prefix the token with a \n, so that it looks like it is the first thing on
47
3.89M
  // its own virtual line in caret diagnostics.
48
3.89M
  CurBuffer[BytesUsed++] = '\n';
49
3.89M
50
3.89M
  // Return a pointer to the character data.
51
3.89M
  DestPtr = CurBuffer+BytesUsed;
52
3.89M
53
3.89M
  // Copy the token data into the buffer.
54
3.89M
  memcpy(CurBuffer+BytesUsed, Buf, Len);
55
3.89M
56
3.89M
  // Remember that we used these bytes.
57
3.89M
  BytesUsed += Len+1;
58
3.89M
59
3.89M
  // Add a NUL terminator to the token.  This keeps the tokens separated, in
60
3.89M
  // case they get relexed, and puts them on their own virtual lines in case a
61
3.89M
  // diagnostic points to one.
62
3.89M
  CurBuffer[BytesUsed-1] = '\0';
63
3.89M
64
3.89M
  return BufferStartLoc.getLocWithOffset(BytesUsed-Len-1);
65
3.89M
}
66
67
20.5k
void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
68
20.5k
  // Only pay attention to the requested length if it is larger than our default
69
20.5k
  // page size.  If it is, we allocate an entire chunk for it.  This is to
70
20.5k
  // support gigantic tokens, which almost certainly won't happen. :)
71
20.5k
  if (RequestLen < ScratchBufSize)
72
20.5k
    RequestLen = ScratchBufSize;
73
20.5k
74
20.5k
  // Get scratch buffer. Zero-initialize it so it can be dumped into a PCH file
75
20.5k
  // deterministically.
76
20.5k
  std::unique_ptr<llvm::WritableMemoryBuffer> OwnBuf =
77
20.5k
      llvm::WritableMemoryBuffer::getNewMemBuffer(RequestLen,
78
20.5k
                                                  "<scratch space>");
79
20.5k
  CurBuffer = OwnBuf->getBufferStart();
80
20.5k
  FileID FID = SourceMgr.createFileID(std::move(OwnBuf));
81
20.5k
  BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
82
20.5k
  BytesUsed = 0;
83
20.5k
}