[clang] [OpenMP-5.2] deprecate delimited form of 'declare target' (PR #145854)

Urvi Rav via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 26 01:24:29 PDT 2025


https://github.com/ravurvi20 created https://github.com/llvm/llvm-project/pull/145854

According to OpenMP 5.2 (Section 7.8.2), the directive name `declare target` may be used as a synonym for `begin declare target` only when no clauses are specified. This clause-less delimited form is now deprecated and should emit a deprecation warning.

```
// Deprecated usage (should trigger warning):
#pragma omp declare target // deprecated in OpenMP 5.2
void foo1() {
}
#pragma omp end declare target
// Valid usage with clause (should not trigger warning):
#pragma omp declare target enter(foo2)
void foo2() {
}
```
```
// Recommended replacement for deprecated delimited form:
#pragma omp begin declare target
void foo() {
}
#pragma omp end declare target
```

>From 15935b92827c26a7697084201980f23305f3b348 Mon Sep 17 00:00:00 2001
From: urvi-rav <urvi.rav at hpe.com>
Date: Thu, 26 Jun 2025 03:19:50 -0500
Subject: [PATCH] deprecate delimited form of 'declare target'

---
 .../clang/Basic/DiagnosticParseKinds.td       |  3 ++
 clang/lib/Parse/ParseOpenMP.cpp               |  2 ++
 .../OpenMP/Inputs/declare_target_include.h    |  2 +-
 .../test/OpenMP/declare_target_ast_print.cpp  | 28 +++++++++----------
 clang/test/OpenMP/declare_target_messages.cpp | 24 ++++++++--------
 clang/test/OpenMP/target_ast_print.cpp        |  2 +-
 6 files changed, 33 insertions(+), 28 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 6c30da376dafb..3b55980f57c03 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1571,6 +1571,9 @@ def err_omp_declare_target_multiple : Error<
   "%0 appears multiple times in clauses on the same declare target directive">;
 def err_omp_declare_target_indirect_device_type: Error<
   "only 'device_type(any)' clause is allowed with indirect clause">;
+def warn_omp_deprecated_declare_target_delimited_form :
+  Warning<"the delimited form of '#pragma omp declare target' without clauses is deprecated; use '#pragma omp begin declare target' instead">,
+  InGroup<Deprecated>;
 def err_omp_expected_clause: Error<
   "expected at least one clause on '#pragma omp %0' directive">;
 def err_omp_mapper_illegal_identifier : Error<
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index f694ae1d0d112..d7efc0bb8bc90 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2309,6 +2309,8 @@ Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
     SourceLocation DTLoc = ConsumeAnyToken();
     bool HasClauses = Tok.isNot(tok::annot_pragma_openmp_end);
     SemaOpenMP::DeclareTargetContextInfo DTCI(DKind, DTLoc);
+    if (DKind == OMPD_declare_target && !HasClauses && getLangOpts().OpenMP >= 52)
+         Diag(DTLoc, diag::warn_omp_deprecated_declare_target_delimited_form);
     if (HasClauses)
       ParseOMPDeclareTargetClauses(DTCI);
     bool HasImplicitMappings = DKind == OMPD_begin_declare_target ||
diff --git a/clang/test/OpenMP/Inputs/declare_target_include.h b/clang/test/OpenMP/Inputs/declare_target_include.h
index b74cd00819db3..6a6a01ab81526 100644
--- a/clang/test/OpenMP/Inputs/declare_target_include.h
+++ b/clang/test/OpenMP/Inputs/declare_target_include.h
@@ -1,3 +1,3 @@
-#pragma omp declare target
+#pragma omp begin declare target
   void zyx();
 #pragma omp end declare target
diff --git a/clang/test/OpenMP/declare_target_ast_print.cpp b/clang/test/OpenMP/declare_target_ast_print.cpp
index 27d7a9fe21e52..68f73d5433595 100644
--- a/clang/test/OpenMP/declare_target_ast_print.cpp
+++ b/clang/test/OpenMP/declare_target_ast_print.cpp
@@ -133,7 +133,7 @@ int out_decl_target = 0;
 // CHECK: void lambda()
 // CHECK: #pragma omp end declare target{{$}}
 
-#pragma omp declare target
+#pragma omp begin declare target
 void lambda () {
 #ifdef __cpp_lambdas
   (void)[&] { ++out_decl_target; };
@@ -144,7 +144,7 @@ void lambda () {
 };
 #pragma omp end declare target
 
-#pragma omp declare target
+#pragma omp begin declare target
 // CHECK: #pragma omp declare target{{$}}
 void foo() {}
 // CHECK-NEXT: void foo()
@@ -152,7 +152,7 @@ void foo() {}
 // CHECK: #pragma omp end declare target{{$}}
 
 extern "C" {
-#pragma omp declare target
+#pragma omp begin declare target
 // CHECK: #pragma omp declare target
 void foo_c() {}
 // CHECK-NEXT: void foo_c()
@@ -161,7 +161,7 @@ void foo_c() {}
 }
 
 extern "C++" {
-#pragma omp declare target
+#pragma omp begin declare target
 // CHECK: #pragma omp declare target
 void foo_cpp() {}
 // CHECK-NEXT: void foo_cpp()
@@ -169,7 +169,7 @@ void foo_cpp() {}
 // CHECK: #pragma omp end declare target
 }
 
-#pragma omp declare target
+#pragma omp begin declare target
 template <class T>
 struct C {
 // CHECK: template <class T> struct C {
@@ -262,7 +262,7 @@ int c1, c2, c3;
 // CHECK: #pragma omp end declare target{{$}}
 
 struct SSSt {
-#pragma omp declare target
+#pragma omp begin declare target
   static int a;
   int b;
 #pragma omp end declare target
@@ -276,7 +276,7 @@ struct SSSt {
 
 template <class T>
 struct SSSTt {
-#pragma omp declare target
+#pragma omp begin declare target
   static T a;
   int b;
 #pragma omp end declare target
@@ -288,7 +288,7 @@ struct SSSTt {
 // CHECK: #pragma omp end declare target
 // CHECK: int b;
 
-#pragma omp declare target
+#pragma omp begin declare target
 template <typename T>
 T baz() { return T(); }
 #pragma omp end declare target
@@ -310,7 +310,7 @@ int baz() { return 1; }
 // CHECK: }
 // CHECK: #pragma omp end declare target
 
-#pragma omp declare target
+#pragma omp begin declare target
   #include "declare_target_include.h"
   void xyz();
 #pragma omp end declare target
@@ -322,8 +322,8 @@ int baz() { return 1; }
 // CHECK: void xyz();
 // CHECK: #pragma omp end declare target
 
-#pragma omp declare target
-  #pragma omp declare target
+#pragma omp begin declare target
+  #pragma omp begin declare target
     void abc();
   #pragma omp end declare target
   void cba();
@@ -336,7 +336,7 @@ int baz() { return 1; }
 // CHECK: void cba();
 // CHECK: #pragma omp end declare target
 
-#pragma omp declare target
+#pragma omp begin declare target
 int abc1() { return 1; }
 #if _OPENMP >= 202111
 #pragma omp declare target enter(abc1) device_type(nohost)
@@ -352,7 +352,7 @@ int abc1() { return 1; }
 // CHECK-NEXT: }
 // CHECK-NEXT: #pragma omp end declare target
 
-#pragma omp declare target
+#pragma omp begin declare target
 int inner_link;
 #pragma omp declare target link(inner_link)
 #pragma omp end declare target
@@ -396,7 +396,7 @@ int main (int argc, char **argv) {
 // CHECK-NEXT: #pragma omp end declare target
 
 // Do not expect anything here since the region is empty.
-#pragma omp declare target
+#pragma omp begin declare target
 #pragma omp end declare target
 
 #endif
diff --git a/clang/test/OpenMP/declare_target_messages.cpp b/clang/test/OpenMP/declare_target_messages.cpp
index 4aa4d686eaaf3..26c47ecfb36b0 100644
--- a/clang/test/OpenMP/declare_target_messages.cpp
+++ b/clang/test/OpenMP/declare_target_messages.cpp
@@ -53,7 +53,7 @@ __thread int t;
 // omp52-error at +2 {{expected '(' after 'declare target'}}
 // omp45-to-51-error at +1 {{expected '(' after 'declare target'}}
 #pragma omp declare target . 
-
+// omp52-or-later-warning at +1 {{the delimited form of '#pragma omp declare target' without clauses is deprecated; use '#pragma omp begin declare target' instead}}
 #pragma omp declare target
 void f();
 // omp60-warning at +3 {{extra tokens at the end of '#pragma omp end declare_target' are ignored}}
@@ -156,7 +156,7 @@ typedef int sint;
 template <typename T>
 T bla1() { return 0; }
 
-#pragma omp declare target
+#pragma omp begin declare target
 template <typename T>
 T bla2() { return 0; }
 #pragma omp end declare target
@@ -164,7 +164,7 @@ T bla2() { return 0; }
 template<>
 float bla2() { return 1.0; }
 
-#pragma omp declare target
+#pragma omp begin declare target
 void blub2() {
   bla2<float>();
   bla2<int>();
@@ -179,7 +179,7 @@ void t2() {
   }
 }
 
-#pragma omp declare target
+#pragma omp begin declare target
   void abc();
 #pragma omp end declare target
 void cba();
@@ -188,13 +188,13 @@ void cba();
 // omp45-to-51-error at +1 {{unexpected OpenMP directive '#pragma omp end declare target'}}
 #pragma omp end declare target 
 
-#pragma omp declare target
-#pragma omp declare target
+#pragma omp begin declare target
+#pragma omp begin declare target
 void def();
 #pragma omp end declare target
 void fed();
 
-#pragma omp declare target
+#pragma omp begin declare target
 // expected-note at +1 {{defined as threadprivate or thread local}}
 #pragma omp threadprivate(a) 
 extern int b;
@@ -239,7 +239,7 @@ void foo(int p) {
   q();
   c();
 }
-#pragma omp declare target
+#pragma omp begin declare target
 void foo1() {
   // omp5-or-later-var-note at +1 {{variable 'z' is captured here}}
   [&](){ (void)(b+z);}(); 
@@ -258,7 +258,7 @@ int C::method() {
 }
 
 struct S {
-#pragma omp declare target
+#pragma omp begin declare target
   int v;
 #pragma omp end declare target
 };
@@ -293,7 +293,7 @@ int main (int argc, char **argv) {
 }
 
 namespace {
-#pragma omp declare target
+#pragma omp begin declare target
   int x;
 }
 #pragma omp end declare target
@@ -347,7 +347,7 @@ void host3() {host1();} // dev5-error {{function with 'device_type(host)' is not
 // omp52-or-later-error at +1 {{expected at least one 'enter', 'link' or 'indirect' clause}}
 #pragma omp declare target to(host3)
 
-#pragma omp declare target
+#pragma omp begin declare target
 void any1() {any();}
 // dev5-error at +1 {{function with 'device_type(host)' is not available on device}}
 void any2() {host1();} 
@@ -411,7 +411,7 @@ struct target{
 // expected-warning at +1 {{declaration is not declared in any declare target region}}
 static target S;  
 
-#pragma omp declare target
+#pragma omp begin declare target
 // expected-note at +1 {{used here}}
 int target_var = variable;  
 // expected-note at +1 {{used here}}
diff --git a/clang/test/OpenMP/target_ast_print.cpp b/clang/test/OpenMP/target_ast_print.cpp
index ec6cf2130d7a5..db838f30511ab 100644
--- a/clang/test/OpenMP/target_ast_print.cpp
+++ b/clang/test/OpenMP/target_ast_print.cpp
@@ -367,7 +367,7 @@ extern const omp_allocator_handle_t omp_thread_mem_alloc;
 
 void foo() {}
 
-#pragma omp declare target
+#pragma omp begin declare target
 void bar() {}
 #pragma omp end declare target
 



More information about the cfe-commits mailing list