]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
OpenMP: give error when variant is the same as the base function [PR118839]
authorSandra Loosemore <sloosemore@baylibre.com>
Mon, 25 Aug 2025 01:43:50 +0000 (01:43 +0000)
committerSandra Loosemore <sloosemore@baylibre.com>
Tue, 26 Aug 2025 23:39:23 +0000 (23:39 +0000)
As noted in the issue, the C++ front end has deeper problems: it's
supposed to do the name lookup of the variant at the call site but is
instead doing it when parsing the "declare variant" construct, before
registering the decl for the base function.  The C++ part of the
patch is a band-aid to catch the case where there is a previous declaration
of the function and it doesn't give an undefined symbol error instead.
Some real solution ought to be included as part of fixing PR118791.

gcc/c/
PR middle-end/118839
* c-parser.cc (c_finish_omp_declare_variant): Error if variant
is the same as base.

gcc/cp/
PR middle-end/118839
* decl.cc (omp_declare_variant_finalize_one): Error if variant
is the same as base.

gcc/fortran/
PR middle-end/118839
* trans-openmp.cc (gfc_trans_omp_declare_variant): Error if variant
is the same as base.

gcc/testsuite/
PR middle-end/118839
* gcc.dg/gomp/declare-variant-3.c: New.
* gfortran.dg/gomp/declare-variant-22.f90: New.

gcc/c/c-parser.cc
gcc/cp/decl.cc
gcc/fortran/trans-openmp.cc
gcc/testsuite/gcc.dg/gomp/declare-variant-3.c [new file with mode: 0644]
gcc/testsuite/gfortran.dg/gomp/declare-variant-22.f90 [new file with mode: 0644]

index db669242d58a06504f142288813f851952146d99..566ab1baebed1f375db75ea5ba1867643df49099 100644 (file)
@@ -27322,6 +27322,12 @@ c_finish_omp_declare_variant (c_parser *parser, tree fndecl, tree parms)
                variant);
       variant = error_mark_node;
     }
+  else if (variant == fndecl)
+    {
+      error_at (token->location, "variant %qD is the same as base function",
+               variant);
+      variant = error_mark_node;
+    }
 
   c_parser_consume_token (parser);
 
index 140cc9b4699a9cbebf464e6aa6ca5e0745c4ace5..4b1a335910b15d295d04efb9c572660630deca29 100644 (file)
@@ -8607,6 +8607,13 @@ omp_declare_variant_finalize_one (tree decl, tree attr)
   variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
   input_location = save_loc;
 
+  if (variant == decl)
+    {
+      error_at (varid_loc, "variant %qD is the same as base function",
+               variant);
+      return true;
+    }
+
   if (variant)
     {
       bool fail;
index 278e91c2c495b93305e8ab571c5042cc01cdad3c..69a70d7138cf4a9b3aadc2d7b6c27b4ce7031c8f 100644 (file)
@@ -9735,6 +9735,12 @@ gfc_trans_omp_declare_variant (gfc_namespace *ns, gfc_namespace *parent_ns)
                         variant_proc_name, &odv->where);
              variant_proc_sym = NULL;
            }
+         else if (variant_proc_sym == ns->proc_name)
+           {
+             gfc_error ("variant %qs at %L is the same as base function",
+                        variant_proc_name, &odv->where);
+             variant_proc_sym = NULL;
+           }
          else if (omp_get_context_selector (set_selectors,
                                             OMP_TRAIT_SET_CONSTRUCT,
                                             OMP_TRAIT_CONSTRUCT_SIMD)
diff --git a/gcc/testsuite/gcc.dg/gomp/declare-variant-3.c b/gcc/testsuite/gcc.dg/gomp/declare-variant-3.c
new file mode 100644 (file)
index 0000000..92b71fe
--- /dev/null
@@ -0,0 +1,18 @@
+/* PR118839: Check that error is diagnosed when the variant is the same as
+   the base function.  */
+
+/* No previous declaration.  */
+#pragma omp declare variant(f) match(user={condition(1)})  /* { dg-error "variant 'f' is the same as base function" } */
+void f(int *x);
+
+/* Previous declaration.  */
+void g(int *x)
+{
+  *x = 42;
+}
+
+#pragma omp declare variant(g) match(user={condition(1)})  /* { dg-error "variant 'g' is the same as base function" } */
+void g(int *x);
+
+
+
diff --git a/gcc/testsuite/gfortran.dg/gomp/declare-variant-22.f90 b/gcc/testsuite/gfortran.dg/gomp/declare-variant-22.f90
new file mode 100644 (file)
index 0000000..a1b2f2a
--- /dev/null
@@ -0,0 +1,6 @@
+! PR118839:  Check that error is diagnosed when the variant is the same
+! as the base function.
+
+subroutine f()
+  !$omp declare variant(f) match(user={condition(.true.)})  ! { dg-error "variant 'f' at .1. is the same as base function" }
+end subroutine