Skip to content

Commit 38d0a55

Browse files
committed
Merge commit '737e4216c537' from llvm.org/main into next
Conflicts: clang/lib/CodeGen/CGDebugInfo.cpp llvm/include/llvm/IR/DIBuilder.h llvm/lib/IR/DIBuilder.cpp
2 parents 1e20c5d + 737e421 commit 38d0a55

File tree

11 files changed

+385
-61
lines changed

11 files changed

+385
-61
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 153 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,28 @@ static llvm::dwarf::Tag getNextQualifier(Qualifiers &Q) {
929929
return (llvm::dwarf::Tag)0;
930930
}
931931

932-
llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
933-
llvm::DIFile *Unit) {
932+
// Strip MacroQualifiedTypeLoc and AttributedTypeLoc
933+
// as their corresponding types will be ignored
934+
// during code generation. Stripping them allows
935+
// to maintain proper TypeLoc for a given type
936+
// during code generation.
937+
static TypeLoc StripMacroAttributed(TypeLoc TL) {
938+
if (!TL)
939+
return TL;
940+
941+
while (true) {
942+
if (auto MTL = TL.getAs<MacroQualifiedTypeLoc>())
943+
TL = MTL.getInnerLoc();
944+
else if (auto ATL = TL.getAs<AttributedTypeLoc>())
945+
TL = ATL.getModifiedLoc();
946+
else
947+
break;
948+
}
949+
return TL;
950+
}
951+
952+
llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile *Unit,
953+
TypeLoc TL) {
934954
QualifierCollector Qc;
935955
const Type *T = Qc.strip(Ty);
936956

@@ -953,7 +973,15 @@ llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
953973
return getOrCreateType(QualType(T, 0), Unit);
954974
}
955975

956-
auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
976+
QualType NextTy = Qc.apply(CGM.getContext(), T);
977+
TypeLoc NextTL;
978+
if (NextTy.hasQualifiers())
979+
NextTL = TL;
980+
else if (TL) {
981+
if (auto QTL = TL.getAs<QualifiedTypeLoc>())
982+
NextTL = StripMacroAttributed(QTL.getNextTypeLoc());
983+
}
984+
auto *FromTy = getOrCreateType(NextTy, Unit, NextTL);
957985

958986
// No need to fill in the Name, Line, Size, Alignment, Offset in case of
959987
// CVR derived types.
@@ -997,10 +1025,10 @@ llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
9971025
Ty->getPointeeType(), Unit);
9981026
}
9991027

1000-
llvm::DIType *
1001-
CGDebugInfo::CreateType(const PointerType *Ty, llvm::DIFile *Unit) {
1028+
llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty, llvm::DIFile *Unit,
1029+
TypeLoc TL) {
10021030
return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
1003-
Ty->getPointeeType(), Unit);
1031+
Ty->getPointeeType(), Unit, TL);
10041032
}
10051033

10061034
/// \return whether a C++ mangling exists for the type defined by TD.
@@ -1138,9 +1166,11 @@ CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
11381166
return RetTy;
11391167
}
11401168

1141-
llvm::DIType *CGDebugInfo::CreatePointerLikeType(
1142-
llvm::dwarf::Tag Tag, const Type *Ty, QualType PointeeTy,
1143-
llvm::DIFile *Unit) {
1169+
llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
1170+
const Type *Ty,
1171+
QualType PointeeTy,
1172+
llvm::DIFile *Unit,
1173+
TypeLoc TL) {
11441174
// Bit size, align and offset of the type.
11451175
// Size is always the size of a pointer. We can't use getTypeSize here
11461176
// because that does not return the correct value for references.
@@ -1150,13 +1180,52 @@ llvm::DIType *CGDebugInfo::CreatePointerLikeType(
11501180
Optional<unsigned> DWARFAddressSpace =
11511181
CGM.getTarget().getDWARFAddressSpace(AddressSpace);
11521182

1183+
llvm::DINodeArray Annotations = nullptr;
1184+
TypeLoc NextTL;
1185+
if (TL) {
1186+
SmallVector<llvm::Metadata *, 4> Annots;
1187+
NextTL = TL.getNextTypeLoc();
1188+
if (NextTL) {
1189+
// Traverse all MacroQualifiedTypeLoc, QualifiedTypeLoc and
1190+
// AttributedTypeLoc type locations so we can collect
1191+
// BTFTypeTag attributes for this pointer.
1192+
while (true) {
1193+
if (auto MTL = NextTL.getAs<MacroQualifiedTypeLoc>()) {
1194+
NextTL = MTL.getInnerLoc();
1195+
} else if (auto QTL = NextTL.getAs<QualifiedTypeLoc>()) {
1196+
NextTL = QTL.getNextTypeLoc();
1197+
} else if (auto ATL = NextTL.getAs<AttributedTypeLoc>()) {
1198+
if (const auto *A = ATL.getAttrAs<BTFTypeTagAttr>()) {
1199+
StringRef BTFTypeTag = A->getBTFTypeTag();
1200+
if (!BTFTypeTag.empty()) {
1201+
llvm::Metadata *Ops[2] = {
1202+
llvm::MDString::get(CGM.getLLVMContext(),
1203+
StringRef("btf_type_tag")),
1204+
llvm::MDString::get(CGM.getLLVMContext(), BTFTypeTag)};
1205+
Annots.insert(Annots.begin(),
1206+
llvm::MDNode::get(CGM.getLLVMContext(), Ops));
1207+
}
1208+
}
1209+
NextTL = ATL.getModifiedLoc();
1210+
} else {
1211+
break;
1212+
}
1213+
}
1214+
}
1215+
1216+
NextTL = StripMacroAttributed(TL.getNextTypeLoc());
1217+
if (Annots.size() > 0)
1218+
Annotations = DBuilder.getOrCreateArray(Annots);
1219+
}
1220+
11531221
if (Tag == llvm::dwarf::DW_TAG_reference_type ||
11541222
Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
11551223
return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
11561224
Size, Align, DWARFAddressSpace);
11571225
else
1158-
return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
1159-
Align, DWARFAddressSpace);
1226+
return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit, NextTL),
1227+
Size, Align, DWARFAddressSpace,
1228+
StringRef(), Annotations);
11601229
}
11611230

