Hi!

This patch diagnoses cases like:
  #pragma omp parallel
  [[omp::directive (declare simd)]] int foo ();
or
  #pragma omp taskgroup
  int bar [[omp::directive (declare simd)]] (int);
where the pragma is on the same declaration statement as the declare simd
attribute.

Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk.

2021-08-12  Jakub Jelinek  <ja...@redhat.com>

        * parser.c (cp_parser_lambda_body): Add temp overrides
        for parser->{omp_declare_simd,oacc_routine,omp_attrs_forbidden_p}.
        (cp_parser_statement): Restore parser->omp_attrs_forbidden_p for
        cp_parser_declaration_statement.
        (cp_parser_default_argument): Add temp override for
        parser->omp_attrs_forbidden_p.
        (cp_parser_late_parsing_omp_declare_simd): Diagnose declare simd
        or declare variant in attribute syntax on a declaration immediately
        following an OpenMP construct in pragma syntax.

        * g++.dg/gomp/attrs-11.C: Add new tests.

--- gcc/cp/parser.c.jj  2021-08-11 14:17:53.980173449 +0200
+++ gcc/cp/parser.c     2021-08-11 16:51:58.057481477 +0200
@@ -11625,6 +11625,9 @@ cp_parser_lambda_body (cp_parser* parser
        middle of an expression.  */
     ++function_depth;
 
+  auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
+  auto ord = make_temp_override (parser->oacc_routine, NULL);
+  auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
   vec<tree> omp_privatization_save;
   save_omp_privatization_clauses (omp_privatization_save);
   /* Clear this in case we're in the middle of a default argument.  */
@@ -12268,9 +12271,11 @@ cp_parser_statement (cp_parser* parser,
               so let's un-parse them.  */
            saved_tokens.rollback();
 
+         parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
          cp_parser_parse_tentatively (parser);
          /* Try to parse the declaration-statement.  */
          cp_parser_declaration_statement (parser);
+         parser->omp_attrs_forbidden_p = false;
          /* If that worked, we're done.  */
          if (cp_parser_parse_definitely (parser))
            return;
@@ -24716,6 +24721,8 @@ cp_parser_default_argument (cp_parser *p
   parser->greater_than_is_operator_p = !template_parm_p;
   auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
   auto ord = make_temp_override (parser->oacc_routine, NULL);
+  auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
+
   /* Local variable names (and the `this' keyword) may not
      appear in a default argument.  */
   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
@@ -44445,6 +44452,14 @@ cp_parser_late_parsing_omp_declare_simd
                    continue;
                  }
 
+               if (parser->omp_attrs_forbidden_p)
+                 {
+                   error_at (first->location,
+                             "mixing OpenMP directives with attribute and "
+                             "pragma syntax on the same statement");
+                   parser->omp_attrs_forbidden_p = false;
+                 }
+
                if (!flag_openmp && strcmp (directive[1], "simd") != 0)
                  continue;
                if (lexer == NULL)
--- gcc/testsuite/g++.dg/gomp/attrs-11.C.jj     2021-08-10 11:22:14.441607489 
+0200
+++ gcc/testsuite/g++.dg/gomp/attrs-11.C        2021-08-11 16:56:49.262489927 
+0200
@@ -72,3 +72,15 @@ int f28 [[omp::directive (declare simd),
 int f29 [[omp::directive (foobar), omp::directive (declare simd)]] (int);      
// { dg-error "unknown OpenMP directive name" }
 int f30 [[omp::directive (threadprivate (t7)), omp::directive (declare simd)]] 
(int);  // { dg-error "OpenMP directive other than 'declare simd' or 'declare 
variant' appertains to a declaration" }
 int f31 [[omp::directive (declare simd), omp::directive (threadprivate (t8))]] 
(int);  // { dg-error "OpenMP directive other than 'declare simd' or 'declare 
variant' appertains to a declaration" }
+
+void
+baz ()
+{
+  #pragma omp parallel
+  [[omp::directive (declare simd)]] extern int f32 (int);      // { dg-error 
"mixing OpenMP directives with attribute and pragma syntax on the same 
statement" }
+  #pragma omp parallel
+  extern int f33 [[omp::directive (declare simd)]] (int);      // { dg-error 
"mixing OpenMP directives with attribute and pragma syntax on the same 
statement" }
+  [[omp::directive (parallel)]]
+  #pragma omp declare simd     // { dg-error "mixing OpenMP directives with 
attribute and pragma syntax on the same statement" }
+  extern int f34 (int);
+}


        Jakub

Reply via email to