[clang] default clause replaced by otherwise clause for metadirective in OpenMP 5.2 (PR #128640)

Urvi Rav via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 1 22:38:43 PDT 2025


https://github.com/ravurvi20 updated https://github.com/llvm/llvm-project/pull/128640

>From 6ebd5991788608fbd104ea9c23230912044462d3 Mon Sep 17 00:00:00 2001
From: Urvi Rav <urvi.rav20 at gmail.com>
Date: Tue, 25 Feb 2025 00:49:07 -0600
Subject: [PATCH 1/2] default clause replaced by otherwise clause for
 metadirective

---
 .../clang/Basic/DiagnosticParseKinds.td       |  4 ++
 clang/lib/Parse/ParseOpenMP.cpp               | 20 ++++++
 clang/test/OpenMP/metadirective_messages.cpp  | 61 +++++++++++++------
 3 files changed, 68 insertions(+), 17 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index c513dab810d1f..4b8449e9ee9b6 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1657,6 +1657,10 @@ def err_omp_expected_colon : Error<"missing ':' in %0">;
 def err_omp_missing_comma : Error< "missing ',' after %0">;
 def err_omp_expected_context_selector
     : Error<"expected valid context selector in %0">;
+def err_omp_unknown_clause
+    : Error<"unknown clause '%0' in %1">;
+def warn_omp_default_deprecated : Warning<"'default' clause for"
+  " 'metadirective' is deprecated; use 'otherwise' instead">, InGroup<Deprecated>;
 def err_omp_requires_out_inout_depend_type : Error<
   "reserved locator 'omp_all_memory' requires 'out' or 'inout' "
   "dependency types">;
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 42e6aac681c1c..3b86847e937a2 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2759,6 +2759,19 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
       OpenMPClauseKind CKind = Tok.isAnnotation()
                                    ? OMPC_unknown
                                    : getOpenMPClauseKind(PP.getSpelling(Tok));
+      // Check if the clause is unrecognized.
+      if (getLangOpts().OpenMP < 52 &&
+          (CKind == OMPC_unknown || CKind == OMPC_otherwise)) {
+        Diag(Tok, diag::err_omp_unknown_clause)
+            << PP.getSpelling(Tok) << "metadirective";
+      }
+      if (getLangOpts().OpenMP >= 52 && CKind == OMPC_unknown) {
+        Diag(Tok, diag::err_omp_unknown_clause)
+            << PP.getSpelling(Tok) << "metadirective";
+      }
+      if (CKind == OMPC_default && getLangOpts().OpenMP >= 52) {
+        Diag(Tok, diag::warn_omp_default_deprecated);
+      }
       SourceLocation Loc = ConsumeToken();
 
       // Parse '('.
@@ -2785,6 +2798,13 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
           return Directive;
         }
       }
