[clang] [CallGraph] Collect callables from global variable initializers (PR #206458)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 29 06:33:26 PDT 2026


=?utf-8?q?Gábor_Horváth?= <xazax.hun at gmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/206458 at github.com>


================
@@ -196,6 +196,19 @@ void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
   }
 }
 
+void CallGraph::addNodesForVarInit(VarDecl *VD) {
+  // Only variables with static or thread storage duration need this: their
+  // initializers run with no function caller and are not reached by any body
+  // walk, so a lambda or block defined in one would be missed. Local variables
+  // are covered by the enclosing body walk, and parameter default arguments by
+  // CGBuilder at call sites. Attach discovered calls to the root.
+  if (!VD->hasGlobalStorage())
+    return;
+  if (Expr *Init = VD->getInit()) {
+    CGBuilder{this, Root}.Visit(Init);
+  }
----------------
usx95 wrote:

nit: You can remove the braces for the `if` body.

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


More information about the cfe-commits mailing list