Skip to content

Commit 10fdd09

Browse files
authored
[clang][DebugInfo] Emit DW_AT_object_pointer on function declarations with explicit this (llvm#122928)
In llvm#122897 we started attaching `DW_AT_object_pointer` to function definitions. This patch does the same but for function declarations (which we do for implicit object pointers already). Fixes llvm#120974
1 parent fec503d commit 10fdd09

File tree

6 files changed

+39
-23
lines changed

6 files changed

+39
-23
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 15 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 && Args.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(),
@@ -5118,7 +5127,7 @@ llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
51185127
llvm::DIType *CachedTy = getTypeOrNull(QualTy);
51195128
if (CachedTy)
51205129
Ty = CachedTy;
5121-
return DBuilder.createObjectPointerType(Ty);
5130+
return DBuilder.createObjectPointerType(Ty, /*Implicit=*/true);
51225131
}
51235132

51245133
void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(

clang/test/CodeGenCXX/debug-info-object-pointer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
// CHECK: !DIDerivedType(tag: DW_TAG_pointer_type
66
// CHECK-SAME: flags: DIFlagArtificial | DIFlagObjectPointer
77
//
8-
// // FIXME: DIFlagObjectPointer not attached to the explicit object
9-
// // argument in the subprogram declaration.
108
// CHECK: !DISubprogram(name: "explicit_this",
119
// flags: DIFlagPrototyped
12-
// CHECK-NOT: DIFlagObjectPointer
13-
// CHECK-NOT: DIFlagArtificial
10+
//
11+
// CHECK: !DIDerivedType(tag: DW_TAG_rvalue_reference_type
12+
// CHECK-SAME: flags: DIFlagObjectPointer)
1413
//
1514
// CHECK: !DILocalVariable(name: "this", arg: 1
1615
// CHECK-SAME: flags: DIFlagArtificial | DIFlagObjectPointer

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)