Skip to content

Commit eaa78a3

Browse files
committed
[clang] Mark constructors with invalid initializers as invalid
If a member or base initializer of a constructor turns out to be invalid, mark the entire constructor as invalid.
1 parent 959905a commit eaa78a3

5 files changed

+17
-11
lines changed

clang/lib/Parse/ParseDeclCXX.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -4064,8 +4064,10 @@ void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
40644064
MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
40654065
if (!MemInit.isInvalid())
40664066
MemInitializers.push_back(MemInit.get());
4067-
else
4067+
else {
4068+
ConstructorDecl->setInvalidDecl();
40684069
AnyErrors = true;
4070+
}
40694071

40704072
if (Tok.is(tok::comma))
40714073
ConsumeToken();

clang/test/SemaCXX/class-base-member-init.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace test5 {
8686
decltype(Base(1))(2), // expected-error {{multiple initializations given for base 'decltype(Base(1))' (aka 'test5::Base')}}
8787
decltype(int())() { // expected-error {{constructor initializer 'decltype(int())' (aka 'int') does not name a class}}
8888
}
89-
A(float) : decltype(A())(3) {
89+
A(float) : decltype(A())(3) { // expected-error {{constructor for 'A' creates a delegation cycle}}
9090
}
9191
};
9292
}

clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,9 @@ namespace InvalidBaseClass {
535535
};
536536

537537
class InBetween : public F{};
538-
class E : public InBetween {
538+
class E : public InBetween { // expected-note 2{{candidate constructor}}
539539
public:
540540
constexpr E() : F{3} {} // expected-error {{not a direct or virtual base}}
541541
};
542-
static_assert(__builtin_bit_cast(char, E()) == 0); // expected-error {{not an integral constant expression}}
542+
static_assert(__builtin_bit_cast(char, E()) == 0); // expected-error {{no matching constructor}}
543543
}

clang/test/SemaCXX/constexpr-subobj-initialization.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ struct Bar : Foo {
1616
constexpr Bar bar; // expected-error {{must be initialized by a constant expression}}
1717

1818
struct Base {};
19-
struct A : Base { // expected-note-re {{constructor of base class '{{.*}}Base' is not called}}
19+
struct A : Base { // expected-note 2{{candidate constructor}}
2020
constexpr A() : value() {} // expected-error {{member initializer 'value' does not name a non-static data member or base class}}
2121
};
2222

23-
constexpr A a; // expected-error {{must be initialized by a constant expression}}
23+
constexpr A a; // expected-error {{no matching constructor}}
2424

25-
struct B : Base { // expected-note-re {{constructor of base class '{{.*}}Base' is not called}}
25+
struct B : Base { // expected-note 2{{candidate constructor}}
2626
constexpr B() : {} // expected-error {{expected class member or base class name}}
2727
};
2828

29-
constexpr B b; // expected-error {{must be initialized by a constant expression}}
29+
constexpr B b; // expected-error {{no matching constructor}}
3030
} // namespace baseclass_uninit
3131

3232

clang/test/SemaCXX/constructor-initializer.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
// RUN: %clang_cc1 -Wreorder -fsyntax-only -verify -std=c++98 %s
33
// RUN: %clang_cc1 -Wreorder -fsyntax-only -verify -std=c++11 %s
44

5-
class A {
5+
class A {
6+
// expected-note@-1 {{candidate constructor}}
7+
#if __cplusplus >= 201103L // C++11 or later
8+
// expected-note@-3 {{candidate constructor}}
9+
#endif
610
int m;
711
public:
812
A() : A::m(17) { } // expected-error {{member initializer 'm' does not name a non-static data member or base class}}
9-
A(int);
13+
A(int); // expected-note {{candidate constructor}}
1014
};
1115

1216
class B : public A {
1317
public:
14-
B() : A(), m(1), n(3.14) { }
18+
B() : A(), m(1), n(3.14) { } // expected-error {{no matching constructor for initialization of 'A'}}
1519

1620
private:
1721
int m;

0 commit comments

Comments
 (0)