[clang] Pointer dereference collapse loop (PR #202990)
Zahira Ammarguellat via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 10 12:57:28 PDT 2026
https://github.com/zahiraam updated https://github.com/llvm/llvm-project/pull/202990
>From b925b2c3fa0f15b20898bd6456329eddfdb027ea Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
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 at +4{{extra tokens at the end of '#pragma omp parallel for' are ignored}}
+ // omp60-error at +3{{unexpected OpenMP clause 'induction' in directive '#pragma omp parallel for'}}
+ // omp51-warning at +2{{extra tokens at the end of '#pragma omp parallel for' are ignored}}
+ // omp51-error at +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 at +4{{extra tokens at the end of '#pragma omp tile' are ignored}}
+ // omp60-error at +3{{unexpected OpenMP clause 'apply' in directive '#pragma omp tile'}}
+ // omp51-error at +2{{unexpected OpenMP clause 'apply' in directive '#pragma omp tile'}}
+ // omp51-warning at +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 at +4{{extra tokens at the end of '#pragma omp tile' are ignored}}
+ // omp60-error at +3{{unexpected OpenMP clause 'apply' in directive '#pragma omp tile'}}
+ // omp51-error at +2{{unexpected OpenMP clause 'apply' in directive '#pragma omp tile'}}
+ // omp51-warning at +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 at +5{{unexpected OpenMP clause 'apply' in directive '#pragma omp tile'}}
+ // omp60-warning at +4{{extra tokens at the end of '#pragma omp tile' are ignored}}
+ //omp51-error at +3{{unexpected OpenMP clause 'apply' in directive '#pragma omp tile'}}
+ // omp51-warning at +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 at +4{{extra tokens at the end of '#pragma omp parallel for' are ignored}}
+ // omp60-error at +3{{unexpected OpenMP clause 'induction' in directive '#pragma omp parallel for'}}
+ // omp51-error at +2{{unexpected OpenMP clause 'induction' in directive '#pragma omp parallel for'}}
+ // omp51-warning at +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 at +4{{unexpected OpenMP clause 'induction' in directive '#pragma omp parallel for'}}
+ // omp60-warning at +3{{extra tokens at the end of '#pragma omp parallel for' are ignored}}
+ // omp51-error at +2{{unexpected OpenMP clause 'induction' in directive '#pragma omp parallel for'}}
+ // omp51-warning at +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 at +2{{unexpected OpenMP directive '#pragma omp taskgraph'}}
+ // omp51-error at +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 9e9d2d13480afe64cba167631f00355ff5a8d138 Mon Sep 17 00:00:00 2001
From: Ammarguellat <zahira.ammarguellat at intel.com>
Date: Wed, 10 Jun 2026 07:15:39 -0700
Subject: [PATCH 2/2] [OpenMP] Fix null pointer dereference in OpenMP loop
collapse
---
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 76b40a5039180..635676b03c06d 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..e4b24b93c425c
--- /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
+
+// expected-no-diagnostics
+
+// Verify there is no crash when collapsing a loop nest where the induction
+// variable is an extern reference type.
+
+extern int &dim;
+auto test() {
+#pragma omp parallel for collapse(2)
+ for (int i = 0; i < dim; ++i) {
+ for (i = 0; i < 10; i++) {
+ int dummy;
+ }
+ }
+}
More information about the cfe-commits
mailing list