[clang] [Clang] Fix the printout of CXXParenListInitExpr involving default arguments (PR #130731)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 11 01:06:02 PDT 2025


https://github.com/zyn0217 created https://github.com/llvm/llvm-project/pull/130731

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.

>From dafb057cc47479ede31ac8076e627c6d5355dea4 Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Tue, 11 Mar 2025 14:18:20 +0800
Subject: [PATCH] [Clang] Fix the printout of CXXParenListInitExpr involving
 default arguments

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.
---
 clang/lib/AST/StmtPrinter.cpp             |  4 +--
 clang/test/CXX/dcl.decl/dcl.decomp/p3.cpp | 42 ++++++++++++++++++++++-
 clang/test/CodeGen/p0963r3.cpp            |  3 --
 3 files changed, 42 insertions(+), 7 deletions(-)

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 at -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);



More information about the cfe-commits mailing list