https://github.com/zahiraam created 
https://github.com/llvm/llvm-project/pull/203252

None

>From b925b2c3fa0f15b20898bd6456329eddfdb027ea Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <[email protected]>
Date: Wed, 20 May 2026 05:48:13 -0700
Subject: [PATCH 1/2] [OpenMP] Prevent parser infinite loop on unimplemented
 clauses

---
 clang/lib/Basic/OpenMPKinds.cpp               |  1 +
 clang/lib/Parse/ParseOpenMP.cpp               |  9 ++
 clang/lib/Sema/SemaOpenMP.cpp                 |  4 +
 .../OpenMP/unimplemented_clause_messages.cpp  | 93 +++++++++++++++++++
 4 files changed, 107 insertions(+)
 create mode 100644 clang/test/OpenMP/unimplemented_clause_messages.cpp

diff --git a/clang/lib/Basic/OpenMPKinds.cpp b/clang/lib/Basic/OpenMPKinds.cpp
index 287eb217ba458..675d86349c933 100644
--- a/clang/lib/Basic/OpenMPKinds.cpp
+++ b/clang/lib/Basic/OpenMPKinds.cpp
@@ -965,6 +965,7 @@ void clang::getOpenMPCaptureRegions(
     case OMPD_simd:
     case OMPD_single:
     case OMPD_target_data:
+    case OMPD_taskgraph:
     case OMPD_taskgroup:
     case OMPD_stripe:
       // These directives (when standalone) use OMPD_unknown as the region,
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 45a47ec797f01..ba3d3113700ff 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2388,6 +2388,8 @@ StmtResult Parser::ParseOpenMPExecutableDirective(
     ImplicitClauseAllowed = false;
     Actions.OpenMP().StartOpenMPClause(CKind);
     HasImplicitClause = false;
+    SourceLocation ClauseLoc = Tok.getLocation();
+
     OMPClause *Clause =
         ParseOpenMPClause(DKind, CKind, !SeenClauses[unsigned(CKind)]);
     SeenClauses[unsigned(CKind)] = true;
@@ -2398,6 +2400,13 @@ StmtResult Parser::ParseOpenMPExecutableDirective(
     if (Tok.is(tok::comma))
       ConsumeToken();
     Actions.OpenMP().EndOpenMPClause();
+
+    // If ParseOpenMPClause returned without consuming any tokens, skip
+    // to end to avoid an infinite loop.
+    if (Tok.getLocation() == ClauseLoc) {
+      skipUntilPragmaOpenMPEnd(DKind);
+      break;
+    }
   }
   // End location of the directive.
   EndLoc = Tok.getLocation();
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index d6f6bc919a31b..76b40a5039180 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -6778,6 +6778,10 @@ StmtResult SemaOpenMP::ActOnOpenMPExecutableDirective(
   case OMPD_begin_declare_variant:
   case OMPD_end_declare_variant:
     llvm_unreachable("OpenMP Directive is not allowed");
+  case OMPD_taskgraph:
+    Diag(StartLoc, diag::err_omp_unexpected_directive)
+        << 1 << getOpenMPDirectiveName(OMPD_taskgraph);
+    return StmtError();
   case OMPD_unknown:
   default:
     llvm_unreachable("Unknown OpenMP directive");
diff --git a/clang/test/OpenMP/unimplemented_clause_messages.cpp 
b/clang/test/OpenMP/unimplemented_clause_messages.cpp
new file mode 100644
index 0000000000000..172203ea5d040
--- /dev/null
+++ b/clang/test/OpenMP/unimplemented_clause_messages.cpp
@@ -0,0 +1,93 @@
+// RUN: %clang_cc1 -verify=expected,omp60 -fopenmp -fopenmp-version=60 %s
+// RUN: %clang_cc1 -verify=expected,omp51 -fopenmp -fopenmp-version=51 %s
+// RUN: %clang_cc1 -verify=expected,omp60 -fopenmp-simd -fopenmp-version=60 %s
+// RUN: %clang_cc1 -verify=expected,omp51 -fopenmp-simd -fopenmp-version=51 %s
+
+
+void test_induction_basic() {
+  int i;
+  // omp60-warning@+4{{extra tokens at the end of '#pragma omp parallel for' 
are ignored}}
+  // omp60-error@+3{{unexpected OpenMP clause 'induction' in directive 
'#pragma omp parallel for'}}
+  // omp51-warning@+2{{extra tokens at the end of '#pragma omp parallel for' 
are ignored}}
+  // omp51-error@+1{{unexpected OpenMP clause 'induction' in directive 
'#pragma omp parallel for'}}
+#pragma omp parallel for induction(i)
+  for (i = 0; i < 10; ++i)
+    ;
+}
+
+void test_apply() {
+  // omp60-warning@+4{{extra tokens at the end of '#pragma omp tile' are 
ignored}}
+  // omp60-error@+3{{unexpected OpenMP clause 'apply' in directive '#pragma 
omp tile'}}
+  // omp51-error@+2{{unexpected OpenMP clause 'apply' in directive '#pragma 
omp tile'}}
+  // omp51-warning@+1{{extra tokens at the end of '#pragma omp tile' are 
ignored}}
+#pragma omp tile sizes(10) apply(intratile: unroll)
+  for (int i = 0; i < 10; ++i)
+    ;
+}
+
+void test_empty_apply() {
+ // omp60-warning@+4{{extra tokens at the end of '#pragma omp tile' are 
ignored}}
+  // omp60-error@+3{{unexpected OpenMP clause 'apply' in directive '#pragma 
omp tile'}}
+  // omp51-error@+2{{unexpected OpenMP clause 'apply' in directive '#pragma 
omp tile'}}
+  // omp51-warning@+1{{extra tokens at the end of '#pragma omp tile' are 
ignored}}
+#pragma omp tile sizes(10) apply()
+  for (int i = 0; i < 10; ++i)
+    ;
+}
+
+void test_nested_apply()
+{
+  // omp60-error@+5{{unexpected OpenMP clause 'apply' in directive '#pragma 
omp tile'}}
+  // omp60-warning@+4{{extra tokens at the end of '#pragma omp tile' are 
ignored}}
+  //omp51-error@+3{{unexpected OpenMP clause 'apply' in directive '#pragma omp 
tile'}}
+  // omp51-warning@+2{{extra tokens at the end of '#pragma omp tile' are 
ignored}}
+#pragma omp tile sizes(10) \
+            apply(intratile: unroll partial(2) apply(reverse))
+  for (int i = 0; i < 100; ++i)
+    ;
+}
+
+void test_induction_with_following_clause() {
+  int i;
+  // omp60-warning@+4{{extra tokens at the end of '#pragma omp parallel for' 
are ignored}}
+  // omp60-error@+3{{unexpected OpenMP clause 'induction' in directive 
'#pragma omp parallel for'}}
+  // omp51-error@+2{{unexpected OpenMP clause 'induction' in directive 
'#pragma omp parallel for'}}
+  // omp51-warning@+1{{extra tokens at the end of '#pragma omp parallel for' 
are ignored}}
+#pragma omp parallel for induction(i) num_threads(4)
+  for (i = 0; i < 10; ++i)
+    ;
+}
+
+class Point {
+  float x, y, m;
+  char color;
+
+};
+
+void processPointsInLine() {
+  float separation;
+  // omp60-error@+4{{unexpected OpenMP clause 'induction' in directive 
'#pragma omp parallel for'}}
+  // omp60-warning@+3{{extra tokens at the end of '#pragma omp parallel for' 
are ignored}}
+  // omp51-error@+2{{unexpected OpenMP clause 'induction' in directive 
'#pragma omp parallel for'}}
+  // omp51-warning@+1{{extra tokens at the end of '#pragma omp parallel for' 
are ignored}}
+#pragma omp parallel for induction(step(Separation))
+  for (int i = 0; i < 10; ++i) {
+    ;
+  }
+}
+
+// Make sure test doesn't crash.
+void test_tasgraph()
+{
+  // omp60-error@+2{{unexpected OpenMP directive '#pragma omp taskgraph'}}
+  // omp51-error@+1{{unexpected OpenMP directive '#pragma omp taskgraph'}}
+#pragma omp taskgraph
+  for (int i = 0; i < 10; ++i)
+    ;
+}
+
+void test_implemented_clause() {
+#pragma omp tile sizes(10)
+  for (int i = 0; i < 10; ++i)
+    ;
+}