11621231
llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
@@ -1273,8 +1342,11 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
12731342

12741343
llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
12751344
llvm::DIFile *Unit) {
1345+
TypeLoc TL;
1346+
if (const TypeSourceInfo *TSI = Ty->getDecl()->getTypeSourceInfo())
1347+
TL = TSI->getTypeLoc();
12761348
llvm::DIType *Underlying =
1277-
getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
1349+
getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit, TL);
12781350

12791351
if (Ty->getDecl()->hasAttr<NoDebugAttr>())
12801352
return Underlying;
@@ -1347,7 +1419,7 @@ static llvm::DINode::DIFlags getRefFlags(const FunctionProtoType *Func) {
13471419
}
13481420

13491421
llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
1350-
llvm::DIFile *Unit) {
1422+
llvm::DIFile *Unit, TypeLoc TL) {
13511423
const auto *FPT = dyn_cast<FunctionProtoType>(Ty);
13521424
if (FPT) {
13531425
if (llvm::DIType *QTy = CreateQualifiedType(FPT, Unit))
@@ -1359,17 +1431,41 @@ llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
13591431
SmallVector<llvm::Metadata *, 16> EltTys;
13601432

13611433
// Add the result type at least.
1362-
EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
1434+
TypeLoc RetTL;
1435+
if (TL) {
1436+
if (auto FTL = TL.getAs<FunctionTypeLoc>())
1437+
RetTL = FTL.getReturnLoc();
1438+
}
1439+
EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit, RetTL));
13631440

13641441
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
13651442
// Set up remainder of arguments if there is a prototype.
13661443
// otherwise emit it as a variadic function.
1367-
if (!FPT)
1444+
if (!FPT) {
13681445
EltTys.push_back(DBuilder.createUnspecifiedParameter());
1369-
else {
1446+
} else {
13701447
Flags = getRefFlags(FPT);
1371-
for (const QualType &ParamType : FPT->param_types())
1372-
EltTys.push_back(getOrCreateType(ParamType, Unit));
1448+
bool DoneWithTL = false;
1449+
if (TL) {
1450+
if (auto FTL = TL.getAs<FunctionTypeLoc>()) {
1451+
DoneWithTL = true;
1452+
int Idx = 0;
1453+
for (const QualType &ParamType : FPT->param_types()) {
1454+
TypeLoc ParamTL;
1455+
if (ParmVarDecl *Param = FTL.getParam(Idx)) {
1456+
if (const TypeSourceInfo *TSI = Param->getTypeSourceInfo())
1457+
ParamTL = TSI->getTypeLoc();
1458+
}
1459+
EltTys.push_back(getOrCreateType(ParamType, Unit, ParamTL));
1460+
Idx++;
1461+
}
1462+
}
1463+
}
1464+
1465+
if (!DoneWithTL) {
1466+
for (const QualType &ParamType : FPT->param_types())
1467+
EltTys.push_back(getOrCreateType(ParamType, Unit));
1468+
}
13731469
if (FPT->isVariadic())
13741470
EltTys.push_back(DBuilder.createUnspecifiedParameter());
13751471
}
@@ -1440,11 +1536,13 @@ llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
14401536
Flags, DebugType, Annotations);
14411537
}
14421538

