Skip to content

[clang] Do not diagnose unused deleted operator delete[] #134357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/include/clang/AST/DeclCXX.h
Original file line number Diff line number Diff line change
Expand Up @@ -2878,7 +2878,7 @@ class CXXDestructorDecl : public CXXMethodDecl {
static CXXDestructorDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);

void setOperatorDelete(FunctionDecl *OD, Expr *ThisArg);
void setOperatorArrayDelete(FunctionDecl *OD, Expr *ThisArg);
void setOperatorArrayDelete(FunctionDecl *OD);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like unrelated changes, but the changes themselves are correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a drive-by removal of unused argument added by a patch that caused the bug this patch is fixing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I do that in a separate commit?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think it's related enough as a drive-by it's fine.


const FunctionDecl *getOperatorDelete() const {
return getCanonicalDecl()->OperatorDelete;
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -8336,7 +8336,8 @@ class Sema final : public SemaBase {
DeclarationName Name);
FunctionDecl *FindDeallocationFunctionForDestructor(SourceLocation StartLoc,
CXXRecordDecl *RD,
DeclarationName Name);
DeclarationName Name,
bool Diagnose = true);

/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
/// @code ::delete ptr; @endcode
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/AST/DeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3031,8 +3031,7 @@ void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
}
}

void CXXDestructorDecl::setOperatorArrayDelete(FunctionDecl *OD,
Expr *ThisArg) {
void CXXDestructorDecl::setOperatorArrayDelete(FunctionDecl *OD) {
auto *First = cast<CXXDestructorDecl>(getFirstDecl());
if (OD && !First->OperatorArrayDelete)
First->OperatorArrayDelete = OD;
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11048,12 +11048,12 @@ bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) {
// Lookup delete[] too in case we have to emit a vector deleting dtor;
DeclarationName VDeleteName =
Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete);
FunctionDecl *ArrOperatorDelete =
FindDeallocationFunctionForDestructor(Loc, RD, VDeleteName);
FunctionDecl *ArrOperatorDelete = FindDeallocationFunctionForDestructor(
Loc, RD, VDeleteName, /*Diagnose=*/false);
// delete[] in the TU will make sure the operator is referenced and its
// uses diagnosed, otherwise vector deleting dtor won't be called anyway,
// so just record it in the destructor.
Destructor->setOperatorArrayDelete(ArrOperatorDelete, ThisArg);
Destructor->setOperatorArrayDelete(ArrOperatorDelete);
}
}

Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3265,11 +3265,13 @@ FunctionDecl *Sema::FindUsualDeallocationFunction(SourceLocation StartLoc,
return Result.FD;
}

FunctionDecl *Sema::FindDeallocationFunctionForDestructor(
SourceLocation Loc, CXXRecordDecl *RD, DeclarationName Name) {
FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc,
CXXRecordDecl *RD,
DeclarationName Name,
bool Diagnose) {

FunctionDecl *OperatorDelete = nullptr;
if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete))
if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete, Diagnose))
return nullptr;
if (OperatorDelete)
return OperatorDelete;
Expand Down
22 changes: 22 additions & 0 deletions clang/test/SemaCXX/gh134265.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_cc1 %s -verify -fsyntax-only

struct Foo {
virtual ~Foo() {} // expected-error {{attempt to use a deleted function}}
static void operator delete(void* ptr) = delete; // expected-note {{explicitly marked deleted here}}
};


struct Bar {
virtual ~Bar() {}
static void operator delete[](void* ptr) = delete;
};

struct Baz {
virtual ~Baz() {}
static void operator delete[](void* ptr) = delete; // expected-note {{explicitly marked deleted here}}
};

void foobar() {
Baz *B = new Baz[10]();
delete [] B; // expected-error {{attempt to use a deleted function}}
}