[clang] [Clang] Consider reachability for file-scope warnings on initializers (PR #163885)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 19 00:26:11 PDT 2025


================
@@ -13727,6 +13734,16 @@ void Sema::DiagnoseUniqueObjectDuplication(const VarDecl *VD) {
 }
 
 void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
+  // RAII helper to ensure DeclForInitializer is cleared on all exit paths
+  struct ClearDeclForInitializer {
+    Sema &S;
+    ClearDeclForInitializer(Sema &S) : S(S) {}
+    ~ClearDeclForInitializer() {
+      if (!S.ExprEvalContexts.empty())
+        S.ExprEvalContexts.back().DeclForInitializer = nullptr;
+    }
+  } Clearer(*this);
----------------
cor3ntin wrote:

It would be simpler to use `make_scope_exit` here

https://github.com/llvm/llvm-project/pull/163885


More information about the cfe-commits mailing list