@@ -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
@@ -953,7 +973,15 @@ llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
953
973
return getOrCreateType (QualType (T, 0 ), Unit);
954
974
}
955
975
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);
957
985
958
986
// No need to fill in the Name, Line, Size, Alignment, Offset in case of
959
987
// CVR derived types.
@@ -997,10 +1025,10 @@ llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
997
1025
Ty->getPointeeType (), Unit);
998
1026
}
999
1027
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 ) {
1002
1030
return CreatePointerLikeType (llvm::dwarf::DW_TAG_pointer_type, Ty,
1003
- Ty->getPointeeType (), Unit);
1031
+ Ty->getPointeeType (), Unit, TL );
1004
1032
}
1005
1033
1006
1034
// / \return whether a C++ mangling exists for the type defined by TD.
@@ -1138,9 +1166,11 @@ CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
1138
1166
return RetTy;
1139
1167
}
1140
1168
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) {
1144
1174
// Bit size, align and offset of the type.
1145
1175
// Size is always the size of a pointer. We can't use getTypeSize here
1146
1176
// because that does not return the correct value for references.
@@ -1150,13 +1180,52 @@ llvm::DIType *CGDebugInfo::CreatePointerLikeType(
1150
1180
Optional<unsigned > DWARFAddressSpace =
1151
1181
CGM.getTarget ().getDWARFAddressSpace (AddressSpace);
1152
1182
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
+
1153
1221
if (Tag == llvm::dwarf::DW_TAG_reference_type ||
1154
1222
Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
1155
1223
return DBuilder.createReferenceType (Tag, getOrCreateType (PointeeTy, Unit),
1156
1224
Size, Align, DWARFAddressSpace);
1157
1225
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);
1160
1229
}
1161
1230
1162
1231
llvm::DIType *CGDebugInfo::getOrCreateStructPtrType (StringRef Name,
@@ -1273,8 +1342,11 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
1273
1342
1274
1343
llvm::DIType *CGDebugInfo::CreateType (const TypedefType *Ty,
1275
1344
llvm::DIFile *Unit) {
1345
+ TypeLoc TL;
1346
+ if (const TypeSourceInfo *TSI = Ty->getDecl ()->getTypeSourceInfo ())
1347
+ TL = TSI->getTypeLoc ();
1276
1348
llvm::DIType *Underlying =
1277
- getOrCreateType (Ty->getDecl ()->getUnderlyingType (), Unit);
1349
+ getOrCreateType (Ty->getDecl ()->getUnderlyingType (), Unit, TL );
1278
1350
1279
1351
if (Ty->getDecl ()->hasAttr <NoDebugAttr>())
1280
1352
return Underlying;
@@ -1347,7 +1419,7 @@ static llvm::DINode::DIFlags getRefFlags(const FunctionProtoType *Func) {
1347
1419
}
1348
1420
1349
1421
llvm::DIType *CGDebugInfo::CreateType (const FunctionType *Ty,
1350
- llvm::DIFile *Unit) {
1422
+ llvm::DIFile *Unit, TypeLoc TL ) {
1351
1423
const auto *FPT = dyn_cast<FunctionProtoType>(Ty);
1352
1424
if (FPT) {
1353
1425
if (llvm::DIType *QTy = CreateQualifiedType (FPT, Unit))
@@ -1359,17 +1431,41 @@ llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
1359
1431
SmallVector<llvm::Metadata *, 16 > EltTys;
1360
1432
1361
1433
// 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));
1363
1440
1364
1441
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
1365
1442
// Set up remainder of arguments if there is a prototype.
1366
1443
// otherwise emit it as a variadic function.
1367
- if (!FPT)
1444
+ if (!FPT) {
1368
1445
EltTys.push_back (DBuilder.createUnspecifiedParameter ());
1369
- else {
1446
+ } else {
1370
1447
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
+ }
1373
1469
if (FPT->isVariadic ())
1374
1470
EltTys.push_back (DBuilder.createUnspecifiedParameter ());
1375
1471
}
@@ -1440,11 +1536,13 @@ llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1440
1536
Flags, DebugType, Annotations);
1441
1537
}
1442
1538
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);
1448
1546
1449
1547
// Get the location for the field.
1450
1548
llvm::DIFile *file = getOrCreateFile (loc);
@@ -1552,9 +1650,12 @@ void CGDebugInfo::CollectRecordNormalField(
1552
1650
} else {
1553
1651
auto Align = getDeclAlignIfRequired (field, CGM.getContext ());
1554
1652
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);
1558
1659
}
1559
1660
1560
1661
elements.push_back (FieldType);
@@ -3312,7 +3413,8 @@ void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
3312
3413
RetainedTypes.push_back (CGM.getContext ().getRecordType (&D).getAsOpaquePtr ());
3313
3414
}
3314
3415
3315
- llvm::DIType *CGDebugInfo::getOrCreateType (QualType Ty, llvm::DIFile *Unit) {
3416
+ llvm::DIType *CGDebugInfo::getOrCreateType (QualType Ty, llvm::DIFile *Unit,
3417
+ TypeLoc TL) {
3316
3418
if (Ty.isNull ())
3317
3419
return nullptr ;
3318
3420
@@ -3329,7 +3431,7 @@ llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
3329
3431
if (auto *T = getTypeOrNull (Ty))
3330
3432
return T;
3331
3433
3332
- llvm::DIType *Res = CreateTypeNode (Ty, Unit);
3434
+ llvm::DIType *Res = CreateTypeNode (Ty, Unit, TL );
3333
3435
void *TyPtr = Ty.getAsOpaquePtr ();
3334
3436
3335
3437
// And update the type cache.
@@ -3373,10 +3475,11 @@ llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
3373
3475
return nullptr ;
3374
3476
}
3375
3477
3376
- llvm::DIType *CGDebugInfo::CreateTypeNode (QualType Ty, llvm::DIFile *Unit) {
3478
+ llvm::DIType *CGDebugInfo::CreateTypeNode (QualType Ty, llvm::DIFile *Unit,
3479
+ TypeLoc TL) {
3377
3480
// Handle qualifiers, which recursively handles what they refer to.
3378
3481
if (Ty.hasLocalQualifiers ())
3379
- return CreateQualifiedType (Ty, Unit);
3482
+ return CreateQualifiedType (Ty, Unit, TL );
3380
3483
3381
3484
// Work out details of type.
3382
3485
switch (Ty->getTypeClass ()) {
@@ -3405,7 +3508,7 @@ llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
3405
3508
case Type::Complex:
3406
3509
return CreateType (cast<ComplexType>(Ty));
3407
3510
case Type::Pointer:
3408
- return CreateType (cast<PointerType>(Ty), Unit);
3511
+ return CreateType (cast<PointerType>(Ty), Unit, TL );
3409
3512
case Type::BlockPointer:
3410
3513
return CreateType (cast<BlockPointerType>(Ty), Unit);
3411
3514
case Type::Typedef:
@@ -3416,7 +3519,7 @@ llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
3416
3519
return CreateEnumType (cast<EnumType>(Ty));
3417
3520
case Type::FunctionProto:
3418
3521
case Type::FunctionNoProto:
3419
- return CreateType (cast<FunctionType>(Ty), Unit);
3522
+ return CreateType (cast<FunctionType>(Ty), Unit, TL );
3420
3523
case Type::ConstantArray:
3421
3524
case Type::VariableArray:
3422
3525
case Type::IncompleteArray:
@@ -3961,7 +4064,12 @@ llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
3961
4064
getDwarfCC (CC));
3962
4065
}
3963
4066
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));
3965
4073
}
3966
4074
3967
4075
QualType
@@ -4363,8 +4471,12 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
4363
4471
uint64_t XOffset = 0 ;
4364
4472
if (VD->hasAttr <BlocksAttr>())
4365
4473
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
+ }
4368
4480
4369
4481
// If there is no debug info for this type then do not emit debug info
4370
4482
// for this variable.
@@ -5088,10 +5200,14 @@ void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
5088
5200
}
5089
5201
AppendAddressSpaceXDeref (AddressSpace, Expr);
5090
5202
5203
+ TypeLoc TL;
5204
+ if (const TypeSourceInfo *TSI = D->getTypeSourceInfo ())
5205
+ TL = TSI->getTypeLoc ();
5206
+
5091
5207
llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations (D);
5092
5208
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 ,
5095
5211
Expr.empty () ? nullptr : DBuilder.createExpression (Expr),
5096
5212
getOrCreateStaticDataMemberDeclarationOrNull (D), TemplateParameters,
5097
5213
Align, Annotations);
0 commit comments