Skip to content

Commit b1a937b

Browse files
committed
[clang][DebugInfo] Emit DW_AT_object_pointer on function definitions with explicit this
We currently don't emit `DW_AT_object_pointer` on function declarations or definitions. The DWARFv5 spec doesn't mandate this attribute be present *only* for implicit object parameters: ``` If the member function entry describes a non-static member function, then that entry has a DW_AT_object_pointer attribute whose value is a reference to the formal parameter entry that corresponds to the object for which the function is called. That parameter also has a DW_AT_artificial attribute whose value is true. ``` The part about `DW_AT_artificial` seems overly restrictive, and not true for explicit object parameters. We probably should relax this part of the DWARF spec. This will help LLDB identify static vs. non-static member functions (see #120856). GCC suffers from the same issue: https://godbolt.org/z/h4jeT54G5 Partially fixes #120974
1 parent 7e01a32 commit b1a937b

File tree

6 files changed

+67
-19
lines changed

6 files changed

+67
-19
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,20 +2016,29 @@ llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
20162016
// First element is always return type. For 'void' functions it is NULL.
20172017
Elts.push_back(Args[0]);
20182018

2019-
// "this" pointer is always first argument.
2020-
// ThisPtr may be null if the member function has an explicit 'this'
2021-
// parameter.
2022-
if (!ThisPtr.isNull()) {
2019+
const bool HasExplicitObjectParameter = ThisPtr.isNull();
2020+
2021+
// "this" pointer is always first argument. For explicit "this"
2022+
// parameters, it will already be in Args[1].
2023+
if (!HasExplicitObjectParameter) {
20232024
llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
20242025
TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
2025-
ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
2026+
ThisPtrType =
2027+
DBuilder.createObjectPointerType(ThisPtrType, /*Implicit=*/true);
20262028
Elts.push_back(ThisPtrType);
20272029
}
20282030

20292031
// Copy rest of the arguments.
20302032
for (unsigned i = 1, e = Args.size(); i != e; ++i)
20312033
Elts.push_back(Args[i]);
20322034

2035+
// Attach FlagObjectPointer to the explicit "this" parameter.
2036+
if (HasExplicitObjectParameter) {
2037+
assert(Elts.size() >= 2 &&
2038+
"Expected at least return type and object parameter.");
2039+
Elts[1] = DBuilder.createObjectPointerType(Args[1], /*Implicit=*/false);
2040+
}
2041+
20332042
llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
20342043

20352044
return DBuilder.createSubroutineType(EltTypeArray, OriginalFunc->getFlags(),
@@ -4829,6 +4838,9 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
48294838
if (IPD->getParameterKind() == ImplicitParamKind::CXXThis ||
48304839
IPD->getParameterKind() == ImplicitParamKind::ObjCSelf)
48314840
Flags |= llvm::DINode::FlagObjectPointer;
4841+
} else if (const auto *PVD = dyn_cast<ParmVarDecl>(VD)) {
4842+
if (PVD->isExplicitObjectParameter())
4843+
Flags |= llvm::DINode::FlagObjectPointer;
48324844
}
48334845

48344846
// Note: Older versions of clang used to emit byval references with an extra
@@ -5115,7 +5127,7 @@ llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
51155127
llvm::DIType *CachedTy = getTypeOrNull(QualTy);
51165128
if (CachedTy)
51175129
Ty = CachedTy;
5118-
return DBuilder.createObjectPointerType(Ty);
5130+
return DBuilder.createObjectPointerType(Ty, /*Implicit=*/true);
51195131
}
51205132

51215133
void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clang_cc1 -x c++ -std=c++23 -debug-info-kind=limited -emit-llvm < %s | FileCheck %s
2+
3+
// CHECK: !DISubprogram(name: "bar",
4+
// CHECK-SAME: flags: DIFlagPrototyped
5+
// CHECK: !DIDerivedType(tag: DW_TAG_pointer_type
6+
// CHECK-SAME: flags: DIFlagArtificial | DIFlagObjectPointer
7+
//
8+
// CHECK: !DISubprogram(name: "explicit_this",
9+
// flags: DIFlagPrototyped
10+
//
11+
// CHECK: !DIDerivedType(tag: DW_TAG_rvalue_reference_type
12+
// CHECK-SAME: flags: DIFlagObjectPointer)
13+
//
14+
// CHECK: !DILocalVariable(name: "this", arg: 1
15+
// CHECK-SAME: flags: DIFlagArtificial | DIFlagObjectPointer
16+
//
17+
// CHECK-NOT: DIFlagArtificial
18+
// CHECK: !DILocalVariable(arg: 1, {{.*}}, flags: DIFlagObjectPointer)
19+
20+
struct Foo {
21+
void bar() {}
22+
void explicit_this(this Foo &&) {}
23+
};
24+
25+
void f() {
26+
Foo{}.bar();
27+
Foo{}.explicit_this();
28+
}

llvm/include/llvm-c/DebugInfo.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,13 +870,16 @@ LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,
870870
LLVMMetadataRef Ty);
871871

