https://gcc.gnu.org/g:440449a667190137ad3465a9c1f921567e5e2604

commit r17-6101-g440449a667190137ad3465a9c1f921567e5e2604
Author: Waffl3x <[email protected]>
Date:   Fri Oct 30 02:10:27 2026 -0600

    OpenMP/C++: Reject omp_{cgroup,pteam,thread}_mem_alloc for static vars in 
ALLOCATE directive [PR122892]
    
    Trivial fix to reject omp_cgroup_mem_alloc, omp_pteam_mem_alloc, and
    omp_thread_mem_alloc for variables of static lifetime.
    
    Also adjusts the related testcase as it incorrectly uses static on global
    variables, which instead means internal linkage in this context.
    
            PR c/122892
    
    gcc/cp/ChangeLog:
    
            * semantics.cc (finish_omp_allocate): Reject
            omp_{cgroup,pteam,thread}_mem_alloc for static vars.
    
    gcc/testsuite/ChangeLog:
    
            * c-c++-common/gomp/allocate-20.c: Remove sorry, add tests, remove
            static from globals.
    
    Signed-off-by: Waffl3x <[email protected]>

Diff:
---
 gcc/cp/semantics.cc                           | 13 ++++++++++++
 gcc/testsuite/c-c++-common/gomp/allocate-20.c | 30 +++++++++++++++++----------
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 77aa9556b772..481b7442176c 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12935,6 +12935,19 @@ finish_omp_allocate (const location_t loc, const tree 
var_list,
                  "expression evaluates to %qwu", tree_to_uhwi (ret));
          return error_mark_node;
        }
+      else if (alloc_value == GOMP_OMP_PREDEF_ALLOC_CGROUP
+              || alloc_value == GOMP_OMP_PREDEF_ALLOC_PTEAM
+              || alloc_value == GOMP_OMP_PREDEF_ALLOC_THREAD)
+       {
+         auto_diagnostic_group d;
+         error_at (EXPR_LOCATION (alloc_in),
+                   "%<allocator%> clause must not "
+                   "be %<omp_cgroup_mem_alloc%>, %<omp_pteam_mem_alloc%>, "
+                   "or %<omp_thread_mem_alloc%>");
+         emit_diag_for_static_vars ();
+         inform (EXPR_LOCATION (alloc_in),
+                 "expression evaluates to %qwu", tree_to_uhwi (ret));
+       }
       return ret;
     } (); /* IILE.  */
 
diff --git a/gcc/testsuite/c-c++-common/gomp/allocate-20.c 
b/gcc/testsuite/c-c++-common/gomp/allocate-20.c
index 2cc46988f6ad..cdc1685f7ad3 100644
--- a/gcc/testsuite/c-c++-common/gomp/allocate-20.c
+++ b/gcc/testsuite/c-c++-common/gomp/allocate-20.c
@@ -24,27 +24,31 @@ typedef enum omp_allocator_handle_t __GOMP_UINTPTR_T_ENUM
   __omp_allocator_handle_t_max__ = __UINTPTR_MAX__
 } omp_allocator_handle_t;
 
-static int g0;
-static int g1;
-static int g2;
-static int g3;
-static int g4;
-static int g5;
-static int g6;
-static int g7;
-static int g8;
-static int g9;
-static int g10;
+int g0;
+int g1;
+int g2;
+int g3;
+int g4;
+int g5;
+int g6;
+int g7;
+int g8;
+int g9;
+int g10;
 
 #pragma omp allocate(g0) allocator(omp_null_allocator)  // { dg-error 
"'allocator' clause requires a predefined allocator as 'g0' is static" "" { 
target c } }
+// { dg-error "'allocator' clause requires a constant predefined allocator" "" 
{ target c++ } .-1 }
 #pragma omp allocate(g1) allocator(omp_default_mem_alloc)
 #pragma omp allocate(g2) allocator(omp_large_cap_mem_alloc)
 #pragma omp allocate(g3) allocator(omp_const_mem_alloc)
 #pragma omp allocate(g4) allocator(omp_high_bw_mem_alloc)
 #pragma omp allocate(g5) allocator(omp_low_lat_mem_alloc)
 #pragma omp allocate(g6) allocator(omp_cgroup_mem_alloc)  // { dg-error 
"'allocator' clause for static variable 'g6' must not be 
'omp_cgroup_mem_alloc', 'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { 
target c } }
+// { dg-error "'allocator' clause must not be 'omp_cgroup_mem_alloc', 
'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { target c++ } .-1 }
 #pragma omp allocate(g7) allocator(omp_pteam_mem_alloc)   // { dg-error 
"'allocator' clause for static variable 'g7' must not be 
'omp_cgroup_mem_alloc', 'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { 
target c } }
+// { dg-error "'allocator' clause must not be 'omp_cgroup_mem_alloc', 
'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { target c++ } .-1 }
 #pragma omp allocate(g8) allocator(omp_thread_mem_alloc)  // { dg-error 
"'allocator' clause for static variable 'g8' must not be 
'omp_cgroup_mem_alloc', 'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { 
target c } }
+// { dg-error "'allocator' clause must not be 'omp_cgroup_mem_alloc', 
'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { target c++ } .-1 }
 #pragma omp allocate(g9) allocator(ompx_gnu_pinned_mem_alloc)
 #pragma omp allocate(g10) allocator(ompx_gnu_managed_mem_alloc)
 
@@ -64,14 +68,18 @@ void local_static ()
   static int s10;
 
   #pragma omp allocate(s0) allocator(omp_null_allocator)  // { dg-error 
"'allocator' clause requires a predefined allocator as 's0' is static" "" { 
target c } }
+  // { dg-error "'allocator' clause requires a constant predefined allocator" 
"" { target c++ } .-1 }
   #pragma omp allocate(s1) allocator(omp_default_mem_alloc)
   #pragma omp allocate(s2) allocator(omp_large_cap_mem_alloc)
   #pragma omp allocate(s3) allocator(omp_const_mem_alloc)
   #pragma omp allocate(s4) allocator(omp_high_bw_mem_alloc)
   #pragma omp allocate(s5) allocator(omp_low_lat_mem_alloc)
   #pragma omp allocate(s6) allocator(omp_cgroup_mem_alloc)  // { dg-error 
"'allocator' clause for static variable 's6' must not be 
'omp_cgroup_mem_alloc', 'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { 
target c } }
+  // { dg-error "'allocator' clause must not be 'omp_cgroup_mem_alloc', 
'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { target c++ } .-1 }
   #pragma omp allocate(s7) allocator(omp_pteam_mem_alloc)   // { dg-error 
"'allocator' clause for static variable 's7' must not be 
'omp_cgroup_mem_alloc', 'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { 
target c } }
+  // { dg-error "'allocator' clause must not be 'omp_cgroup_mem_alloc', 
'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { target c++ } .-1 }
   #pragma omp allocate(s8) allocator(omp_thread_mem_alloc)  // { dg-error 
"'allocator' clause for static variable 's8' must not be 
'omp_cgroup_mem_alloc', 'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { 
target c } }
+  // { dg-error "'allocator' clause must not be 'omp_cgroup_mem_alloc', 
'omp_pteam_mem_alloc', or 'omp_thread_mem_alloc'" "" { target c++ } .-1 }
   #pragma omp allocate(s9) allocator(ompx_gnu_pinned_mem_alloc)
   #pragma omp allocate(s10) allocator(ompx_gnu_managed_mem_alloc)
 }

Reply via email to