[gcc/devel/omp/gcc-16] OpenMP/C++: Diagnose constexpr vars in an allocate directive

Sandra Loosemore via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:b702cec3bf734b853e3b5fb5dcc7fb41e0a28bf2

commit b702cec3bf734b853e3b5fb5dcc7fb41e0a28bf2
Author: Waffl3x <[email protected]>
Date:   Tue May 5 09:02:02 2026 -0600

    OpenMP/C++: Diagnose constexpr vars in an allocate directive
    
    It is unclear whether this should be allowed or not. For now, diagnose it
    until one can come up with a compelling reason to allow it.
    
    gcc/cp/ChangeLog:
    
            * parser.cc (cp_parser_omp_allocate): Add diagnostics.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/gomp/allocate-15.C: Add cases.
    
    Signed-off-by: Waffl3x <[email protected]>

Diff:
---
 gcc/cp/parser.cc                        | 13 ++++++++++
 gcc/testsuite/g++.dg/gomp/allocate-15.C | 42 +++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index e844867c13a3..826ff368c92a 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -47564,6 +47564,19 @@ cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
 	    continue;
 	  }
 
+	if (DECL_DECLARED_CONSTEXPR_P (var))
+	  {
+	    auto_diagnostic_group d;
+	    error_at (arg_loc,
+		      "constexpr variable %qD may not appear as list item in "
+		      "an %<allocate%> directive", var);
+	    inform (DECL_SOURCE_LOCATION (var),
+		    "%qD declared here", var);
+	    /* Remove the node.  */
+	    *chain = TREE_CHAIN (node);
+	    continue;
+	  }
+
 	/* Do this before checking if the var was used in another allocate
 	   directive, as the latter diagnostic implies that removing the var
 	   from the previous directive would fix the problem.  */
diff --git a/gcc/testsuite/g++.dg/gomp/allocate-15.C b/gcc/testsuite/g++.dg/gomp/allocate-15.C
index 605e10e477f8..ef4bd098fa94 100644
--- a/gcc/testsuite/g++.dg/gomp/allocate-15.C
+++ b/gcc/testsuite/g++.dg/gomp/allocate-15.C
@@ -48,3 +48,45 @@ void instantiate_var_templ()
   dependent_var_templ<int const&&>(); /* { dg-message "required from here" } */
 }
 
+
+/* Diagnostics for constexpr vars used in an allocate directive.  */
+
+void cx_var()
+{
+  constexpr int cx = 42; /* { dg-note "'cx' declared here" } */
+  #pragma omp allocate(cx) /* { dg-error "constexpr variable 'cx' may not appear as list item in an 'allocate' directive" } */
+}
+
+template<typename>
+void cx_var_templ_not_instantiated()
+{
+  constexpr int cx = 42; /* { dg-note "'cx' declared here" } */
+  #pragma omp allocate(cx) /* { dg-error "constexpr variable 'cx' may not appear as list item in an 'allocate' directive" } */
+}
+
+template<typename>
+void cx_var_templ()
+{
+  constexpr int cx = 42; /* { dg-note "'cx' declared here" } */
+  #pragma omp allocate(cx) /* { dg-error "constexpr variable 'cx' may not appear as list item in an 'allocate' directive" } */
+}
+
+template<typename T>
+void dependent_cx_var_templ_not_instantiated()
+{
+  constexpr T cx = 42; /* { dg-note "'cx' declared here" } */
+  #pragma omp allocate(cx) /* { dg-error "constexpr variable 'cx' may not appear as list item in an 'allocate' directive" } */
+}
+
+template<typename T>
+void dependent_cx_var_templ()
+{
+  constexpr T cx = 42; /* { dg-note "'cx' declared here" } */
+  #pragma omp allocate(cx) /* { dg-error "constexpr variable 'cx' may not appear as list item in an 'allocate' directive" } */
+}
+
+void instantiate_cx_templ()
+{
+  cx_var_templ<void>();
+  dependent_cx_var_templ<int>();
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.