clang 24.0.0git
InterpHelpers.h
Go to the documentation of this file.
1//===--- InterpHelpers.h - Interpreter Helper Functions --------*- 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#ifndef LLVM_CLANG_AST_INTERP_INTERPHELPERS_H
10#define LLVM_CLANG_AST_INTERP_INTERPHELPERS_H
11
12#include "DynamicAllocator.h"
13#include "InterpState.h"
14#include "Pointer.h"
15
16namespace clang {
17class CallExpr;
18class OffsetOfExpr;
19
20namespace interp {
21class Block;
22struct Descriptor;
23
24/// Interpreter entry point.
25bool Interpret(InterpState &S);
26
27/// Interpret a builtin function.
28bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
29 uint32_t BuiltinID);
30
31/// Interpret an offsetof operation.
32bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E,
33 ArrayRef<int64_t> ArrayIndices, int64_t &Result);
34
35/// Checks if the array is offsetable.
36bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
37
38/// Checks if a pointer is live and accessible.
39bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
40 AccessKinds AK);
41
42/// Checks if a pointer is a dummy pointer.
43bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
44 AccessKinds AK);
45
46bool arrayElemPtrOpaque(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
47 APSInt &&Index, bool AllowReplace = true);
48
49/// Checks if a pointer is in range.
50template <typename T>
51bool CheckRange(InterpState &S, CodePtr OpPC, T Ptr, AccessKinds AK) {
52 if (!Ptr.isOnePastEnd() && !Ptr.isZeroSizeArray())
53 return true;
54 if (S.getLangOpts().CPlusPlus) {
55 const SourceInfo &Loc = S.Current->getSource(OpPC);
56 S.FFDiag(Loc, diag::note_constexpr_access_past_end)
57 << AK << S.Current->getRange(OpPC);
58 }
59 return false;
60}
61
62/// Checks if a field from which a pointer is going to be derived is valid.
63bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
65
66/// Checks if a pointer points to a mutable field.
67bool CheckMutable(InterpState &S, CodePtr OpPC, PtrView Ptr,
68 AccessKinds AK = AK_Read);
69inline bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
70 AccessKinds AK = AK_Read) {
71 if (!Ptr.isBlockPointer())
72 return true;
73 return CheckMutable(S, OpPC, Ptr.view(), AK);
74}
75
76/// Checks if a value can be loaded from a block.
77bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
78 AccessKinds AK = AK_Read);
79
80/// Diagnose mismatched new[]/delete or new/delete[] pairs.
81bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC,
82 DynamicAllocator::Form AllocForm,
83 DynamicAllocator::Form DeleteForm, const Descriptor *D,
84 const Expr *NewExpr);
85
86/// Copy the contents of Src into Dest.
87bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest);
88
90 unsigned Kind, Pointer &Ptr,
91 const Expr *E, bool IsDynamic = false);
92
93template <typename T>
94bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue) {
95 const Expr *E = S.Current->getExpr(OpPC);
96 S.CCEDiag(E, diag::note_constexpr_overflow) << SrcValue << E->getType();
97 return S.noteUndefinedBehavior();
98}
99
100inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
101 uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
102 if (Limit != 0 && NumElems > Limit) {
103 S.FFDiag(S.Current->getSource(OpPC),
104 diag::note_constexpr_new_exceeds_limits, 1)
105 << NumElems << Limit;
106 S.Note(S.Current->getSource(OpPC), diag::note_constexpr_steps);
107 return false;
108 }
109 return true;
110}
111
112static inline llvm::RoundingMode getRoundingMode(FPOptions FPO) {
113 auto RM = FPO.getRoundingMode();
114 if (RM == llvm::RoundingMode::Dynamic)
115 return llvm::RoundingMode::NearestTiesToEven;
116 return RM;
117}
118
119inline bool Invalid(InterpState &S, CodePtr OpPC) {
120 const SourceLocation &Loc = S.Current->getLocation(OpPC);
121 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr)
122 << S.Current->getRange(OpPC);
123 return false;
124}
125
126template <typename SizeT>
127bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT *NumElements,
128 unsigned ElemSize, bool IsNoThrow) {
129
130 if (ElemSize == 0)
131 return true;
132
133 // FIXME: Both the SizeT::from() as well as the
134 // NumElements.toAPSInt() in this function are rather expensive.
135
136 // Can't be too many elements if the bitwidth of NumElements is lower than
137 // that of Descriptor::MaxArrayElemBytes.
138 if ((NumElements->bitWidth() - NumElements->isSigned()) <
139 (sizeof(Descriptor::MaxArrayElemBytes) * 8))
140 return true;
141
142 // FIXME: GH63562
143 // APValue stores array extents as unsigned,
144 // so anything that is greater that unsigned would overflow when
145 // constructing the array, we catch this here.
146 SizeT MaxElements = SizeT::from(Descriptor::MaxArrayElemBytes / ElemSize);
147 assert(MaxElements.isPositive());
148 if (NumElements->toAPSInt().getActiveBits() >
150 *NumElements > MaxElements) {
151 if (!IsNoThrow) {
152 const SourceInfo &Loc = S.Current->getSource(OpPC);
153
154 if (NumElements->isSigned() && NumElements->isNegative()) {
155 S.FFDiag(Loc, diag::note_constexpr_new_negative)
156 << NumElements->toDiagnosticString(S.getASTContext());
157 } else {
158 S.FFDiag(Loc, diag::note_constexpr_new_too_large)
159 << NumElements->toDiagnosticString(S.getASTContext());
160 }
161 }
162 return false;
163 }
164 return true;
165}
166
167} // namespace interp
168} // namespace clang
169
170#endif // LLVM_CLANG_AST_INTERP_INTERPHELPERS_H
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:239
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2987
static unsigned getMaxSizeBits(const ASTContext &Context)
Determine the maximum number of active bits that an array's size can require, which limits the maximu...
Definition Type.cpp:291
This represents one expression.
Definition Expr.h:113
QualType getType() const
Definition Expr.h:145
RoundingMode getRoundingMode() const
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition Expr.h:2571
Encodes a location in the source.
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:43
Pointer into the code segment.
Definition Source.h:31
SourceLocation getLocation(CodePtr PC) const
SourceInfo getSource(CodePtr PC) const
Map a location to a source.
SourceRange getRange(CodePtr PC) const
const Expr * getExpr(CodePtr PC) const
Interpreter context.
Definition InterpState.h:43
InterpFrame * Current
The current frame.
A pointer to a memory block, live or dead.
Definition Pointer.h:531
Describes the statement/declaration an opcode was generated from.
Definition Source.h:77
OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId)
Add a note to a prior diagnostic.
Definition State.cpp:86
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:37
ASTContext & getASTContext() const
Definition State.h:90
bool noteUndefinedBehavior() const
Note that we hit something that was technically undefined behavior, but that we can evaluate past it ...
Definition State.h: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:60
const LangOptions & getLangOpts() const
Definition State.h:91
bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if a field from which a pointer is going to be derived is valid.
Definition Interp.cpp:600
bool arrayElemPtrOpaque(InterpState &S, CodePtr OpPC, const Pointer &Ptr, APSInt &&Index, bool AllowReplace)
Definition Interp.cpp:3429
bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue)
bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC, DynamicAllocator::Form AllocForm, DynamicAllocator::Form DeleteForm, const Descriptor *D, const Expr *NewExpr)
Diagnose mismatched new[]/delete or new/delete[] pairs.
Definition Interp.cpp:1293
static llvm::RoundingMode getRoundingMode(FPOptions FPO)
bool Call(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition Interp.cpp:2055
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a value can be loaded from a block.
Definition Interp.cpp:932
bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E, ArrayRef< int64_t > ArrayIndices, int64_t &IntResult)
Interpret an offsetof operation.
bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is live and accessible.
Definition Interp.cpp:489
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, uint32_t BuiltinID)
Interpret a builtin function.
bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the array is offsetable.
Definition Interp.cpp:481
bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is a dummy pointer.
Definition Interp.cpp:1357
bool CheckMutable(InterpState &S, CodePtr OpPC, PtrView Ptr, AccessKinds AK)
Checks if a pointer points to a mutable field.
Definition Interp.cpp:705
bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems)
UnsignedOrNone evaluateBuiltinObjectSize(const ASTContext &ASTCtx, unsigned Kind, Pointer &Ptr, const Expr *E, bool IsDynamic)
Evaluate __builtin_object_size or __builtin_dynamic_object_size for the given pointer and Kind.
bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest)
Copy the contents of Src into Dest.
bool Interpret(InterpState &S)
Interpreter entry point.
Definition Interp.cpp:3637
llvm::APSInt APSInt
Definition FixedPoint.h:20
Top level wrappers for InstallAPI frontend operations.
CheckSubobjectKind
The order of this enum is important for diagnostics.
Definition State.h:42
@ Result
The result type of a method or function.
Definition TypeBase.h:906
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition State.h:26
@ AK_Read
Definition State.h:27
OptionalUnsigned< unsigned > UnsignedOrNone
const FunctionProtoType * T
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t
Describes a memory block created by an allocation site.
Definition Descriptor.h:122
static constexpr unsigned MaxArrayElemBytes
Maximum number of bytes to be used for array elements.
Definition Descriptor.h:142