clang 23.0.0git
EvaluationResult.cpp
Go to the documentation of this file.
1//===----- EvaluationResult.cpp - Result class for the 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 "EvaluationResult.h"
10#include "InterpState.h"
11#include "Pointer.h"
12#include "Record.h"
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/SetVector.h"
15#include <iterator>
16
17namespace clang {
18namespace interp {
19
21 const FieldDecl *SubObjDecl) {
22 assert(SubObjDecl && "Subobject declaration does not exist");
23 S.FFDiag(Loc, diag::note_constexpr_uninitialized)
24 << /*(name)*/ 1 << SubObjDecl;
25 S.Note(SubObjDecl->getLocation(),
26 diag::note_constexpr_subobject_declared_here);
27}
28
29static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc,
30 PtrView BasePtr, const Record *R);
31
33 PtrView BasePtr) {
34 const Descriptor *BaseDesc = BasePtr.getFieldDesc();
35 assert(BaseDesc->isArray());
36
37 size_t NumElems = BaseDesc->getNumElems();
38 if (NumElems == 0)
39 return true;
40
41 bool Result = true;
42
43 if (BaseDesc->isPrimitiveArray()) {
44 if (BasePtr.allElementsInitialized())
45 return true;
47 return false;
48 }
49 const Descriptor *ElemDesc = BaseDesc->ElemDesc;
50
51 if (ElemDesc->isRecord()) {
52 const Record *R = ElemDesc->ElemRecord;
53 for (size_t I = 0; I != NumElems; ++I) {
54 PtrView ElemPtr = BasePtr.atIndex(I).narrow();
55 Result &= CheckFieldsInitialized(S, Loc, ElemPtr, R);
56 }
57 } else {
58 assert(ElemDesc->isArray());
59 for (size_t I = 0; I != NumElems; ++I) {
60 PtrView ElemPtr = BasePtr.atIndex(I).narrow();
61 Result &= CheckArrayInitialized(S, Loc, ElemPtr);
62 }
63 }
64
65 return Result;
66}
67
69 PtrView BasePtr, const Record *R) {
70 assert(R);
71 bool Result = true;
72 // Check all fields of this record are initialized.
73 for (const Record::Field &F : R->fields()) {
74 PtrView FieldPtr = BasePtr.atField(F.Offset);
75
76 // Don't check inactive union members.
77 if (R->isUnion() && !FieldPtr.isActive())
78 continue;
79
80 QualType FieldType = F.Decl->getType();
81 const Descriptor *FieldDesc = FieldPtr.getFieldDesc();
82
83 if (FieldDesc->isRecord()) {
84 Result &= CheckFieldsInitialized(S, Loc, FieldPtr, FieldPtr.getRecord());
85 } else if (FieldType->isIncompleteArrayType()) {
86 // Nothing to do here.
87 } else if (F.Decl->isUnnamedBitField()) {
88 // Nothing do do here.
89 } else if (FieldDesc->isArray()) {
90 Result &= CheckArrayInitialized(S, Loc, FieldPtr);
91 } else if (!FieldPtr.isInitialized()) {
92 DiagnoseUninitializedSubobject(S, Loc, F.Decl);
93 Result = false;
94 }
95 }
96
97 // Check Fields in all bases
98 for (auto [I, B] : llvm::enumerate(R->bases())) {
99 PtrView P = BasePtr.atField(B.Offset);
100 if (!P.isInitialized()) {
101 const Descriptor *Desc = BasePtr.getDeclDesc();
102 if (const auto *CD = dyn_cast_if_present<CXXRecordDecl>(R->getDecl())) {
103 const auto &BS = *std::next(CD->bases_begin(), I);
104 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
105 S.FFDiag(TypeBeginLoc, diag::note_constexpr_uninitialized_base)
106 << B.Desc->getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
107 } else {
108 S.FFDiag(Desc->getLocation(), diag::note_constexpr_uninitialized_base)
109 << B.Desc->getType();
110 }
111 return false;
112 }
113 Result &= CheckFieldsInitialized(S, Loc, P, B.R);
114 }
115
116 // TODO: Virtual bases
117 return Result;
118}
119
121 const Pointer &Ptr) const {
122 assert(Source);
123 assert(empty());
124
125 if (Ptr.isZero())
126 return true;
127 if (!Ptr.isBlockPointer())
128 return true;
129
130 // We can't inspect dead pointers at all. Return true here so we can
131 // diagnose them later.
132 if (!Ptr.isLive())
133 return true;
134
135 SourceLocation InitLoc;
136 if (const auto *D = dyn_cast<const Decl *>(Source))
137 InitLoc = cast<VarDecl>(D)->getAnyInitializer()->getExprLoc();
138 else if (const auto *E = dyn_cast<const Expr *>(Source))
139 InitLoc = E->getExprLoc();
140
141 if (const Record *R = Ptr.getRecord())
142 return CheckFieldsInitialized(S, InitLoc, Ptr.view(), R);
143
144 if (isa_and_nonnull<ConstantArrayType>(Ptr.getType()->getAsArrayTypeUnsafe()))
145 return CheckArrayInitialized(S, InitLoc, Ptr.view());
146
147 return true;
148}
149
150static bool isOrHasPtr(const Descriptor *D) {
151 if ((D->isPrimitive() || D->isPrimitiveArray()) && D->getPrimType() == PT_Ptr)
152 return true;
153
154 if (D->ElemRecord)
155 return D->ElemRecord->hasPtrField();
156 return false;
157}
158
159static void collectBlocks(PtrView Ptr, llvm::SetVector<const Block *> &Blocks) {
160 auto isUsefulPtr = [](const Pointer &P) -> bool {
161 return P.isLive() && P.isBlockPointer() && !P.isZero() && !P.isDummy() &&
162 P.isDereferencable() && !P.isUnknownSizeArray() && !P.isOnePastEnd();
163 };
164
165 if (!isUsefulPtr(Pointer(Ptr)))
166 return;
167
168 Blocks.insert(Ptr.Pointee);
169
170 const Descriptor *Desc = Ptr.getFieldDesc();
171 if (!Desc)
172 return;
173
174 if (const Record *R = Desc->ElemRecord; R && R->hasPtrField()) {
175
176 for (const Record::Field &F : R->fields()) {
177 if (!isOrHasPtr(F.Desc))
178 continue;
179 PtrView FieldPtr = Ptr.atField(F.Offset);
180 collectBlocks(FieldPtr, Blocks);
181 }
182 } else if (Desc->isPrimitive() && Desc->getPrimType() == PT_Ptr) {
183 Pointer Pointee = Ptr.deref<Pointer>();
184 if (isUsefulPtr(Pointee) && !Blocks.contains(Pointee.block()))
185 collectBlocks(Pointee.view(), Blocks);
186
187 } else if (Desc->isPrimitiveArray() && Desc->getPrimType() == PT_Ptr) {
188 for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
189 Pointer ElemPointee = Ptr.elem<Pointer>(I);
190 if (isUsefulPtr(ElemPointee) && !Blocks.contains(ElemPointee.block()))
191 collectBlocks(ElemPointee.view(), Blocks);
192 }
193 } else if (Desc->isCompositeArray() && isOrHasPtr(Desc->ElemDesc)) {
194 for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
195 PtrView ElemPtr = Ptr.atIndex(I).narrow();
196 collectBlocks(ElemPtr, Blocks);
197 }
198 }
199}
200
202 const Pointer &Ptr,
203 const SourceInfo &Info) {
204 if (!Ptr.isBlockPointer())
205 return true;
206 // Collect all blocks that this pointer (transitively) points to and
207 // return false if any of them is a dynamic block.
208 llvm::SetVector<const Block *> Blocks;
209
210 collectBlocks(Ptr.view(), Blocks);
211
212 for (const Block *B : Blocks) {
213 if (B->isDynamic()) {
214 assert(B->getDescriptor());
215 assert(B->getDescriptor()->asExpr());
216
217 bool IsSubobj = !Ptr.isRoot() || Ptr.isArrayElement();
218 S.FFDiag(Info, diag::note_constexpr_dynamic_alloc)
219 << Ptr.getType()->isReferenceType() << IsSubobj;
220 S.Note(B->getDescriptor()->asExpr()->getExprLoc(),
221 diag::note_constexpr_dynamic_alloc_here);
222 return false;
223 }
224 }
225
226 return true;
227}
228
229} // namespace interp
230} // namespace clang
llvm::MachO::Record Record
Definition MachO.h:31
SourceLocation getLocation() const
Definition DeclBase.h:447
Represents a member of a struct/union/class.
Definition Decl.h:3182
A (possibly-)qualified type.
Definition TypeBase.h:937
Encodes a location in the source.
A trivial tuple used to represent a source range.
bool isIncompleteArrayType() const
Definition TypeBase.h:8789
bool isReferenceType() const
Definition TypeBase.h:8706
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition TypeBase.h:9328
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:47
bool checkReturnValue(InterpState &S, const Context &Ctx, const Pointer &Ptr, const SourceInfo &Info)
Check that none of the blocks the given pointer (transitively) points to are dynamically allocated.
bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const
Check that all subobjects of the given pointer have been initialized.
Interpreter context.
Definition InterpState.h:36
A pointer to a memory block, live or dead.
Definition Pointer.h:393
QualType getType() const
Returns the type of the innermost field.
Definition Pointer.h:562
bool isArrayElement() const
Checks if the pointer points to an array.
Definition Pointer.h:630
bool isLive() const
Checks if the pointer is live.
Definition Pointer.h:510
bool isZero() const
Checks if the pointer is null.
Definition Pointer.h:496
bool isRoot() const
Pointer points directly to a block.
Definition Pointer.h:637
bool isBlockPointer() const
Definition Pointer.h:667
const Block * block() const
Definition Pointer.h:798
PtrView view() const
Definition Pointer.h:449
const Record * getRecord() const
Returns the record descriptor of a class.
Definition Pointer.h:673
Structure/Class descriptor.
Definition Record.h:25
bool hasPtrField() const
If this record (or any of its bases) contains a field of type PT_Ptr.
Definition Record.h:83
Describes the statement/declaration an opcode was generated from.
Definition Source.h:74
OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId)
Add a note to a prior diagnostic.
Definition State.cpp:63
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
static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc, PtrView BasePtr, const Record *R)
static void collectBlocks(PtrView Ptr, llvm::SetVector< const Block * > &Blocks)
static bool CheckArrayInitialized(InterpState &S, SourceLocation Loc, PtrView BasePtr)
static bool isOrHasPtr(const Descriptor *D)
static void DiagnoseUninitializedSubobject(InterpState &S, SourceLocation Loc, const FieldDecl *SubObjDecl)
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
U cast(CodeGen::Address addr)
Definition Address.h:327
Describes a memory block created by an allocation site.
Definition Descriptor.h:123
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition Descriptor.h:260
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition Descriptor.h:274
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition Descriptor.h:267
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition Descriptor.h:156
SourceLocation getLocation() const
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition Descriptor.h:265
PrimType getPrimType() const
Definition Descriptor.h:242
bool isRecord() const
Checks if the descriptor is of a record.
Definition Descriptor.h:279
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition Descriptor.h:154
bool isArray() const
Checks if the descriptor is of an array.
Definition Descriptor.h:277
const Descriptor * getDeclDesc() const
Definition Pointer.h:82
bool allElementsInitialized() const
Definition Pointer.cpp:604
PtrView atField(unsigned Offset) const
Definition Pointer.h:252
const Record * getRecord() const
Definition Pointer.h:144
const Descriptor * getFieldDesc() const
Definition Pointer.h:76
const FieldDecl * getField() const
Definition Pointer.h:149
PtrView atIndex(unsigned Idx) const
Definition Pointer.h:188
PtrView narrow() const
Definition Pointer.h:86
T & elem(unsigned I) const
Definition Pointer.h:234
bool isInitialized() const
Definition Pointer.h:280
bool isActive() const
Definition Pointer.h:46
T & deref() const
Definition Pointer.h:223