Skip to content

Commit 0e4c5cc

Browse files
committed
[clang][DeclPrinter] Fix AST print of curly constructor initializers
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
1 parent 27459a3 commit 0e4c5cc

File tree

2 files changed

+71
-26
lines changed

2 files changed

+71
-26
lines changed

clang/lib/AST/DeclPrinter.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,12 @@ void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
327327
Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
328328
}
329329

330-
Out << "(";
331-
if (!BMInitializer->getInit()) {
332-
// Nothing to print
333-
} else {
334-
Expr *Init = BMInitializer->getInit();
330+
if (Expr *Init = BMInitializer->getInit()) {
331+
bool OutParens = !isa<InitListExpr>(Init);
332+
333+
if (OutParens)
334+
Out << "(";
335+
335336
if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
336337
Init = Tmp->getSubExpr();
337338

@@ -365,8 +366,13 @@ void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
365366
&Context);
366367
}
367368
}
369+
370+
if (OutParens)
371+
Out << ")";
372+
} else {
373+
Out << "()";
368374
}
369-
Out << ")";
375+
370376
if (BMInitializer->isPackExpansion())
371377
Out << "...";
372378
}

clang/test/AST/ast-print-method-decl.cpp

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,85 @@
11
// RUN: %clang_cc1 -ast-print %s -o - -std=c++20 | FileCheck %s
22

3-
// CHECK: struct A {
4-
struct A {
5-
// CHECK-NEXT: A();
6-
A();
3+
// CHECK: struct DelegatingCtor1 {
4+
struct DelegatingCtor1 {
5+
// CHECK-NEXT: DelegatingCtor1();
6+
DelegatingCtor1();
77

8-
// CHECK-NEXT: A(int) : A() {
9-
A(int) : A() {
8+
// CHECK-NEXT: DelegatingCtor1(int) : DelegatingCtor1() {
9+
DelegatingCtor1(int) : DelegatingCtor1() {
1010
// CHECK-NEXT: }
1111
}
1212

1313
// CHECK-NEXT: };
1414
};
1515

1616

17-
// CHECK: struct B {
18-
struct B {
19-
// CHECK-NEXT: template <typename Ty> B(Ty);
20-
template <typename Ty> B(Ty);
17+
// CHECK: struct DelegatingCtor2 {
18+
struct DelegatingCtor2 {
19+
// CHECK-NEXT: template <typename Ty> DelegatingCtor2(Ty);
20+
template <typename Ty> DelegatingCtor2(Ty);
2121

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

25-
// CHECK-NEXT: B(int X) : B((float)X) {
26-
B(int X) : B((float)X) {
25+
// CHECK-NEXT: DelegatingCtor2(int X) : DelegatingCtor2((float)X) {
26+
DelegatingCtor2(int X) : DelegatingCtor2((float)X) {
2727
// CHECK-NEXT: }
2828
}
2929

3030
// CHECK-NEXT: };
3131
};
3232

33-
// CHECK: struct C {
34-
struct C {
33+
// CHECK: struct DelegatingCtor3 {
34+
struct DelegatingCtor3 {
3535
// FIXME: template <> should not be output
36-
// CHECK: template <> C(auto);
37-
C(auto);
36+
// CHECK: template <> DelegatingCtor3(auto);
37+
DelegatingCtor3(auto);
3838

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

42-
// CHECK: C(int) : C("") {
43-
C(int) : C("") {
42+
// CHECK: DelegatingCtor3(int) : DelegatingCtor3("") {
43+
DelegatingCtor3(int) : DelegatingCtor3("") {
44+
// CHECK-NEXT: }
45+
}
46+
47+
// CHECK-NEXT: };
48+
};
49+
50+
// CHECK: struct CurlyCtorInit {
51+
struct CurlyCtorInit {
52+
// CHECK-NEXT: struct A {
53+
struct A {
54+
// CHECK-NEXT: int x;
55+
int x;
56+
// CHECK-NEXT: };
57+
};
58+
59+
// CHECK-NEXT: A a;
60+
A a;
61+
// CHECK-NEXT: int i;
62+
int i;
63+
64+
// FIXME: /*implicit*/(int)0 should not be output
65+
// CHECK-NEXT: CurlyCtorInit(int *) : a(), i(/*implicit*/(int)0) {
66+
CurlyCtorInit(int *) : a(), i() {
67+
// CHECK-NEXT: }
68+
}
69+
70+
// CHECK-NEXT: CurlyCtorInit(int **) : a{}, i{} {
71+
CurlyCtorInit(int **) : a{}, i{} {
72+
// CHECK-NEXT: }
73+
}
74+
75+
// CHECK-NEXT: CurlyCtorInit(int ***) : a({}), i(0) {
76+
CurlyCtorInit(int ***) : a({}), i(0) {
77+
// CHECK-NEXT: }
78+
}
79+
80+
// FIXME: Implicit this should not be output
81+
// CHECK-NEXT: CurlyCtorInit(int ****) : a({.x = 0}), i(this->a.x) {
82+
CurlyCtorInit(int ****) : a({.x = 0}), i(a.x) {
4483
// CHECK-NEXT: }
4584
}
4685

0 commit comments

Comments
 (0)