@@ -1313,6 +1313,44 @@ llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
1313
1313
return DBuilder.createPointerType (EltTy, Size);
1314
1314
}
1315
1315
1316
+ static llvm::SmallVector<TemplateArgument>
1317
+ GetTemplateArgs (const TemplateDecl *TD, const TemplateSpecializationType *Ty) {
1318
+ assert (Ty->isTypeAlias ());
1319
+ // TemplateSpecializationType doesn't know if its template args are
1320
+ // being substituted into a parameter pack. We can find out if that's
1321
+ // the case now by inspecting the TypeAliasTemplateDecl template
1322
+ // parameters. Insert Ty's template args into SpecArgs, bundling args
1323
+ // passed to a parameter pack into a TemplateArgument::Pack. It also
1324
+ // doesn't know the value of any defaulted args, so collect those now
1325
+ // too.
1326
+ SmallVector<TemplateArgument> SpecArgs;
1327
+ ArrayRef SubstArgs = Ty->template_arguments ();
1328
+ for (const NamedDecl *Param : TD->getTemplateParameters ()->asArray ()) {
1329
+ // If Param is a parameter pack, pack the remaining arguments.
1330
+ if (Param->isParameterPack ()) {
1331
+ SpecArgs.push_back (TemplateArgument (SubstArgs));
1332
+ break ;
1333
+ }
1334
+
1335
+ // Skip defaulted args.
1336
+ // FIXME: Ideally, we wouldn't do this. We can read the default values
1337
+ // for each parameter. However, defaulted arguments which are dependent
1338
+ // values or dependent types can't (easily?) be resolved here.
1339
+ if (SubstArgs.empty ()) {
1340
+ // If SubstArgs is now empty (we're taking from it each iteration) and
1341
+ // this template parameter isn't a pack, then that should mean we're
1342
+ // using default values for the remaining template parameters (after
1343
+ // which there may be an empty pack too which we will ignore).
1344
+ break ;
1345
+ }
1346
+
1347
+ // Take the next argument.
1348
+ SpecArgs.push_back (SubstArgs.front ());
1349
+ SubstArgs = SubstArgs.drop_front ();
1350
+ }
1351
+ return SpecArgs;
1352
+ }
1353
+
1316
1354
llvm::DIType *CGDebugInfo::CreateType (const TemplateSpecializationType *Ty,
1317
1355
llvm::DIFile *Unit) {
1318
1356
assert (Ty->isTypeAlias ());
@@ -1332,6 +1370,31 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
1332
1370
auto PP = getPrintingPolicy ();
1333
1371
Ty->getTemplateName ().print (OS, PP, TemplateName::Qualified::None);
1334
1372
1373
+ SourceLocation Loc = AliasDecl->getLocation ();
1374
+
1375
+ if (CGM.getCodeGenOpts ().DebugTemplateAlias ) {
1376
+ auto ArgVector = ::GetTemplateArgs (TD, Ty);
1377
+ TemplateArgs Args = {TD->getTemplateParameters (), ArgVector};
1378
+
1379
+ // FIXME: Respect DebugTemplateNameKind::Mangled, e.g. by using GetName.
1380
+ // Note we can't use GetName without additional work: TypeAliasTemplateDecl
1381
+ // doesn't have instantiation information, so
1382
+ // TypeAliasTemplateDecl::getNameForDiagnostic wouldn't have access to the
1383
+ // template args.
1384
+ std::string Name;
1385
+ llvm::raw_string_ostream OS (Name);
1386
+ TD->getNameForDiagnostic (OS, PP, /* Qualified=*/ false );
1387
+ if (CGM.getCodeGenOpts ().getDebugSimpleTemplateNames () !=
1388
+ llvm::codegenoptions::DebugTemplateNamesKind::Simple ||
1389
+ !HasReconstitutableArgs (Args.Args ))
1390
+ printTemplateArgumentList (OS, Args.Args , PP);
1391
+
1392
+ llvm::DIDerivedType *AliasTy = DBuilder.createTemplateAlias (
1393
+ Src, Name, getOrCreateFile (Loc), getLineNumber (Loc),
1394
+ getDeclContextDescriptor (AliasDecl), CollectTemplateParams (Args, Unit));
1395
+ return AliasTy;
1396
+ }
1397
+
1335
1398
// Disable PrintCanonicalTypes here because we want
1336
1399
// the DW_AT_name to benefit from the TypePrinter's ability
1337
1400
// to skip defaulted template arguments.
@@ -1343,8 +1406,6 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
1343
1406
PP.PrintCanonicalTypes = false ;
1344
1407
printTemplateArgumentList (OS, Ty->template_arguments (), PP,
1345
1408
TD->getTemplateParameters ());
1346
-
1347
- SourceLocation Loc = AliasDecl->getLocation ();
1348
1409
return DBuilder.createTypedef (Src, OS.str (), getOrCreateFile (Loc),
1349
1410
getLineNumber (Loc),
1350
1411
getDeclContextDescriptor (AliasDecl));
@@ -5363,6 +5424,54 @@ static bool IsReconstitutableType(QualType QT) {
5363
5424
return T.Reconstitutable ;
5364
5425
}
5365
5426
5427
+ bool CGDebugInfo::HasReconstitutableArgs (
5428
+ ArrayRef<TemplateArgument> Args) const {
5429
+ return llvm::all_of (Args, [&](const TemplateArgument &TA) {
5430
+ switch (TA.getKind ()) {
5431
+ case TemplateArgument::Template:
5432
+ // Easy to reconstitute - the value of the parameter in the debug
5433
+ // info is the string name of the template. The template name
5434
+ // itself won't benefit from any name rebuilding, but that's a
5435
+ // representational limitation - maybe DWARF could be
5436
+ // changed/improved to use some more structural representation.
5437
+ return true ;
5438
+ case TemplateArgument::Declaration:
5439
+ // Reference and pointer non-type template parameters point to
5440
+ // variables, functions, etc and their value is, at best (for
5441
+ // variables) represented as an address - not a reference to the
5442
+ // DWARF describing the variable/function/etc. This makes it hard,
5443
+ // possibly impossible to rebuild the original name - looking up
5444
+ // the address in the executable file's symbol table would be
5445
+ // needed.
5446
+ return false ;
5447
+ case TemplateArgument::NullPtr:
5448
+ // These could be rebuilt, but figured they're close enough to the
5449
+ // declaration case, and not worth rebuilding.
5450
+ return false ;
5451
+ case TemplateArgument::Pack:
5452
+ // A pack is invalid if any of the elements of the pack are
5453
+ // invalid.
5454
+ return HasReconstitutableArgs (TA.getPackAsArray ());
5455
+ case TemplateArgument::Integral:
5456
+ // Larger integers get encoded as DWARF blocks which are a bit
5457
+ // harder to parse back into a large integer, etc - so punting on
5458
+ // this for now. Re-parsing the integers back into APInt is
5459
+ // probably feasible some day.
5460
+ return TA.getAsIntegral ().getBitWidth () <= 64 &&
5461
+ IsReconstitutableType (TA.getIntegralType ());
5462
+ case TemplateArgument::StructuralValue:
5463
+ return false ;
5464
+ case TemplateArgument::Type:
5465
+ return IsReconstitutableType (TA.getAsType ());
5466
+ case TemplateArgument::Expression:
5467
+ return IsReconstitutableType (TA.getAsExpr ()->getType ());
5468
+ default :
5469
+ llvm_unreachable (" Other, unresolved, template arguments should "
5470
+ " not be seen here" );
5471
+ }
5472
+ });
5473
+ }
5474
+
5366
5475
std::string CGDebugInfo::GetName (const Decl *D, bool Qualified) const {
5367
5476
std::string Name;
5368
5477
llvm::raw_string_ostream OS (Name);
@@ -5389,49 +5498,7 @@ std::string CGDebugInfo::GetName(const Decl *D, bool Qualified) const {
5389
5498
} else if (auto *VD = dyn_cast<VarDecl>(ND)) {
5390
5499
Args = GetTemplateArgs (VD);
5391
5500
}
5392
- std::function<bool (ArrayRef<TemplateArgument>)> HasReconstitutableArgs =
5393
- [&](ArrayRef<TemplateArgument> Args) {
5394
- return llvm::all_of (Args, [&](const TemplateArgument &TA) {
5395
- switch (TA.getKind ()) {
5396
- case TemplateArgument::Template:
5397
- // Easy to reconstitute - the value of the parameter in the debug
5398
- // info is the string name of the template. (so the template name
5399
- // itself won't benefit from any name rebuilding, but that's a
5400
- // representational limitation - maybe DWARF could be
5401
- // changed/improved to use some more structural representation)
5402
- return true ;
5403
- case TemplateArgument::Declaration:
5404
- // Reference and pointer non-type template parameters point to
5405
- // variables, functions, etc and their value is, at best (for
5406
- // variables) represented as an address - not a reference to the
5407
- // DWARF describing the variable/function/etc. This makes it hard,
5408
- // possibly impossible to rebuild the original name - looking up the
5409
- // address in the executable file's symbol table would be needed.
5410
- return false ;
5411
- case TemplateArgument::NullPtr:
5412
- // These could be rebuilt, but figured they're close enough to the
5413
- // declaration case, and not worth rebuilding.
5414
- return false ;
5415
- case TemplateArgument::Pack:
5416
- // A pack is invalid if any of the elements of the pack are invalid.
5417
- return HasReconstitutableArgs (TA.getPackAsArray ());
5418
- case TemplateArgument::Integral:
5419
- // Larger integers get encoded as DWARF blocks which are a bit
5420
- // harder to parse back into a large integer, etc - so punting on
5421
- // this for now. Re-parsing the integers back into APInt is probably
5422
- // feasible some day.
5423
- return TA.getAsIntegral ().getBitWidth () <= 64 &&
5424
- IsReconstitutableType (TA.getIntegralType ());
5425
- case TemplateArgument::StructuralValue:
5426
- return false ;
5427
- case TemplateArgument::Type:
5428
- return IsReconstitutableType (TA.getAsType ());
5429
- default :
5430
- llvm_unreachable (" Other, unresolved, template arguments should "
5431
- " not be seen here" );
5432
- }
5433
- });
5434
- };
5501
+
5435
5502
// A conversion operator presents complications/ambiguity if there's a
5436
5503
// conversion to class template that is itself a template, eg:
5437
5504
// template<typename T>
0 commit comments