Skip to content

[clang] [ASTDump] Add support for structural value template arguments in TextNodeDumper #126341

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
merged 8 commits into from
Feb 13, 2025

Conversation

sakria9
Copy link
Contributor

@sakria9 sakria9 commented Feb 8, 2025

Dump structural value in AST.

Example code

template<_Complex int x>
struct E;
template<>
struct E<{1, 2}>;

Command clang++ -Xclang -ast-dump usr.cpp -fsyntax-only -std=c++23

Current AST dump result

|-ClassTemplateDecl 0x61a3b52364e8 <usr.cpp:172:1, line:173:8> col:8 E
| |-NonTypeTemplateParmDecl 0x61a3b52363b8 <line:172:10, col:23> col:23 '_Complex int' depth 0 index 0 x
| |-CXXRecordDecl 0x61a3b5236438 <line:173:1, col:8> col:8 struct E
| `-ClassTemplateSpecialization 0x61a3b5236918 'E'
`-ClassTemplateSpecializationDecl 0x61a3b5236918 <line:174:1, line:175:16> col:8 struct E explicit_specialization
  `-TemplateArgument

After apply this fix

|-ClassTemplateDecl 0x7ce2a834fd60 <usr.cpp:172:1, line:173:8> col:8 E
| |-NonTypeTemplateParmDecl 0x7ce2a834fc10 <line:172:10, col:23> col:23 '_Complex int' depth 0 index 0 x
| |-CXXRecordDecl 0x7ce2a834fca0 <line:173:1, col:8> col:8 struct E
| `-ClassTemplateSpecialization 0x7ce2a8350218 'E'
`-ClassTemplateSpecializationDecl 0x7ce2a8350218 <line:174:1, line:175:16> col:8 struct E explicit_specialization
  `-TemplateArgument structural value '1+2i'

Copy link

github-actions bot commented Feb 8, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 8, 2025

@llvm/pr-subscribers-clang

Author: None (sakria9)

Changes

Dump structural value in AST.

Example code

template&lt;_Complex int x&gt;
struct E;
template&lt;&gt;
struct E&lt;{1, 2}&gt;;

Command clang++ -Xclang -ast-dump usr.cpp -fsyntax-only -std=c++23

Current AST dump result

|-ClassTemplateDecl 0x61a3b52364e8 &lt;usr.cpp:172:1, line:173:8&gt; col:8 E
| |-NonTypeTemplateParmDecl 0x61a3b52363b8 &lt;line:172:10, col:23&gt; col:23 '_Complex int' depth 0 index 0 x
| |-CXXRecordDecl 0x61a3b5236438 &lt;line:173:1, col:8&gt; col:8 struct E
| `-ClassTemplateSpecialization 0x61a3b5236918 'E'
`-ClassTemplateSpecializationDecl 0x61a3b5236918 &lt;line:174:1, line:175:16&gt; col:8 struct E explicit_specialization
  `-TemplateArgument

After apply this fix

|-ClassTemplateDecl 0x7ce2a834fd60 &lt;usr.cpp:172:1, line:173:8&gt; col:8 E
| |-NonTypeTemplateParmDecl 0x7ce2a834fc10 &lt;line:172:10, col:23&gt; col:23 '_Complex int' depth 0 index 0 x
| |-CXXRecordDecl 0x7ce2a834fca0 &lt;line:173:1, col:8&gt; col:8 struct E
| `-ClassTemplateSpecialization 0x7ce2a8350218 'E'
`-ClassTemplateSpecializationDecl 0x7ce2a8350218 &lt;line:174:1, line:175:16&gt; col:8 struct E explicit_specialization
  `-TemplateArgument structural value '1+2i'

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

2 Files Affected:

  • (modified) clang/include/clang/AST/TextNodeDumper.h (+1)
  • (modified) clang/lib/AST/TextNodeDumper.cpp (+5)
diff --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h
index bfd205ffb0d99a..4b5ad2b5fa74c0 100644
--- a/clang/include/clang/AST/TextNodeDumper.h
+++ b/clang/include/clang/AST/TextNodeDumper.h
@@ -249,6 +249,7 @@ class TextNodeDumper
   void VisitDeclarationTemplateArgument(const TemplateArgument &TA);
   void VisitNullPtrTemplateArgument(const TemplateArgument &TA);
   void VisitIntegralTemplateArgument(const TemplateArgument &TA);
+  void VisitStructuralValueTemplateArgument(const TemplateArgument &TA);
   void VisitTemplateTemplateArgument(const TemplateArgument &TA);
   void VisitTemplateExpansionTemplateArgument(const TemplateArgument &TA);
   void VisitExpressionTemplateArgument(const TemplateArgument &TA);
diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp
index 6da1f776b4b635..cb0c742177d2c7 100644
--- a/clang/lib/AST/TextNodeDumper.cpp
+++ b/clang/lib/AST/TextNodeDumper.cpp
@@ -1226,6 +1226,11 @@ void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) {
   dumpTemplateArgument(TA);
 }
 
+void TextNodeDumper::VisitStructuralValueTemplateArgument(const TemplateArgument &TA) {
+  OS << " structural value";
+  dumpTemplateArgument(TA);
+}
+
 void TextNodeDumper::dumpTemplateName(TemplateName TN, StringRef Label) {
   AddChild(Label, [=] {
     {

Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch. This needs a test and a release note.

@sakria9
Copy link
Contributor Author

sakria9 commented Feb 8, 2025

@zyn0217 Hi, I have added a test and modfied the release note. Please take a look.

Test log:

$ ./bin/llvm-lit ../clang/test/AST/ast-dump-template-argument-structural-value.cpp 
llvm-lit: /workspaces/clice/deps/llvm/llvm/utils/lit/lit/llvm/config.py:506: note: using clang: /workspaces/clice/deps/llvm/build-debug/bin/clang
-- Testing: 1 tests, 1 workers --
PASS: Clang :: AST/ast-dump-template-argument-structural-value.cpp (1 of 1)

Testing Time: 0.22s

Total Discovered Tests: 1
  Passed: 1 (100.00%)

@@ -0,0 +1,12 @@
// RUN: %clang_cc1 -std=c++20 -ast-dump -ast-dump-filter=pr126341 %s | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you consolidate the test into a pre-existing file e.g. AST/ast-dump-templates.cpp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved.

@sakria9
Copy link
Contributor Author

sakria9 commented Feb 8, 2025

@zyn0217 Hi, I have 1) modified the release note 2) moved the test 3) supported dump structural value in JSONNodeDumper.
Please take a look.

Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@zyn0217 zyn0217 merged commit 7050e7d into llvm:main Feb 13, 2025
7 checks passed
Copy link

@sakria9 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

flovent pushed a commit to flovent/llvm-project that referenced this pull request Feb 13, 2025
… in TextNodeDumper (llvm#126341)

It was missed in 5518a9d which introduced this new template argument kind.
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
… in TextNodeDumper (llvm#126341)

It was missed in 5518a9d which introduced this new template argument kind.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
… in TextNodeDumper (llvm#126341)

It was missed in 5518a9d which introduced this new template argument kind.
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.

3 participants