[clang] d1a59ee - [Clang] Remove redundant init-parens in AST print

Zhihao Yuan via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 28 17:31:41 PST 2022


Author: Zhihao Yuan
Date: 2022-02-28T19:31:16-06:00
New Revision: d1a59eefd3a09f08ba425a20899fbd1766babc45

URL: https://github.com/llvm/llvm-project/commit/d1a59eefd3a09f08ba425a20899fbd1766babc45
DIFF: https://github.com/llvm/llvm-project/commit/d1a59eefd3a09f08ba425a20899fbd1766babc45.diff

LOG: [Clang] Remove redundant init-parens in AST print

Given a dependent `T` (maybe an undeduced `auto`),

Before:

    new T(z)  -->  new T((z))  # changes meaning with more args
    new T{z}  -->  new T{z}
        T(z)  -->      T(z)
        T{z}  -->      T({z})  # forbidden if T is auto

After:

    new T(z)  -->  new T(z)
    new T{z}  -->  new T{z}
        T(z)   -->     T(z)
        T{z}   -->     T{z}

Depends on D113393

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D120608

Added: 
    

Modified: 
    clang/lib/AST/StmtPrinter.cpp
    clang/test/CXX/expr/expr.prim/expr.prim.req/simple-requirement.cpp
    clang/test/SemaCXX/cxx2b-ast-print.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp
index 677181f925a54..a6aa9fe45b027 100644
--- a/clang/lib/AST/StmtPrinter.cpp
+++ b/clang/lib/AST/StmtPrinter.cpp
@@ -2153,11 +2153,13 @@ void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
     OS << ")";
 
   CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
-  if (InitStyle) {
-    if (InitStyle == CXXNewExpr::CallInit)
+  if (InitStyle != CXXNewExpr::NoInit) {
+    bool Bare = InitStyle == CXXNewExpr::CallInit &&
+                !isa<ParenListExpr>(E->getInitializer());
+    if (Bare)
       OS << "(";
     PrintExpr(E->getInitializer());
-    if (InitStyle == CXXNewExpr::CallInit)
+    if (Bare)
       OS << ")";
   }
 }
@@ -2219,19 +2221,19 @@ void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
   PrintExpr(E->getSubExpr());
 }
 
-void
-StmtPrinter::VisitCXXUnresolvedConstructExpr(
-                                           CXXUnresolvedConstructExpr *Node) {
+void StmtPrinter::VisitCXXUnresolvedConstructExpr(
+    CXXUnresolvedConstructExpr *Node) {
   Node->getTypeAsWritten().print(OS, Policy);
-  OS << "(";
-  for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
-                                             ArgEnd = Node->arg_end();
-       Arg != ArgEnd; ++Arg) {
+  if (!Node->isListInitialization())
+    OS << '(';
+  for (auto Arg = Node->arg_begin(), ArgEnd = Node->arg_end(); Arg != ArgEnd;
+       ++Arg) {
     if (Arg != Node->arg_begin())
       OS << ", ";
     PrintExpr(*Arg);
   }
-  OS << ")";
+  if (!Node->isListInitialization())
+    OS << ')';
 }
 
 void StmtPrinter::VisitCXXDependentScopeMemberExpr(

diff  --git a/clang/test/CXX/expr/expr.prim/expr.prim.req/simple-requirement.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.req/simple-requirement.cpp
index 39e882b8fa5f9..4e37a195398e8 100644
--- a/clang/test/CXX/expr/expr.prim/expr.prim.req/simple-requirement.cpp
+++ b/clang/test/CXX/expr/expr.prim/expr.prim.req/simple-requirement.cpp
@@ -72,7 +72,7 @@ struct E {
 };
 
 template<typename T> requires requires(T t) { typename E<T>::non_default_constructible{}; }
-// expected-note at -1 {{because 'typename E<T>::non_default_constructible({})' would be invalid: no matching constructor for initialization of 'typename E<int>::non_default_constructible'}}
+// expected-note at -1 {{because 'typename E<T>::non_default_constructible{}' would be invalid: no matching constructor for initialization of 'typename E<int>::non_default_constructible'}}
 struct r6 {};
 
 using r6i1 = r6<int>;

diff  --git a/clang/test/SemaCXX/cxx2b-ast-print.cpp b/clang/test/SemaCXX/cxx2b-ast-print.cpp
index 4aa1b23280c39..58eb785a00771 100644
--- a/clang/test/SemaCXX/cxx2b-ast-print.cpp
+++ b/clang/test/SemaCXX/cxx2b-ast-print.cpp
@@ -1,5 +1,6 @@
 // RUN: %clang_cc1 -std=c++2b -fsyntax-only -ast-print %s | FileCheck %s
 
+template <template <class...> class C>
 void test_auto_expr(long long y, auto &&z) {
   int x[] = {3, 4};
 
@@ -15,16 +16,36 @@ void test_auto_expr(long long y, auto &&z) {
 
   // CHECK{LITERAL}: auto(z)
   void(auto(z));
-  // CHECK{LITERAL}: auto({z})
-  void(auto{z}); // T({z}) is legal unless T = auto
+  // CHECK{LITERAL}: auto{z}
+  void(auto{z});
 
   // CHECK{LITERAL}: new int *(x)
   void(new auto(x));
   // CHECK{LITERAL}: new int *{x}
   void(new auto{x});
 
+  // CHECK{LITERAL}: new auto(z)
+  void(new auto(z));
+  // CHECK{LITERAL}: new auto{z}
+  void(new auto{z});
+
   // CHECK{LITERAL}: new long long(y)
   void(new decltype(auto)(y));
   // CHECK{LITERAL}: new long long{y}
   void(new decltype(auto){y});
+
+  // CHECK{LITERAL}: new decltype(auto)(z)
+  void(new decltype(auto)(z));
+  // CHECK{LITERAL}: new decltype(auto){z}
+  void(new decltype(auto){z});
+
+  // CHECK{LITERAL}: C(x, y, z)
+  void(C(x, y, z));
+  // CHECK{LITERAL}: C{x, y, z}
+  void(C{x, y, z});
+
+  // CHECK{LITERAL}: new C(x, y, z)
+  void(new C(x, y, z));
+  // CHECK{LITERAL}: new C{x, y, z}
+  void(new C{x, y, z});
 }


        


More information about the cfe-commits mailing list