[PATCH] D122585: [OpenMP] Handle function declaration or definition omp pragmas in C mode

Raúl Peñacoba via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 28 07:03:21 PDT 2022


rpenacob created this revision.
rpenacob added reviewers: ABataev, jdoerfert.
Herald added subscribers: guansong, yaxunl.
Herald added a project: All.
rpenacob requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1.
Herald added a project: clang.

Clang crashes when a C++ like struct with a `declare variant` or `declare simd` pragma is parsed in C mode. Skip the pragma in this situation and leave the parser emit a diagnostic.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122585

Files:
  clang/lib/Parse/ParseOpenMP.cpp
  clang/test/OpenMP/declare_simd_messages.c
  clang/test/OpenMP/declare_variant_messages.c


Index: clang/test/OpenMP/declare_variant_messages.c
===================================================================
--- clang/test/OpenMP/declare_variant_messages.c
+++ clang/test/OpenMP/declare_variant_messages.c
@@ -212,3 +212,16 @@
 #pragma omp declare variant(foo) match(user = {condition(1)}) // expected-error {{nested user conditions in OpenMP context selector not supported (yet)}}
 int conflicting_nested_condition(void);
 #pragma omp end declare variant
+
+struct S0 {
+  // expected-error at +2 {{field 'foo' declared as a function}}
+  #pragma omp declare variant
+  void foo();
+};
+
+struct S1 {
+  // expected-error at +3 {{field 'bar' declared as a function}}
+  // expected-error at +2 {{expected ';' at end of declaration list}}
+  #pragma omp declare variant
+  void bar() {}
+};
Index: clang/test/OpenMP/declare_simd_messages.c
===================================================================
--- /dev/null
+++ clang/test/OpenMP/declare_simd_messages.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple=x86_64-pc-win32 -verify -fopenmp -x c -fms-extensions -Wno-pragma-pack %s
+
+struct S0 {
+  // expected-error at +2 {{field 'foo' declared as a function}}
+  #pragma omp declare simd
+  void foo();
+};
+
+struct S1 {
+  // expected-error at +3 {{field 'bar' declared as a function}}
+  // expected-error at +2 {{expected ';' at end of declaration list}}
+  #pragma omp declare simd
+  void bar() {}
+};
Index: clang/lib/Parse/ParseOpenMP.cpp
===================================================================
--- clang/lib/Parse/ParseOpenMP.cpp
+++ clang/lib/Parse/ParseOpenMP.cpp
@@ -2262,6 +2262,9 @@
   }
   case OMPD_declare_variant:
   case OMPD_declare_simd: {
+    // Only allowed in C++ mode
+    if (!getLangOpts().CPlusPlus && getCurScope()->isClassScope())
+      break;
     // The syntax is:
     // { #pragma omp declare {simd|variant} }
     // <function-declaration-or-definition>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122585.418575.patch
Type: text/x-patch
Size: 1905 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220328/45c6ae45/attachment.bin>


More information about the cfe-commits mailing list