1443-
llvm::DIType *CGDebugInfo::createFieldType(
1444-
StringRef name, QualType type, SourceLocation loc, AccessSpecifier AS,
1445-
uint64_t offsetInBits, uint32_t AlignInBits, llvm::DIFile *tunit,
1446-
llvm::DIScope *scope, const RecordDecl *RD, llvm::DINodeArray Annotations) {
1447-
llvm::DIType *debugType = getOrCreateType(type, tunit);
1539+
llvm::DIType *
1540+
CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1541+
AccessSpecifier AS, uint64_t offsetInBits,
1542+
uint32_t AlignInBits, llvm::DIFile *tunit,
1543+
llvm::DIScope *scope, const RecordDecl *RD,
1544+
llvm::DINodeArray Annotations, TypeLoc TL) {
1545+
llvm::DIType *debugType = getOrCreateType(type, tunit, TL);
14481546

14491547
// Get the location for the field.
14501548
llvm::DIFile *file = getOrCreateFile(loc);
@@ -1552,9 +1650,12 @@ void CGDebugInfo::CollectRecordNormalField(
15521650
} else {
15531651
auto Align = getDeclAlignIfRequired(field, CGM.getContext());
15541652
llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(field);
1555-
FieldType =
1556-
createFieldType(name, type, field->getLocation(), field->getAccess(),
1557-
OffsetInBits, Align, tunit, RecordTy, RD, Annotations);
1653+
TypeLoc TL;
1654+
if (const TypeSourceInfo *TSI = field->getTypeSourceInfo())
1655+
TL = TSI->getTypeLoc();
1656+
FieldType = createFieldType(name, type, field->getLocation(),
1657+
field->getAccess(), OffsetInBits, Align, tunit,
1658+
RecordTy, RD, Annotations, TL);
15581659
}
15591660

15601661
elements.push_back(FieldType);
@@ -3312,7 +3413,8 @@ void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
33123413
RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
33133414
}
33143415

3315-
llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
3416+
llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit,
3417+
TypeLoc TL) {
33163418
if (Ty.isNull())
33173419
return nullptr;
33183420

@@ -3329,7 +3431,7 @@ llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
33293431
if (auto *T = getTypeOrNull(Ty))
33303432
return T;
33313433

3332-
llvm::DIType *Res = CreateTypeNode(Ty, Unit);
3434+
llvm::DIType *Res = CreateTypeNode(Ty, Unit, TL);
33333435
void *TyPtr = Ty.getAsOpaquePtr();
33343436

33353437
// And update the type cache.
@@ -3373,10 +3475,11 @@ llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
33733475
return nullptr;
33743476
}
33753477

3376-
llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
3478+
llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit,
3479+
TypeLoc TL) {
33773480
// Handle qualifiers, which recursively handles what they refer to.
33783481
if (Ty.hasLocalQualifiers())
3379-
return CreateQualifiedType(Ty, Unit);
3482+
return CreateQualifiedType(Ty, Unit, TL);
33803483

33813484
// Work out details of type.
33823485
switch (Ty->getTypeClass()) {
@@ -3405,7 +3508,7 @@ llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
34053508
case Type::Complex:
34063509
return CreateType(cast<ComplexType>(Ty));
34073510
case Type::Pointer:
3408-
return CreateType(cast<PointerType>(Ty), Unit);
3511+
return CreateType(cast<PointerType>(Ty), Unit, TL);
34093512
case Type::BlockPointer:
34103513
return CreateType(cast<BlockPointerType>(Ty), Unit);
34113514
case Type::Typedef:
@@ -3416,7 +3519,7 @@ llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
34163519
return CreateEnumType(cast<EnumType>(Ty));
34173520
case Type::FunctionProto:
34183521
case Type::FunctionNoProto:
3419-
return CreateType(cast<FunctionType>(Ty), Unit);
3522+
return CreateType(cast<FunctionType>(Ty), Unit, TL);
34203523
case Type::ConstantArray:
34213524
case Type::VariableArray:
34223525
case Type::IncompleteArray:
@@ -3961,7 +4064,12 @@ llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
39614064
getDwarfCC(CC));
39624065
}
39634066

3964-
return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
4067+
TypeLoc TL;
4068+
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
4069+
if (const TypeSourceInfo *TSI = FD->getTypeSourceInfo())
4070+
TL = TSI->getTypeLoc();
4071+
}
4072+
return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F, TL));
39654073
}
39664074

39674075
QualType
@@ -4363,8 +4471,12 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
43634471
uint64_t XOffset = 0;
43644472
if (VD->hasAttr<BlocksAttr>())
43654473
Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType;
4366-
else
4367-
Ty = getOrCreateType(VD->getType(), Unit);
4474+
else {
4475+
TypeLoc TL;
4476+
if (const TypeSourceInfo *TSI = VD->getTypeSourceInfo())
4477+
TL = TSI->getTypeLoc();
4478+
Ty = getOrCreateType(VD->getType(), Unit, TL);
4479+
}
43684480

