[PATCH] D144181: [clang][DebugInfo] Add abi-tags on constructors/destructors as LLVM annotations

Michael Buch via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 16 05:33:13 PST 2023


Michael137 updated this revision to Diff 497986.
Michael137 added a comment.

- Remove leftover comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144181/new/

https://reviews.llvm.org/D144181

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/test/CodeGen/attr-abi_tag-ctors-dtors.cpp


Index: clang/test/CodeGen/attr-abi_tag-ctors-dtors.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGen/attr-abi_tag-ctors-dtors.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang -g -S -emit-llvm -o - %s | FileCheck %s
+
+struct Foo {
+  [[gnu::abi_tag("Ctor")]] Foo() {}
+  [[gnu::abi_tag("Dtor")]] ~Foo() {}
+};
+
+Foo f;
+
+// CHECK:      ![[#]] = !DISubprogram(name: "Foo",
+// CHECK-SAME: flags: DIFlagPrototyped, spFlags: [[#]], annotations: ![[CTOR_ANNOTS:[0-9]+]])
+// CHECK:      ![[CTOR_ANNOTS]] = !{![[CTOR:[0-9]+]]}
+// CHECK:      ![[CTOR]] = !{!"abi_tag", !"Ctor"}
+// CHECK:      ![[#]] = !DISubprogram(name: "~Foo",
+// CHECK-SAME: flags: DIFlagPrototyped, spFlags: [[#]], annotations: ![[DTOR_ANNOTS:[0-9]+]])
+// CHECK:      ![[DTOR_ANNOTS]] = !{![[DTOR:[0-9]+]]}
+// CHECK:      ![[DTOR]] = !{!"abi_tag", !"Dtor"}
+// CHECK:      ![[#]] = distinct !DISubprogram(name: "Foo",
+// FCHECK-SAME: flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: ![[#]], declaration: ![[#]], retainedNodes: ![[#]], annotations: ![[CTOR_ANNOTS]])
+// FCHECK:      ![[#]] = distinct !DISubprogram(name: "~Foo",
+// FCHECK-SAME: flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: ![[#]], declaration: ![[#]], retainedNodes: ![[#]], annotations: ![[DTOR_ANNOTS]])
+// FCHECK:      ![[#]] = distinct !DISubprogram(name: "Foo",
+// FCHECK-SAME: flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: ![[#]], declaration: ![[#]], retainedNodes: ![[#]], annotations: ![[CTOR_ANNOTS]])
+// FCHECK:      ![[#]] = distinct !DISubprogram(name: "~Foo",
+// FCHECK-SAME: flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: ![[#]], declaration: ![[#]], retainedNodes: ![[#]], annotations: ![[DTOR_ANNOTS]])
Index: clang/lib/CodeGen/CGDebugInfo.h
===================================================================
--- clang/lib/CodeGen/CGDebugInfo.h
+++ clang/lib/CodeGen/CGDebugInfo.h
@@ -303,6 +303,10 @@
   /// A helper function to collect debug info for btf_decl_tag annotations.
   llvm::DINodeArray CollectBTFDeclTagAnnotations(const Decl *D);
 
+  /// A helper function to collect debug info for abi_tag annotations.
+  /// Returns a null DINodeArray if 'D' doesn't have any 'clang::AbiTagAttr's.
+  llvm::DINodeArray CollectAbiTagAnnotations(const Decl *D);
+
   llvm::DIType *createFieldType(StringRef name, QualType type,
                                 SourceLocation loc, AccessSpecifier AS,
                                 uint64_t offsetInBits, uint32_t AlignInBits,
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===================================================================
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -1842,11 +1842,19 @@
       SPFlags |= llvm::DISubprogram::SPFlagDeleted;
   };
 
+  llvm::DINodeArray AbiTagAnnotations = nullptr;
+
   switch (Method->getKind()) {
 
   case Decl::CXXConstructor:
   case Decl::CXXDestructor:
     checkAttrDeleted(Method);
+
+    // Add annotations for abi_tag's for constructors/destructors
+    // because their declarations don't carry linkage names (which
+    // encodes the existance of abi-tags). LLDB uses these annotations
+    // to resolve calls to abi-tagged constructors/destructors.
+    AbiTagAnnotations = CollectAbiTagAnnotations(Method);
     break;
   case Decl::CXXMethod:
     if (Method->isCopyAssignmentOperator() ||
@@ -1893,7 +1901,7 @@
   llvm::DISubprogram *SP = DBuilder.createMethod(
       RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
       MethodTy, VIndex, ThisAdjustment, ContainingType, Flags, SPFlags,
-      TParamsArray.get());
+      TParamsArray.get(), nullptr, AbiTagAnnotations);
 
   SPCache[Method->getCanonicalDecl()].reset(SP);
 
@@ -2195,6 +2203,21 @@
   return DBuilder.getOrCreateArray(Annotations);
 }
 
+llvm::DINodeArray CGDebugInfo::CollectAbiTagAnnotations(const Decl *D) {
+  if (!D->hasAttr<AbiTagAttr>())
+    return nullptr;
+
+  SmallVector<llvm::Metadata *, 4> Annotations;
+  auto const *Attr = D->getAttr<AbiTagAttr>();
+  for (const auto Tag : Attr->tags()) {
+    llvm::Metadata *Ops[2] = {
+        llvm::MDString::get(CGM.getLLVMContext(), StringRef("abi_tag")),
+        llvm::MDString::get(CGM.getLLVMContext(), Tag)};
+    Annotations.push_back(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
+  }
+  return DBuilder.getOrCreateArray(Annotations);
+}
+
 llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
   if (VTablePtrType)
     return VTablePtrType;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144181.497986.patch
Type: text/x-patch
Size: 4532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230216/8da71b89/attachment.bin>


More information about the cfe-commits mailing list