Skip to content

Commit b2c656a

Browse files
committed
[Clang] disallow the use of asterisks preceding constructor and destructor names
1 parent eabf931 commit b2c656a

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ Improvements to Clang's diagnostics
764764
scope.Unlock();
765765
require(scope); // Warning! Requires mu1.
766766
}
767+
- Clang now disallows the use of asterisks preceding constructor and destructor names (#GH121706).
767768

768769
Improvements to Clang's time-trace
769770
----------------------------------

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,8 @@ def err_invalid_qualified_constructor : Error<
22022202
"'%0' qualifier is not allowed on a constructor">;
22032203
def err_ref_qualifier_constructor : Error<
22042204
"ref-qualifier '%select{&&|&}0' is not allowed on a constructor">;
2205+
def err_invalid_constructor_decl : Error<
2206+
"invalid constructor declaration">;
22052207

22062208
def err_constructor_return_type : Error<
22072209
"constructor cannot have a return type">;
@@ -2223,6 +2225,8 @@ def err_destructor_not_member : Error<
22232225
def err_destructor_cannot_be : Error<"destructor cannot be declared '%0'">;
22242226
def err_invalid_qualified_destructor : Error<
22252227
"'%0' qualifier is not allowed on a destructor">;
2228+
def err_invalid_destructor_decl : Error<
2229+
"invalid destructor declaration">;
22262230
def err_ref_qualifier_destructor : Error<
22272231
"ref-qualifier '%select{&&|&}0' is not allowed on a destructor">;
22282232
def err_destructor_return_type : Error<"destructor cannot have a return type">;

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10757,6 +10757,17 @@ static void checkMethodTypeQualifiers(Sema &S, Declarator &D, unsigned DiagID) {
1075710757
}
1075810758
}
1075910759

10760+
static void checkMethodPointerType(Sema &S, Declarator &D, unsigned DiagID) {
10761+
if (D.getNumTypeObjects() > 0) {
10762+
DeclaratorChunk &Chunk = D.getTypeObject(D.getNumTypeObjects() - 1);
10763+
if (Chunk.Kind == DeclaratorChunk::Pointer) {
10764+
SourceLocation PointerLoc = Chunk.getSourceRange().getBegin();
10765+
S.Diag(PointerLoc, DiagID) << Chunk.getSourceRange();
10766+
D.setInvalidType();
10767+
}
10768+
}
10769+
}
10770+
1076010771
QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R,
1076110772
StorageClass &SC) {
1076210773
bool isVirtual = D.getDeclSpec().isVirtualSpecified();
@@ -10792,6 +10803,7 @@ QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R,
1079210803
}
1079310804

1079410805
checkMethodTypeQualifiers(*this, D, diag::err_invalid_qualified_constructor);
10806+
checkMethodPointerType(*this, D, diag::err_invalid_constructor_decl);
1079510807

1079610808
// C++0x [class.ctor]p4:
1079710809
// A constructor shall not be declared with a ref-qualifier.
@@ -10958,6 +10970,7 @@ QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R,
1095810970
}
1095910971

1096010972
checkMethodTypeQualifiers(*this, D, diag::err_invalid_qualified_destructor);
10973+
checkMethodPointerType(*this, D, diag::err_invalid_destructor_decl);
1096110974

1096210975
// C++0x [class.dtor]p2:
1096310976
// A destructor shall not be declared with a ref-qualifier.

clang/test/SemaCXX/constructor.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,13 @@ namespace PR38286 {
9696
template<typename> struct C; // expected-note {{non-type declaration found}}
9797
template<typename T> C<T>::~C() {} // expected-error {{identifier 'C' after '~' in destructor name does not name a type}}
9898
}
99+
100+
namespace GH121706 {
101+
struct S {
102+
*S(); // expected-error {{invalid constructor declaration}}
103+
**S(); // expected-error {{invalid constructor declaration}}
104+
105+
**S(const S &); // expected-error {{invalid constructor declaration}}
106+
*S(S &&); // expected-error {{invalid constructor declaration}}
107+
};
108+
}

clang/test/SemaCXX/destructor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,4 +586,11 @@ struct Y : X {} y1{ }; // expected-error {{call to implicitly-deleted default co
586586
// expected-note {{default constructor of 'Y' is implicitly deleted because base class 'X' has no destructor}}
587587
}
588588

589+
namespace GH121706 {
590+
struct S {
591+
*~S(); // expected-error {{invalid destructor declaration}}
592+
**~S(); // expected-error {{invalid destructor declaration}}
593+
};
594+
}
595+
589596
#endif // BE_THE_HEADER

0 commit comments

Comments
 (0)