From cf840a7f7c14242ab7018071310851486a557d4f Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <thomas@codesourcery.com>
Date: Mon, 18 Dec 2023 17:25:17 +0100
Subject: [PATCH] Unify OpenACC/C and C++ behavior re duplicate OpenACC
 'declare' directives for 'extern' variables [PR90868]

This likely still isn't what OpenACC actually intends (addressing that is for
another day), but at least we now misbehave consistently for C and C++.

	PR c++/90868
	gcc/cp/
	* parser.cc (cp_parser_oacc_declare): For "more than once", check
	the DECL that we're actually setting the attribute on.
	gcc/testsuite/
	* c-c++-common/goacc/declare-1.c: Adjust.
	* c-c++-common/goacc/declare-2.c: Likewise.
---
 gcc/cp/parser.cc                             | 23 +++++++------
 gcc/testsuite/c-c++-common/goacc/declare-1.c |  9 +++---
 gcc/testsuite/c-c++-common/goacc/declare-2.c | 34 ++++++++------------
 3 files changed, 29 insertions(+), 37 deletions(-)

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index e4fbab1bab5..1e2d520345b 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -46962,20 +46962,8 @@ cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
 	  continue;
 	}
 
-      if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
-	  || lookup_attribute ("omp declare target link",
-			       DECL_ATTRIBUTES (decl)))
-	{
-	  error_at (loc, "variable %qD used more than once with "
-		    "%<#pragma acc declare%>", decl);
-	  error = true;
-	  continue;
-	}
-
       if (!error)
 	{
-	  tree id;
-
 	  if (DECL_LOCAL_DECL_P (decl))
 	    /* We need to mark the aliased decl, as that is the entity
 	       that is being referred to.  This won't work for
@@ -46987,6 +46975,17 @@ cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
 	      if (alias != error_mark_node)
 		decl = alias;
 
+	  if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
+	      || lookup_attribute ("omp declare target link",
+				   DECL_ATTRIBUTES (decl)))
+	    {
+	      error_at (loc, "variable %qD used more than once with "
+			"%<#pragma acc declare%>", decl);
+	      error = true;
+	      continue;
+	    }
+
+	  tree id;
 	  if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
 	    id = get_identifier ("omp declare target link");
 	  else
diff --git a/gcc/testsuite/c-c++-common/goacc/declare-1.c b/gcc/testsuite/c-c++-common/goacc/declare-1.c
index 46ee01b6759..808dc2ac818 100644
--- a/gcc/testsuite/c-c++-common/goacc/declare-1.c
+++ b/gcc/testsuite/c-c++-common/goacc/declare-1.c
@@ -113,11 +113,11 @@ f_2 (void)
   int va3;
 #pragma acc declare device_resident(va3)
 
-#ifndef __cplusplus
+#if 0
   /* TODO PR90868
 
-     C: "error: variable '[...]' used more than once with '#pragma acc declare'".  */
-#else
+     "error: variable '[...]' used more than once with '#pragma acc declare'".  */
+
   extern int ve0;
 #pragma acc declare create(ve0)
 
@@ -196,8 +196,7 @@ f_data (void)
 #if 0
     /* TODO PR90868
 
-       C: "error: variable '[...]' used more than once with '#pragma acc declare'".
-       C++: ICE during gimplification.  */
+       "error: variable '[...]' used more than once with '#pragma acc declare'".  */
 
     extern int ve0;
 # pragma acc declare create(ve0)
diff --git a/gcc/testsuite/c-c++-common/goacc/declare-2.c b/gcc/testsuite/c-c++-common/goacc/declare-2.c
index e2e22be57e9..35e45004561 100644
--- a/gcc/testsuite/c-c++-common/goacc/declare-2.c
+++ b/gcc/testsuite/c-c++-common/goacc/declare-2.c
@@ -137,61 +137,55 @@ void
 f_pr90868_2 (void)
 {
   extern int we0;
-#pragma acc declare create(we0) /* { dg-error "variable 'we0' used more than once with '#pragma acc declare'" "" { target c } } */
+#pragma acc declare create(we0) /* { dg-error "variable 'we0' used more than once with '#pragma acc declare'" } */
 
   extern int we1;
-#pragma acc declare copyin(we1) /* { dg-error "variable 'we1' used more than once with '#pragma acc declare'" "" { target c } } */
+#pragma acc declare copyin(we1) /* { dg-error "variable 'we1' used more than once with '#pragma acc declare'" } */
 
   extern int *we2;
-#pragma acc declare deviceptr(we2) /* { dg-error "variable 'we2' used more than once with '#pragma acc declare'" "" { target c } } */
+#pragma acc declare deviceptr(we2) /* { dg-error "variable 'we2' used more than once with '#pragma acc declare'" } */
 
   extern int we3;
-#pragma acc declare device_resident(we3) /* { dg-error "variable 'we3' used more than once with '#pragma acc declare'" "" { target c } } */
+#pragma acc declare device_resident(we3) /* { dg-error "variable 'we3' used more than once with '#pragma acc declare'" } */
 
   extern int we4;
-#pragma acc declare link(we4) /* { dg-error "variable 'we4' used more than once with '#pragma acc declare'" "" { target c } } */
+#pragma acc declare link(we4) /* { dg-error "variable 'we4' used more than once with '#pragma acc declare'" } */
 
   extern int we5;
-#pragma acc declare present_or_copyin(we5) /* { dg-error "variable 'we5' used more than once with '#pragma acc declare'" "" { target c } } */
+#pragma acc declare present_or_copyin(we5) /* { dg-error "variable 'we5' used more than once with '#pragma acc declare'" } */
  
   extern int we6;
-#pragma acc declare present_or_create(we6) /* { dg-error "variable 'we6' used more than once with '#pragma acc declare'" "" { target c } } */
+#pragma acc declare present_or_create(we6) /* { dg-error "variable 'we6' used more than once with '#pragma acc declare'" } */
 }
 
 
 /* The same as 'f_pr90868' but everything contained in an OpenACC 'data'
    construct.  */
 
-#ifdef __cplusplus
-/* TODO PR90868
-
-   C++: ICE during gimplification.  */
-#else
 void
 f_pr90868_data (void)
 {
 #pragma acc data
   {
     extern int we0;
-# pragma acc declare create(we0) /* { dg-error "variable 'we0' used more than once with '#pragma acc declare'" "" { target c } } */
+# pragma acc declare create(we0) /* { dg-error "variable 'we0' used more than once with '#pragma acc declare'" } */
 
     extern int we1;
-# pragma acc declare copyin(we1) /* { dg-error "variable 'we1' used more than once with '#pragma acc declare'" "" { target c } } */
+# pragma acc declare copyin(we1) /* { dg-error "variable 'we1' used more than once with '#pragma acc declare'" } */
 
     extern int *we2;
-# pragma acc declare deviceptr(we2) /* { dg-error "variable 'we2' used more than once with '#pragma acc declare'" "" { target c } } */
+# pragma acc declare deviceptr(we2) /* { dg-error "variable 'we2' used more than once with '#pragma acc declare'" } */
 
     extern int we3;
-# pragma acc declare device_resident(we3) /* { dg-error "variable 'we3' used more than once with '#pragma acc declare'" "" { target c } } */
+# pragma acc declare device_resident(we3) /* { dg-error "variable 'we3' used more than once with '#pragma acc declare'" } */
 
     extern int we4;
-# pragma acc declare link(we4) /* { dg-error "variable 'we4' used more than once with '#pragma acc declare'" "" { target c } } */
+# pragma acc declare link(we4) /* { dg-error "variable 'we4' used more than once with '#pragma acc declare'" } */
 
     extern int we5;
-# pragma acc declare present_or_copyin(we5) /* { dg-error "variable 'we5' used more than once with '#pragma acc declare'" "" { target c } } */
+# pragma acc declare present_or_copyin(we5) /* { dg-error "variable 'we5' used more than once with '#pragma acc declare'" } */
  
     extern int we6;
-# pragma acc declare present_or_create(we6) /* { dg-error "variable 'we6' used more than once with '#pragma acc declare'" "" { target c } } */
+# pragma acc declare present_or_create(we6) /* { dg-error "variable 'we6' used more than once with '#pragma acc declare'" } */
   }
 }
-#endif
-- 
2.34.1

