clang 20.0.0git
InterpState.cpp
Go to the documentation of this file.
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
15using namespace clang;
16using namespace clang::interp;
17
19 Context &Ctx, SourceMapper *M)
20 : Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), Current(nullptr) {}
21
23 while (Current) {
24 InterpFrame *Next = Current->Caller;
25 delete Current;
26 Current = Next;
27 }
28
29 while (DeadBlocks) {
30 DeadBlock *Next = DeadBlocks->Next;
31 std::free(DeadBlocks);
32 DeadBlocks = Next;
33 }
34}
35
37 // As a last resort, make sure all pointers still pointing to a dead block
38 // don't point to it anymore.
39 for (DeadBlock *DB = DeadBlocks; DB; DB = DB->Next) {
40 for (Pointer *P = DB->B.Pointers; P; P = P->Next) {
41 P->PointeeStorage.BS.Pointee = nullptr;
42 }
43 }
44
45 Alloc.cleanup();
46}
47
49 if (Current && Current->Caller)
50 return Current;
51 return Parent.getCurrentFrame();
52}
53
54bool InterpState::reportOverflow(const Expr *E, const llvm::APSInt &Value) {
56 CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
57 return noteUndefinedBehavior();
58}
59
61 assert(B);
62 const Descriptor *Desc = B->getDescriptor();
63 assert(Desc);
64
65 if (B->hasPointers()) {
66 size_t Size = B->getSize();
67
68 // Allocate a new block, transferring over pointers.
69 char *Memory =
70 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size));
71 auto *D = new (Memory) DeadBlock(DeadBlocks, B);
72 std::memset(D->B.rawData(), 0, D->B.getSize());
73
74 // Move data and metadata from the old block to the new (dead)block.
75 if (B->IsInitialized && Desc->MoveFn) {
76 Desc->MoveFn(B, B->data(), D->data(), Desc);
77 if (Desc->getMetadataSize() > 0)
78 std::memcpy(D->rawData(), B->rawData(), Desc->getMetadataSize());
79 }
80 D->B.IsInitialized = B->IsInitialized;
81
82 // We moved the contents over to the DeadBlock.
83 B->IsInitialized = false;
84 } else if (B->IsInitialized) {
85 B->invokeDtor();
86 }
87}
88
90 bool NoAllocationsLeft = (Alloc.getNumAllocations() == 0);
91
93 for (const auto &It : Alloc.allocation_sites()) {
94 assert(It.second.size() > 0);
95
96 const Expr *Source = It.first;
97 CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)
98 << (It.second.size() - 1) << Source->getSourceRange();
99 }
100 }
101 return NoAllocationsLeft;
102}
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
const Decl * D
Expr * E
This represents one expression.
Definition: Expr.h:110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:277
QualType getType() const
Definition: Expr.h:142
A (possibly-)qualified type.
Definition: Type.h:941
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:326
The base class of the type hierarchy.
Definition: Type.h:1829
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
unsigned getSize() const
Returns the size of the block.
Definition: InterpBlock.h:78
void invokeDtor()
Invokes the Destructor.
Definition: InterpBlock.h:121
std::byte * data()
Returns a pointer to the stored data.
Definition: InterpBlock.h:89
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition: InterpBlock.h:67
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Definition: InterpBlock.h:102
bool hasPointers() const
Checks if the block has any live pointers.
Definition: InterpBlock.h:69
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
Descriptor for a dead block.
Definition: InterpBlock.h:181
llvm::iterator_range< const_virtual_iter > allocation_sites() const
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
Frame storing local variables.
Definition: InterpFrame.h:26
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:29
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:27
bool reportOverflow(const Expr *E, const llvm::APSInt &Value)
Reports overflow and return true if evaluation should continue.
Definition: InterpState.cpp:54
bool noteUndefinedBehavior() override
Definition: InterpState.h:74
Frame * getCurrentFrame() override
Definition: InterpState.cpp:48
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
Definition: InterpState.cpp:89
InterpFrame * Current
The current frame.
Definition: InterpState.h:132
InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
Definition: InterpState.cpp:18
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:60
bool checkingPotentialConstantExpression() const override
Definition: InterpState.h:71
Program & P
Reference to the module containing all bytecode.
Definition: InterpState.h:126
A pointer to a memory block, live or dead.
Definition: Pointer.h:82
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Interface for classes which map locations to sources.
Definition: Source.h:99
Interface for the VM to interact with the AST walker's context.
Definition: State.h:56
virtual Frame * getCurrentFrame()=0
OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation does not produce a C++11 core constant expression.
Definition: State.cpp:42
The JSON file list parser is used to communicate input to InstallAPI.
Describes a memory block created by an allocation site.
Definition: Descriptor.h:111
const BlockMoveFn MoveFn
Definition: Descriptor.h:162
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition: Descriptor.h:234