[clang] [RFC] Initial implementation of P2719 (PR #113510)
Oliver Hunt via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 30 13:53:36 PDT 2025
================
@@ -7353,6 +7354,69 @@ void Sema::CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record) {
else if (Record->hasAttr<CUDADeviceBuiltinTextureTypeAttr>())
checkCUDADeviceBuiltinTextureClassTemplate(*this, Record);
}
+
+ llvm::SmallVector<const FunctionDecl *, 2> TypeAwareNewDecls;
+ llvm::SmallVector<const FunctionDecl *, 2> TypeAwareDeleteDecls;
+ llvm::SmallVector<const FunctionDecl *, 2> TypeAwareArrayNewDecls;
+ llvm::SmallVector<const FunctionDecl *, 2> TypeAwareArrayDeleteDecls;
+
+ for (auto *D : Record->decls()) {
+ const FunctionDecl *FnDecl = nullptr;
+ if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
+ FnDecl = FTD->getTemplatedDecl();
+ else if (auto *FD = dyn_cast<FunctionDecl>(D))
+ FnDecl = FD;
+ if (!FnDecl || !FnDecl->isTypeAwareOperatorNewOrDelete())
+ continue;
+ switch (FnDecl->getOverloadedOperator()) {
+ case OO_New:
+ TypeAwareNewDecls.push_back(FnDecl);
+ break;
+ case OO_Array_New:
+ TypeAwareArrayNewDecls.push_back(FnDecl);
+ break;
+ case OO_Delete:
+ TypeAwareDeleteDecls.push_back(FnDecl);
+ break;
+ case OO_Array_Delete:
+ TypeAwareArrayDeleteDecls.push_back(FnDecl);
+ break;
+ default:
+ continue;
+ }
+ }
+ auto CheckMismatchedTypeAwareAllocators =
+ [this,
+ Record](OverloadedOperatorKind NewKind,
+ const llvm::SmallVector<const FunctionDecl *, 2> &NewDecls,
+ OverloadedOperatorKind DeleteKind,
+ const llvm::SmallVector<const FunctionDecl *, 2> &DeleteDecls) {
+ if (NewDecls.empty() == DeleteDecls.empty())
+ return;
+ DeclarationName FoundOperator =
+ Context.DeclarationNames.getCXXOperatorName(
+ NewDecls.empty() ? DeleteKind : NewKind);
+ DeclarationName MissingOperator =
+ Context.DeclarationNames.getCXXOperatorName(
+ NewDecls.empty() ? NewKind : DeleteKind);
+ Diag(Record->getLocation(),
+ diag::err_type_aware_allocator_missing_matching_operator)
+ << FoundOperator << Context.getRecordType(Record)
+ << MissingOperator;
----------------
ojhunt wrote:
@mizvekov it seemed reasonable from a QoL point of view, but let me see what happens without it
https://github.com/llvm/llvm-project/pull/113510
More information about the cfe-commits
mailing list