clang 24.0.0git
State.h
Go to the documentation of this file.
1//===--- State.h - State chain for the VM and AST Walker --------*- 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// Defines the base class of the interpreter and evaluator state.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_STATE_H
14#define LLVM_CLANG_AST_INTERP_STATE_H
15
18#include "clang/AST/Expr.h"
20
21namespace clang {
22/// Kinds of access we can perform on an object, for diagnostics. Note that
23/// we consider a member function call to be a kind of access, even though
24/// it is not formally an access of the object, because it has (largely) the
25/// same set of semantic restrictions.
40
41/// The order of this enum is important for diagnostics.
52
53enum class EvaluationMode {
54 /// Evaluate as a constant expression. Stop if we find that the expression
55 /// is not a constant expression.
57
58 /// Evaluate as a constant expression. Stop if we find that the expression
59 /// is not a constant expression. Some expressions can be retried in the
60 /// optimizer if we don't constant fold them here, but in an unevaluated
61 /// context we try to fold them immediately since the optimizer never
62 /// gets a chance to look at it.
64
65 /// Fold the expression to a constant. Stop if we hit a side-effect that
66 /// we can't model.
68
69 /// Evaluate in any way we know how. Don't worry about side-effects that
70 /// can't be modeled.
72};
73
74namespace interp {
75class Frame;
76class SourceInfo;
77
78/// Interface for the VM to interact with the AST walker's context.
79class State {
80public:
83 virtual ~State();
84
85 virtual const Frame *getCurrentFrame() = 0;
86 virtual unsigned getCallStackDepth() = 0;
87 virtual bool stepsLeft() const = 0;
88
90 ASTContext &getASTContext() const { return Ctx; }
91 const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); }
92
93 /// If \c DiagId should be relaxed as per the current evaluation settings,
94 /// emit it as a warning instead of an error. Returns \c true if a relaxed
95 /// diagnostic was emitted, \c false otherwise.
97
98 /// Note that we have had a side-effect, and determine whether we should
99 /// keep evaluating.
100 bool noteSideEffect() const {
101 EvalStatus.HasSideEffects = true;
103 }
104
105 /// Should we continue evaluation as much as possible after encountering a
106 /// construct which can't be reduced to a value?
107 bool keepEvaluatingAfterFailure() const;
108 /// Should we continue evaluation after encountering a side-effect that we
109 /// couldn't model?
111
112 /// Note that we hit something that was technically undefined behavior, but
113 /// that we can evaluate past it (such as signed overflow or floating-point
114 /// division by zero.)
116 EvalStatus.HasUndefinedBehavior = true;
117 return keepEvaluatingAfterUndefinedBehavior();
118 }
119
120 /// Are we checking whether the expression is a potential constant
121 /// expression?
125 /// Are we checking an expression for overflow?
129
130 /// Diagnose that the evaluation could not be folded (FF => FoldFailure)
133 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
134 unsigned ExtraNotes = 0);
135
137 FFDiag(const Expr *E,
138 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
139 unsigned ExtraNotes = 0);
140
143 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
144 unsigned ExtraNotes = 0);
145
146 /// Diagnose that the evaluation does not produce a C++11 core constant
147 /// expression.
148 ///
149 /// FIXME: Stop evaluating if we're in EM_ConstantExpression or
150 /// EM_PotentialConstantExpression mode and we produce one of these.
153 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
154 unsigned ExtraNotes = 0);
155
157 CCEDiag(const Expr *E,
158 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
159 unsigned ExtraNotes = 0);
160
163 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
164 unsigned ExtraNotes = 0);
165
166 /// Add a note to a prior diagnostic.
169
170 /// Add a stack of notes to a prior diagnostic.
172
173 /// Directly reports a diagnostic message.
175
176 /// Whether or not we're in a context where the front end requires a
177 /// constant value.
178 bool InConstantContext = false;
179
180 /// Whether we're checking that an expression is a potential constant
181 /// expression. If so, do not fail on constructs that could become constant
182 /// later on (such as a use of an undefined global).
184
185 /// Whether we're checking for an expression that has undefined behavior.
186 /// If so, we will produce warnings if we encounter an operation that is
187 /// always undefined.
188 ///
189 /// Note that we still need to evaluate the expression normally when this
190 /// is set; this is used when evaluating ICEs in C.
192
196
197private:
198 /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further
199 /// notes attached to it will also be stored, otherwise they will not be.
200 bool HasActiveDiagnostic = false;
201
202 /// Have we emitted a diagnostic explaining why we couldn't constant
203 /// fold (not just why it's not strictly a constant expression)?
204 bool HasFoldFailureDiagnostic = false;
205
206 void addCallStack(unsigned Limit);
207
208 PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId);
209
210 void addExtendedDiag(SourceLocation Loc, diag::kind DiagId);
211
213 unsigned ExtraNotes, bool IsCCEDiag);
214
215 /// Should we continue evaluation after encountering undefined behavior?
216 bool keepEvaluatingAfterUndefinedBehavior() const;
217
218 // If we have a prior diagnostic, it will be noting that the expression
219 // isn't a constant expression. This diagnostic is more important,
220 // unless we require this evaluation to produce a constant expression.
221 //
222 // FIXME: We might want to show both diagnostics to the user in
223 // EvaluationMode::ConstantFold mode.
224 bool hasPriorDiagnostic();
225
226 void setFoldFailureDiagnostic(bool Flag) { HasFoldFailureDiagnostic = Flag; };
227 void setActiveDiagnostic(bool Flag) { HasActiveDiagnostic = Flag; };
228 bool hasActiveDiagnostic() const { return HasActiveDiagnostic; }
229};
230
231} // namespace interp
232} // namespace clang
233
234#endif
Defines the clang::ASTContext interface.
Implements a partial diagnostic which may not be emitted.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
A little helper class used to produce diagnostics.
This represents one expression.
Definition Expr.h:113
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
A partial diagnostic which we might know in advance that we are not going to emit.
Encodes a location in the source.
Base class for stack frames, shared between VM and walker.
Definition Frame.h:28
Describes the statement/declaration an opcode was generated from.
Definition Source.h:77
bool checkingForUndefinedBehavior() const
Are we checking an expression for overflow?
Definition State.h:126
virtual const Frame * getCurrentFrame()=0
EvaluationMode EvalMode
Definition State.h:193
virtual bool stepsLeft() const =0
Expr::EvalStatus & getEvalStatus() const
Definition State.h:89
DiagnosticBuilder report(SourceLocation Loc, diag::kind DiagId)
Directly reports a diagnostic message.
Definition State.cpp:103
bool emitRelaxedDiag(SourceLocation Loc, diag::kind DiagId)
If DiagId should be relaxed as per the current evaluation settings, emit it as a warning instead of a...
Definition State.cpp:20
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
State(ASTContext &ASTCtx, Expr::EvalStatus &EvalStatus)
Definition State.h:81
bool CheckingPotentialConstantExpression
Whether we're checking that an expression is a potential constant expression.
Definition State.h:183
OptionalDiagnostic Note(SourceInfo Loc, diag::kind DiagId)
virtual unsigned getCallStackDepth()=0
bool CheckingForUndefinedBehavior
Whether we're checking for an expression that has undefined behavior.
Definition State.h:191
bool noteSideEffect() const
Note that we have had a side-effect, and determine whether we should keep evaluating.
Definition State.h:100
Expr::EvalStatus & EvalStatus
Definition State.h:195
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
void addNotes(ArrayRef< PartialDiagnosticAt > Diags)
Add a stack of notes to a prior diagnostic.
Definition State.cpp:98
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 checkingPotentialConstantExpression() const
Are we checking whether the expression is a potential constant expression?
Definition State.h:122
bool keepEvaluatingAfterSideEffect() const
Should we continue evaluation after encountering a side-effect that we couldn't model?
Definition State.cpp:230
bool keepEvaluatingAfterFailure() const
Should we continue evaluation as much as possible after encountering a construct which can't be reduc...
Definition State.cpp:214
bool InConstantContext
Whether or not we're in a context where the front end requires a constant value.
Definition State.h:178
ASTContext & Ctx
Definition State.h:194
virtual ~State()
Definition State.cpp:18
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Top level wrappers for InstallAPI frontend operations.
CheckSubobjectKind
The order of this enum is important for diagnostics.
Definition State.h:42
@ CSK_ArrayToPointer
Definition State.h:46
@ CSK_Derived
Definition State.h:44
@ CSK_Base
Definition State.h:43
@ CSK_Real
Definition State.h:48
@ CSK_ArrayIndex
Definition State.h:47
@ CSK_Imag
Definition State.h:49
@ CSK_VectorElement
Definition State.h:50
@ CSK_Field
Definition State.h:45
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition State.h:26
@ AK_TypeId
Definition State.h:34
@ AK_Construct
Definition State.h:35
@ AK_Increment
Definition State.h:30
@ AK_DynamicCast
Definition State.h:33
@ AK_Read
Definition State.h:27
@ AK_Assign
Definition State.h:29
@ AK_IsWithinLifetime
Definition State.h:37
@ AK_MemberCall
Definition State.h:32
@ AK_ReadObjectRepresentation
Definition State.h:28
@ AK_Dereference
Definition State.h:38
@ AK_Destroy
Definition State.h:36
@ AK_Decrement
Definition State.h:31
EvaluationMode
Definition State.h:53
@ ConstantFold
Fold the expression to a constant.
Definition State.h:67
@ ConstantExpressionUnevaluated
Evaluate as a constant expression.
Definition State.h:63
@ ConstantExpression
Evaluate as a constant expression.
Definition State.h:56
@ IgnoreSideEffects
Evaluate in any way we know how.
Definition State.h:71
EvalStatus is a struct with detailed info about an evaluation in progress.
Definition Expr.h:622