[clang] [clang][DebugInfo] Make fwd decl call site debug info consistent for methods (PR #222263)

via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 9 01:05:36 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Orlando Cazalet-Hyams (OCHyams)

<details>
<summary>Changes</summary>

Prior to this patch, using `EmitFunctionDecl` for methods results in different fields and flags than if `getFunctionDeclaration` (which calls `CreateCXXMemberFunction`) is used. This can arbitrarily result in differences depending on the shape of the source code (missing `scopeLine` or access flags in some cases which are present in others).

[Compile-time-tracker shows negligible file size impact](https://llvm-compile-time-tracker.com/compare.php?from=d2bd0203bee02681b0a150fb8d2d6563b7e56b2e&to=1b8ab0c426edf89de665ce86481742f39f086054&stat=size-file)

This is needed to provide a stable base in order to have #<!-- -->217042 (fixing metadata ODR uniquing) produce an identical clang.

---
Full diff: https://github.com/llvm/llvm-project/pull/222263.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+10-3) 
- (added) clang/test/DebugInfo/CXX/fwd-decl-for-call-site.cpp (+15) 


``````````diff
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 27db6a3110695..e78c065897e22 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -5181,9 +5181,16 @@ void CGDebugInfo::EmitFuncDeclForCallSite(llvm::CallBase *CallOrInvoke,
   // If there is no DISubprogram attached to the function being called,
   // create the one describing the function in order to have complete
   // call site debug info.
-  if (!CalleeDecl->isStatic() && !CalleeDecl->isInlined())
-    EmitFunctionDecl(CalleeGlobalDecl, CalleeDecl->getLocation(), CalleeType,
-                     Func);
+  if (!CalleeDecl->isStatic() && !CalleeDecl->isInlined()) {
+    if (isa<CXXMethodDecl>(CalleeDecl->getCanonicalDecl())) {
+      auto *SP = getFunctionDeclaration(CalleeDecl);
+      assert(SP && "Couldn't create CXX method DISubprogram?");
+      Func->setSubprogram(SP);
+    } else {
+      EmitFunctionDecl(CalleeGlobalDecl, CalleeDecl->getLocation(), CalleeType,
+                       Func);
+    }
+  }
 }
 
 void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
diff --git a/clang/test/DebugInfo/CXX/fwd-decl-for-call-site.cpp b/clang/test/DebugInfo/CXX/fwd-decl-for-call-site.cpp
new file mode 100644
index 0000000000000..d588561216567
--- /dev/null
+++ b/clang/test/DebugInfo/CXX/fwd-decl-for-call-site.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -O1 -disable-llvm-passes -gcall-site-info -dwarf-version=5 -emit-llvm \
+// RUN:    -debug-info-kind=constructor -triple x86_64-unknown-unknown %s -o - \
+// RUN: | FileCheck %s
+
+// Check that DISubprogram metadata that is attached to methods out of
+// necessity for call-site-info inclues all the fields/info that would be
+// otherwise used for method fwd decls. Tested in this case by checking for the
+// presence of `scopeLine`, which would be omitted if the method were treated
+// as free function fwd decl.
+
+struct a {
+  a();
+} b;
+
+// CHECK: !DISubprogram(name: "a", linkageName: "_ZN1aC4Ev", scope: ![[#]], file: ![[#]], line: [[# @LINE - 3]], type: ![[#]], scopeLine: [[# @LINE - 3]], flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)

``````````

</details>


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


More information about the cfe-commits mailing list