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 Block *B, AccessKinds AK);
44
45/// Checks if a pointer is in range.
46template <typename T>
47bool CheckRange(InterpState &S, CodePtr OpPC, T Ptr, AccessKinds AK) {
48 if (!Ptr.isOnePastEnd() && !Ptr.isZeroSizeArray())
49 return true;
50 if (S.getLangOpts().CPlusPlus) {
51 const SourceInfo &Loc = S.Current->getSource(OpPC);
52 S.FFDiag(Loc, diag::note_constexpr_access_past_end)
53 << AK << S.Current->getRange(OpPC);
54 }
55 return false;
56}
57
58/// Checks if a field from which a pointer is going to be derived is valid.
59bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
61
62/// Checks if a pointer points to a mutable field.
63bool CheckMutable(InterpState &S, CodePtr OpPC, PtrView Ptr,
64 AccessKinds AK = AK_Read);
65inline bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
66 AccessKinds AK = AK_Read) {
67 if (!Ptr.isBlockPointer())
68 return true;
69 return CheckMutable(S, OpPC, Ptr.view(), AK);
70}
71
72/// Checks if a value can be loaded from a block.
73bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
74 AccessKinds AK = AK_Read);
75
76/// Diagnose mismatched new[]/delete or new/delete[] pairs.
77bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC,
78 DynamicAllocator::Form AllocForm,
79 DynamicAllocator::Form DeleteForm, const Descriptor *D,
80 const Expr *NewExpr);
81
82/// Copy the contents of Src into Dest.
83bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest);
84
86 unsigned Kind, Pointer &Ptr);
87
88template <typename T>
89bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue) {
90 const Expr *E = S.Current->getExpr(OpPC);
91 S.CCEDiag(E, diag::note_constexpr_overflow) << SrcValue << E->getType();
92 return S.noteUndefinedBehavior();
93}
94
95inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
96 uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
97 if (Limit != 0 && NumElems > Limit) {
98 S.FFDiag(S.Current->getSource(OpPC),
99 diag::note_constexpr_new_exceeds_limits, 1)
100 << NumElems << Limit;
101 S.Note(S.Current->getSource(OpPC), diag::note_constexpr_steps);
102 return false;
103 }
104 return true;
105}
106
107static inline llvm::RoundingMode getRoundingMode(FPOptions FPO) {
108 auto RM = FPO.getRoundingMode();
109 if (RM == llvm::RoundingMode::Dynamic)
110 return llvm::RoundingMode::NearestTiesToEven;
111 return RM;
112}
113
114inline bool Invalid(InterpState &S, CodePtr OpPC) {
115 const SourceLocation &Loc = S.Current->getLocation(OpPC);
116 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr)
117 << S.Current->getRange(OpPC);
118 return false;
119}
120
121template <typename SizeT>
122bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT *NumElements,
123 unsigned ElemSize, bool IsNoThrow) {
124
125 if (ElemSize == 0)
126 return true;
127
128 // FIXME: Both the SizeT::from() as well as the
129 // NumElements.toAPSInt() in this function are rather expensive.
130
131 // Can't be too many elements if the bitwidth of NumElements is lower than
132 // that of Descriptor::MaxArrayElemBytes.
133 if ((NumElements->bitWidth() - NumElements->isSigned()) <
134 (sizeof(Descriptor::MaxArrayElemBytes) * 8))
135 return true;
136
137 // FIXME: GH63562
138 // APValue stores array extents as unsigned,
139 // so anything that is greater that unsigned would overflow when
140 // constructing the array, we catch this here.
141 SizeT MaxElements = SizeT::from(Descriptor::MaxArrayElemBytes / ElemSize);
142 assert(MaxElements.isPositive());
143 if (NumElements->toAPSInt().getActiveBits() >
145 *NumElements > MaxElements) {
146 if (!IsNoThrow) {
147 const SourceInfo &Loc = S.Current->getSource(OpPC);
148
149 if (NumElements->isSigned() && NumElements->isNegative()) {
150 S.FFDiag(Loc, diag::note_constexpr_new_negative)
151 << NumElements->toDiagnosticString(S.getASTContext());
152 } else {
153 S.FFDiag(Loc, diag::note_constexpr_new_too_large)
154 << NumElements->toDiagnosticString(S.getASTContext());
155 }
156 }
157 return false;
158 }
159 return true;
160}
161
162} // namespace interp
163} // namespace clang
164
165#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:223
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
const Expr * getExpr(CodePtr PC) const
SourceInfo getSource(CodePtr PC) const
Map a location to a source.
SourceLocation getLocation(CodePtr PC) const
SourceRange getRange(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:427
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:87
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:38
ASTContext & getASTContext() const
Definition State.h:92
bool noteUndefinedBehavior() const
Note that we hit something that was technically undefined behavior, but that we can evaluate past it ...
Definition State.h:117
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:61
const LangOptions & getLangOpts() const
Definition State.h:93
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:545
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:1238
static llvm::RoundingMode getRoundingMode(FPOptions FPO)
bool Call(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition Interp.cpp:1905
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a value can be loaded from a block.
Definition Interp.cpp:879
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:434
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, uint32_t BuiltinID)
Interpret a builtin function.
bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK)
Checks if a pointer is a dummy pointer.
Definition Interp.cpp:1300
bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the array is offsetable.
Definition Interp.cpp:426
bool CheckMutable(InterpState &S, CodePtr OpPC, PtrView Ptr, AccessKinds AK)
Checks if a pointer points to a mutable field.
Definition Interp.cpp:650
bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems)
bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest)
Copy the contents of Src into Dest.
UnsignedOrNone evaluateBuiltinObjectSize(const ASTContext &ASTCtx, unsigned Kind, Pointer &Ptr)
bool Interpret(InterpState &S)
Interpreter entry point.
Definition Interp.cpp:3270
Top level wrappers for InstallAPI frontend operations.
CheckSubobjectKind
The order of this enum is important for diagnostics.
Definition State.h:44
@ 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:28
@ AK_Read
Definition State.h:29
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