clang 23.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
32
46
53
55 while (Current && !Current->isBottomFrame()) {
56 InterpFrame *Next = Current->Caller;
57 delete Current;
58 Current = Next;
59 }
60 BottomFrame.destroyScopes();
61
62 while (DeadBlocks) {
63 DeadBlock *Next = DeadBlocks->Next;
64
65 // There might be a pointer in a global structure pointing to the dead
66 // block.
67 for (Pointer *P = DeadBlocks->B.Pointers; P; P = P->asBlockPointer().Next)
68 DeadBlocks->B.removePointer(P);
69
70 std::free(DeadBlocks);
71 DeadBlocks = Next;
72 }
73}
74
76 // As a last resort, make sure all pointers still pointing to a dead block
77 // don't point to it anymore.
78 if (Alloc)
79 Alloc->cleanup();
80}
81
83
85 assert(B);
86 assert(!B->isDynamic());
87 assert(!B->isStatic());
88 assert(!B->isDead());
89
90 // The block might have a pointer saved in a field in its data
91 // that points to the block itself. We call the dtor first,
92 // which will destroy all the data but leave InlineDescriptors
93 // intact. If the block THEN still has pointers, we create a
94 // DeadBlock for it.
95 if (B->IsInitialized)
96 B->invokeDtor();
97
98 assert(!B->isInitialized());
99 if (B->hasPointers()) {
100 size_t Size = B->getSize();
101 // Allocate a new block, transferring over pointers.
102 char *Memory =
103 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size));
104 auto *D = new (Memory) DeadBlock(DeadBlocks, B);
105 // Since the block doesn't hold any actual data anymore, we can just
106 // memcpy() everything over.
107 std::memcpy(D->rawData(), B->rawData(), Size);
108 D->B.IsInitialized = false;
109 }
110}
111
113 if (!Alloc)
114 return true;
115
116 bool NoAllocationsLeft = !Alloc->hasAllocations();
117
119 for (const auto &[Source, Site] : Alloc->allocation_sites()) {
120 assert(!Site.empty());
121
122 CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)
123 << (Site.size() - 1) << Source->getSourceRange();
124 }
125 }
126 // Keep evaluating before C++20, since the CXXNewExpr wasn't valid there
127 // in the first place.
128 return NoAllocationsLeft || !getLangOpts().CPlusPlus20;
129}
130
132 for (const InterpFrame *F = Current; F; F = F->Caller) {
133 const Function *Func = F->getFunction();
134 if (!Func)
135 continue;
136 const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Func->getDecl());
137 if (!MD)
138 continue;
139 const IdentifierInfo *FnII = MD->getIdentifier();
140 if (!FnII || !FnII->isStr(Name))
141 continue;
142
143 const auto *CTSD =
144 dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent());
145 if (!CTSD)
146 continue;
147
148 const IdentifierInfo *ClassII = CTSD->getIdentifier();
149 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
150 if (CTSD->isInStdNamespace() && ClassII && ClassII->isStr("allocator") &&
151 TAL.size() >= 1 && TAL[0].getKind() == TemplateArgument::Type) {
152 QualType ElemType = TAL[0].getAsType();
153 const auto *NewCall = cast<CallExpr>(F->Caller->getExpr(F->getRetPC()));
154 return {NewCall, ElemType};
155 }
156 }
157
158 return {};
159}
160
162 if (InfiniteSteps)
163 return true;
164
165 --StepsLeft;
166 if (StepsLeft != 0)
167 return true;
168
169 FFDiag(Current->getSource(OpPC), diag::note_constexpr_step_limit_exceeded);
170 return false;
171}
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
FormatToken * Next
The next token in the unwrapped line.
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.
unsigned size() const
Retrieve the number of template arguments in this template argument list.
@ Type
The template argument is a type.
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.
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.
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:42
Descriptor for a dead block.
Base class for stack frames, shared between VM and walker.
Definition Frame.h:25
Bytecode function.
Definition Function.h:99
Frame storing local variables.
Definition InterpFrame.h:27
Stack frame storing temporaries and parameters.
Definition InterpStack.h:25
InterpFrame BottomFrame
Bottom function frame.
Context & Ctx
Interpreter Context.
bool noteStep(CodePtr OpPC)
Note that a step has been executed.
const unsigned EvalID
ID identifying this evaluation.
InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
InterpStack & Stk
Temporary stack.
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
unsigned StepsLeft
Steps left during evaluation.
InterpFrame * Current
The current frame.
const Frame * getCurrentFrame() override
std::optional< bool > ConstantContextOverride
const bool InfiniteSteps
Whether infinite evaluation steps have been requested.
void deallocate(Block *B)
Deallocates a pointer.
StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const
Program & P
Reference to the module containing all bytecode.
A pointer to a memory block, live or dead.
Definition Pointer.h:93
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:101
EvaluationMode EvalMode
Definition State.h:190
Expr::EvalStatus & getEvalStatus() const
Definition State.h:92
OptionalDiagnostic FFDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation could not be folded (FF => FoldFailure)
Definition State.cpp:21
State(ASTContext &ASTCtx, Expr::EvalStatus &EvalStatus)
Definition State.h:83
bool CheckingPotentialConstantExpression
Whether we're checking that an expression is a potential constant expression.
Definition State.h:180
bool CheckingForUndefinedBehavior
Whether we're checking for an expression that has undefined behavior.
Definition State.h:188
ASTContext & getASTContext() const
Definition State.h:93
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
const LangOptions & getLangOpts() const
Definition State.h:94
bool checkingPotentialConstantExpression() const
Are we checking whether the expression is a potential constant expression?
Definition State.h:120
bool InConstantContext
Whether or not we're in a context where the front end requires a constant value.
Definition State.h:175
The JSON file list parser is used to communicate input to InstallAPI.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
U cast(CodeGen::Address addr)
Definition Address.h:327