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

via cfe-commits cfe-commits at lists.llvm.org
Mon May 5 16:44:32 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Oliver Hunt (ojhunt)

<details>
<summary>Changes</summary>

We were testing the immediate DeclContext for found new and delete operators, which is incorrect if the declarations are contained by a transparent decl as can be induced with extern or export statements.

---
Full diff: https://github.com/llvm/llvm-project/pull/138616.diff


1 Files Affected:

- (modified) clang/lib/Sema/SemaExprCXX.cpp (+13-8) 


``````````diff
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 235ea1529b0b8..5bfd608afba04 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -3070,10 +3070,16 @@ 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) {
@@ -3081,7 +3087,7 @@ bool Sema::FindAllocationFunctions(
           << 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()) {
@@ -3224,6 +3230,7 @@ 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();
@@ -3231,8 +3238,7 @@ bool Sema::FindAllocationFunctions(
       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 =
@@ -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;
       }
     }
 

``````````

</details>


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


More information about the cfe-commits mailing list