Skip to content

Commit 807001e

Browse files
FznamznonKornevNikita
authored andcommitted
[clang] Do not diagnose unused deleted operator delete[] (#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/llvm-project#134265
1 parent 47ae65e commit 807001e

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

clang/include/clang/AST/DeclCXX.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2886,7 +2886,7 @@ class CXXDestructorDecl : public CXXMethodDecl {
28862886
static CXXDestructorDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
28872887

28882888
void setOperatorDelete(FunctionDecl *OD, Expr *ThisArg);
2889-
void setOperatorArrayDelete(FunctionDecl *OD, Expr *ThisArg);
2889+
void setOperatorArrayDelete(FunctionDecl *OD);
28902890

28912891
const FunctionDecl *getOperatorDelete() const {
28922892
return getCanonicalDecl()->OperatorDelete;

clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8254,7 +8254,8 @@ class Sema final : public SemaBase {
82548254
DeclarationName Name);
82558255
FunctionDecl *FindDeallocationFunctionForDestructor(SourceLocation StartLoc,
82568256
CXXRecordDecl *RD,
8257-
DeclarationName Name);
8257+
DeclarationName Name,
8258+
bool Diagnose = true);
82588259

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

clang/lib/AST/DeclCXX.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,8 +3014,7 @@ void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
30143014
}
30153015
}
30163016

3017-
void CXXDestructorDecl::setOperatorArrayDelete(FunctionDecl *OD,
3018-
Expr *ThisArg) {
3017+
void CXXDestructorDecl::setOperatorArrayDelete(FunctionDecl *OD) {
30193018
auto *First = cast<CXXDestructorDecl>(getFirstDecl());
30203019
if (OD && !First->OperatorArrayDelete)
30213020
First->OperatorArrayDelete = OD;

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11015,12 +11015,12 @@ bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) {
1101511015
// Lookup delete[] too in case we have to emit a vector deleting dtor;
1101611016
DeclarationName VDeleteName =
1101711017
Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete);
11018-
FunctionDecl *ArrOperatorDelete =
11019-
FindDeallocationFunctionForDestructor(Loc, RD, VDeleteName);
11018+
FunctionDecl *ArrOperatorDelete = FindDeallocationFunctionForDestructor(
11019+
Loc, RD, VDeleteName, /*Diagnose=*/false);
1102011020
// delete[] in the TU will make sure the operator is referenced and its
1102111021
// uses diagnosed, otherwise vector deleting dtor won't be called anyway,
1102211022
// so just record it in the destructor.
11023-
Destructor->setOperatorArrayDelete(ArrOperatorDelete, ThisArg);
11023+
Destructor->setOperatorArrayDelete(ArrOperatorDelete);
1102411024
}
1102511025
}
1102611026

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3295,11 +3295,13 @@ FunctionDecl *Sema::FindUsualDeallocationFunction(SourceLocation StartLoc,
32953295
return Result.FD;
32963296
}
32973297

3298-
FunctionDecl *Sema::FindDeallocationFunctionForDestructor(
3299-
SourceLocation Loc, CXXRecordDecl *RD, DeclarationName Name) {
3298+
FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc,
3299+
CXXRecordDecl *RD,
3300+
DeclarationName Name,
3301+
bool Diagnose) {
33003302

33013303
FunctionDecl *OperatorDelete = nullptr;
3302-
if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete))
3304+
if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete, Diagnose))
33033305
return nullptr;
33043306
if (OperatorDelete)
33053307
return OperatorDelete;

clang/test/SemaCXX/gh134265.cpp

Lines changed: 22 additions & 0 deletions
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)