Skip to content

Commit

Permalink
[clang][DeclPrinter] Fix AST print of curly constructor initializers
Browse files Browse the repository at this point in the history
DeclPrinter::PrintConstructorInitializers did not consider curly constructor initializers. Any curly constructor initializers (e.g. `A() : Field{}`) was printed with round brackets (e.g. `A() : Field({})`).

#64061

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D156307
  • Loading branch information
strimo378 committed Jul 27, 2023
1 parent 27459a3 commit 0e4c5cc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 26 deletions.
18 changes: 12 additions & 6 deletions clang/lib/AST/DeclPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,12 @@ void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
}

Out << "(";
if (!BMInitializer->getInit()) {
// Nothing to print
} else {
Expr *Init = BMInitializer->getInit();
if (Expr *Init = BMInitializer->getInit()) {
bool OutParens = !isa<InitListExpr>(Init);

if (OutParens)
Out << "(";

if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
Init = Tmp->getSubExpr();

Expand Down Expand Up @@ -365,8 +366,13 @@ void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
&Context);
}
}

if (OutParens)
Out << ")";
} else {
Out << "()";
}
Out << ")";

if (BMInitializer->isPackExpansion())
Out << "...";
}
Expand Down
79 changes: 59 additions & 20 deletions clang/test/AST/ast-print-method-decl.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,85 @@
// RUN: %clang_cc1 -ast-print %s -o - -std=c++20 | FileCheck %s

// CHECK: struct A {
struct A {
// CHECK-NEXT: A();
A();
// CHECK: struct DelegatingCtor1 {
struct DelegatingCtor1 {
// CHECK-NEXT: DelegatingCtor1();
DelegatingCtor1();

// CHECK-NEXT: A(int) : A() {
A(int) : A() {
// CHECK-NEXT: DelegatingCtor1(int) : DelegatingCtor1() {
DelegatingCtor1(int) : DelegatingCtor1() {
// CHECK-NEXT: }
}

// CHECK-NEXT: };
};


// CHECK: struct B {
struct B {
// CHECK-NEXT: template <typename Ty> B(Ty);
template <typename Ty> B(Ty);
// CHECK: struct DelegatingCtor2 {
struct DelegatingCtor2 {
// CHECK-NEXT: template <typename Ty> DelegatingCtor2(Ty);
template <typename Ty> DelegatingCtor2(Ty);

// FIXME: Implicitly specialized method should not be output
// CHECK-NEXT: template<> B<float>(float);
// CHECK-NEXT: template<> DelegatingCtor2<float>(float);

// CHECK-NEXT: B(int X) : B((float)X) {
B(int X) : B((float)X) {
// CHECK-NEXT: DelegatingCtor2(int X) : DelegatingCtor2((float)X) {
DelegatingCtor2(int X) : DelegatingCtor2((float)X) {
// CHECK-NEXT: }
}

// CHECK-NEXT: };
};

// CHECK: struct C {
struct C {
// CHECK: struct DelegatingCtor3 {
struct DelegatingCtor3 {
// FIXME: template <> should not be output
// CHECK: template <> C(auto);
C(auto);
// CHECK: template <> DelegatingCtor3(auto);
DelegatingCtor3(auto);

// FIXME: Implicitly specialized method should not be output
// CHECK: template<> C<const char *>(const char *);
// CHECK: template<> DelegatingCtor3<const char *>(const char *);

// CHECK: C(int) : C("") {
C(int) : C("") {
// CHECK: DelegatingCtor3(int) : DelegatingCtor3("") {
DelegatingCtor3(int) : DelegatingCtor3("") {
// CHECK-NEXT: }
}

// CHECK-NEXT: };
};

// CHECK: struct CurlyCtorInit {
struct CurlyCtorInit {
// CHECK-NEXT: struct A {
struct A {
// CHECK-NEXT: int x;
int x;
// CHECK-NEXT: };
};

// CHECK-NEXT: A a;
A a;
// CHECK-NEXT: int i;
int i;

// FIXME: /*implicit*/(int)0 should not be output
// CHECK-NEXT: CurlyCtorInit(int *) : a(), i(/*implicit*/(int)0) {
CurlyCtorInit(int *) : a(), i() {
// CHECK-NEXT: }
}

// CHECK-NEXT: CurlyCtorInit(int **) : a{}, i{} {
CurlyCtorInit(int **) : a{}, i{} {
// CHECK-NEXT: }
}

// CHECK-NEXT: CurlyCtorInit(int ***) : a({}), i(0) {
CurlyCtorInit(int ***) : a({}), i(0) {
// CHECK-NEXT: }
}

// FIXME: Implicit this should not be output
// CHECK-NEXT: CurlyCtorInit(int ****) : a({.x = 0}), i(this->a.x) {
CurlyCtorInit(int ****) : a({.x = 0}), i(a.x) {
// CHECK-NEXT: }
}

Expand Down

0 comments on commit 0e4c5cc

Please sign in to comment.