[clang] [RFC] Initial implementation of P2719 (PR #113510)

Oliver Hunt via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 8 15:04:41 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:

@cor3ntin pinging you here as GitHub refuses to show your prior comment inline, which I though was the entire reason I was not permitted to squash or rebase PRs :-/

You had asked to switch this to a set of maps instead, but I find that that makes the code harder to read, and the diagnostics harder to produce - the reason for this structure is so we can say "this is wrong because X is/is not type aware but has no corresponding T", and the map approach just made that more convoluted.

https://github.com/llvm/llvm-project/pull/113510


More information about the cfe-commits mailing list