[clang] [llvm] [Clang] Emit DW_TAG_template_alias for template aliases (PR #87623)

Orlando Cazalet-Hyams via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 18 02:00:49 PDT 2024


================
@@ -1332,6 +1332,54 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
   auto PP = getPrintingPolicy();
   Ty->getTemplateName().print(OS, PP, TemplateName::Qualified::None);
 
+  SourceLocation Loc = AliasDecl->getLocation();
+
+  if (CGM.getCodeGenOpts().DebugTemplateAlias) {
+    // TemplateSpecializationType doesn't know if its template args are
+    // being substituted into a parameter pack. We can find out if that's
+    // the case now by inspecting the TypeAliasTemplateDecl template
+    // parameters. Insert Ty's template args into SpecArgs, bundling args
+    // passed to a parameter pack into a TemplateArgument::Pack.
+    SmallVector<TemplateArgument> SpecArgs;
+    {
+      ArrayRef SubstArgs = Ty->template_arguments();
+      for (const NamedDecl *P : TD->getTemplateParameters()->asArray()) {
+        if (P->isParameterPack()) {
+          SpecArgs.push_back(TemplateArgument(SubstArgs));
+          break;
+        }
+        // Skip defaulted args.
+        if (SubstArgs.empty()) {
+          // If SubstArgs is now empty (we're taking from it each iteration) and
+          // this template parameter isn't a pack, then that should mean we're
+          // using default values for the remaining template parameters.
+          break;
----------------
OCHyams wrote:

Looking at a class template instantiation with a default dependent value:
```
template <int I1 = 5, int I2 = I1>
struct B {};
B<> b;
```
We get:
```
0x00000029:   DW_TAG_structure_type
                DW_AT_calling_convention        (DW_CC_pass_by_value)
                DW_AT_name      ("B<5, 5>")
                DW_AT_byte_size (0x01)
                DW_AT_decl_file ("/home/och/scratch/test11.cpp")
                DW_AT_decl_line (2)

0x0000002f:     DW_TAG_template_value_parameter
                  DW_AT_type    (0x0000003e "int")
                  DW_AT_name    ("I1")
                  DW_AT_default_value   (true)
                  DW_AT_const_value     (5)

0x00000036:     DW_TAG_template_value_parameter
                  DW_AT_type    (0x0000003e "int")
                  DW_AT_name    ("I2")
                  DW_AT_default_value   (true)
                  DW_AT_const_value     (5)

0x0000003d:     NULL
 ```
 
Is your suggestion to emit something like this for the template alias case (from my previous comment)?
```
0x00000029:   DW_TAG_template_alias
                DW_AT_name      ("B<5, ?>")
                ...

0x0000002f:     DW_TAG_template_value_parameter
                  DW_AT_type    (0x0000003e "int")
                  DW_AT_name    ("I1")
                  DW_AT_default_value   (true)
                  DW_AT_const_value     (5)

0x00000036:     DW_TAG_template_value_parameter
                  DW_AT_type    (0x0000003e "int")
                  DW_AT_name    ("I2")

0x0000003d:     NULL
```

I think that is doable but I'm not sure how that would interact with name reconstruction (in Clang debug info without -gsimple-template-names, and in debuggers/tools with it).

tyvm for your ongoing help with this.

https://github.com/llvm/llvm-project/pull/87623


More information about the cfe-commits mailing list