[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/mizvekov closed https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/mizvekov updated https://github.com/llvm/llvm-project/pull/93431 >From fabcce0d7a4a1633b4d5ed49cb78fdf441e3c11e Mon Sep 17 00:00:00 2001 From: Matheus Izvekov Date: Fri, 24 May 2024 12:22:55 -0300 Subject: [PATCH] [clang] Improve ast-dumper text printing of TemplateArgument This improves and unifies our approach to printing all template arguments. The same approach to printing types is extended to all TemplateArguments: A sugared version is printed in quotes, followed by printing the canonical form, unless they would print the same. Special improvements are done to add more detail to template template arguments. It's planned in a future patch to use this improved TemplateName printer for other places besides TemplateArguments. Note: The sugared/desugared printing does not show up for TemplateNames in tests yet, because we do a poor job of preserving their type sugar. This will be improved in a future patch. --- clang/docs/ReleaseNotes.rst | 2 + clang/include/clang/AST/TextNodeDumper.h | 2 + clang/lib/AST/TextNodeDumper.cpp | 104 +++--- clang/test/AST/ast-dump-decl.cpp | 25 +++-- ...penmp-begin-declare-variant_template_2.cpp | 6 +- clang/test/AST/ast-dump-template-name.cpp | 54 + clang/test/AST/ast-dump-using-template.cpp| 8 +- .../constraints-explicit-instantiation.cpp| 6 +- clang/test/OpenMP/align_clause_ast_print.cpp | 2 +- clang/test/OpenMP/generic_loop_ast_print.cpp | 2 +- clang/test/OpenMP/interop_ast_print.cpp | 2 +- clang/test/SemaOpenACC/sub-array-ast.cpp | 2 +- .../aggregate-deduction-candidate.cpp | 18 +-- clang/test/SemaTemplate/attributes.cpp| 64 +-- clang/test/SemaTemplate/deduction-guide.cpp | 19 ++-- clang/test/SemaTemplate/make_integer_seq.cpp | 68 +++- clang/test/SemaTemplate/type_pack_element.cpp | 20 ++-- 17 files changed, 276 insertions(+), 128 deletions(-) create mode 100644 clang/test/AST/ast-dump-template-name.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index bd92818f0c09d..e1c6d55eeeacd 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -98,6 +98,8 @@ ABI Changes in This Version AST Dumping Potentially Breaking Changes +- The text ast-dumper has improved printing of TemplateArguments. + Clang Frontend Potentially Breaking Changes --- - Removed support for constructing on-stack ``TemplateArgumentList``\ s; interfaces should instead diff --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h index 1fede6e462e92..63fa16c9ec47c 100644 --- a/clang/include/clang/AST/TextNodeDumper.h +++ b/clang/include/clang/AST/TextNodeDumper.h @@ -213,6 +213,8 @@ class TextNodeDumper void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK); void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS); void dumpConceptReference(const ConceptReference *R); + void dumpTemplateArgument(const TemplateArgument &TA); + void dumpTemplateName(TemplateName TN); void dumpDeclRef(const Decl *D, StringRef Label = {}); diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp index 4a1e94ffe283b..627f8d3477d4e 100644 --- a/clang/lib/AST/TextNodeDumper.cpp +++ b/clang/lib/AST/TextNodeDumper.cpp @@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); + !CanonTA.structurallyEquals(TA)) { +llvm::SmallString<128> CanonStr; +{ + llvm::raw_svector_ostream SS(CanonStr); + CanonTA.print(PrintPolicy, SS, /*IncludeType=*/true); +} +if (CanonStr != Str) + OS << ":'" << CanonStr << "'"; + } +} + const char *TextNodeDumper::getCommandName(unsigned CommandID) { if (Traits) return Traits->getCommandInfo(CommandID)->Name; @@ -1086,45 +1106,101 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplate
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; mizvekov wrote: I think the problem is that it makes little sense to show the char as a char literal for this integral dumper. This is a resolved argument, it's not supposed to print as-written anyway. This dumper is a debugging aid and the most important things are showing the value and the type it had. The suffixed literals are going to obscure any typedefs. The char literal is going to obscure the signed-ness of the char besides that. The most helpful representation is the cast followed by the plain literal. Ie (uintptr_t)28999 I think we should have a mode in that literal printer for that. This would remove the plain ugly case of the char literal, but still the single quotes can appear within the printed type of the literal, which can already happen anyway. Shafik would that also alleviate your concern? https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
mizvekov wrote: > Should we also be updating JSONNodeDumper.cpp at the same time? We would ideally update it as well. Not necessarily in the same PR, as it's a separate change that doesn't need to be synchronized. It's just not important to me, because I don't personally use the JSON dumper for debugging. https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; shafik wrote: I am going to fight over this but I feel like it makes less sense for integer literals and char literals. Expressions, type etc sure. Consistency for consistency sake while tempting does not feel convincing to me. https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -1086,45 +1106,100 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) { - OS << " integral " << TA.getAsIntegral(); + OS << " integral"; + dumpTemplateArgument(TA); +} + +void TextNodeDumper::dumpTemplateName(TemplateName TN) { + switch (TN.getKind()) { + case TemplateName::Template: +AddChild([=] { Visit(TN.getAsTemplateDecl()); }); +return; + case TemplateName::UsingTemplate: { +const UsingShadowDecl *USD = TN.getAsUsingShadowDecl(); +AddChild([=] { Visit(USD); }); +AddChild("target", [=] { Visit(USD->getTargetDecl()); }); +return; + } + case TemplateName::QualifiedTemplate: { +OS << " qualified"; +const QualifiedTemplateName *QTN = TN.getAsQualifiedTemplateName(); +if (QTN->hasTemplateKeyword()) + OS << " keyword"; +dumpNestedNameSpecifier(QTN->getQualifier()); +dumpTemplateName(QTN->getUnderlyingTemplate()); +return; + } + case TemplateName::DependentTemplate: { +OS << " dependent"; +const DependentTemplateName *DTN = TN.getAsDependentTemplateName(); +dumpNestedNameSpecifier(DTN->getQualifier()); +return; + } + case TemplateName::SubstTemplateTemplateParm: { +const SubstTemplateTemplateParmStorage *STS = +TN.getAsSubstTemplateTemplateParm(); +OS << " subst index " << STS->getIndex(); +if (std::optional PackIndex = STS->getPackIndex()) + OS << " pack_index " << *PackIndex; cor3ntin wrote: OH, I see. I retract my comment. It's not ideal but i do not have a great suggestion (especially as I'm afraid anything we try to do will affect many tests) Maybe the code could spell out `"subst" << " " << "index"` to make it clearer https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/cor3ntin approved this pull request. LGTM (we should find better delimiters but it is out of scope for this change) https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; cor3ntin wrote: Yes, lets keep the quotes. I'm not a fan but it appears consistent. https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/cor3ntin edited https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); mizvekov wrote: `getCanonicalTemplateArgument` returns a TemplateArgument by value. These are small objects we don't manually allocate, and don't unique tem (but may unique things it references internally). This is similar to how we don't take const references to QualType. https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/mizvekov edited https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -1086,45 +1106,100 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) { - OS << " integral " << TA.getAsIntegral(); + OS << " integral"; + dumpTemplateArgument(TA); +} + +void TextNodeDumper::dumpTemplateName(TemplateName TN) { + switch (TN.getKind()) { + case TemplateName::Template: +AddChild([=] { Visit(TN.getAsTemplateDecl()); }); +return; + case TemplateName::UsingTemplate: { +const UsingShadowDecl *USD = TN.getAsUsingShadowDecl(); +AddChild([=] { Visit(USD); }); +AddChild("target", [=] { Visit(USD->getTargetDecl()); }); +return; + } + case TemplateName::QualifiedTemplate: { +OS << " qualified"; +const QualifiedTemplateName *QTN = TN.getAsQualifiedTemplateName(); +if (QTN->hasTemplateKeyword()) + OS << " keyword"; +dumpNestedNameSpecifier(QTN->getQualifier()); +dumpTemplateName(QTN->getUnderlyingTemplate()); +return; + } + case TemplateName::DependentTemplate: { +OS << " dependent"; +const DependentTemplateName *DTN = TN.getAsDependentTemplateName(); +dumpNestedNameSpecifier(DTN->getQualifier()); +return; + } + case TemplateName::SubstTemplateTemplateParm: { +const SubstTemplateTemplateParmStorage *STS = +TN.getAsSubstTemplateTemplateParm(); +OS << " subst index " << STS->getIndex(); +if (std::optional PackIndex = STS->getPackIndex()) + OS << " pack_index " << *PackIndex; mizvekov wrote: Oh, okay, I see the confusion. `subst index` in this case is not one thing, they are two separate things. It only looks that way because we are avoiding writing to the stream two separate times. Ie see the other TemplateArgument kinds above. `subst` refers to the kind of TemplateArgument, ie: type, expr, template, subst, subst_pack, etc. `index 0` refers to the position in the parameter list this substitution represents. `pack_index 0` refers to the position in the pack, in case `index` above points to a pack parameter. This 'pack_index` is the term we already use in the dumper for other Subst nodes. I agree this is not great, but the way we format this text ast-dumper leads to this sort of confusion. Any other suggestions? https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -1086,45 +1106,100 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) { - OS << " integral " << TA.getAsIntegral(); + OS << " integral"; + dumpTemplateArgument(TA); +} + +void TextNodeDumper::dumpTemplateName(TemplateName TN) { + switch (TN.getKind()) { + case TemplateName::Template: +AddChild([=] { Visit(TN.getAsTemplateDecl()); }); +return; + case TemplateName::UsingTemplate: { +const UsingShadowDecl *USD = TN.getAsUsingShadowDecl(); +AddChild([=] { Visit(USD); }); +AddChild("target", [=] { Visit(USD->getTargetDecl()); }); +return; + } + case TemplateName::QualifiedTemplate: { +OS << " qualified"; +const QualifiedTemplateName *QTN = TN.getAsQualifiedTemplateName(); +if (QTN->hasTemplateKeyword()) + OS << " keyword"; +dumpNestedNameSpecifier(QTN->getQualifier()); +dumpTemplateName(QTN->getUnderlyingTemplate()); +return; + } + case TemplateName::DependentTemplate: { +OS << " dependent"; +const DependentTemplateName *DTN = TN.getAsDependentTemplateName(); +dumpNestedNameSpecifier(DTN->getQualifier()); +return; + } + case TemplateName::SubstTemplateTemplateParm: { +const SubstTemplateTemplateParmStorage *STS = +TN.getAsSubstTemplateTemplateParm(); +OS << " subst index " << STS->getIndex(); +if (std::optional PackIndex = STS->getPackIndex()) + OS << " pack_index " << *PackIndex; AaronBallman wrote: Yeah, I'd say let's go with `subst_index` instead. https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/AaronBallman edited https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/AaronBallman commented: Should we also be updating JSONNodeDumper.cpp at the same time? https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); AaronBallman wrote: ```suggestion if (const TemplateArgument &CanonTA = Context->getCanonicalTemplateArgument(TA); ``` https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -1086,45 +1106,100 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) { - OS << " integral " << TA.getAsIntegral(); + OS << " integral"; + dumpTemplateArgument(TA); +} + +void TextNodeDumper::dumpTemplateName(TemplateName TN) { + switch (TN.getKind()) { + case TemplateName::Template: +AddChild([=] { Visit(TN.getAsTemplateDecl()); }); +return; + case TemplateName::UsingTemplate: { +const UsingShadowDecl *USD = TN.getAsUsingShadowDecl(); +AddChild([=] { Visit(USD); }); +AddChild("target", [=] { Visit(USD->getTargetDecl()); }); +return; + } + case TemplateName::QualifiedTemplate: { +OS << " qualified"; +const QualifiedTemplateName *QTN = TN.getAsQualifiedTemplateName(); +if (QTN->hasTemplateKeyword()) + OS << " keyword"; +dumpNestedNameSpecifier(QTN->getQualifier()); +dumpTemplateName(QTN->getUnderlyingTemplate()); +return; + } + case TemplateName::DependentTemplate: { +OS << " dependent"; +const DependentTemplateName *DTN = TN.getAsDependentTemplateName(); +dumpNestedNameSpecifier(DTN->getQualifier()); +return; + } + case TemplateName::SubstTemplateTemplateParm: { +const SubstTemplateTemplateParmStorage *STS = +TN.getAsSubstTemplateTemplateParm(); +OS << " subst index " << STS->getIndex(); +if (std::optional PackIndex = STS->getPackIndex()) + OS << " pack_index " << *PackIndex; cor3ntin wrote: if we decide individual properties have no space (which is reasonable), them "subst index" should be renamed. IE, as long as we are consistent I'm happy @AaronBallman https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/mizvekov updated https://github.com/llvm/llvm-project/pull/93431 >From 86e3852d0501bd24738c094359799c72781ad808 Mon Sep 17 00:00:00 2001 From: Matheus Izvekov Date: Fri, 24 May 2024 12:22:55 -0300 Subject: [PATCH] [clang] Improve ast-dumper text printing of TemplateArgument This improves and unifies our approach to printing all template arguments. The same approach to printing types is extended to all TemplateArguments: A sugared version is printed in quotes, followed by printing the canonical form, unless they would print the same. Special improvements are done to add more detail to template template arguments. It's planned in a future patch to use this improved TemplateName printer for other places besides TemplateArguments. Note: The sugared/desugared printing does not show up for TemplateNames in tests yet, because we do a poor job of preserving their type sugar. This will be improved in a future patch. --- clang/docs/ReleaseNotes.rst | 2 + clang/include/clang/AST/TextNodeDumper.h | 2 + clang/lib/AST/TextNodeDumper.cpp | 103 +++--- clang/test/AST/ast-dump-decl.cpp | 25 +++-- ...penmp-begin-declare-variant_template_2.cpp | 6 +- clang/test/AST/ast-dump-template-name.cpp | 54 + clang/test/AST/ast-dump-using-template.cpp| 8 +- .../constraints-explicit-instantiation.cpp| 6 +- clang/test/OpenMP/align_clause_ast_print.cpp | 2 +- clang/test/OpenMP/generic_loop_ast_print.cpp | 2 +- clang/test/OpenMP/interop_ast_print.cpp | 2 +- clang/test/SemaOpenACC/sub-array-ast.cpp | 2 +- .../aggregate-deduction-candidate.cpp | 18 +-- clang/test/SemaTemplate/attributes.cpp| 64 +-- clang/test/SemaTemplate/deduction-guide.cpp | 19 ++-- clang/test/SemaTemplate/make_integer_seq.cpp | 68 +++- clang/test/SemaTemplate/type_pack_element.cpp | 20 ++-- 17 files changed, 275 insertions(+), 128 deletions(-) create mode 100644 clang/test/AST/ast-dump-template-name.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 825e91876ffce..f7562ce74f4ed 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -98,6 +98,8 @@ ABI Changes in This Version AST Dumping Potentially Breaking Changes +- The text ast-dumper has improved printing of TemplateArguments. + Clang Frontend Potentially Breaking Changes --- - Removed support for constructing on-stack ``TemplateArgumentList``\ s; interfaces should instead diff --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h index 1fede6e462e92..63fa16c9ec47c 100644 --- a/clang/include/clang/AST/TextNodeDumper.h +++ b/clang/include/clang/AST/TextNodeDumper.h @@ -213,6 +213,8 @@ class TextNodeDumper void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK); void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS); void dumpConceptReference(const ConceptReference *R); + void dumpTemplateArgument(const TemplateArgument &TA); + void dumpTemplateName(TemplateName TN); void dumpDeclRef(const Decl *D, StringRef Label = {}); diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp index 4a1e94ffe283b..ed343ffb74124 100644 --- a/clang/lib/AST/TextNodeDumper.cpp +++ b/clang/lib/AST/TextNodeDumper.cpp @@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); + !CanonTA.structurallyEquals(TA)) { +llvm::SmallString<128> CanonStr; +{ + llvm::raw_svector_ostream SS(CanonStr); + CanonTA.print(PrintPolicy, SS, /*IncludeType=*/true); +} +if (CanonStr != Str) + OS << ":'" << CanonStr << "'"; + } +} + const char *TextNodeDumper::getCommandName(unsigned CommandID) { if (Traits) return Traits->getCommandInfo(CommandID)->Name; @@ -1086,45 +1106,100 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplate
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -1086,45 +1106,100 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) { - OS << " integral " << TA.getAsIntegral(); + OS << " integral"; + dumpTemplateArgument(TA); +} + +void TextNodeDumper::dumpTemplateName(TemplateName TN) { + switch (TN.getKind()) { + case TemplateName::Template: +AddChild([=] { Visit(TN.getAsTemplateDecl()); }); +return; + case TemplateName::UsingTemplate: { +const UsingShadowDecl *USD = TN.getAsUsingShadowDecl(); +AddChild([=] { Visit(USD); }); +AddChild("target", [=] { Visit(USD->getTargetDecl()); }); +return; + } + case TemplateName::QualifiedTemplate: { +OS << " qualified"; +const QualifiedTemplateName *QTN = TN.getAsQualifiedTemplateName(); +if (QTN->hasTemplateKeyword()) + OS << " keyword"; +dumpNestedNameSpecifier(QTN->getQualifier()); +dumpTemplateName(QTN->getUnderlyingTemplate()); +return; + } + case TemplateName::DependentTemplate: { +OS << " dependent"; +const DependentTemplateName *DTN = TN.getAsDependentTemplateName(); +dumpNestedNameSpecifier(DTN->getQualifier()); +return; + } + case TemplateName::SubstTemplateTemplateParm: { +const SubstTemplateTemplateParmStorage *STS = +TN.getAsSubstTemplateTemplateParm(); +OS << " subst index " << STS->getIndex(); +if (std::optional PackIndex = STS->getPackIndex()) + OS << " pack_index " << *PackIndex; mizvekov wrote: The spelling `pack_index` is already used for the same purpose in the Subst* node printers. The flags in the AST text dumper are separated by spaces, I think this spelling could mislead someone to think these are two separate things. Any other ideas? If we make a change here, we better change the other printer as well. https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; mizvekov wrote: Yeah, the quotes are what we already do for types, and I think the problem shows up there, though of course not as self-evident as here. I don't think there is a formal solution besides to start escaping the string. https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); + !CanonTA.structurallyEquals(TA)) { +llvm::SmallString<128> CanonStr; +{ + llvm::raw_svector_ostream SS(CanonStr); + CanonTA.print(PrintPolicy, SS, /*IncludeType=*/true); +} +if (CanonStr != Str) + OS << ":'" << CanonStr << "'"; + } mizvekov wrote: This starts showing up in tests after the improvements in https://github.com/llvm/llvm-project/pull/93433, which is stacked on top of this PR. Take a look at some of the modified AST tests there. https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -1086,45 +1106,100 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) { - OS << " integral " << TA.getAsIntegral(); + OS << " integral"; + dumpTemplateArgument(TA); +} + +void TextNodeDumper::dumpTemplateName(TemplateName TN) { + switch (TN.getKind()) { + case TemplateName::Template: +AddChild([=] { Visit(TN.getAsTemplateDecl()); }); +return; + case TemplateName::UsingTemplate: { +const UsingShadowDecl *USD = TN.getAsUsingShadowDecl(); +AddChild([=] { Visit(USD); }); +AddChild("target", [=] { Visit(USD->getTargetDecl()); }); +return; + } + case TemplateName::QualifiedTemplate: { +OS << " qualified"; +const QualifiedTemplateName *QTN = TN.getAsQualifiedTemplateName(); +if (QTN->hasTemplateKeyword()) + OS << " keyword"; +dumpNestedNameSpecifier(QTN->getQualifier()); +dumpTemplateName(QTN->getUnderlyingTemplate()); +return; + } + case TemplateName::DependentTemplate: { +OS << " dependent"; +const DependentTemplateName *DTN = TN.getAsDependentTemplateName(); +dumpNestedNameSpecifier(DTN->getQualifier()); +return; + } + case TemplateName::SubstTemplateTemplateParm: { +const SubstTemplateTemplateParmStorage *STS = +TN.getAsSubstTemplateTemplateParm(); +OS << " subst index " << STS->getIndex(); +if (std::optional PackIndex = STS->getPackIndex()) + OS << " pack_index " << *PackIndex; cor3ntin wrote: ```suggestion OS << " subst index " << STS->getIndex(); if (std::optional PackIndex = STS->getPackIndex()) OS << " pack index " << *PackIndex; ``` https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -1086,45 +1106,100 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) { - OS << " integral " << TA.getAsIntegral(); + OS << " integral"; + dumpTemplateArgument(TA); +} + +void TextNodeDumper::dumpTemplateName(TemplateName TN) { + switch (TN.getKind()) { + case TemplateName::Template: +AddChild([=] { Visit(TN.getAsTemplateDecl()); }); +return; + case TemplateName::UsingTemplate: { +const UsingShadowDecl *USD = TN.getAsUsingShadowDecl(); +AddChild([=] { Visit(USD); }); +AddChild("target", [=] { Visit(USD->getTargetDecl()); }); +return; + } + case TemplateName::QualifiedTemplate: { +OS << " qualified"; +const QualifiedTemplateName *QTN = TN.getAsQualifiedTemplateName(); +if (QTN->hasTemplateKeyword()) + OS << " keyword"; +dumpNestedNameSpecifier(QTN->getQualifier()); +dumpTemplateName(QTN->getUnderlyingTemplate()); +return; + } + case TemplateName::DependentTemplate: { +OS << " dependent"; +const DependentTemplateName *DTN = TN.getAsDependentTemplateName(); +dumpNestedNameSpecifier(DTN->getQualifier()); +return; + } + case TemplateName::SubstTemplateTemplateParm: { +const SubstTemplateTemplateParmStorage *STS = +TN.getAsSubstTemplateTemplateParm(); +OS << " subst index " << STS->getIndex(); +if (std::optional PackIndex = STS->getPackIndex()) + OS << " pack_index " << *PackIndex; +if (const TemplateTemplateParmDecl *P = STS->getParameter()) + AddChild("parameter", [=] { Visit(P); }); +dumpDeclRef(STS->getAssociatedDecl(), "associated"); +AddChild("replacement", [=] { dumpTemplateName(STS->getReplacement()); }); +return; + } + // FIXME: Implement these. + case TemplateName::OverloadedTemplate: +OS << " overloaded"; +return; + case TemplateName::AssumedTemplate: +OS << " assumed"; +return; + case TemplateName::SubstTemplateTemplateParmPack: +OS << " subst_pack"; cor3ntin wrote: ```suggestion OS << " subst pack"; ``` https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; cor3ntin wrote: I'm not sure how i feel about the quotes. It's particularly awkward in the char literal case https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
@@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); + !CanonTA.structurallyEquals(TA)) { +llvm::SmallString<128> CanonStr; +{ + llvm::raw_svector_ostream SS(CanonStr); + CanonTA.print(PrintPolicy, SS, /*IncludeType=*/true); +} +if (CanonStr != Str) + OS << ":'" << CanonStr << "'"; + } cor3ntin wrote: Do we have tests for that? https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/cor3ntin edited https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/cor3ntin commented: Thanks! I think this is an improvement but we probably want to find a better syntax than quotes https://github.com/llvm/llvm-project/pull/93431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/mizvekov updated https://github.com/llvm/llvm-project/pull/93431 >From f9892ebed002d73c74f44629e926386006f7bec1 Mon Sep 17 00:00:00 2001 From: Matheus Izvekov Date: Fri, 24 May 2024 12:22:55 -0300 Subject: [PATCH] [clang] Improve ast-dumper text printing of TemplateArgument This improves and unifies our approach to printing all template arguments. The same approach to printing types is extended to all TemplateArguments: A sugared version is printed in quotes, followed by printing the canonical form, unless they would print the same. Special improvements are done to add more detail to template template arguments. It's planned in a future patch to use this improved TemplateName printer for other places besides TemplateArguments. Note: The sugared/desugared printing does not show up for TemplateNames in tests yet, because we do a poor job of preserving their type sugar. This will be improved in a future patch. --- clang/docs/ReleaseNotes.rst | 2 + clang/include/clang/AST/TextNodeDumper.h | 2 + clang/lib/AST/TextNodeDumper.cpp | 103 +++--- clang/test/AST/ast-dump-decl.cpp | 25 +++-- ...penmp-begin-declare-variant_template_2.cpp | 6 +- clang/test/AST/ast-dump-template-name.cpp | 54 + clang/test/AST/ast-dump-using-template.cpp| 8 +- .../constraints-explicit-instantiation.cpp| 6 +- clang/test/OpenMP/align_clause_ast_print.cpp | 2 +- clang/test/OpenMP/generic_loop_ast_print.cpp | 2 +- clang/test/OpenMP/interop_ast_print.cpp | 2 +- clang/test/SemaOpenACC/sub-array-ast.cpp | 2 +- .../aggregate-deduction-candidate.cpp | 18 +-- clang/test/SemaTemplate/attributes.cpp| 64 +-- clang/test/SemaTemplate/deduction-guide.cpp | 14 +-- clang/test/SemaTemplate/make_integer_seq.cpp | 68 +++- clang/test/SemaTemplate/type_pack_element.cpp | 20 ++-- 17 files changed, 272 insertions(+), 126 deletions(-) create mode 100644 clang/test/AST/ast-dump-template-name.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 825e91876ffce..f7562ce74f4ed 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -98,6 +98,8 @@ ABI Changes in This Version AST Dumping Potentially Breaking Changes +- The text ast-dumper has improved printing of TemplateArguments. + Clang Frontend Potentially Breaking Changes --- - Removed support for constructing on-stack ``TemplateArgumentList``\ s; interfaces should instead diff --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h index 1fede6e462e92..63fa16c9ec47c 100644 --- a/clang/include/clang/AST/TextNodeDumper.h +++ b/clang/include/clang/AST/TextNodeDumper.h @@ -213,6 +213,8 @@ class TextNodeDumper void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK); void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS); void dumpConceptReference(const ConceptReference *R); + void dumpTemplateArgument(const TemplateArgument &TA); + void dumpTemplateName(TemplateName TN); void dumpDeclRef(const Decl *D, StringRef Label = {}); diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp index 4a1e94ffe283b..ed343ffb74124 100644 --- a/clang/lib/AST/TextNodeDumper.cpp +++ b/clang/lib/AST/TextNodeDumper.cpp @@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); + !CanonTA.structurallyEquals(TA)) { +llvm::SmallString<128> CanonStr; +{ + llvm::raw_svector_ostream SS(CanonStr); + CanonTA.print(PrintPolicy, SS, /*IncludeType=*/true); +} +if (CanonStr != Str) + OS << ":'" << CanonStr << "'"; + } +} + const char *TextNodeDumper::getCommandName(unsigned CommandID) { if (Traits) return Traits->getCommandInfo(CommandID)->Name; @@ -1086,45 +1106,100 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplateA
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/mizvekov updated https://github.com/llvm/llvm-project/pull/93431 >From 031e7c235ce5cbae31504c21eeecc7655fbd1566 Mon Sep 17 00:00:00 2001 From: Matheus Izvekov Date: Fri, 24 May 2024 12:22:55 -0300 Subject: [PATCH] [clang] Improve ast-dumper text printing of TemplateArgument This improves and unifies our approach to printing all template arguments. The same approach to printing types is extended to all TemplateArguments: A sugared version is printed in quotes, followed by printing the canonical form, unless they would print the same. Special improvements are done to add more detail to template template arguments. It's planned in a future patch to use this improved TemplateName printer for other places besides TemplateArguments. Note: The sugared/desugared printing does not show up for TemplateNames in tests yet, because we do a poor job of preserving their type sugar. This will be improved in a future patch. --- clang/docs/ReleaseNotes.rst | 5 + clang/include/clang/AST/TextNodeDumper.h | 2 + clang/lib/AST/TextNodeDumper.cpp | 103 +++--- clang/test/AST/ast-dump-decl.cpp | 25 +++-- ...penmp-begin-declare-variant_template_2.cpp | 6 +- clang/test/AST/ast-dump-template-name.cpp | 54 + clang/test/AST/ast-dump-using-template.cpp| 8 +- .../constraints-explicit-instantiation.cpp| 6 +- clang/test/OpenMP/align_clause_ast_print.cpp | 2 +- clang/test/OpenMP/generic_loop_ast_print.cpp | 2 +- clang/test/OpenMP/interop_ast_print.cpp | 2 +- clang/test/SemaOpenACC/sub-array-ast.cpp | 2 +- .../aggregate-deduction-candidate.cpp | 16 +-- clang/test/SemaTemplate/attributes.cpp| 64 +-- clang/test/SemaTemplate/deduction-guide.cpp | 14 +-- clang/test/SemaTemplate/make_integer_seq.cpp | 68 +++- clang/test/SemaTemplate/type_pack_element.cpp | 20 ++-- 17 files changed, 274 insertions(+), 125 deletions(-) create mode 100644 clang/test/AST/ast-dump-template-name.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 825e91876ffce..403a107edef17 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -818,6 +818,11 @@ Miscellaneous Clang Crashes Fixed when ``-fdump-record-layouts-complete`` is passed. Fixes #GH83684. - Unhandled StructuralValues in the template differ (#GH93068). +Miscellaneous Clang Improvements +^ + +- The text ast-dumper has improved printing of TemplateArguments. + OpenACC Specific Changes diff --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h index 1fede6e462e92..63fa16c9ec47c 100644 --- a/clang/include/clang/AST/TextNodeDumper.h +++ b/clang/include/clang/AST/TextNodeDumper.h @@ -213,6 +213,8 @@ class TextNodeDumper void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK); void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS); void dumpConceptReference(const ConceptReference *R); + void dumpTemplateArgument(const TemplateArgument &TA); + void dumpTemplateName(TemplateName TN); void dumpDeclRef(const Decl *D, StringRef Label = {}); diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp index 4a1e94ffe283b..ed343ffb74124 100644 --- a/clang/lib/AST/TextNodeDumper.cpp +++ b/clang/lib/AST/TextNodeDumper.cpp @@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); + !CanonTA.structurallyEquals(TA)) { +llvm::SmallString<128> CanonStr; +{ + llvm::raw_svector_ostream SS(CanonStr); + CanonTA.print(PrintPolicy, SS, /*IncludeType=*/true); +} +if (CanonStr != Str) + OS << ":'" << CanonStr << "'"; + } +} + const char *TextNodeDumper::getCommandName(unsigned CommandID) { if (Traits) return Traits->getCommandInfo(CommandID)->Name; @@ -1086,45 +1106,100 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplateArgumen
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/mizvekov updated https://github.com/llvm/llvm-project/pull/93431 >From c23a96038a8233b44b49bc0a1d2a2475e4d2a8ae Mon Sep 17 00:00:00 2001 From: Matheus Izvekov Date: Fri, 24 May 2024 12:22:55 -0300 Subject: [PATCH] [clang] Improve ast-dumper text printing of TemplateArgument This improves and unifies our approach to printing all template arguments. The same approach to printing types is extended to all TemplateArguments: A sugared version is printed in quotes, followed by printing the canonical form, unless they would print the same. Special improvements are done to add more detail to template template arguments. It's planned in a future patch to use this improved TemplateName printer for other places besides TemplateArguments. Note: The sugared/desugared printing does not show up for TemplateNames in tests yet, because we do a poor job of preserving their type sugar. This will be improved in a future patch. --- clang/docs/ReleaseNotes.rst | 5 + clang/include/clang/AST/TextNodeDumper.h | 2 + clang/lib/AST/TextNodeDumper.cpp | 94 --- clang/test/AST/ast-dump-decl.cpp | 25 ++--- ...penmp-begin-declare-variant_template_2.cpp | 6 +- clang/test/AST/ast-dump-template-name.cpp | 54 +++ clang/test/AST/ast-dump-using-template.cpp| 8 +- .../constraints-explicit-instantiation.cpp| 6 +- clang/test/OpenMP/align_clause_ast_print.cpp | 2 +- clang/test/OpenMP/generic_loop_ast_print.cpp | 2 +- clang/test/OpenMP/interop_ast_print.cpp | 2 +- clang/test/SemaOpenACC/sub-array-ast.cpp | 2 +- .../aggregate-deduction-candidate.cpp | 16 ++-- clang/test/SemaTemplate/attributes.cpp| 64 ++--- clang/test/SemaTemplate/deduction-guide.cpp | 14 +-- clang/test/SemaTemplate/make_integer_seq.cpp | 68 -- clang/test/SemaTemplate/type_pack_element.cpp | 20 ++-- 17 files changed, 265 insertions(+), 125 deletions(-) create mode 100644 clang/test/AST/ast-dump-template-name.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 825e91876ffce..403a107edef17 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -818,6 +818,11 @@ Miscellaneous Clang Crashes Fixed when ``-fdump-record-layouts-complete`` is passed. Fixes #GH83684. - Unhandled StructuralValues in the template differ (#GH93068). +Miscellaneous Clang Improvements +^ + +- The text ast-dumper has improved printing of TemplateArguments. + OpenACC Specific Changes diff --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h index 1fede6e462e92..63fa16c9ec47c 100644 --- a/clang/include/clang/AST/TextNodeDumper.h +++ b/clang/include/clang/AST/TextNodeDumper.h @@ -213,6 +213,8 @@ class TextNodeDumper void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK); void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS); void dumpConceptReference(const ConceptReference *R); + void dumpTemplateArgument(const TemplateArgument &TA); + void dumpTemplateName(TemplateName TN); void dumpDeclRef(const Decl *D, StringRef Label = {}); diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp index 4a1e94ffe283b..742203e3b946d 100644 --- a/clang/lib/AST/TextNodeDumper.cpp +++ b/clang/lib/AST/TextNodeDumper.cpp @@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); + !CanonTA.structurallyEquals(TA)) { +llvm::SmallString<128> CanonStr; +{ + llvm::raw_svector_ostream SS(CanonStr); + CanonTA.print(PrintPolicy, SS, /*IncludeType=*/true); +} +if (CanonStr != Str) + OS << ":'" << CanonStr << "'"; + } +} + const char *TextNodeDumper::getCommandName(unsigned CommandID) { if (Traits) return Traits->getCommandInfo(CommandID)->Name; @@ -1086,45 +1106,91 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) { OS << " nullptr"; + dumpTemplateArgument(TA); } void TextNodeDumper::VisitIntegralTemplateArgument(const Te
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Matheus Izvekov (mizvekov) Changes This improves and unifies our approach to printing all template arguments. The same approach to printing types is extended to all TemplateArguments: A sugared version is printed in quotes, followed by printing the canonical form, unless they would print the same. Special improvements are done to add more detail to template template arguments. It's planned in a future patch to use this improved TemplateName printer for other places besides TemplateArguments. Note: The sugared/desugared printing does not show up for TemplateNames in tests yet, because we do a poor job of preserving their type sugar. This will be improved in a future patch. --- Patch is 50.89 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/93431.diff 17 Files Affected: - (modified) clang/docs/ReleaseNotes.rst (+5) - (modified) clang/include/clang/AST/TextNodeDumper.h (+3) - (modified) clang/lib/AST/TextNodeDumper.cpp (+80-14) - (modified) clang/test/AST/ast-dump-decl.cpp (+14-11) - (modified) clang/test/AST/ast-dump-openmp-begin-declare-variant_template_2.cpp (+3-3) - (added) clang/test/AST/ast-dump-template-name.cpp (+54) - (modified) clang/test/AST/ast-dump-using-template.cpp (+5-3) - (modified) clang/test/AST/constraints-explicit-instantiation.cpp (+3-3) - (modified) clang/test/OpenMP/align_clause_ast_print.cpp (+1-1) - (modified) clang/test/OpenMP/generic_loop_ast_print.cpp (+1-1) - (modified) clang/test/OpenMP/interop_ast_print.cpp (+1-1) - (modified) clang/test/SemaOpenACC/sub-array-ast.cpp (+1-1) - (modified) clang/test/SemaTemplate/aggregate-deduction-candidate.cpp (+8-8) - (modified) clang/test/SemaTemplate/attributes.cpp (+32-32) - (modified) clang/test/SemaTemplate/deduction-guide.cpp (+7-7) - (modified) clang/test/SemaTemplate/make_integer_seq.cpp (+38-30) - (modified) clang/test/SemaTemplate/type_pack_element.cpp (+10-10) ``diff diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 825e91876ffce..403a107edef17 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -818,6 +818,11 @@ Miscellaneous Clang Crashes Fixed when ``-fdump-record-layouts-complete`` is passed. Fixes #GH83684. - Unhandled StructuralValues in the template differ (#GH93068). +Miscellaneous Clang Improvements +^ + +- The text ast-dumper has improved printing of TemplateArguments. + OpenACC Specific Changes diff --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h index 1fede6e462e92..0d057b8011164 100644 --- a/clang/include/clang/AST/TextNodeDumper.h +++ b/clang/include/clang/AST/TextNodeDumper.h @@ -211,8 +211,11 @@ class TextNodeDumper void dumpAccessSpecifier(AccessSpecifier AS); void dumpCleanupObject(const ExprWithCleanups::CleanupObject &C); void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK); + void dumpBareNestedNameSpecifier(NestedNameSpecifier *NNS); void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS); void dumpConceptReference(const ConceptReference *R); + void dumpTemplateArgument(const TemplateArgument &TA); + void dumpTemplateName(TemplateName TN); void dumpDeclRef(const Decl *D, StringRef Label = {}); diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp index 4a1e94ffe283b..742203e3b946d 100644 --- a/clang/lib/AST/TextNodeDumper.cpp +++ b/clang/lib/AST/TextNodeDumper.cpp @@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); + !CanonTA.structurallyEquals(TA)) { +llvm::SmallString<128> CanonStr; +{ + llvm::raw_svector_ostream SS(CanonStr); + CanonTA.print(PrintPolicy, SS, /*IncludeType=*/true); +} +if (CanonStr != Str) + OS << ":'" << CanonStr << "'"; + } +} + const char *TextNodeDumper::getCommandName(unsigned CommandID) { if (Traits) return Traits->getCommandInfo(CommandID)->Name; @@ -1086,45 +1106,91 @@ void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { OS << " type"; - dumpType(TA.getAsType()); + dumpTemplateArgument(TA); } void TextNodeDumper::VisitDeclarationTemplateArgument( const TemplateArgument &TA) { OS << " decl"; + dumpTemplateArgument(TA); dumpDeclRef(TA.getAsDecl()); } -void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { +void TextNodeDumper::VisitNullPtrTemplateArgument(const T
[clang] [clang] Improve ast-dumper text printing of TemplateArgument (PR #93431)
https://github.com/mizvekov created https://github.com/llvm/llvm-project/pull/93431 This improves and unifies our approach to printing all template arguments. The same approach to printing types is extended to all TemplateArguments: A sugared version is printed in quotes, followed by printing the canonical form, unless they would print the same. Special improvements are done to add more detail to template template arguments. It's planned in a future patch to use this improved TemplateName printer for other places besides TemplateArguments. Note: The sugared/desugared printing does not show up for TemplateNames in tests yet, because we do a poor job of preserving their type sugar. This will be improved in a future patch. >From 51ca62950c19648895f1947bbf981408193d9002 Mon Sep 17 00:00:00 2001 From: Matheus Izvekov Date: Fri, 24 May 2024 12:22:55 -0300 Subject: [PATCH] [clang] Improve ast-dumper text printing of TemplateArgument This improves and unifies our approach to printing all template arguments. The same approach to printing types is extended to all TemplateArguments: A sugared version is printed in quotes, followed by printing the canonical form, unless they would print the same. Special improvements are done to add more detail to template template arguments. It's planned in a future patch to use this improved TemplateName printer for other places besides TemplateArguments. Note: The sugared/desugared printing does not show up for TemplateNames in tests yet, because we do a poor job of preserving their type sugar. This will be improved in a future patch. --- clang/docs/ReleaseNotes.rst | 5 + clang/include/clang/AST/TextNodeDumper.h | 3 + clang/lib/AST/TextNodeDumper.cpp | 94 --- clang/test/AST/ast-dump-decl.cpp | 25 ++--- ...penmp-begin-declare-variant_template_2.cpp | 6 +- clang/test/AST/ast-dump-template-name.cpp | 54 +++ clang/test/AST/ast-dump-using-template.cpp| 8 +- .../constraints-explicit-instantiation.cpp| 6 +- clang/test/OpenMP/align_clause_ast_print.cpp | 2 +- clang/test/OpenMP/generic_loop_ast_print.cpp | 2 +- clang/test/OpenMP/interop_ast_print.cpp | 2 +- clang/test/SemaOpenACC/sub-array-ast.cpp | 2 +- .../aggregate-deduction-candidate.cpp | 16 ++-- clang/test/SemaTemplate/attributes.cpp| 64 ++--- clang/test/SemaTemplate/deduction-guide.cpp | 14 +-- clang/test/SemaTemplate/make_integer_seq.cpp | 68 -- clang/test/SemaTemplate/type_pack_element.cpp | 20 ++-- 17 files changed, 266 insertions(+), 125 deletions(-) create mode 100644 clang/test/AST/ast-dump-template-name.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 825e91876ffce..403a107edef17 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -818,6 +818,11 @@ Miscellaneous Clang Crashes Fixed when ``-fdump-record-layouts-complete`` is passed. Fixes #GH83684. - Unhandled StructuralValues in the template differ (#GH93068). +Miscellaneous Clang Improvements +^ + +- The text ast-dumper has improved printing of TemplateArguments. + OpenACC Specific Changes diff --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h index 1fede6e462e92..0d057b8011164 100644 --- a/clang/include/clang/AST/TextNodeDumper.h +++ b/clang/include/clang/AST/TextNodeDumper.h @@ -211,8 +211,11 @@ class TextNodeDumper void dumpAccessSpecifier(AccessSpecifier AS); void dumpCleanupObject(const ExprWithCleanups::CleanupObject &C); void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK); + void dumpBareNestedNameSpecifier(NestedNameSpecifier *NNS); void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS); void dumpConceptReference(const ConceptReference *R); + void dumpTemplateArgument(const TemplateArgument &TA); + void dumpTemplateName(TemplateName TN); void dumpDeclRef(const Decl *D, StringRef Label = {}); diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp index 4a1e94ffe283b..742203e3b946d 100644 --- a/clang/lib/AST/TextNodeDumper.cpp +++ b/clang/lib/AST/TextNodeDumper.cpp @@ -947,6 +947,26 @@ void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { }); } +void TextNodeDumper::dumpTemplateArgument(const TemplateArgument &TA) { + llvm::SmallString<128> Str; + { +llvm::raw_svector_ostream SS(Str); +TA.print(PrintPolicy, SS, /*IncludeType=*/true); + } + OS << " '" << Str << "'"; + + if (TemplateArgument CanonTA = Context->getCanonicalTemplateArgument(TA); + !CanonTA.structurallyEquals(TA)) { +llvm::SmallString<128> CanonStr; +{ + llvm::raw_svector_ostream SS(CanonStr); + CanonTA.print(PrintPolicy, SS, /*IncludeType=*/true); +} +if (CanonStr != Str) + OS << ":'" << CanonStr << "'"; + } +} + const