Skip to content

Commit

Permalink
[clang][Interp] Don't diagnose non-const reads from the evaluating decl
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaederr committed Jun 27, 2024
1 parent 6d61d83 commit 50b1534
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
2 changes: 2 additions & 0 deletions clang/lib/AST/Interp/EvalEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ EvaluationResult EvalEmitter::interpretExpr(const Expr *E,
EvaluationResult EvalEmitter::interpretDecl(const VarDecl *VD,
bool CheckFullyInitialized) {
this->CheckFullyInitialized = CheckFullyInitialized;
S.EvaluatingDecl = VD;

if (const Expr *Init = VD->getAnyInitializer()) {
QualType T = VD->getType();
Expand All @@ -69,6 +70,7 @@ EvaluationResult EvalEmitter::interpretDecl(const VarDecl *VD,
if (!this->visitDecl(VD, S.inConstantContext()) && EvalResult.empty())
EvalResult.setInvalid();

S.EvaluatingDecl = nullptr;
return std::move(this->EvalResult);
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/Interp/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
};

if (const auto *D = Desc->asVarDecl();
D && D->hasGlobalStorage() && !IsConstType(D)) {
D && D->hasGlobalStorage() && D != S.EvaluatingDecl && !IsConstType(D)) {
diagnoseNonConstVariable(S, OpPC, D);
return S.inConstantContext();
}
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/AST/Interp/InterpState.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class InterpState final : public State, public SourceMapper {
InterpFrame *Current = nullptr;
/// Source location of the evaluating expression
SourceLocation EvalLocation;
/// Declaration we're initializing/evaluting, if any.
const VarDecl *EvaluatingDecl = nullptr;
};

} // namespace interp
Expand Down

0 comments on commit 50b1534

Please sign in to comment.