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 53b7c2749a6..885b3fa677b 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 fe22e062861..c579fda7ff5 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -8603,6 +8603,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 98b725b0312..fa99192adf6 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 { char err[256]; 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.39.5