[clang] [Clang][OpenMP] Fixed an assertion when `#pragma omp declare simd` or `#pragma omp declare variant` is followed by another OpenMP declarative directive containing a qualified identifier (PR #217875)

Shengxin Pei via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 21 10:19:35 PDT 2026


https://github.com/TPPPP72 updated https://github.com/llvm/llvm-project/pull/217875

>From 170ed10ff77e5e1350c3365c635b1b1fdf5b7596 Mon Sep 17 00:00:00 2001
From: Shengxin Pei <TPPPP72 at outlook.com>
Date: Fri, 21 Aug 2026 18:23:59 +0800
Subject: [PATCH 1/4] [Clang][OpenMP] Fixed an assertion when `#pragma omp
 declare simd` or `#pragma omp declare variant` is followed by another OpenMP
 declarative directive containing a qualified identifier

---
 clang/docs/ReleaseNotes.md         |  1 +
 clang/lib/Parse/ParseOpenMP.cpp    | 16 ++++++++++++++++
 clang/test/SemaOpenMP/gh217204.cpp | 11 +++++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 clang/test/SemaOpenMP/gh217204.cpp

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 8c9467ca7b742..2f70f2aa94c1f 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -438,6 +438,7 @@ features cannot lower the translation-unit ABI level;
 - Fixed a bug where preprocessor directives following comments were not correctly recognized when using -C. (#GH48361)
 - Fixed a crash when declaring a member template within a local class inside an OpenMP region. (#GH216052)
 - Fixed a bug where repeated #imports of modular headers in non-modular compilation were translated to #pragma clang module import. (#GH216924)
+- Fixed an assertion when `#pragma omp declare simd` or `#pragma omp declare variant` is followed by another OpenMP declarative directive containing a qualified identifier. (#GH217204)
 
 #### Bug Fixes to Compiler Builtins
 
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 30b6c64e69f4c..81084a8830c9d 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2214,6 +2214,22 @@ Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
           << (DKind == OMPD_declare_simd ? 0 : 1);
       return DeclGroupPtrTy();
     }
+
+    DeclGroupRef DG = Ptr.get();
+    bool HasNewDecl = false;
+    SourceManager &SM = PP.getSourceManager();
+    for (const Decl *D : DG) {
+      if (SM.isBeforeInTranslationUnit(Loc, D->getBeginLoc())) {
+        HasNewDecl = true;
+        break;
+      }
+    }
+    if (!HasNewDecl) {
+      Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
+          << (DKind == OMPD_declare_simd ? 0 : 1);
+      return DeclGroupPtrTy();
+    }
+
     if (DKind == OMPD_declare_simd)
       return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
     assert(DKind == OMPD_declare_variant &&
diff --git a/clang/test/SemaOpenMP/gh217204.cpp b/clang/test/SemaOpenMP/gh217204.cpp
new file mode 100644
index 0000000000000..4063f38f85a0d
--- /dev/null
+++ b/clang/test/SemaOpenMP/gh217204.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fopenmp -fsyntax-only -verify %s
+
+namespace N {
+  void foo();
+}
+
+#pragma omp declare simd // expected-error {{function declaration is expected after 'declare simd' directive}}
+#pragma omp declare target to(N::foo)
+
+#pragma omp declare variant // expected-error {{function declaration is expected after 'declare variant' directive}}
+#pragma omp declare target to(N::foo)

>From 35fa3b98fe8af6537f8f920c45a1bc4d36b275b3 Mon Sep 17 00:00:00 2001
From: Shengxin Pei <TPPPP72 at outlook.com>
Date: Fri, 21 Aug 2026 20:20:37 +0800
Subject: [PATCH 2/4] use lambda

---
 clang/lib/Parse/ParseOpenMP.cpp | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 81084a8830c9d..37a36acfc0fbb 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2215,16 +2215,17 @@ Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
       return DeclGroupPtrTy();
     }
 
-    DeclGroupRef DG = Ptr.get();
-    bool HasNewDecl = false;
-    SourceManager &SM = PP.getSourceManager();
-    for (const Decl *D : DG) {
-      if (SM.isBeforeInTranslationUnit(Loc, D->getBeginLoc())) {
-        HasNewDecl = true;
-        break;
+    auto HasNewDecl = [&] {
+      DeclGroupRef DG = Ptr.get();
+      SourceManager &SM = PP.getSourceManager();
+      for (const Decl *D : DG) {
+        if (SM.isBeforeInTranslationUnit(Loc, D->getBeginLoc()))
+          return true;
       }
-    }
-    if (!HasNewDecl) {
+      return false;
+    };
+
+    if (!HasNewDecl()) {
       Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
           << (DKind == OMPD_declare_simd ? 0 : 1);
       return DeclGroupPtrTy();

>From 291bc9eb807dc03dcd0d0f2c940b1db963e607bd Mon Sep 17 00:00:00 2001
From: Shengxin Pei <TPPPP72 at outlook.com>
Date: Sat, 22 Aug 2026 01:11:33 +0800
Subject: [PATCH 3/4] use any_of

---
 clang/lib/Parse/ParseOpenMP.cpp | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 37a36acfc0fbb..86aba1ca95341 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2215,17 +2215,11 @@ Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
       return DeclGroupPtrTy();
     }
 
-    auto HasNewDecl = [&] {
-      DeclGroupRef DG = Ptr.get();
-      SourceManager &SM = PP.getSourceManager();
-      for (const Decl *D : DG) {
-        if (SM.isBeforeInTranslationUnit(Loc, D->getBeginLoc()))
-          return true;
-      }
-      return false;
-    };
-
-    if (!HasNewDecl()) {
+    DeclGroupRef DG = Ptr.get();
+    SourceManager &SM = PP.getSourceManager();
+    if (!llvm::any_of(DG, [&](const Decl *D) {
+          return SM.isBeforeInTranslationUnit(Loc, D->getBeginLoc());
+        })) {
       Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
           << (DKind == OMPD_declare_simd ? 0 : 1);
       return DeclGroupPtrTy();

>From de7cfe2b1a662564af864cea0ebe04d140c45669 Mon Sep 17 00:00:00 2001
From: Shengxin Pei <TPPPP72 at outlook.com>
Date: Sat, 22 Aug 2026 01:19:20 +0800
Subject: [PATCH 4/4] use none_of

Co-authored-by: Alexey Bataev <a.bataev at outlook.com>
---
 clang/lib/Parse/ParseOpenMP.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 86aba1ca95341..651c2802e48c4 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2217,8 +2217,8 @@ Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
 
     DeclGroupRef DG = Ptr.get();
     SourceManager &SM = PP.getSourceManager();
-    if (!llvm::any_of(DG, [&](const Decl *D) {
-          return SM.isBeforeInTranslationUnit(Loc, D->getBeginLoc());
+    if (llvm::none_of(DG, [&](const Decl *D) {
+          return !SM.isBeforeInTranslationUnit(Loc, D->getBeginLoc());
         })) {
       Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
           << (DKind == OMPD_declare_simd ? 0 : 1);



More information about the cfe-commits mailing list