@@ -929,8 +929,28 @@ static llvm::dwarf::Tag getNextQualifier(Qualifiers &Q) {
929
929
return (llvm::dwarf::Tag)0 ;
930
930
}
931
931
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) {
934
954
QualifierCollector Qc;
935
955
const Type *T = Qc.strip (Ty);
936
956
@@ -944,7 +964,15 @@ llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
944
964
return getOrCreateType (QualType (T, 0 ), Unit);
945
965
}
946
966
947
- auto *FromTy = getOrCreateType (Qc.apply (CGM.getContext (), T), Unit);
967
+ QualType NextTy = Qc.apply (CGM.getContext (), T);
968
+ TypeLoc NextTL;
969
+ if (NextTy.hasQualifiers ())
970
+ NextTL = TL;
971
+ else if (TL) {
972
+ if (auto QTL = TL.getAs <QualifiedTypeLoc>())
973
+ NextTL = StripMacroAttributed (QTL.getNextTypeLoc ());
974
+ }
975
+ auto *FromTy = getOrCreateType (NextTy, Unit, NextTL);
948
976
949
977
// No need to fill in the Name, Line, Size, Alignment, Offset in case of
950
978
// CVR derived types.
@@ -988,10 +1016,10 @@ llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
988
1016
Ty->getPointeeType (), Unit);
989
1017
}
990
1018
991
- llvm::DIType *CGDebugInfo::CreateType (const PointerType *Ty,
992
- llvm::DIFile *Unit ) {
1019
+ llvm::DIType *CGDebugInfo::CreateType (const PointerType *Ty, llvm::DIFile *Unit,
1020
+ TypeLoc TL ) {
993
1021
return CreatePointerLikeType (llvm::dwarf::DW_TAG_pointer_type, Ty,
994
- Ty->getPointeeType (), Unit);
1022
+ Ty->getPointeeType (), Unit, TL );
995
1023
}
996
1024
997
1025
// / \return whether a C++ mangling exists for the type defined by TD.
@@ -1132,7 +1160,8 @@ CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
1132
1160
llvm::DIType *CGDebugInfo::CreatePointerLikeType (llvm::dwarf::Tag Tag,
1133
1161
const Type *Ty,
1134
1162
QualType PointeeTy,
1135
- llvm::DIFile *Unit) {
1163
+ llvm::DIFile *Unit,
1164
+ TypeLoc TL) {
1136
1165
// Bit size, align and offset of the type.
1137
1166
// Size is always the size of a pointer. We can't use getTypeSize here
1138
1167
// because that does not return the correct value for references.
@@ -1142,13 +1171,52 @@ llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
1142
1171
Optional<unsigned > DWARFAddressSpace =
1143
1172
CGM.getTarget ().getDWARFAddressSpace (AddressSpace);
1144
1173
1174
+ llvm::DINodeArray Annotations = nullptr ;
1175
+ TypeLoc NextTL;
1176
+ if (TL) {
1177
+ SmallVector<llvm::Metadata *, 4 > Annots;
1178
+ NextTL = TL.getNextTypeLoc ();
1179
+ if (NextTL) {
1180
+ // Traverse all MacroQualifiedTypeLoc, QualifiedTypeLoc and
1181
+ // AttributedTypeLoc type locations so we can collect
1182
+ // BTFTypeTag attributes for this pointer.
1183
+ while (true ) {
1184
+ if (auto MTL = NextTL.getAs <MacroQualifiedTypeLoc>()) {
1185
+ NextTL = MTL.getInnerLoc ();
1186
+ } else if (auto QTL = NextTL.getAs <QualifiedTypeLoc>()) {
1187
+ NextTL = QTL.getNextTypeLoc ();
1188
+ } else if (auto ATL = NextTL.getAs <AttributedTypeLoc>()) {
1189
+ if (const auto *A = ATL.getAttrAs <BTFTypeTagAttr>()) {
1190
+ StringRef BTFTypeTag = A->getBTFTypeTag ();
1191
+ if (!BTFTypeTag.empty ()) {
1192
+ llvm::Metadata *Ops[2 ] = {
1193
+ llvm::MDString::get (CGM.getLLVMContext (),
1194
+ StringRef (" btf_type_tag" )),
1195
+ llvm::MDString::get (CGM.getLLVMContext (), BTFTypeTag)};
1196
+ Annots.insert (Annots.begin (),
1197
+ llvm::MDNode::get (CGM.getLLVMContext (), Ops));
1198
+ }
1199
+ }
1200
+ NextTL = ATL.getModifiedLoc ();
1201
+ } else {
1202
+ break ;
1203
+ }
1204
+ }
1205
+ }
1206
+
1207
+ NextTL = StripMacroAttributed (TL.getNextTypeLoc ());
1208
+ if (Annots.size () > 0 )
1209
+ Annotations = DBuilder.getOrCreateArray (Annots);
1210
+ }
1211
+
1145
1212
if (Tag == llvm::dwarf::DW_TAG_reference_type ||
1146
1213
Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
1147
1214
return DBuilder.createReferenceType (Tag, getOrCreateType (PointeeTy, Unit),
1148
1215
Size, Align, DWARFAddressSpace);
1149
1216
else
1150
- return DBuilder.createPointerType (getOrCreateType (PointeeTy, Unit), Size,
1151
- Align, DWARFAddressSpace);
1217
+ return DBuilder.createPointerType (getOrCreateType (PointeeTy, Unit, NextTL),
1218
+ Size, Align, DWARFAddressSpace,
1219
+ StringRef (), Annotations);
1152
1220
}
1153
1221
1154
1222
llvm::DIType *CGDebugInfo::getOrCreateStructPtrType (StringRef Name,
@@ -1265,8 +1333,11 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
1265
1333
1266
1334
llvm::DIType *CGDebugInfo::CreateType (const TypedefType *Ty,
1267
1335
llvm::DIFile *Unit) {
1336
+ TypeLoc TL;
1337
+ if (const TypeSourceInfo *TSI = Ty->getDecl ()->getTypeSourceInfo ())
1338
+ TL = TSI->getTypeLoc ();
1268
1339
llvm::DIType *Underlying =
1269
- getOrCreateType (Ty->getDecl ()->getUnderlyingType (), Unit);
1340
+ getOrCreateType (Ty->getDecl ()->getUnderlyingType (), Unit, TL );
1270
1341
1271
1342
if (Ty->getDecl ()->hasAttr <NoDebugAttr>())
1272
1343
return Underlying;
@@ -1340,7 +1411,7 @@ static llvm::DINode::DIFlags getRefFlags(const FunctionProtoType *Func) {
1340
1411
}
1341
1412
1342
1413
llvm::DIType *CGDebugInfo::CreateType (const FunctionType *Ty,
1343
- llvm::DIFile *Unit) {
1414
+ llvm::DIFile *Unit, TypeLoc TL ) {
1344
1415
const auto *FPT = dyn_cast<FunctionProtoType>(Ty);
1345
1416
if (FPT) {
1346
1417
if (llvm::DIType *QTy = CreateQualifiedType (FPT, Unit))
@@ -1352,17 +1423,41 @@ llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
1352
1423
SmallVector<llvm::Metadata *, 16 > EltTys;
1353
1424
1354
1425
// Add the result type at least.
1355
- EltTys.push_back (getOrCreateType (Ty->getReturnType (), Unit));
1426
+ TypeLoc RetTL;
1427
+ if (TL) {
1428
+ if (auto FTL = TL.getAs <FunctionTypeLoc>())
1429
+ RetTL = FTL.getReturnLoc ();
1430
+ }
1431
+ EltTys.push_back (getOrCreateType (Ty->getReturnType (), Unit, RetTL));
1356
1432
1357
1433
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
1358
1434
// Set up remainder of arguments if there is a prototype.
1359
1435
// otherwise emit it as a variadic function.
1360
- if (!FPT)
1436
+ if (!FPT) {
1361
1437
EltTys.push_back (DBuilder.createUnspecifiedParameter ());
1362
- else {
1438
+ } else {
1363
1439
Flags = getRefFlags (FPT);
1364
- for (const QualType &ParamType : FPT->param_types ())
1365
- EltTys.push_back (getOrCreateType (ParamType, Unit));
1440
+ bool DoneWithTL = false ;
1441
+ if (TL) {
1442
+ if (auto FTL = TL.getAs <FunctionTypeLoc>()) {
1443
+ DoneWithTL = true ;
1444
+ int Idx = 0 ;
1445
+ for (const QualType &ParamType : FPT->param_types ()) {
1446
+ TypeLoc ParamTL;
1447
+ if (ParmVarDecl *Param = FTL.getParam (Idx)) {
1448
+ if (const TypeSourceInfo *TSI = Param->getTypeSourceInfo ())
1449
+ ParamTL = TSI->getTypeLoc ();
1450
+ }
1451
+ EltTys.push_back (getOrCreateType (ParamType, Unit, ParamTL));
1452
+ Idx++;
1453
+ }
1454
+ }
1455
+ }
1456
+
1457
+ if (!DoneWithTL) {
1458
+ for (const QualType &ParamType : FPT->param_types ())
1459
+ EltTys.push_back (getOrCreateType (ParamType, Unit));
1460
+ }
1366
1461
if (FPT->isVariadic ())
1367
1462
EltTys.push_back (DBuilder.createUnspecifiedParameter ());
1368
1463
}
@@ -1433,11 +1528,13 @@ llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1433
1528
Flags, DebugType, Annotations);
1434
1529
}
1435
1530
1436
- llvm::DIType *CGDebugInfo::createFieldType (
1437
- StringRef name, QualType type, SourceLocation loc, AccessSpecifier AS,
1438
- uint64_t offsetInBits, uint32_t AlignInBits, llvm::DIFile *tunit,
1439
- llvm::DIScope *scope, const RecordDecl *RD, llvm::DINodeArray Annotations) {
1440
- llvm::DIType *debugType = getOrCreateType (type, tunit);
1531
+ llvm::DIType *
1532
+ CGDebugInfo::createFieldType (StringRef name, QualType type, SourceLocation loc,
1533
+ AccessSpecifier AS, uint64_t offsetInBits,
1534
+ uint32_t AlignInBits, llvm::DIFile *tunit,
1535
+ llvm::DIScope *scope, const RecordDecl *RD,
1536
+ llvm::DINodeArray Annotations, TypeLoc TL) {
1537
+ llvm::DIType *debugType = getOrCreateType (type, tunit, TL);
1441
1538
1442
1539
// Get the location for the field.
1443
1540
llvm::DIFile *file = getOrCreateFile (loc);
@@ -1545,9 +1642,12 @@ void CGDebugInfo::CollectRecordNormalField(
1545
1642
} else {
1546
1643
auto Align = getDeclAlignIfRequired (field, CGM.getContext ());
1547
1644
llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations (field);
1548
- FieldType =
1549
- createFieldType (name, type, field->getLocation (), field->getAccess (),
1550
- OffsetInBits, Align, tunit, RecordTy, RD, Annotations);
1645
+ TypeLoc TL;
1646
+ if (const TypeSourceInfo *TSI = field->getTypeSourceInfo ())
1647
+ TL = TSI->getTypeLoc ();
1648
+ FieldType = createFieldType (name, type, field->getLocation (),
1649
+ field->getAccess (), OffsetInBits, Align, tunit,
1650
+ RecordTy, RD, Annotations, TL);
1551
1651
}
1552
1652
1553
1653
elements.push_back (FieldType);
@@ -3305,7 +3405,8 @@ void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
3305
3405
RetainedTypes.push_back (CGM.getContext ().getRecordType (&D).getAsOpaquePtr ());
3306
3406
}
3307
3407
3308
- llvm::DIType *CGDebugInfo::getOrCreateType (QualType Ty, llvm::DIFile *Unit) {
3408
+ llvm::DIType *CGDebugInfo::getOrCreateType (QualType Ty, llvm::DIFile *Unit,
3409
+ TypeLoc TL) {
3309
3410
if (Ty.isNull ())
3310
3411
return nullptr ;
3311
3412
@@ -3322,7 +3423,7 @@ llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
3322
3423
if (auto *T = getTypeOrNull (Ty))
3323
3424
return T;
3324
3425
3325
- llvm::DIType *Res = CreateTypeNode (Ty, Unit);
3426
+ llvm::DIType *Res = CreateTypeNode (Ty, Unit, TL );
3326
3427
void *TyPtr = Ty.getAsOpaquePtr ();
3327
3428
3328
3429
// And update the type cache.
@@ -3366,10 +3467,11 @@ llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
3366
3467
return nullptr ;
3367
3468
}
3368
3469
3369
- llvm::DIType *CGDebugInfo::CreateTypeNode (QualType Ty, llvm::DIFile *Unit) {
3470
+ llvm::DIType *CGDebugInfo::CreateTypeNode (QualType Ty, llvm::DIFile *Unit,
3471
+ TypeLoc TL) {
3370
3472
// Handle qualifiers, which recursively handles what they refer to.
3371
3473
if (Ty.hasLocalQualifiers ())
3372
- return CreateQualifiedType (Ty, Unit);
3474
+ return CreateQualifiedType (Ty, Unit, TL );
3373
3475
3374
3476
// Work out details of type.
3375
3477
switch (Ty->getTypeClass ()) {
@@ -3398,7 +3500,7 @@ llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
3398
3500
case Type::Complex:
3399
3501
return CreateType (cast<ComplexType>(Ty));
3400
3502
case Type::Pointer:
3401
- return CreateType (cast<PointerType>(Ty), Unit);
3503
+ return CreateType (cast<PointerType>(Ty), Unit, TL );
3402
3504
case Type::BlockPointer:
3403
3505
return CreateType (cast<BlockPointerType>(Ty), Unit);
3404
3506
case Type::Typedef:
@@ -3409,7 +3511,7 @@ llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
3409
3511
return CreateEnumType (cast<EnumType>(Ty));
3410
3512
case Type::FunctionProto:
3411
3513
case Type::FunctionNoProto:
3412
- return CreateType (cast<FunctionType>(Ty), Unit);
3514
+ return CreateType (cast<FunctionType>(Ty), Unit, TL );
3413
3515
case Type::ConstantArray:
3414
3516
case Type::VariableArray:
3415
3517
case Type::IncompleteArray:
@@ -3954,7 +4056,12 @@ llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
3954
4056
getDwarfCC (CC));
3955
4057
}
3956
4058
3957
- return cast<llvm::DISubroutineType>(getOrCreateType (FnType, F));
4059
+ TypeLoc TL;
4060
+ if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
4061
+ if (const TypeSourceInfo *TSI = FD->getTypeSourceInfo ())
4062
+ TL = TSI->getTypeLoc ();
4063
+ }
4064
+ return cast<llvm::DISubroutineType>(getOrCreateType (FnType, F, TL));
3958
4065
}
3959
4066
3960
4067
QualType
@@ -4356,8 +4463,12 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
4356
4463
uint64_t XOffset = 0 ;
4357
4464
if (VD->hasAttr <BlocksAttr>())
4358
4465
Ty = EmitTypeForVarWithBlocksAttr (VD, &XOffset).WrappedType ;
4359
- else
4360
- Ty = getOrCreateType (VD->getType (), Unit);
4466
+ else {
4467
+ TypeLoc TL;
4468
+ if (const TypeSourceInfo *TSI = VD->getTypeSourceInfo ())
4469
+ TL = TSI->getTypeLoc ();
4470
+ Ty = getOrCreateType (VD->getType (), Unit, TL);
4471
+ }
4361
4472
4362
4473
// If there is no debug info for this type then do not emit debug info
4363
4474
// for this variable.
@@ -5081,10 +5192,14 @@ void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
5081
5192
}
5082
5193
AppendAddressSpaceXDeref (AddressSpace, Expr);
5083
5194
5195
+ TypeLoc TL;
5196
+ if (const TypeSourceInfo *TSI = D->getTypeSourceInfo ())
5197
+ TL = TSI->getTypeLoc ();
5198
+
5084
5199
llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations (D);
5085
5200
GVE = DBuilder.createGlobalVariableExpression (
5086
- DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType (T, Unit),
5087
- Var->hasLocalLinkage (), true ,
5201
+ DContext, DeclName, LinkageName, Unit, LineNo,
5202
+ getOrCreateType (T, Unit, TL), Var->hasLocalLinkage (), true ,
5088
5203
Expr.empty () ? nullptr : DBuilder.createExpression (Expr),
5089
5204
getOrCreateStaticDataMemberDeclarationOrNull (D), TemplateParameters,
5090
5205
Align, Annotations);
0 commit comments