+      if (CKind == OMPC_otherwise) {
+        // Check for 'otherwise' keyword.
+        if (Tok.is(tok::identifier) &&
+            Tok.getIdentifierInfo()->getName() == "otherwise") {
+          ConsumeToken(); // Consume 'otherwise'
+        }
+      }
       // Skip Directive for now. We will parse directive in the second iteration
       int paren = 0;
       while (Tok.isNot(tok::r_paren) || paren != 0) {
diff --git a/clang/test/OpenMP/metadirective_messages.cpp b/clang/test/OpenMP/metadirective_messages.cpp
index 7fce9fa446058..40ea37845fdff 100644
--- a/clang/test/OpenMP/metadirective_messages.cpp
+++ b/clang/test/OpenMP/metadirective_messages.cpp
@@ -2,21 +2,48 @@
 
 // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -verify -fopenmp-simd -x c++ -std=c++14 -fexceptions -fcxx-exceptions %s
 
+// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -verify=expected,omp52 -fopenmp -fopenmp-version=52 -ferror-limit 100 -o - %s -Wuninitialized
+
 void foo() {
-#pragma omp metadirective // expected-error {{expected expression}}
-  ;
-#pragma omp metadirective when() // expected-error {{expected valid context selector in when clause}} expected-error {{expected expression}} expected-warning {{expected identifier or string literal describing a context set; set skipped}} expected-note {{context set options are: 'construct' 'device' 'target_device' 'implementation' 'user'}} expected-note {{the ignored set spans until here}}
-  ;
-#pragma omp metadirective when(device{}) // expected-warning {{expected '=' after the context set name "device"; '=' assumed}} expected-warning {{expected identifier or string literal describing a context selector; selector skipped}} expected-note {{context selector options are: 'kind' 'arch' 'isa'}} expected-note {{the ignored selector spans until here}} expected-error {{expected valid context selector in when clause}} expected-error {{expected expression}}
-  ;
-#pragma omp metadirective when(device{arch(nvptx)}) // expected-error {{missing ':' in when clause}} expected-error {{expected expression}} expected-warning {{expected '=' after the context set name "device"; '=' assumed}}
-  ;
-#pragma omp metadirective when(device{arch(nvptx)}: ) default() // expected-warning {{expected '=' after the context set name "device"; '=' assumed}}
-  ;
-#pragma omp metadirective when(device = {arch(nvptx)} : ) default(xyz) // expected-error {{expected an OpenMP directive}} expected-error {{use of undeclared identifier 'xyz'}}
-  ;
-#pragma omp metadirective when(device = {arch(nvptx)} : parallel default() // expected-error {{expected ',' or ')' in 'when' clause}} expected-error {{expected expression}}
-  ;
-#pragma omp metadirective when(device = {isa("some-unsupported-feature")} : parallel) default(single) // expected-warning {{isa trait 'some-unsupported-feature' is not known to the current target; verify the spelling or consider restricting the context selector with the 'arch' selector further}}
-  ;
-}
+  #if _OPENMP >= 202111
+    #pragma omp metadirective // omp52-error {{expected expression}}
+      ;
+    #pragma omp metadirective when() // omp52-error {{expected valid context selector in when clause}} expected-error {{expected expression}} expected-warning {{expected identifier or string literal describing a context set; set skipped}} expected-note {{context set options are: 'construct' 'device' 'target_device' 'implementation' 'user'}} expected-note {{the ignored set spans until here}}
+      ;
+    #pragma omp metadirective when(device{}) // omp52-warning {{expected '=' after the context set name "device"; '=' assumed}} expected-warning {{expected identifier or string literal describing a context selector; selector skipped}} expected-note {{context selector options are: 'kind' 'arch' 'isa'}} expected-note {{the ignored selector spans until here}} expected-error {{expected valid context selector in when clause}} expected-error {{expected expression}}
+      ;
+    #pragma omp metadirective when(device{arch(nvptx)}) // omp52-error {{missing ':' in when clause}} expected-error {{expected expression}} expected-warning {{expected '=' after the context set name "device"; '=' assumed}}
+      ;
+    #pragma omp metadirective when(device{arch(nvptx)}: ) otherwise() // omp52-warning {{expected '=' after the context set name "device"; '=' assumed}}
+      ;
+    #pragma omp metadirective when(device = {arch(nvptx)} : ) otherwise(xyz) // omp52-error {{expected an OpenMP directive}} expected-error {{use of undeclared identifier 'xyz'}}
+      ;
+    #pragma omp metadirective when(device = {arch(nvptx)} : parallel otherwise() // omp52-error {{expected ',' or ')' in 'when' clause}} expected-error {{expected expression}}
+      ;
+    #pragma omp metadirective when(device = {isa("some-unsupported-feature")} : parallel) otherwise(single) // omp52-warning {{isa trait 'some-unsupported-feature' is not known to the current target; verify the spelling or consider restricting the context selector with the 'arch' selector further}}
+      ;
+    #pragma omp metadirective when(device = {arch(nvptx)} : parallel) default() // omp52-warning {{'default' clause for 'metadirective' is deprecated; use 'otherwise' instead}}
+      ;
+    #pragma omp metadirective when(device = {arch(nvptx)} : parallel) xyz() //omp52-error {{unknown clause 'xyz' in metadirective}} 
+      ;
+  #else
+    #pragma omp metadirective // expected-error {{expected expression}}
+      ;
+    #pragma omp metadirective when() // expected-error {{expected valid context selector in when clause}} expected-error {{expected expression}} expected-warning {{expected identifier or string literal describing a context set; set skipped}} expected-note {{context set options are: 'construct' 'device' 'target_device' 'implementation' 'user'}} expected-note {{the ignored set spans until here}}
+      ;
+    #pragma omp metadirective when(device{}) // expected-warning {{expected '=' after the context set name "device"; '=' assumed}} expected-warning {{expected identifier or string literal describing a context selector; selector skipped}} expected-note {{context selector options are: 'kind' 'arch' 'isa'}} expected-note {{the ignored selector spans until here}} expected-error {{expected valid context selector in when clause}} expected-error {{expected expression}}
+      ;
+    #pragma omp metadirective when(device{arch(nvptx)}) // expected-error {{missing ':' in when clause}} expected-error {{expected expression}} expected-warning {{expected '=' after the context set name "device"; '=' assumed}}
+      ;
+    #pragma omp metadirective when(device{arch(nvptx)}: ) default() // expected-warning {{expected '=' after the context set name "device"; '=' assumed}}
+      ;
+    #pragma omp metadirective when(device = {arch(nvptx)} : ) default(xyz) // expected-error {{expected an OpenMP directive}} expected-error {{use of undeclared identifier 'xyz'}}
+      ;
+    #pragma omp metadirective when(device = {arch(nvptx)} : parallel default() // expected-error {{expected ',' or ')' in 'when' clause}} expected-error {{expected expression}}
+      ;
+    #pragma omp metadirective when(device = {isa("some-unsupported-feature")} : parallel) default(single) // expected-warning {{isa trait 'some-unsupported-feature' is not known to the current target; verify the spelling or consider restricting the context selector with the 'arch' selector further}}
+      ;
+    #pragma omp metadirective when(device = {arch(nvptx)} : parallel) xyz() //expected-error {{unknown clause 'xyz' in metadirective}} 
+      ;
+  #endif
+  }
\ No newline at end of file

