[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 04:24:54 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:

> I suppose you could call EvaluateKnownConstInt (on the TemplateArgument expression, if it's !isValueDependent() && isIntegerConstantExpr(), similar to what templateArgumentExpressionsEqual does). Maybe we can do this from within CollectTemplateParams where it asserts that the expression is a Constant?

The constant value default appears to be working, it's the dependent value `int I2 = I1` that is causing problems for me.

> Seems like you're tripping over the assert now because you're calling CollectTemplateParams on the arguments to a TemplateAliasDecl which don't have their arguments evaluated in the front-end it looks like.

That sounds like it might be the problem. It feels like we have all the parts needed to instantiate the dependent expression at this point... (because all the types/values it depends on exist as non-default argument values), I'm just not sure how to ask Clang to do that.


Here's an example using type parameters rather than values (you can also do `typename C = X<B>` if you want to hit yet another assertion...)
```
template <typename B = int, typename C = B>
using A = int;
A<> a;
```

```
Dependent types cannot show up in debug information
UNREACHABLE executed at clang/lib/CodeGen/CGDebugInfo.cpp:3686!
...
CGDebugInfo::CreateTypeNode(clang::QualType, llvm::DIFile*)
CGDebugInfo::getOrCreateType(clang::QualType, llvm::DIFile*) 
CollectTemplateParams # from the TemplateArgument::Type case processing the type of the argument for C
```

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


More information about the cfe-commits mailing list