43694481
// If there is no debug info for this type then do not emit debug info
43704482
// for this variable.
@@ -5088,10 +5200,14 @@ void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
50885200
}
50895201
AppendAddressSpaceXDeref(AddressSpace, Expr);
50905202

5203+
TypeLoc TL;
5204+
if (const TypeSourceInfo *TSI = D->getTypeSourceInfo())
5205+
TL = TSI->getTypeLoc();
5206+
50915207
llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
50925208
GVE = DBuilder.createGlobalVariableExpression(
5093-
DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
5094-
Var->hasLocalLinkage(), true,
5209+
DContext, DeclName, LinkageName, Unit, LineNo,
5210+
getOrCreateType(T, Unit, TL), Var->hasLocalLinkage(), true,
50955211
Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
50965212
getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
50975213
Align, Annotations);

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,19 @@ class CGDebugInfo {
178178
llvm::DIType *CreateType(const ComplexType *Ty);
179179
llvm::DIType *CreateType(const AutoType *Ty);
180180
llvm::DIType *CreateType(const ExtIntType *Ty);
181-
llvm::DIType *CreateQualifiedType(QualType Ty, llvm::DIFile *Fg);
181+
llvm::DIType *CreateQualifiedType(QualType Ty, llvm::DIFile *Fg,
182+
TypeLoc TL = TypeLoc());
182183
llvm::DIType *CreateQualifiedType(const FunctionProtoType *Ty,
183184
llvm::DIFile *Fg);
184185
llvm::DIType *CreateType(const TypedefType *Ty, llvm::DIFile *Fg);
185186
llvm::DIType *CreateType(const TemplateSpecializationType *Ty,
186187
llvm::DIFile *Fg);
187188
llvm::DIType *CreateType(const ObjCObjectPointerType *Ty, llvm::DIFile *F);
188-
llvm::DIType *CreateType(const PointerType *Ty, llvm::DIFile *F);
189+
llvm::DIType *CreateType(const PointerType *Ty, llvm::DIFile *F,
190+
TypeLoc TL = TypeLoc());
189191
llvm::DIType *CreateType(const BlockPointerType *Ty, llvm::DIFile *F);
190-
llvm::DIType *CreateType(const FunctionType *Ty, llvm::DIFile *F);
192+
llvm::DIType *CreateType(const FunctionType *Ty, llvm::DIFile *F,
193+
TypeLoc TL = TypeLoc());
191194
/// Get structure or union type.
192195
llvm::DIType *CreateType(const RecordType *Tyg);
193196
llvm::DIType *CreateTypeDefinition(const RecordType *Ty);
@@ -242,7 +245,8 @@ class CGDebugInfo {
242245
/// \return namespace descriptor for the given namespace decl.
243246
llvm::DINamespace *getOrCreateNamespace(const NamespaceDecl *N);
244247
llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty,
245-
QualType PointeeTy, llvm::DIFile *F);
248+
QualType PointeeTy, llvm::DIFile *F,
249+
TypeLoc TL = TypeLoc());
246250
llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache);
247251

248252
/// A helper function to create a subprogram for a single member
@@ -308,7 +312,8 @@ class CGDebugInfo {
308312
uint64_t offsetInBits, uint32_t AlignInBits,
309313
llvm::DIFile *tunit, llvm::DIScope *scope,
310314
const RecordDecl *RD = nullptr,
311-
llvm::DINodeArray Annotations = nullptr);
315+
llvm::DINodeArray Annotations = nullptr,
316+
TypeLoc TL = TypeLoc());
312317

313318
llvm::DIType *createFieldType(StringRef name, QualType type,
314319
SourceLocation loc, AccessSpecifier AS,
@@ -628,7 +633,8 @@ class CGDebugInfo {
628633
Optional<StringRef> Source);
629634

630635
/// Get the type from the cache or create a new type if necessary.
631-
llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg);
636+
llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg,
637+
TypeLoc TL = TypeLoc());
632638

633639
/// Get a reference to a clang module. If \p CreateSkeletonCU is true,
634640
/// this also creates a split dwarf skeleton compile unit.
@@ -643,7 +649,8 @@ class CGDebugInfo {
643649
llvm::DICompositeType *getOrCreateLimitedType(const RecordType *Ty);
644650

645651
/// Create type metadata for a source language type.
646-
llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg);
652+
llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg,
653+
TypeLoc TL = TypeLoc());
647654

648655
/// Create new member and increase Offset by FType's size.
649656
llvm::DIType *CreateMemberType(llvm::DIFile *Unit, QualType FType,

0 commit comments

Comments
 (0)