clang 22.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#include "clang/AST/DeclCXX.h"
16
17using namespace clang;
18using namespace clang::interp;
19
21 Context &Ctx, SourceMapper *M)
22 : Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), BottomFrame(*this),
23 Current(&BottomFrame) {}
24
26 Context &Ctx, const Function *Func)
27 : Parent(Parent), M(nullptr), P(P), Stk(Stk), Ctx(Ctx),
28 BottomFrame(*this, Func, nullptr, CodePtr(), Func->getArgSize()),
29 Current(&BottomFrame) {}
30
34
35 return Parent.InConstantContext;
36}
37
39 while (Current && !Current->isBottomFrame()) {
40 InterpFrame *Next = Current->Caller;
41 delete Current;
42 Current = Next;
43 }
45
46 while (DeadBlocks) {
47 DeadBlock *Next = DeadBlocks->Next;
48
49 // There might be a pointer in a global structure pointing to the dead
50 // block.
51 for (Pointer *P = DeadBlocks->B.Pointers; P; P = P->asBlockPointer().Next)
52 DeadBlocks->B.removePointer(P);
53
54 std::free(DeadBlocks);
55 DeadBlocks = Next;
56 }
57}
58
60 // As a last resort, make sure all pointers still pointing to a dead block
61 // don't point to it anymore.
62 if (Alloc)
63 Alloc->cleanup();
64}
65
67
68bool InterpState::reportOverflow(const Expr *E, const llvm::APSInt &Value) {
70 CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
71 return noteUndefinedBehavior();
72}
73
75 assert(B);
76 assert(!B->isDynamic());
77 assert(!B->isStatic());
78 assert(!B->isDead());
79
80 // The block might have a pointer saved in a field in its data
81 // that points to the block itself. We call the dtor first,
82 // which will destroy all the data but leave InlineDescriptors
83 // intact. If the block THEN still has pointers, we create a
84 // DeadBlock for it.
85 if (B->IsInitialized)
86 B->invokeDtor();
87
88 assert(!B->isInitialized());
89 if (B->hasPointers()) {
90 size_t Size = B->getSize();
91 // Allocate a new block, transferring over pointers.
92 char *Memory =
93 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size));
94 auto *D = new (Memory) DeadBlock(DeadBlocks, B);
95 // Since the block doesn't hold any actual data anymore, we can just
96 // memcpy() everything over.
97 std::memcpy(D->rawData(), B->rawData(), Size);
98 D->B.IsInitialized = false;
99 }
100}
101
103 if (!Alloc)
104 return true;
105
106 bool NoAllocationsLeft = !Alloc->hasAllocations();
107
109 for (const auto &[Source, Site] : Alloc->allocation_sites()) {
110 assert(!Site.empty());
111
112 CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)
113 << (Site.size() - 1) << Source->getSourceRange();
114 }
115 }
116 // Keep evaluating before C++20, since the CXXNewExpr wasn't valid there
117 // in the first place.
118 return NoAllocationsLeft || !getLangOpts().CPlusPlus20;
119}
120
122 for (const InterpFrame *F = Current; F; F = F->Caller) {
123 const Function *Func = F->getFunction();
124 if (!Func)
125 continue;
126 const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Func->getDecl());
127 if (!MD)
128 continue;
129 const IdentifierInfo *FnII = MD->getIdentifier();
130 if (!FnII || !FnII->isStr(Name))
131 continue;
132
133 const auto *CTSD =
134 dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent());
135 if (!CTSD)
136 continue;
137
138 const IdentifierInfo *ClassII = CTSD->getIdentifier();
139 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
140 if (CTSD->isInStdNamespace() && ClassII && ClassII->isStr("allocator") &&
141 TAL.size() >= 1 && TAL[0].getKind() == TemplateArgument::Type) {
142 QualType ElemType = TAL[0].getAsType();
143 const auto *NewCall = cast<CallExpr>(F->Caller->getExpr(F->getRetPC()));
144 return {NewCall, ElemType};
145 }
146 }
147
148 return {};
149}
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
const Decl * D
Expr * E
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
This represents one expression.
Definition: Expr.h:112
QualType getType() const
Definition: Expr.h:144
One of these records is kept for each identifier that is lexed.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
A (possibly-)qualified type.
Definition: TypeBase.h:937
A template argument list.
Definition: DeclTemplate.h:250
unsigned size() const
Retrieve the number of template arguments in this template argument list.
Definition: DeclTemplate.h:286
@ Type
The template argument is a type.
Definition: TemplateBase.h:70
The base class of the type hierarchy.
Definition: TypeBase.h:1833
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:44
unsigned getSize() const
Returns the size of the block.
Definition: InterpBlock.h:87
void invokeDtor()
Invokes the Destructor.
Definition: InterpBlock.h:135
bool isDead() const
Definition: InterpBlock.h:85
bool isStatic() const
Checks if the block has static storage duration.
Definition: InterpBlock.h:79
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Definition: InterpBlock.h:111
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition: InterpBlock.h:92
bool isDynamic() const
Definition: InterpBlock.h:83
bool hasPointers() const
Checks if the block has any live pointers.
Definition: InterpBlock.h:75
Pointer into the code segment.
Definition: Source.h:30
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:41
Descriptor for a dead block.
Definition: InterpBlock.h:200
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
Bytecode function.
Definition: Function.h:86
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:25
InterpFrame BottomFrame
Bottom function frame.
Definition: InterpState.h:180
bool reportOverflow(const Expr *E, const llvm::APSInt &Value)
Reports overflow and return true if evaluation should continue.
Definition: InterpState.cpp:68
bool noteUndefinedBehavior() override
Definition: InterpState.h:85
Frame * getCurrentFrame() override
Definition: InterpState.cpp:66
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
InterpFrame * Current
The current frame.
Definition: InterpState.h:182
InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
Definition: InterpState.cpp:20
std::optional< bool > ConstantContextOverride
Definition: InterpState.h:190
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:74
bool checkingPotentialConstantExpression() const override
Definition: InterpState.h:82
StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const
bool inConstantContext() const
Definition: InterpState.cpp:31
Program & P
Reference to the module containing all bytecode.
Definition: InterpState.h:174
A pointer to a memory block, live or dead.
Definition: Pointer.h:90
The program contains and links the bytecode for all functions.
Definition: Program.h:36
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:58
const LangOptions & getLangOpts() const
Definition: State.cpp:115
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
bool InConstantContext
Whether or not we're in a context where the front end requires a constant value.
Definition: State.h:129
The JSON file list parser is used to communicate input to InstallAPI.