[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 05:20:54 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/2] [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/2] 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();



More information about the cfe-commits mailing list