[clang] [Clang] Add default arguments to the parameter mapping (PR #192071)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 15 00:50:24 PDT 2026


================
@@ -2105,6 +2105,25 @@ void SubstituteParameterMappings::buildParameterMapping(
       SemaRef.MarkUsedTemplateParameters(Args->arguments(),
                                          /*Depth=*/0, OccurringIndices);
   }
+
+  // If a parameter is only referenced in a default template argument,
+  // we need to add it to the mapping explicitly.
+  {
+    llvm::SmallVector<TemplateArgument> DefaultArgs;
+    for (unsigned I = TemplateParams->getMinRequiredArguments();
+         I < TemplateParams->size(); I++) {
+      const NamedDecl *Param = TemplateParams->getParam(I);
+      if (Param->isParameterPack())
+        break;
+      const TemplateArgument *Arg =
+          SemaRef.getASTContext().getDefaultTemplateArgumentOrNone(Param);
+      assert(Arg && "expected a default argument");
+      DefaultArgs.emplace_back(std::move(*Arg));
+    }
+    SemaRef.MarkUsedTemplateParameters(DefaultArgs, /*Depth=*/0,
+                                       OccurringIndices);
----------------
zyn0217 wrote:

I'd suggest we do not touch the substitution part if unnecessary, as it's already slow: if we added default template parameters, we would have more (maybe wasteful, because default arguments will be added in CheckTemplateArguments) template parameters to handle.

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


More information about the cfe-commits mailing list