Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/Interp/InterpStack.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- InterpStack.cpp - Stack implementation for the VM ------*- C++ -*-===//
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
#include <cassert>
10
#include <cstdlib>
11
#include "InterpStack.h"
12
13
using namespace clang;
14
using namespace clang::interp;
15
16
1
InterpStack::~InterpStack() {
17
1
  clear();
18
1
}
19
20
2
void InterpStack::clear() {
21
2
  if (Chunk && 
Chunk->Next0
)
22
0
    free(Chunk->Next);
23
2
  if (Chunk)
24
0
    free(Chunk);
25
2
  Chunk = nullptr;
26
2
  StackSize = 0;
27
2
}
28
29
0
void *InterpStack::grow(size_t Size) {
30
0
  assert(Size < ChunkSize - sizeof(StackChunk) && "Object too large");
31
0
32
0
  if (!Chunk || sizeof(StackChunk) + Chunk->size() + Size > ChunkSize) {
33
0
    if (Chunk && Chunk->Next) {
34
0
      Chunk = Chunk->Next;
35
0
    } else {
36
0
      StackChunk *Next = new (malloc(ChunkSize)) StackChunk(Chunk);
37
0
      if (Chunk)
38
0
        Chunk->Next = Next;
39
0
      Chunk = Next;
40
0
    }
41
0
  }
42
0
43
0
  auto *Object = reinterpret_cast<void *>(Chunk->End);
44
0
  Chunk->End += Size;
45
0
  StackSize += Size;
46
0
  return Object;
47
0
}
48
49
0
void *InterpStack::peek(size_t Size) {
50
0
  assert(Chunk && "Stack is empty!");
51
0
52
0
  StackChunk *Ptr = Chunk;
53
0
  while (Size > Ptr->size()) {
54
0
    Size -= Ptr->size();
55
0
    Ptr = Ptr->Prev;
56
0
    assert(Ptr && "Offset too large");
57
0
  }
58
0
59
0
  return reinterpret_cast<void *>(Ptr->End - Size);
60
0
}
61
62
0
void InterpStack::shrink(size_t Size) {
63
0
  assert(Chunk && "Chunk is empty!");
64
0
65
0
  while (Size > Chunk->size()) {
66
0
    Size -= Chunk->size();
67
0
    if (Chunk->Next) {
68
0
      free(Chunk->Next);
69
0
      Chunk->Next = nullptr;
70
0
    }
71
0
    Chunk->End = Chunk->start();
72
0
    Chunk = Chunk->Prev;
73
0
    assert(Chunk && "Offset too large");
74
0
  }
75
0
76
0
  Chunk->End -= Size;
77
0
  StackSize -= Size;
78
0
}