[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
This commit is contained in:
Zhihao Yuan 2022-02-28 19:29:54 -06:00 committed by Zhihao Yuan
parent 136b293129
commit d1a59eefd3
No known key found for this signature in database
GPG Key ID: A2E474BDAA37E11C
3 changed files with 37 additions and 14 deletions

View File

@ -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(

View File

@ -72,7 +72,7 @@ struct E {
};
template<typename T> requires requires(T t) { typename E<T>::non_default_constructible{}; }
// expected-note@-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@-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>;

View File

@ -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});
}