Author: Timm Bäder
Date: 2024-06-27T18:01:11+02:00
New Revision: 50b15341182e0a5f53dd6e9b4a00fef56f31147c

URL: 
https://github.com/llvm/llvm-project/commit/50b15341182e0a5f53dd6e9b4a00fef56f31147c
DIFF: 
https://github.com/llvm/llvm-project/commit/50b15341182e0a5f53dd6e9b4a00fef56f31147c.diff

LOG: [clang][Interp] Don't diagnose non-const reads from the evaluating decl

Added: 
    

Modified: 
    clang/lib/AST/Interp/EvalEmitter.cpp
    clang/lib/AST/Interp/Interp.cpp
    clang/lib/AST/Interp/InterpState.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/EvalEmitter.cpp 
b/clang/lib/AST/Interp/EvalEmitter.cpp
index 77ff901634a46..a13cd94b0ba1c 100644
--- a/clang/lib/AST/Interp/EvalEmitter.cpp
+++ b/clang/lib/AST/Interp/EvalEmitter.cpp
@@ -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();
@@ -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);
 }
 

diff  --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp
index fc4c0058fbda4..76a0751a006e4 100644
--- a/clang/lib/AST/Interp/Interp.cpp
+++ b/clang/lib/AST/Interp/Interp.cpp
@@ -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();
   }

diff  --git a/clang/lib/AST/Interp/InterpState.h 
b/clang/lib/AST/Interp/InterpState.h
index 0938a723a76d0..925395b6eba0c 100644
--- a/clang/lib/AST/Interp/InterpState.h
+++ b/clang/lib/AST/Interp/InterpState.h
@@ -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


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to