clang 23.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.
46bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
47 AccessKinds AK);
48
49/// Checks if a field from which a pointer is going to be derived is valid.
50bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
52
53/// Checks if a pointer points to a mutable field.
54bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
55
56/// Checks if a value can be loaded from a block.
57bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
58 AccessKinds AK = AK_Read);
59
60/// Diagnose mismatched new[]/delete or new/delete[] pairs.
62 DynamicAllocator::Form AllocForm,
63 DynamicAllocator::Form DeleteForm, const Descriptor *D,
64 const Expr *NewExpr);
65
66/// Copy the contents of Src into Dest.
67bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest);
68
69UnsignedOrNone evaluateBuiltinObjectSize(const ASTContext &ASTCtx,
70 unsigned Kind, Pointer &Ptr);
71
72template <typename T>
73static bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue) {
74 const Expr *E = S.Current->getExpr(OpPC);
75 S.CCEDiag(E, diag::note_constexpr_overflow) << SrcValue << E->getType();
76 return S.noteUndefinedBehavior();
77}
78
79inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
80 uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
81 if (Limit != 0 && NumElems > Limit) {
82 S.FFDiag(S.Current->getSource(OpPC),
83 diag::note_constexpr_new_exceeds_limits)
84 << NumElems << Limit;
85 return false;
86 }
87 return true;
88}
89
90static inline llvm::RoundingMode getRoundingMode(FPOptions FPO) {
91 auto RM = FPO.getRoundingMode();
92 if (RM == llvm::RoundingMode::Dynamic)
93 return llvm::RoundingMode::NearestTiesToEven;
94 return RM;
95}
96
97inline bool Invalid(InterpState &S, CodePtr OpPC) {
98 const SourceLocation &Loc = S.Current->getLocation(OpPC);
99 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr)
100 << S.Current->getRange(OpPC);
101 return false;
102}
103
104template <typename SizeT>
105bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT *NumElements,
106 unsigned ElemSize, bool IsNoThrow) {
107 // FIXME: Both the SizeT::from() as well as the
108 // NumElements.toAPSInt() in this function are rather expensive.
109
110 // Can't be too many elements if the bitwidth of NumElements is lower than
111 // that of Descriptor::MaxArrayElemBytes.
112 if ((NumElements->bitWidth() - NumElements->isSigned()) <
113 (sizeof(Descriptor::MaxArrayElemBytes) * 8))
114 return true;
115
116 // FIXME: GH63562
117 // APValue stores array extents as unsigned,
118 // so anything that is greater that unsigned would overflow when
119 // constructing the array, we catch this here.
120 SizeT MaxElements = SizeT::from(Descriptor::MaxArrayElemBytes / ElemSize);
121 assert(MaxElements.isPositive());
122 if (NumElements->toAPSInt().getActiveBits() >
124 *NumElements > MaxElements) {
125 if (!IsNoThrow) {
126 const SourceInfo &Loc = S.Current->getSource(OpPC);
127
128 if (NumElements->isSigned() && NumElements->isNegative()) {
129 S.FFDiag(Loc, diag::note_constexpr_new_negative)
130 << NumElements->toDiagnosticString(S.getASTContext());
131 } else {
132 S.FFDiag(Loc, diag::note_constexpr_new_too_large)
133 << NumElements->toDiagnosticString(S.getASTContext());
134 }
135 }
136 return false;
137 }
138 return true;
139}
140
141} // namespace interp
142} // namespace clang
143
144#endif // LLVM_CLANG_AST_INTERP_INTERPHELPERS_H
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2946
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:255
This represents one expression.
Definition Expr.h:112
QualType getType() const
Definition Expr.h:144
RoundingMode getRoundingMode() const
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition Expr.h:2530
Encodes a location in the source.
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
Pointer into the code segment.
Definition Source.h:30
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:36
InterpFrame * Current
The current frame.
A pointer to a memory block, live or dead.
Definition Pointer.h:93
Describes the statement/declaration an opcode was generated from.
Definition Source.h:74
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
ASTContext & getASTContext() const
Definition State.h:93
bool noteUndefinedBehavior() const
Note that we hit something that was technically undefined behavior, but that we can evaluate past it ...
Definition State.h:113
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 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:1150
static llvm::RoundingMode getRoundingMode(FPOptions FPO)
bool Call(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition Interp.cpp:1685
bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a pointer points to a mutable field.
Definition Interp.cpp:622
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a value can be loaded from a block.
Definition Interp.cpp:824
bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E, ArrayRef< int64_t > ArrayIndices, int64_t &IntResult)
Interpret an offsetof operation.
bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is in range.
Definition Interp.cpp:547
bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is live and accessible.
Definition Interp.cpp:441
static bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue)
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:1216
bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the array is offsetable.
Definition Interp.cpp:433
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:2591
The JSON file list parser is used to communicate input to InstallAPI.
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:905
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition State.h:28
@ AK_Read
Definition State.h:29
Describes a memory block created by an allocation site.
Definition Descriptor.h:121
static constexpr unsigned MaxArrayElemBytes
Maximum number of bytes to be used for array elements.
Definition Descriptor.h:147