Skip to content

[Clang] Fix the printout of CXXParenListInitExpr involving default arguments #130731

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 4 commits into from
Mar 12, 2025

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented Mar 11, 2025

The parantheses are unnecessary IMO because they should have been handled in the parents of such expressions, e.g. in CXXFunctionalCastExpr.

Moreover, we shouldn't join CXXDefaultInitExpr either because they are not printed at all.

@zyn0217 zyn0217 requested a review from cor3ntin March 11, 2025 08:05
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 11, 2025

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

The parantheses are unnecessary IMO because they should have been handled in the parents of such expressions, e.g. in CXXFunctionalCastExpr.

Moreover, we shouldn't join CXXDefaultInitExpr either because they are not printed at all.


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

3 Files Affected:

  • (modified) clang/lib/AST/StmtPrinter.cpp (+1-3)
  • (modified) clang/test/CXX/dcl.decl/dcl.decomp/p3.cpp (+41-1)
  • (modified) clang/test/CodeGen/p0963r3.cpp (-3)
diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp
index facdc4104c374..e0063ec5f25eb 100644
--- a/clang/lib/AST/StmtPrinter.cpp
+++ b/clang/lib/AST/StmtPrinter.cpp
@@ -2659,10 +2659,8 @@ void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
 }
 
 void StmtPrinter::VisitCXXParenListInitExpr(CXXParenListInitExpr *Node) {
-  OS << "(";
-  llvm::interleaveComma(Node->getInitExprs(), OS,
+  llvm::interleaveComma(Node->getUserSpecifiedInitExprs(), OS,
                         [&](Expr *E) { PrintExpr(E); });
-  OS << ")";
 }
 
 void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
diff --git a/clang/test/CXX/dcl.decl/dcl.decomp/p3.cpp b/clang/test/CXX/dcl.decl/dcl.decomp/p3.cpp
index ce5eefc6bfdb4..09c6cb4a25421 100644
--- a/clang/test/CXX/dcl.decl/dcl.decomp/p3.cpp
+++ b/clang/test/CXX/dcl.decl/dcl.decomp/p3.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -std=c++1z -verify %s
+// RUN: %clang_cc1 -std=c++17 -verify %s
+// RUN: %clang_cc1 -std=c++20 -verify %s
 
 using size_t = decltype(sizeof(0));
 
@@ -301,3 +302,42 @@ template <> struct std::tuple_size<UsingGet> {
 template <> struct std::tuple_element<0, UsingGet> { typedef int type; };
 
 auto [y] = UsingGet();
+
+#if __cplusplus >= 202002L
+
+namespace DefaultArguments {
+
+struct S {
+  int a, b;
+  bool flag = false;
+
+  constexpr explicit operator bool() {
+    return a != b;
+  }
+
+  constexpr bool operator==(S rhs) {
+    return a == rhs.a && b == rhs.b;
+  }
+
+  template <int I>
+  constexpr int& get() {
+    return I == 0 ? a : b;
+  }
+};
+
+}
+
+template <> struct std::tuple_size<DefaultArguments::S> {
+  static const int value = 2;
+};
+
+template <int I> struct std::tuple_element<I, DefaultArguments::S> {
+  using type = int;
+};
+
+static_assert(DefaultArguments::S(1, 2) == DefaultArguments::S(1, 2));
+
+static_assert(DefaultArguments::S(1, 2) == DefaultArguments::S(3, 4));
+// expected-error@-1 {{failed due to requirement 'DefaultArguments::S(1, 2) == DefaultArguments::S(3, 4)'}}
+
+#endif
diff --git a/clang/test/CodeGen/p0963r3.cpp b/clang/test/CodeGen/p0963r3.cpp
index b48b5294e093e..4a5e6c3f5d751 100644
--- a/clang/test/CodeGen/p0963r3.cpp
+++ b/clang/test/CodeGen/p0963r3.cpp
@@ -139,9 +139,6 @@ constexpr int bar(auto) {
   }();
   static_assert(value == S(1, 2));
 
-  // FIXME: The diagnostic message adds a trailing comma "static assertion failed due to requirement 'value == Case1::S((0, 1, ))'"
-  // static_assert(value == S(0, 1));
-
   constexpr auto value2 = [] {
     if (auto [a, b] = S(1, 2))
       return S(a, b);

…guments

The parantheses are unnecessary IMO because they should have been
handled in the parents of such expressions, e.g. in
CXXFunctionalCastExpr.

Moreover, we shouldn't join CXXDefaultInitExpr either because they
are not printed at all.
@zyn0217 zyn0217 force-pushed the printer-bug-CXXParenListInitExpr branch from dafb057 to c643ba2 Compare March 11, 2025 08:14
@cor3ntin
Copy link
Contributor

The CI failure seems relevant here

@zyn0217
Copy link
Contributor Author

zyn0217 commented Mar 11, 2025

The CI failure seems relevant here

Not a big deal.

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

LGTM, I am a bit discouraged that this did not alter an tests.

@zyn0217 zyn0217 merged commit c127618 into llvm:main Mar 12, 2025
7 of 11 checks passed
@zyn0217 zyn0217 deleted the printer-bug-CXXParenListInitExpr branch March 12, 2025 02:39
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