>From 24fe1cf564887de244bf36be44f0664a909332c3 Mon Sep 17 00:00:00 2001
From: urvi-rav <urvi.rav at hpe.com>
Date: Wed, 2 Apr 2025 00:34:55 -0500
Subject: [PATCH 2/2] Update ParseOpenMP.cpp and error messages

---
 .../clang/Basic/DiagnosticParseKinds.td       |  2 +-
 clang/lib/Parse/ParseOpenMP.cpp               |  8 +++----
 clang/test/OpenMP/metadirective_ast_print.c   | 21 ++++++++++++-------
 clang/test/OpenMP/metadirective_messages.cpp  |  6 ++++--
 4 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index d7346829b047c..0eb496109b5bc 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1661,7 +1661,7 @@ def err_omp_missing_comma : Error< "missing ',' after %0">;
 def err_omp_expected_context_selector
     : Error<"expected valid context selector in %0">;
 def err_omp_unknown_clause
-    : Error<"unknown clause '%0' in %1">;
+    : Error<"expected an OpenMP clause">;
 def warn_omp_default_deprecated : Warning<"'default' clause for"
   " 'metadirective' is deprecated; use 'otherwise' instead">, InGroup<Deprecated>;
 def err_omp_requires_out_inout_depend_type : Error<
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 44375c8849736..21474712ba920 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2760,13 +2760,13 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
                                    ? OMPC_unknown
                                    : getOpenMPClauseKind(PP.getSpelling(Tok));
       // Check if the clause is unrecognized.
