Thanks to visit codestin.com
Credit goes to clang.llvm.org

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
22 : Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), BottomFrame(*this),
24 InConstantContext = Parent.InConstantContext;
26 Parent.CheckingPotentialConstantExpression;
27 CheckingForUndefinedBehavior = Parent.CheckingForUndefinedBehavior;
28 EvalMode = Parent.EvalMode;
29}
30
32 Context &Ctx, const Function *Func)
33 : Parent(Parent), M(nullptr), P(P), Stk(Stk), Ctx(Ctx),
34 BottomFrame(*this, Func, nullptr, CodePtr(), Func->getArgSize()),
36 InConstantContext = Parent.InConstantContext;
38 Parent.CheckingPotentialConstantExpression;
39 CheckingForUndefinedBehavior = Parent.CheckingForUndefinedBehavior;
40 EvalMode = Parent.EvalMode;
41}
42
49
51 while (Current && !Current->isBottomFrame()) {
52 InterpFrame *Next = Current->Caller;
53 delete Current;
54 Current = Next;
55 }
56 BottomFrame.destroyScopes();
57
58 while (DeadBlocks) {
59 DeadBlock *Next = DeadBlocks->Next;
60
61 // There might be a pointer in a global structure pointing to the dead
62 // block.
63 for (Pointer *P = DeadBlocks->B.Pointers; P; P = P->asBlockPointer().Next)
64 DeadBlocks->B.removePointer(P);
65
66 std::free(DeadBlocks);
67 DeadBlocks = Next;
68 }
69}
70
72 // As a last resort, make sure all pointers still pointing to a dead block
73 // don't point to it anymore.
74 if (Alloc)
75 Alloc->cleanup();
76}
77
79
81 assert(B);
82 assert(!B->isDynamic());
83 assert(!B->isStatic());
84 assert(!B->isDead());
85
86 // The block might have a pointer saved in a field in its data
87 // that points to the block itself. We call the dtor first,
88 // which will destroy all the data but leave InlineDescriptors
89 // intact. If the block THEN still has pointers, we create a
90 // DeadBlock for it.
91 if (B->IsInitialized)
92 B->invokeDtor();
93
94 assert(!B->isInitialized());
95 if (B->hasPointers()) {
96 size_t Size = B->getSize();
97 // Allocate a new block, transferring over pointers.
98 char *Memory =
99 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size));
100 auto *D = new (Memory) DeadBlock(DeadBlocks, B);
101 // Since the block doesn't hold any actual data anymore, we can just
102 // memcpy() everything over.
103 std::memcpy(D->rawData(), B->rawData(), Size);
104 D->B.IsInitialized = false;
105 }
106}
107
109 if (!Alloc)
110 return true;
111
112 bool NoAllocationsLeft = !Alloc->hasAllocations();
113
115 for (const auto &[Source, Site] : Alloc->allocation_sites()) {
116 assert(!Site.empty());
117
118 CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)
119 << (Site.size() - 1) << Source->getSourceRange();
120 }
121 }
122 // Keep evaluating before C++20, since the CXXNewExpr wasn't valid there
123 // in the first place.
124 return NoAllocationsLeft || !getLangOpts().CPlusPlus20;
125}
126
128 for (const InterpFrame *F = Current; F; F = F->Caller) {
129 const Function *Func = F->getFunction();
130 if (!Func)
131 continue;
132 const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Func->getDecl());
133 if (!MD)
134 continue;
135 const IdentifierInfo *FnII = MD->getIdentifier();
136 if (!FnII || !FnII->isStr(Name))
137 continue;
138
139 const auto *CTSD =
140 dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent());
141 if (!CTSD)
142 continue;
143
144 const IdentifierInfo *ClassII = CTSD->getIdentifier();
145 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
146 if (CTSD->isInStdNamespace() && ClassII && ClassII->isStr("allocator") &&
147 TAL.size() >= 1 && TAL[0].getKind() == TemplateArgument::Type) {
148 QualType ElemType = TAL[0].getAsType();
149 const auto *NewCall = cast<CallExpr>(F->Caller->getExpr(F->getRetPC()));
150 return {NewCall, ElemType};
151 }
152 }
153
154 return {};
155}
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:41
Descriptor for a dead block.
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
Stack frame storing temporaries and parameters.
Definition InterpStack.h:25
InterpFrame BottomFrame
Bottom function frame.
Context & Ctx
Interpreter Context.
Frame * getCurrentFrame() override
InterpStack & Stk
Temporary stack.
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
InterpFrame * Current
The current frame.
InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
std::optional< bool > ConstantContextOverride
void deallocate(Block *B)
Deallocates a pointer.
const LangOptions & getLangOpts() const
Definition InterpState.h:71
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:91
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:100
EvaluationMode EvalMode
Definition State.h:171
bool CheckingPotentialConstantExpression
Whether we're checking that an expression is a potential constant expression.
Definition State.h:161
bool CheckingForUndefinedBehavior
Whether we're checking for an expression that has undefined behavior.
Definition State.h:169
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 checkingPotentialConstantExpression() const
Are we checking whether the expression is a potential constant expression?
Definition State.h:99
bool InConstantContext
Whether or not we're in a context where the front end requires a constant value.
Definition State.h:156
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