Skip to content

[clang] Ensure type aware allocators handle transparent decl contexts #138616

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
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
21 changes: 13 additions & 8 deletions clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3070,18 +3070,24 @@ bool Sema::FindAllocationFunctions(
Filter.done();
}

auto GetRedeclContext = [](Decl *D) {
return D->getDeclContext()->getRedeclContext();
};

DeclContext *OperatorNewContext = GetRedeclContext(OperatorNew);

bool FoundGlobalDelete = FoundDelete.empty();
bool IsClassScopedTypeAwareNew =
isTypeAwareAllocation(IAP.PassTypeIdentity) &&
OperatorNew->getDeclContext()->isRecord();
OperatorNewContext->isRecord();
auto DiagnoseMissingTypeAwareCleanupOperator = [&](bool IsPlacementOperator) {
assert(isTypeAwareAllocation(IAP.PassTypeIdentity));
if (Diagnose) {
Diag(StartLoc, diag::err_mismatching_type_aware_cleanup_deallocator)
<< OperatorNew->getDeclName() << IsPlacementOperator << DeleteName;
Diag(OperatorNew->getLocation(), diag::note_type_aware_operator_declared)
<< OperatorNew->isTypeAwareOperatorNewOrDelete()
<< OperatorNew->getDeclName() << OperatorNew->getDeclContext();
<< OperatorNew->getDeclName() << OperatorNewContext;
}
};
if (IsClassScopedTypeAwareNew && FoundDelete.empty()) {
Expand Down Expand Up @@ -3224,15 +3230,15 @@ bool Sema::FindAllocationFunctions(
// deallocation function will be called.
if (Matches.size() == 1) {
OperatorDelete = Matches[0].second;
DeclContext *OperatorDeleteContext = GetRedeclContext(OperatorDelete);
bool FoundTypeAwareOperator =
OperatorDelete->isTypeAwareOperatorNewOrDelete() ||
OperatorNew->isTypeAwareOperatorNewOrDelete();
if (Diagnose && FoundTypeAwareOperator) {
bool MismatchedTypeAwareness =
OperatorDelete->isTypeAwareOperatorNewOrDelete() !=
OperatorNew->isTypeAwareOperatorNewOrDelete();
bool MismatchedContext =
OperatorDelete->getDeclContext() != OperatorNew->getDeclContext();
bool MismatchedContext = OperatorDeleteContext != OperatorNewContext;
if (MismatchedTypeAwareness || MismatchedContext) {
FunctionDecl *Operators[] = {OperatorDelete, OperatorNew};
bool TypeAwareOperatorIndex =
Expand All @@ -3241,16 +3247,15 @@ bool Sema::FindAllocationFunctions(
<< Operators[TypeAwareOperatorIndex]->getDeclName()
<< isPlacementNew
<< Operators[!TypeAwareOperatorIndex]->getDeclName()
<< Operators[TypeAwareOperatorIndex]->getDeclContext();
<< GetRedeclContext(Operators[TypeAwareOperatorIndex]);
Diag(OperatorNew->getLocation(),
diag::note_type_aware_operator_declared)
<< OperatorNew->isTypeAwareOperatorNewOrDelete()
<< OperatorNew->getDeclName() << OperatorNew->getDeclContext();
<< OperatorNew->getDeclName() << OperatorNewContext;
Diag(OperatorDelete->getLocation(),
diag::note_type_aware_operator_declared)
<< OperatorDelete->isTypeAwareOperatorNewOrDelete()
<< OperatorDelete->getDeclName()
<< OperatorDelete->getDeclContext();
<< OperatorDelete->getDeclName() << OperatorDeleteContext;
}
}

Expand Down
43 changes: 43 additions & 0 deletions clang/test/SemaCXX/type-aware-new-delete-transparent-contexts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -fexceptions -DTRANSPARENT_DECL=0
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -fexceptions -DTRANSPARENT_DECL=1
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -fexceptions -DTRANSPARENT_DECL=2

// expected-no-diagnostics
#if TRANSPARENT_DECL==2
export module Testing;
#endif

namespace std {
template <class T> struct type_identity {};
using size_t = __SIZE_TYPE__;
enum class align_val_t : size_t {};
struct destroying_delete_t { explicit destroying_delete_t() = default; };
}

#if TRANSPARENT_DECL==0
#define BEGIN_TRANSPARENT_DECL extern "C" {
#define END_TRANSPARENT_DECL }
#elif TRANSPARENT_DECL==1
#define BEGIN_TRANSPARENT_DECL extern "C++" {
#define END_TRANSPARENT_DECL }
#elif TRANSPARENT_DECL==2
#define BEGIN_TRANSPARENT_DECL export {
#define END_TRANSPARENT_DECL }
#else
#error unexpected decl kind
#endif

BEGIN_TRANSPARENT_DECL
void *operator new(std::type_identity<int>, std::size_t, std::align_val_t);
void operator delete[](std::type_identity<int>, void*, std::size_t, std::align_val_t);
END_TRANSPARENT_DECL

void *operator new[](std::type_identity<int>, std::size_t, std::align_val_t);
void operator delete(std::type_identity<int>, void*, std::size_t, std::align_val_t);

void foo() {
int *iptr = new int;
delete iptr;
int *iarray = new int[5];
delete [] iarray;
}
Loading