-      if (getLangOpts().OpenMP < 52 &&
-          (CKind == OMPC_unknown || CKind == OMPC_otherwise)) {
+      if (CKind == OMPC_unknown) {
         Diag(Tok, diag::err_omp_unknown_clause)
             << PP.getSpelling(Tok) << "metadirective";
       }
-      if (getLangOpts().OpenMP >= 52 && CKind == OMPC_unknown) {
-        Diag(Tok, diag::err_omp_unknown_clause)
+      if (getLangOpts().OpenMP < 52 &&
+          CKind == OMPC_otherwise) {
+        Diag(Tok, diag::err_omp_unexpected_clause)
             << PP.getSpelling(Tok) << "metadirective";
       }
       if (CKind == OMPC_default && getLangOpts().OpenMP >= 52) {
diff --git a/clang/test/OpenMP/metadirective_ast_print.c b/clang/test/OpenMP/metadirective_ast_print.c
index 851f08ce37ee7..4cabc92dd6d30 100644
--- a/clang/test/OpenMP/metadirective_ast_print.c
+++ b/clang/test/OpenMP/metadirective_ast_print.c
@@ -78,10 +78,7 @@ void foo(void) {
   for (int i = 0; i < 16; i++)
     ;
 
-#pragma omp metadirective when(user = {condition(0)}	\
-			       : parallel for) otherwise()
-  for (int i=0; i<10; i++)
-    ;
+
 #pragma omp metadirective when(user = {condition(0)}	\
 			       : parallel for)
   for (int i=0; i<10; i++)
@@ -92,10 +89,7 @@ void foo(void) {
   for (int i=0; i<10; i++)
     ;
 
-#pragma omp metadirective when(user = {condition(1)}	\
-			       : parallel for) otherwise()
-  for (int i=0; i<10; i++)
-    ;
+
 #pragma omp metadirective when(user = {condition(1)}	\
 			       : parallel for)
   for (int i=0; i<10; i++)
@@ -105,6 +99,17 @@ void foo(void) {
 						    : parallel) default(parallel for)
   for (int i=0; i<10; i++)
     ;
+#if _OPENMP >= 202111
+    #pragma omp metadirective when(user = {condition(0)}	\
+                 : parallel for) otherwise()
+      for (int i=0; i<10; i++)
+        ;
+    
+    #pragma omp metadirective when(user = {condition(1)}	\
+                : parallel for) otherwise()
+      for (int i=0; i<10; i++)
+        ;
+#endif
 }
 
 // CHECK: void bar(void);
diff --git a/clang/test/OpenMP/metadirective_messages.cpp b/clang/test/OpenMP/metadirective_messages.cpp
index 40ea37845fdff..ac2a5330cbad3 100644
--- a/clang/test/OpenMP/metadirective_messages.cpp
+++ b/clang/test/OpenMP/metadirective_messages.cpp
@@ -24,7 +24,7 @@ void foo() {
       ;
     #pragma omp metadirective when(device = {arch(nvptx)} : parallel) default() // omp52-warning {{'default' clause for 'metadirective' is deprecated; use 'otherwise' instead}}
       ;
-    #pragma omp metadirective when(device = {arch(nvptx)} : parallel) xyz() //omp52-error {{unknown clause 'xyz' in metadirective}} 
+    #pragma omp metadirective when(device = {arch(nvptx)} : parallel) xyz() //omp52-error {{expected an OpenMP clause}} 
       ;
   #else
     #pragma omp metadirective // expected-error {{expected expression}}
@@ -43,7 +43,9 @@ void foo() {
       ;
     #pragma omp metadirective when(device = {isa("some-unsupported-feature")} : parallel) default(single) // expected-warning {{isa trait 'some-unsupported-feature' is not known to the current target; verify the spelling or consider restricting the context selector with the 'arch' selector further}}
       ;
-    #pragma omp metadirective when(device = {arch(nvptx)} : parallel) xyz() //expected-error {{unknown clause 'xyz' in metadirective}} 
+    #pragma omp metadirective when(device = {isa("some-unsupported-feature")} : parallel) otherwise(single) // expected-warning {{isa trait 'some-unsupported-feature' is not known to the current target; verify the spelling or consider restricting the context selector with the 'arch' selector further}} //expected-error{{unexpected OpenMP clause 'otherwise' in directive '#pragma omp metadirective'}}
+      ;
+    #pragma omp metadirective when(device = {arch(nvptx)} : parallel) xyz() //expected-error {{expected an OpenMP clause}} 
       ;
   #endif
   }
\ No newline at end of file



More information about the cfe-commits mailing list