872872
/**
873-
* Create a uniqued DIType* clone with FlagObjectPointer and FlagArtificial set.
873+
* Create a uniqued DIType* clone with FlagObjectPointer. If \c Implicit
874+
* is true, then also set FlagArtificial.
874875
* \param Builder The DIBuilder.
875876
* \param Type The underlying type to which this pointer points.
877+
* \param Implicit Indicates whether this pointer was implicitly generated
878+
* (i.e., not spelled out in source).
876879
*/
877-
LLVMMetadataRef
878-
LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
879-
LLVMMetadataRef Type);
880+
LLVMMetadataRef LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
881+
LLVMMetadataRef Type,
882+
LLVMBool Implicit);
880883

881884
/**
882885
* Create debugging information entry for a qualified

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,9 @@ namespace llvm {
662662
/// Create a uniqued clone of \p Ty with FlagArtificial set.
663663
static DIType *createArtificialType(DIType *Ty);
664664

665-
/// Create a uniqued clone of \p Ty with FlagObjectPointer and
666-
/// FlagArtificial set.
667-
static DIType *createObjectPointerType(DIType *Ty);
665+
/// Create a uniqued clone of \p Ty with FlagObjectPointer set.
666+
/// If \p Implicit is true, also set FlagArtificial.
667+
static DIType *createObjectPointerType(DIType *Ty, bool Implicit);
668668

669669
/// Create a permanent forward-declared type.
670670
DICompositeType *createForwardDecl(unsigned Tag, StringRef Name,

llvm/lib/IR/DIBuilder.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,15 @@ DIType *DIBuilder::createArtificialType(DIType *Ty) {
644644
return createTypeWithFlags(Ty, DINode::FlagArtificial);
645645
}
646646

647-
DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
647+
DIType *DIBuilder::createObjectPointerType(DIType *Ty, bool Implicit) {
648648
// FIXME: Restrict this to the nodes where it's valid.
649649
if (Ty->isObjectPointer())
650650
return Ty;
651-
DINode::DIFlags Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
651+
DINode::DIFlags Flags = DINode::FlagObjectPointer;
652+
653+
if (Implicit)
654+
Flags |= DINode::FlagArtificial;
655+
652656
return createTypeWithFlags(Ty, Flags);
653657
}
654658

llvm/lib/IR/DebugInfo.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,10 +1432,11 @@ LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,
14321432
PropertyAttributes, unwrapDI<DIType>(Ty)));
14331433
}
14341434

1435-
LLVMMetadataRef
1436-
LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
1437-
LLVMMetadataRef Type) {
1438-
return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
1435+
LLVMMetadataRef LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
1436+
LLVMMetadataRef Type,
1437+
LLVMBool Implicit) {
1438+
return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type),
1439+
Implicit));
14391440
}
14401441

14411442
LLVMMetadataRef

0 commit comments

Comments
 (0)