From a43c30cb6f451d60b7e19be09da075ab0ff5830f Mon Sep 17 00:00:00 2001 From: Sandra Loosemore Date: Mon, 25 Aug 2025 01:43:50 +0000 Subject: [PATCH] OpenMP: give error when variant is the same as the base function [PR118839] 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 | 6 ++++++ gcc/cp/decl.cc | 7 +++++++ gcc/fortran/trans-openmp.cc | 6 ++++++ gcc/testsuite/gcc.dg/gomp/declare-variant-3.c | 18 ++++++++++++++++++ .../gfortran.dg/gomp/declare-variant-22.f90 | 6 ++++++ 5 files changed, 43 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/gomp/declare-variant-3.c create mode 100644 gcc/testsuite/gfortran.dg/gomp/declare-variant-22.f90 diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index db669242d58..566ab1baebe 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -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); diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 140cc9b4699..4b1a335910b 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -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; diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc index 278e91c2c49..69a70d7138c 100644 --- a/gcc/fortran/trans-openmp.cc +++ b/gcc/fortran/trans-openmp.cc @@ -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 index 00000000000..92b71fe36e4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/gomp/declare-variant-3.c @@ -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 index 00000000000..a1b2f2a24d6 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/gomp/declare-variant-22.f90 @@ -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 -- 2.47.3