Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/AST/Interp/InterpState.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- InterpState.cpp - Interpreter for the constexpr 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 "InterpState.h"
10
#include "InterpFrame.h"
11
#include "InterpStack.h"
12
#include "Program.h"
13
#include "State.h"
14
15
using namespace clang;
16
using namespace clang::interp;
17
18
InterpState::InterpState(State &Parent, Program &P, InterpStack &Stk,
19
                         Context &Ctx, SourceMapper *M)
20
0
    : Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), Current(nullptr) {}
21
22
0
InterpState::~InterpState() {
23
0
  while (Current) {
24
0
    InterpFrame *Next = Current->Caller;
25
0
    delete Current;
26
0
    Current = Next;
27
0
  }
28
29
0
  while (DeadBlocks) {
30
0
    DeadBlock *Next = DeadBlocks->Next;
31
0
    std::free(DeadBlocks);
32
0
    DeadBlocks = Next;
33
0
  }
34
0
}
35
36
0
Frame *InterpState::getCurrentFrame() {
37
0
  if (Current && Current->Caller)
38
0
    return Current;
39
0
  return Parent.getCurrentFrame();
40
0
}
41
42
0
bool InterpState::reportOverflow(const Expr *E, const llvm::APSInt &Value) {
43
0
  QualType Type = E->getType();
44
0
  CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
45
0
  return noteUndefinedBehavior();
46
0
}
47
48
0
void InterpState::deallocate(Block *B) {
49
0
  assert(B);
50
0
  const Descriptor *Desc = B->getDescriptor();
51
0
  assert(Desc);
52
53
0
  if (B->hasPointers()) {
54
0
    size_t Size = B->getSize();
55
56
    // Allocate a new block, transferring over pointers.
57
0
    char *Memory =
58
0
        reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size));
59
0
    auto *D = new (Memory) DeadBlock(DeadBlocks, B);
60
61
    // Move data and metadata from the old block to the new (dead)block.
62
0
    if (Desc->MoveFn) {
63
0
      Desc->MoveFn(B, B->data(), D->data(), Desc);
64
0
      if (Desc->getMetadataSize() > 0)
65
0
        std::memcpy(D->rawData(), B->rawData(), Desc->getMetadataSize());
66
0
    }
67
68
    // We moved the contents over to the DeadBlock.
69
0
    B->IsInitialized = false;
70
0
  } else {
71
0
    B->invokeDtor();
72
0
  }
73
0
}