[clang] [Sema] Diagnose use of if/else-if condition variable inside else-if/else branch(s) (PR #156436)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 30 06:19:34 PDT 2025


================
@@ -2425,6 +2425,33 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK,
     if (const auto *BE = BD->getBinding())
       E->setObjectKind(BE->getObjectKind());
 
+  auto IsScopeContaining = [](Scope *S, Scope *DeclaredScope) -> bool {
+    while (S) {
+      if (S == DeclaredScope)
+        return true;
+
+      // If we hit an if/else scope boundary, stop searching
+      if (S->getFlags() & Scope::ControlScope)
+        return false;
+
+      S = S->getParent();
+    }
+
+    return false;
+  };
+
+  if (auto *VD = dyn_cast_or_null<VarDecl>(D)) {
+    auto It = IfScopeVars.find(VD);
+
+    if (It != IfScopeVars.end()) {
----------------
erichkeane wrote:

This whole bit looks expensive, we should jsut be able to walk up the Scope stack and find the appropriate scope, and pick check whether it has the var.

ALSO, the var decl probably needs to get its canonical version.

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


More information about the cfe-commits mailing list