>From c5a58c9381dedf5ea55ecf048394bc704797c127 Mon Sep 17 00:00:00 2001
From: Ammarguellat <[email protected]>
Date: Thu, 11 Jun 2026 05:16:12 -0700
Subject: [PATCH 2/2] [OpenMP] Pointer dereference collapse loop

---
 clang/lib/Sema/SemaOpenMP.cpp                   |  2 +-
 clang/test/OpenMP/collapse_extern_ref_crash.cpp | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/OpenMP/collapse_extern_ref_crash.cpp

diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 113c8f3cb3016..79a0c396f787e 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -8044,7 +8044,7 @@ class ForSubExprChecker : public 
DynamicRecursiveASTVisitor {
     VarDecl *V = VD->getPotentiallyDecomposedVarDecl();
     if (V->getType()->isReferenceType()) {
       VarDecl *VD = V->getDefinition();
-      if (VD->hasInit()) {
+      if (VD && VD->hasInit()) {
         Expr *I = VD->getInit();
         DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(I);
         if (!DRE)
diff --git a/clang/test/OpenMP/collapse_extern_ref_crash.cpp 
b/clang/test/OpenMP/collapse_extern_ref_crash.cpp
new file mode 100644
index 0000000000000..4cc1a56115c73
--- /dev/null
+++ b/clang/test/OpenMP/collapse_extern_ref_crash.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=51 %s
+// RUN: %clang_cc1 -verify -fopenmp-simd -fopenmp-version=51 %s
+
+// Verify no crash when collapsing a loop nest where the induction variable
+// is an extern reference type. PR/issue: null dereference in getInitLCDecl
+// when VarDecl::getDefinition() returns nullptr.
+
+extern int &dim;
+auto test() {
+  // expected-error@+1 {{expected-error for malformed collapse}}
+#pragma omp parallel for collapse(2)
+  for (int i = 0; i < dim; ++i) {
+    for (i = 0; i < 10; i++) {
+      int dummy;
+    }
+  }
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to