[clang] [llvm] Reland "[DebugMetadata][DwarfDebug] Support function-local types in lexical block scopes (4/7)" (PR #165032)

Adrian Prantl via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 15 16:15:53 PST 2025


================
@@ -2556,35 +2556,59 @@ class DISubprogram : public DILocalScope {
 
   /// For the given retained node of DISubprogram, applies one of the
   /// given functions depending on the type of the node.
-  template <typename T, typename FuncLVT, typename FuncLabelT,
-            typename FuncImportedEntityT, typename FuncUnknownT>
-  static T
-  visitRetainedNode(const Metadata *N, FuncLVT &&FuncLV, FuncLabelT &&FuncLabel,
-                    FuncImportedEntityT &&FuncIE, FuncUnknownT &&FuncUnknown) {
-    if (const auto *LV = dyn_cast<DILocalVariable>(N))
+  template <typename T, typename MetadataT, typename FuncLVT,
+            typename FuncLabelT, typename FuncImportedEntityT,
+            typename FuncTypeT, typename FuncUnknownT>
+  static T visitRetainedNode(MetadataT *N, FuncLVT &&FuncLV,
+                             FuncLabelT &&FuncLabel,
+                             FuncImportedEntityT &&FuncIE, FuncTypeT &&FuncType,
+                             FuncUnknownT &&FuncUnknown) {
+    static_assert(std::is_base_of_v<Metadata, MetadataT>,
+                  "N must point to Metadata or const Metadata");
+
+    if (auto *LV = dyn_cast<DILocalVariable>(N))
       return FuncLV(LV);
-    if (const auto *L = dyn_cast<DILabel>(N))
+    if (auto *L = dyn_cast<DILabel>(N))
       return FuncLabel(L);
-    if (const auto *IE = dyn_cast<DIImportedEntity>(N))
+    if (auto *IE = dyn_cast<DIImportedEntity>(N))
       return FuncIE(IE);
+    if (auto *Ty = dyn_cast<DIType>(N))
+      return FuncType(Ty);
     return FuncUnknown(N);
   }
 
   /// Returns the scope of subprogram's retainedNodes.
   static const DILocalScope *getRetainedNodeScope(const MDNode *N);
+  static DILocalScope *getRetainedNodeScope(MDNode *N);
   // For use in Verifier.
   static const DIScope *getRawRetainedNodeScope(const MDNode *N);
+  static DIScope *getRawRetainedNodeScope(MDNode *N);
 
   /// For each retained node, applies one of the given functions depending
   /// on the type of a node.
-  template <typename FuncLVT, typename FuncLabelT, typename FuncImportedEntityT>
+  template <typename FuncLVT, typename FuncLabelT, typename FuncImportedEntityT,
+            typename FuncTypeT>
   void forEachRetainedNode(FuncLVT &&FuncLV, FuncLabelT &&FuncLabel,
-                           FuncImportedEntityT &&FuncIE) const {
+                           FuncImportedEntityT &&FuncIE, FuncTypeT &&FuncType) {
     for (MDNode *N : getRetainedNodes())
-      visitRetainedNode<void>(N, FuncLV, FuncLabel, FuncIE,
-                              [](const Metadata *N) {
-                                llvm_unreachable("Unexpected retained node!");
-                              });
+      visitRetainedNode<void>(
+          N, FuncLV, FuncLabel, FuncIE, FuncType,
+          [](auto *N) { llvm_unreachable("Unexpected retained node!"); });
+  }
+
+  /// Remove types that do not belong to the subprogram's scope from
+  /// retainedNodes list.
----------------
adrian-prantl wrote:

It would help to comment here under what circumstances such a situation might arise.

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


More information about the llvm-commits mailing list