Skip to content

[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

Merged

Conversation

mizvekov
Copy link
Contributor

Since they can also occur as the template name of
template specializations, handle them from TemplateName printing instead of TemplateArgument.

@mizvekov mizvekov requested a review from cor3ntin May 27, 2024 09:08
@mizvekov mizvekov self-assigned this May 27, 2024
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 27, 2024
@llvmbot
Copy link
Member

llvmbot commented May 27, 2024

@llvm/pr-subscribers-clang

Author: Matheus Izvekov (mizvekov)

Changes

Since they can also occur as the template name of
template specializations, handle them from TemplateName printing instead of TemplateArgument.


Full diff: https://github.com/llvm/llvm-project/pull/93448.diff

3 Files Affected:

  • (modified) clang/lib/AST/TemplateBase.cpp (+1-10)
  • (modified) clang/lib/AST/TemplateName.cpp (+14)
  • (modified) clang/test/SemaTemplate/deduction-guide.cpp (+5-5)
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'{{$}}

@mizvekov mizvekov force-pushed the users/mizvekov/clang-improved-template-name-qualifiers branch from 40e3c0f to 2f2aa69 Compare May 29, 2024 16:57
@mizvekov mizvekov requested review from Endilll and a team as code owners May 29, 2024 16:57
Copy link
Contributor

@Endilll Endilll left a 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.

@mizvekov mizvekov force-pushed the users/mizvekov/clang-print-canonical-ttp-templatename branch from 7876afe to 62eed81 Compare May 29, 2024 17:07
@mizvekov mizvekov removed the request for review from a team May 29, 2024 17:07
@mizvekov
Copy link
Contributor Author

Sorry, there were no libcxx or Sema.h changes, it was a GitHub issue, I just rebased a PR stacked below this one.

@mizvekov mizvekov force-pushed the users/mizvekov/clang-improved-template-name-qualifiers branch from 2f2aa69 to 8322ce1 Compare May 29, 2024 18:26
Base automatically changed from users/mizvekov/clang-improved-template-name-qualifiers to main May 29, 2024 20:02
Since they can also occur as the template name of
template specializations, handle them from TemplateName
printing instead of TemplateArgument.
@mizvekov mizvekov force-pushed the users/mizvekov/clang-print-canonical-ttp-templatename branch from 62eed81 to 4ed34c9 Compare May 29, 2024 20:47
@mizvekov mizvekov requested a review from cor3ntin May 29, 2024 20:48
@mizvekov mizvekov merged commit ce2927a into main May 30, 2024
7 checks passed
@mizvekov mizvekov deleted the users/mizvekov/clang-print-canonical-ttp-templatename branch May 30, 2024 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants