diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -2162,12 +2162,22 @@ // template followed by the template parameters (including default // template arguments) of the constructor, if any. TemplateParameterList *TemplateParams = Template->getTemplateParameters(); - if (FTD) { - TemplateParameterList *InnerParams = FTD->getTemplateParameters(); - SmallVector AllParams; - AllParams.reserve(TemplateParams->size() + InnerParams->size()); - AllParams.insert(AllParams.begin(), - TemplateParams->begin(), TemplateParams->end()); + TemplateParameterList *InnerParams = + FTD ? FTD->getTemplateParameters() : nullptr; + SmallVector AllParams; + AllParams.reserve(TemplateParams->size() + + (InnerParams ? InnerParams->size() : 0)); + for (NamedDecl *Param : *TemplateParams) { + MultiLevelTemplateArgumentList Args; + Args.setKind(TemplateSubstitutionKind::Rewrite); + Args.addOuterTemplateArguments(SubstArgs); + Args.addOuterRetainedLevel(); + NamedDecl *NewParam = transformTemplateParameter(Param, Args); + if (!NewParam) + return nullptr; + AllParams.push_back(NewParam); + } + if (InnerParams) { SubstArgs.reserve(InnerParams->size()); // Later template parameters could refer to earlier ones, so build up @@ -2184,11 +2194,14 @@ SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( SemaRef.Context.getInjectedTemplateArg(NewParam))); } - TemplateParams = TemplateParameterList::Create( - SemaRef.Context, InnerParams->getTemplateLoc(), - InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), - /*FIXME: RequiresClause*/ nullptr); } + TemplateParameterList *InheritParameters = + InnerParams ? InnerParams : TemplateParams; + TemplateParams = TemplateParameterList::Create( + SemaRef.Context, InheritParameters->getTemplateLoc(), + InheritParameters->getLAngleLoc(), AllParams, + InheritParameters->getRAngleLoc(), + /*FIXME: RequiresClause*/ nullptr); // If we built a new template-parameter-list, track that we need to // substitute references to the old parameters into references to the diff --git a/clang/test/Modules/Inputs/ctad/a.h b/clang/test/Modules/Inputs/ctad/a.h new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/ctad/a.h @@ -0,0 +1,9 @@ +#pragma once + +template +struct A { + template + A(T, U) {} +}; + +A _a(2, 2); diff --git a/clang/test/Modules/Inputs/ctad/b.h b/clang/test/Modules/Inputs/ctad/b.h new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/ctad/b.h @@ -0,0 +1,8 @@ +#pragma once + +template struct A { + template + A(T, U) {} +}; + +A _b(5, 1.2); diff --git a/clang/test/Modules/Inputs/ctad/module.modulemap b/clang/test/Modules/Inputs/ctad/module.modulemap new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/ctad/module.modulemap @@ -0,0 +1,2 @@ +module a { header "a.h" export * } +module b { header "b.h" export * } diff --git a/clang/test/Modules/ctad.cpp b/clang/test/Modules/ctad.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Modules/ctad.cpp @@ -0,0 +1,12 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fmodules -fno-implicit-modules -x c++ -emit-module %S/Inputs/ctad/module.modulemap -fmodule-name=a -o %t/a.pcm -std=c++17 +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fmodules -fno-implicit-modules -x c++ -emit-module %S/Inputs/ctad/module.modulemap -fmodule-name=b -o %t/b.pcm -std=c++17 +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fmodules -fno-implicit-modules -x c++ -emit-llvm -I%S/Inputs/ctad -o - %s -fmodule-file=%t/a.pcm -fmodule-file=%t/b.pcm -std=c++17 | FileCheck %s + +// CHECK: @_a = global +// CHECK: @_b = global +// CHECK: call void @__cxx_global_var_init() +// CHECK: call void @__cxx_global_var_init.1() + +#include "a.h" +#include "b.h"