-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] fix printing of canonical template template parameters take 2 #93448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang] fix printing of canonical template template parameters take 2 #93448
Conversation
@llvm/pr-subscribers-clang Author: Matheus Izvekov (mizvekov) ChangesSince they can also occur as the template name of Full diff: https://github.com/llvm/llvm-project/pull/93448.diff 3 Files Affected:
diff --git a/clang/lib/AST/TemplateBase.cpp b/clang/lib/AST/TemplateBase.cpp
index 4d4991d8c38b5..bb0820f4177e4 100644
--- a/clang/lib/AST/TemplateBase.cpp
+++ b/clang/lib/AST/TemplateBase.cpp
@@ -539,16 +539,7 @@ void TemplateArgument::print(const PrintingPolicy &Policy, raw_ostream &Out,
break;
case Template: {
- TemplateName TN = getAsTemplate();
- if (const auto *TD = TN.getAsTemplateDecl();
- TD && TD->getDeclName().isEmpty()) {
- assert(isa<TemplateTemplateParmDecl>(TD) &&
- "Unexpected anonymous template");
- const auto *TTP = cast<TemplateTemplateParmDecl>(TD);
- Out << "template-parameter-" << TTP->getDepth() << "-" << TTP->getIndex();
- } else {
- TN.print(Out, Policy);
- }
+ getAsTemplate().print(Out, Policy);
break;
}
diff --git a/clang/lib/AST/TemplateName.cpp b/clang/lib/AST/TemplateName.cpp
index 3aae998eceeb0..1ce31f09f2a6a 100644
--- a/clang/lib/AST/TemplateName.cpp
+++ b/clang/lib/AST/TemplateName.cpp
@@ -292,6 +292,14 @@ void TemplateName::Profile(llvm::FoldingSetNodeID &ID) {
void TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
Qualified Qual) const {
+ auto handleCanonicalTTP = [](TemplateDecl *TD, raw_ostream &OS) {
+ if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(TD);
+ TTP && TTP->getIdentifier() == nullptr) {
+ OS << "template-parameter-" << TTP->getDepth() << "-" << TTP->getIndex();
+ return true;
+ }
+ return false;
+ };
if (NameKind Kind = getKind();
Kind == TemplateName::Template || Kind == TemplateName::UsingTemplate) {
// After `namespace ns { using std::vector }`, what is the fully-qualified
@@ -304,6 +312,8 @@ void TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
// names more often than to export them, thus using the original name is
// most useful in this case.
TemplateDecl *Template = getAsTemplateDecl();
+ if (handleCanonicalTTP(Template, OS))
+ return;
if (Qual == Qualified::None)
OS << *Template;
else
@@ -320,6 +330,10 @@ void TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
Underlying.getKind() == TemplateName::UsingTemplate);
TemplateDecl *UTD = Underlying.getAsTemplateDecl();
+
+ if (handleCanonicalTTP(UTD, OS))
+ return;
+
if (IdentifierInfo *II = UTD->getIdentifier();
Policy.CleanUglifiedParameters && II &&
isa<TemplateTemplateParmDecl>(UTD))
diff --git a/clang/test/SemaTemplate/deduction-guide.cpp b/clang/test/SemaTemplate/deduction-guide.cpp
index 96b4cd9622a24..100b580fe9f02 100644
--- a/clang/test/SemaTemplate/deduction-guide.cpp
+++ b/clang/test/SemaTemplate/deduction-guide.cpp
@@ -315,19 +315,19 @@ namespace TTP {
// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} class depth 0 index 0 T{{$}}
// CHECK-NEXT: |-TemplateTemplateParmDecl {{.+}} depth 0 index 1 TT{{$}}
// CHECK-NEXT: | `-TemplateTypeParmDecl {{.+}} class depth 1 index 0{{$}}
-// CHECK-NEXT: |-CXXDeductionGuideDecl {{.+}} 'auto (<T>) -> B<T>'{{$}}
-// CHECK-NEXT: | `-ParmVarDecl {{.+}} '<T>'{{$}}
+// CHECK-NEXT: |-CXXDeductionGuideDecl {{.+}} 'auto (template-parameter-0-1<T>) -> B<T>'{{$}}
+// CHECK-NEXT: | `-ParmVarDecl {{.+}} 'template-parameter-0-1<T>'{{$}}
// CHECK-NEXT: `-CXXDeductionGuideDecl {{.+}} 'auto (A<int>) -> TTP::B<int>'
// CHECK-NEXT: |-TemplateArgument type 'int'
// CHECK-NEXT: | `-BuiltinType {{.+}} 'int'{{$}}
// CHECK-NEXT: |-TemplateArgument template 'TTP::A'{{$}}
// CHECK-NEXT: | `-ClassTemplateDecl {{.+}} A{{$}}
// CHECK-NEXT: `-ParmVarDecl {{.+}} 'A<int>':'TTP::A<int>'{{$}}
-// CHECK-NEXT: FunctionProtoType {{.+}} 'auto (<T>) -> B<T>' dependent trailing_return cdecl{{$}}
+// CHECK-NEXT: FunctionProtoType {{.+}} 'auto (template-parameter-0-1<T>) -> B<T>' dependent trailing_return cdecl{{$}}
// CHECK-NEXT: |-InjectedClassNameType {{.+}} 'B<T>' dependent{{$}}
// CHECK-NEXT: | `-CXXRecord {{.+}} 'B'{{$}}
-// CHECK-NEXT: `-ElaboratedType {{.+}} '<T>' sugar dependent{{$}}
-// CHECK-NEXT: `-TemplateSpecializationType {{.+}} '<T>' dependent {{$}}
+// CHECK-NEXT: `-ElaboratedType {{.+}} 'template-parameter-0-1<T>' sugar dependent{{$}}
+// CHECK-NEXT: `-TemplateSpecializationType {{.+}} 'template-parameter-0-1<T>' dependent template-parameter-0-1{{$}}
// CHECK-NEXT: `-TemplateArgument type 'T':'type-parameter-0-0'{{$}}
// CHECK-NEXT: `-TemplateTypeParmType {{.+}} 'T' dependent depth 0 index 0{{$}}
// CHECK-NEXT: `-TemplateTypeParm {{.+}} 'T'{{$}}
|
40e3c0f
to
2f2aa69
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes to Sema.h
and DR test suite look good.
7876afe
to
62eed81
Compare
Sorry, there were no libcxx or Sema.h changes, it was a GitHub issue, I just rebased a PR stacked below this one. |
2f2aa69
to
8322ce1
Compare
Since they can also occur as the template name of template specializations, handle them from TemplateName printing instead of TemplateArgument.
62eed81
to
4ed34c9
Compare
Since they can also occur as the template name of
template specializations, handle them from TemplateName printing instead of TemplateArgument.