Skip to content

Commit 16a1d5d

Browse files
authored
[clang] Do not diagnose unused deleted operator delete[] (llvm#134357)
For vector deleting dtors support we now also search and save operator delete[]. Avoid diagnosing deleted operator delete[] when doing that because vector deleting dtors are only called when delete[] is present and whenever delete[] is present in the TU it will be diagnosed correctly. Fixes llvm#134265
1 parent da69eb7 commit 16a1d5d

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

clang/include/clang/AST/DeclCXX.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2878,7 +2878,7 @@ class CXXDestructorDecl : public CXXMethodDecl {
28782878
static CXXDestructorDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
28792879

28802880
void setOperatorDelete(FunctionDecl *OD, Expr *ThisArg);
2881-
void setOperatorArrayDelete(FunctionDecl *OD, Expr *ThisArg);
2881+
void setOperatorArrayDelete(FunctionDecl *OD);
28822882

28832883
const FunctionDecl *getOperatorDelete() const {
28842884
return getCanonicalDecl()->OperatorDelete;

clang/include/clang/Sema/Sema.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -8336,7 +8336,8 @@ class Sema final : public SemaBase {
83368336
DeclarationName Name);
83378337
FunctionDecl *FindDeallocationFunctionForDestructor(SourceLocation StartLoc,
83388338
CXXRecordDecl *RD,
8339-
DeclarationName Name);
8339+
DeclarationName Name,
8340+
bool Diagnose = true);
83408341

83418342
/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
83428343
/// @code ::delete ptr; @endcode

clang/lib/AST/DeclCXX.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -3031,8 +3031,7 @@ void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
30313031
}
30323032
}
30333033

3034-
void CXXDestructorDecl::setOperatorArrayDelete(FunctionDecl *OD,
3035-
Expr *ThisArg) {
3034+
void CXXDestructorDecl::setOperatorArrayDelete(FunctionDecl *OD) {
30363035
auto *First = cast<CXXDestructorDecl>(getFirstDecl());
30373036
if (OD && !First->OperatorArrayDelete)
30383037
First->OperatorArrayDelete = OD;

clang/lib/Sema/SemaDeclCXX.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -11048,12 +11048,12 @@ bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) {
1104811048
// Lookup delete[] too in case we have to emit a vector deleting dtor;
1104911049
DeclarationName VDeleteName =
1105011050
Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete);
11051-
FunctionDecl *ArrOperatorDelete =
11052-
FindDeallocationFunctionForDestructor(Loc, RD, VDeleteName);
11051+
FunctionDecl *ArrOperatorDelete = FindDeallocationFunctionForDestructor(
11052+
Loc, RD, VDeleteName, /*Diagnose=*/false);
1105311053
// delete[] in the TU will make sure the operator is referenced and its
1105411054
// uses diagnosed, otherwise vector deleting dtor won't be called anyway,
1105511055
// so just record it in the destructor.
11056-
Destructor->setOperatorArrayDelete(ArrOperatorDelete, ThisArg);
11056+
Destructor->setOperatorArrayDelete(ArrOperatorDelete);
1105711057
}
1105811058
}
1105911059

clang/lib/Sema/SemaExprCXX.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -3265,11 +3265,13 @@ FunctionDecl *Sema::FindUsualDeallocationFunction(SourceLocation StartLoc,
32653265
return Result.FD;
32663266
}
32673267

3268-
FunctionDecl *Sema::FindDeallocationFunctionForDestructor(
3269-
SourceLocation Loc, CXXRecordDecl *RD, DeclarationName Name) {
3268+
FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc,
3269+
CXXRecordDecl *RD,
3270+
DeclarationName Name,
3271+
bool Diagnose) {
32703272

32713273
FunctionDecl *OperatorDelete = nullptr;
3272-
if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete))
3274+
if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete, Diagnose))
32733275
return nullptr;
32743276
if (OperatorDelete)
32753277
return OperatorDelete;

clang/test/SemaCXX/gh134265.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 %s -verify -fsyntax-only
2+
3+
struct Foo {
4+
virtual ~Foo() {} // expected-error {{attempt to use a deleted function}}
5+
static void operator delete(void* ptr) = delete; // expected-note {{explicitly marked deleted here}}
6+
};
7+
8+
9+
struct Bar {
10+
virtual ~Bar() {}
11+
static void operator delete[](void* ptr) = delete;
12+
};
13+
14+
struct Baz {
15+
virtual ~Baz() {}
16+
static void operator delete[](void* ptr) = delete; // expected-note {{explicitly marked deleted here}}
17+
};
18+
19+
void foobar() {
20+
Baz *B = new Baz[10]();
21+
delete [] B; // expected-error {{attempt to use a deleted function}}
22+
}

0 commit comments

Comments
 (0)