From 2dc9294c3c7c81a6d5e1d4dedf58fea87bbadde6 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Wed, 15 Apr 2020 09:59:14 +0200 Subject: [PATCH] openmp: Reject requires directives not at file or namespace scope [PR94593] This change started with a bugreport about a typo in one requires testcase (diagnosed with -Wunknown-pragmas only), but following discussion lead to noting that we do not diagnose restriction that requires directives in C/C++ may only appear at file or namespace scope; and several our tests violated that. 2020-04-15 Jakub Jelinek PR c/94593 * c-parser.c (c_parser_pragma) : Reject requires directive when not at file scope. * parser.c (cp_parser_pragma) : Reject requires directive when not at file or namespace scope. * c-c++-common/gomp/requires-1.c: Fix a typo, requries -> requires. Move directives to file scope. (i): Remove. * c-c++-common/gomp/requires-2.c: Move directives to file scope. (i, foo): Remove. * c-c++-common/gomp/requires-4.c: Move directives to file scope. * c-c++-common/gomp/atomic-19.c: Move requires directive to file scope. * c-c++-common/gomp/atomic-20.c: Likewise. * c-c++-common/gomp/atomic-21.c: Likewise. * c-c++-common/gomp/atomic-22.c: Likewise. * gcc.dg/gomp/requires-1.c: New test. * g++.dg/gomp/requires-1.C: New test. * g++.dg/gomp/requires-2.C: New test. * g++.dg/gomp/atomic-18.C: Move requires directive to file scope. --- gcc/c/ChangeLog | 6 ++++++ gcc/c/c-parser.c | 7 +++++++ gcc/cp/ChangeLog | 6 ++++++ gcc/cp/parser.c | 7 +++++++ gcc/testsuite/ChangeLog | 13 ++++++++++++ gcc/testsuite/c-c++-common/gomp/atomic-19.c | 10 +++++++-- gcc/testsuite/c-c++-common/gomp/atomic-20.c | 10 +++++++-- gcc/testsuite/c-c++-common/gomp/atomic-21.c | 10 +++++++-- gcc/testsuite/c-c++-common/gomp/atomic-22.c | 3 ++- gcc/testsuite/c-c++-common/gomp/requires-1.c | 9 +++----- gcc/testsuite/c-c++-common/gomp/requires-2.c | 14 ++----------- gcc/testsuite/c-c++-common/gomp/requires-4.c | 2 +- gcc/testsuite/g++.dg/gomp/atomic-18.C | 10 ++++++++- gcc/testsuite/g++.dg/gomp/requires-1.C | 12 +++++++++++ gcc/testsuite/g++.dg/gomp/requires-2.C | 22 ++++++++++++++++++++ gcc/testsuite/gcc.dg/gomp/requires-1.c | 22 ++++++++++++++++++++ 16 files changed, 136 insertions(+), 27 deletions(-) create mode 100644 gcc/testsuite/g++.dg/gomp/requires-1.C create mode 100644 gcc/testsuite/g++.dg/gomp/requires-2.C create mode 100644 gcc/testsuite/gcc.dg/gomp/requires-1.c diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 482a01b307a..a1831d46f12 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,9 @@ +2020-04-15 Jakub Jelinek + + PR c/94593 + * c-parser.c (c_parser_pragma) : Reject + requires directive when not at file scope. + 2020-04-08 Tobias Burnus PR middle-end/94120 diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index d1c954cd02a..679c14db29c 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -12402,6 +12402,13 @@ c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p) return false; case PRAGMA_OMP_REQUIRES: + if (context != pragma_external) + { + error_at (c_parser_peek_token (parser)->location, + "%<#pragma omp requires%> may only be used at file scope"); + c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL); + return false; + } c_parser_omp_requires (parser); return false; diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 4547674190e..0f53923e768 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2020-04-15 Jakub Jelinek + + PR c/94593 + * parser.c (cp_parser_pragma) : Reject + requires directive when not at file or namespace scope. + 2020-04-14 Iain Sandoe PR c++/94359 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index fec5203830a..3855416a854 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -43801,6 +43801,13 @@ cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p) return true; case PRAGMA_OMP_REQUIRES: + if (context != pragma_external) + { + error_at (pragma_tok->location, + "%<#pragma omp requires%> may only be used at file or " + "namespace scope"); + break; + } return cp_parser_omp_requires (parser, pragma_tok); case PRAGMA_OMP_ORDERED: diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0e4eea3873d..0804bf57457 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,16 @@ +2020-04-15 Jakub Jelinek + + PR c/94593 + * c-c++-common/gomp/requires-1.c: Fix a typo, requries -> requires. + Move directives to file scope. + (i): Remove. + * c-c++-common/gomp/requires-2.c: Move directives to file scope. + (i, foo): Remove. + * c-c++-common/gomp/requires-4.c: Move directives to file scope. + * gcc.dg/gomp/requires-1.c: New test. + * g++.dg/gomp/requires-1.C: New test. + * g++.dg/gomp/requires-2.C: New test. + 2020-04-15 Richard Biener PR middle-end/94539 diff --git a/gcc/testsuite/c-c++-common/gomp/atomic-19.c b/gcc/testsuite/c-c++-common/gomp/atomic-19.c index b15761e2a5b..c428262cfe0 100644 --- a/gcc/testsuite/c-c++-common/gomp/atomic-19.c +++ b/gcc/testsuite/c-c++-common/gomp/atomic-19.c @@ -10,10 +10,16 @@ int i, j, k, l, m, n; void foo () { - int v; #pragma omp atomic release i = i + 1; - #pragma omp requires atomic_default_mem_order (relaxed) +} + +#pragma omp requires atomic_default_mem_order (relaxed) + +void +bar () +{ + int v; #pragma omp atomic j = j + 1; #pragma omp atomic update diff --git a/gcc/testsuite/c-c++-common/gomp/atomic-20.c b/gcc/testsuite/c-c++-common/gomp/atomic-20.c index 3343d5e9235..d5059f4bb4a 100644 --- a/gcc/testsuite/c-c++-common/gomp/atomic-20.c +++ b/gcc/testsuite/c-c++-common/gomp/atomic-20.c @@ -10,10 +10,16 @@ int i, j, k, l, m, n; void foo () { - int v; #pragma omp atomic release i = i + 1; - #pragma omp requires atomic_default_mem_order (seq_cst) +} + +#pragma omp requires atomic_default_mem_order (seq_cst) + +void +bar () +{ + int v; #pragma omp atomic j = j + 1; #pragma omp atomic update diff --git a/gcc/testsuite/c-c++-common/gomp/atomic-21.c b/gcc/testsuite/c-c++-common/gomp/atomic-21.c index 8fa97b189e9..8d052ae7bc7 100644 --- a/gcc/testsuite/c-c++-common/gomp/atomic-21.c +++ b/gcc/testsuite/c-c++-common/gomp/atomic-21.c @@ -9,10 +9,16 @@ int i, j, k, l, m, n; void foo () { - int v; #pragma omp atomic release i = i + 1; - #pragma omp requires atomic_default_mem_order (acq_rel) +} + +#pragma omp requires atomic_default_mem_order (acq_rel) + +void +bar () +{ + int v; #pragma omp atomic j = j + 1; #pragma omp atomic update diff --git a/gcc/testsuite/c-c++-common/gomp/atomic-22.c b/gcc/testsuite/c-c++-common/gomp/atomic-22.c index 57b0dcbefae..e1db3645ce0 100644 --- a/gcc/testsuite/c-c++-common/gomp/atomic-22.c +++ b/gcc/testsuite/c-c++-common/gomp/atomic-22.c @@ -8,5 +8,6 @@ foo () i = i + 1; #pragma omp atomic read v = j; - #pragma omp requires atomic_default_mem_order (acq_rel) /* { dg-error "'atomic_default_mem_order' clause used lexically after first 'atomic' construct without memory order clause" } */ } + +#pragma omp requires atomic_default_mem_order (acq_rel) /* { dg-error "'atomic_default_mem_order' clause used lexically after first 'atomic' construct without memory order clause" } */ diff --git a/gcc/testsuite/c-c++-common/gomp/requires-1.c b/gcc/testsuite/c-c++-common/gomp/requires-1.c index f1ec8e7a154..e1f2e3a503f 100644 --- a/gcc/testsuite/c-c++-common/gomp/requires-1.c +++ b/gcc/testsuite/c-c++-common/gomp/requires-1.c @@ -3,15 +3,12 @@ #pragma omp requires unified_shared_memory unified_address #pragma omp requires dynamic_allocators,reverse_offload -int i; - void foo () { - if (0) - #pragma omp requires unified_shared_memory unified_address - i++; - #pragma omp requries atomic_default_mem_order(seq_cst) } +#pragma omp requires unified_shared_memory unified_address +#pragma omp requires atomic_default_mem_order(seq_cst) + /* { dg-prune-output "not supported yet" } */ diff --git a/gcc/testsuite/c-c++-common/gomp/requires-2.c b/gcc/testsuite/c-c++-common/gomp/requires-2.c index ea07e763c55..717b65caeea 100644 --- a/gcc/testsuite/c-c++-common/gomp/requires-2.c +++ b/gcc/testsuite/c-c++-common/gomp/requires-2.c @@ -3,18 +3,8 @@ #pragma omp requires unified_address unified_address /* { dg-error "too many 'unified_address' clauses" } */ #pragma omp requires reverse_offload reverse_offload /* { dg-error "too many 'reverse_offload' clauses" } */ #pragma omp requires foobarbaz /* { dg-error "expected 'unified_address', 'unified_shared_memory', 'dynamic_allocators', 'reverse_offload' or 'atomic_default_mem_order' clause" } */ - -int i; - -void -foo () -{ - #pragma omp requires dynamic_allocators , dynamic_allocators /* { dg-error "too many 'dynamic_allocators' clauses" } */ - if (0) - #pragma omp requires atomic_default_mem_order(seq_cst) atomic_default_mem_order(seq_cst) /* { dg-error "too many 'atomic_default_mem_order' clauses" } */ - i++; -} - +#pragma omp requires dynamic_allocators , dynamic_allocators /* { dg-error "too many 'dynamic_allocators' clauses" } */ +#pragma omp requires atomic_default_mem_order(seq_cst) atomic_default_mem_order(seq_cst) /* { dg-error "too many 'atomic_default_mem_order' clauses" } */ #pragma omp requires atomic_default_mem_order (seq_cst) /* { dg-error "more than one 'atomic_default_mem_order' clause in a single compilation unit" } */ /* { dg-prune-output "not supported yet" } */ diff --git a/gcc/testsuite/c-c++-common/gomp/requires-4.c b/gcc/testsuite/c-c++-common/gomp/requires-4.c index 37247a68c74..88ba7746cf8 100644 --- a/gcc/testsuite/c-c++-common/gomp/requires-4.c +++ b/gcc/testsuite/c-c++-common/gomp/requires-4.c @@ -4,9 +4,9 @@ foo (void) { #pragma omp target ; - #pragma omp requires unified_shared_memory /* { dg-error "'unified_shared_memory' clause used lexically after first target construct or offloading API" } */ } +#pragma omp requires unified_shared_memory /* { dg-error "'unified_shared_memory' clause used lexically after first target construct or offloading API" } */ #pragma omp requires unified_address /* { dg-error "'unified_address' clause used lexically after first target construct or offloading API" } */ #pragma omp requires reverse_offload /* { dg-error "'reverse_offload' clause used lexically after first target construct or offloading API" } */ diff --git a/gcc/testsuite/g++.dg/gomp/atomic-18.C b/gcc/testsuite/g++.dg/gomp/atomic-18.C index c93e732d850..896cf468a07 100644 --- a/gcc/testsuite/g++.dg/gomp/atomic-18.C +++ b/gcc/testsuite/g++.dg/gomp/atomic-18.C @@ -36,7 +36,14 @@ foo (T *p) i = v; #pragma omp atomic hint(1),update,release f = f + 2.0; - #pragma omp requires atomic_default_mem_order (acq_rel) +} + +#pragma omp requires atomic_default_mem_order (acq_rel) + +template +void +baz (T *p) +{ #pragma omp atomic hint (M - 1) update *p += 1; #pragma omp atomic capture, hint (M) @@ -47,4 +54,5 @@ void bar () { foo <0, 1, int> (&i); + baz <0, 1, int> (&i); } diff --git a/gcc/testsuite/g++.dg/gomp/requires-1.C b/gcc/testsuite/g++.dg/gomp/requires-1.C new file mode 100644 index 00000000000..aefeb288dad --- /dev/null +++ b/gcc/testsuite/g++.dg/gomp/requires-1.C @@ -0,0 +1,12 @@ +namespace N { +namespace M { +#pragma omp requires unified_address +#pragma omp requires unified_shared_memory +#pragma omp requires unified_shared_memory unified_address +#pragma omp requires dynamic_allocators,reverse_offload +#pragma omp requires unified_shared_memory unified_address +#pragma omp requires atomic_default_mem_order(seq_cst) +} +} + +/* { dg-prune-output "not supported yet" } */ diff --git a/gcc/testsuite/g++.dg/gomp/requires-2.C b/gcc/testsuite/g++.dg/gomp/requires-2.C new file mode 100644 index 00000000000..c044e5ee82a --- /dev/null +++ b/gcc/testsuite/g++.dg/gomp/requires-2.C @@ -0,0 +1,22 @@ +int i; + +void +foo () +{ + #pragma omp requires unified_address // { dg-error "may only be used at file or namespace scope" } + #pragma omp requires unified_shared_memory // { dg-error "may only be used at file or namespace scope" } + #pragma omp requires unified_shared_memory unified_address // { dg-error "may only be used at file or namespace scope" } + #pragma omp requires dynamic_allocators,reverse_offload // { dg-error "may only be used at file or namespace scope" } + #pragma omp requires atomic_default_mem_order(seq_cst) // { dg-error "may only be used at file or namespace scope" } + if (0) + #pragma omp requires unified_address // { dg-error "may only be used at file or namespace scope" } + i++; + if (0) + #pragma omp requires atomic_default_mem_order(seq_cst) // { dg-error "may only be used at file or namespace scope" } + i++; +} + +struct S { + int s; + #pragma omp requires unified_address // { dg-error "may only be used at file or namespace scope" } +}; diff --git a/gcc/testsuite/gcc.dg/gomp/requires-1.c b/gcc/testsuite/gcc.dg/gomp/requires-1.c new file mode 100644 index 00000000000..9f8d0541d8a --- /dev/null +++ b/gcc/testsuite/gcc.dg/gomp/requires-1.c @@ -0,0 +1,22 @@ +int i; + +void +foo () +{ + #pragma omp requires unified_address /* { dg-error "may only be used at file scope" } */ + #pragma omp requires unified_shared_memory /* { dg-error "may only be used at file scope" } */ + #pragma omp requires unified_shared_memory unified_address /* { dg-error "may only be used at file scope" } */ + #pragma omp requires dynamic_allocators,reverse_offload /* { dg-error "may only be used at file scope" } */ + #pragma omp requires atomic_default_mem_order(seq_cst) /* { dg-error "may only be used at file scope" } */ + if (0) + #pragma omp requires unified_address /* { dg-error "may only be used at file scope" } */ + i++; + if (0) + #pragma omp requires atomic_default_mem_order(seq_cst) /* { dg-error "may only be used at file scope" } */ + i++; +} + +struct S { + int s; + #pragma omp requires unified_address /* { dg-error "may only be used at file scope" } */ +}; -- 2.30.2