================
@@ -1526,6 +1526,44 @@ StmtResult Parser::ParseIfStatement(SourceLocation 
*TrailingElseLoc) {
   SourceLocation ElseStmtLoc;
   StmtResult ElseStmt;
 
+  static llvm::DenseMap<const IdentifierInfo*, SourceLocation> UsedCondVars;
+  const VarDecl *VD = dyn_cast_or_null<VarDecl>(Cond.get().first);
+
+  if (VD) {
+    SourceLocation Loc = VD->getLocation();
+
+    if (!UsedCondVars.count(VD->getIdentifier()))
+      UsedCondVars[VD->getIdentifier()] = Loc;
+  }
+
+  std::function<void(const Stmt *)> DiagnoseOutOfScopeUse = [&](const Stmt *S) 
{
+    if (!S) return;
+
+    for (const Stmt *Child : S->children()) {
+      if (!Child) continue;
+
+      if (const auto *DRE = dyn_cast<DeclRefExpr>(Child)) {
+        if (const auto *UsedVD = dyn_cast<VarDecl>(DRE->getDecl())) {
+          if (UsedCondVars.count(UsedVD->getIdentifier()) && UsedVD != VD) {
+            if (Diags.isIgnored(diag::warn_out_of_scope_var_usage, 
DRE->getExprLoc()))
+              continue; 
+
+            Diag(DRE->getExprLoc(), diag::warn_out_of_scope_var_usage)
+              << UsedVD->getName();
+          }
+        }
+      }
+
+      DiagnoseOutOfScopeUse(Child);
+    }
+  };
+
+  if (ThenStmt.get())
+    DiagnoseOutOfScopeUse(ThenStmt.get());
+ 
+  if (ElseStmt.get())
+    DiagnoseOutOfScopeUse(ElseStmt.get());
----------------
arrowten wrote:

@erichkeane @Sirraide Please take a look at the new changes. If it still does 
not work, then it would require some bit of help. Thanks.

https://github.com/llvm/llvm-project/pull/156436
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to