[clang] [Clang][OpenMP][NFC] Add test for `class-type` data members as loop counters (PR #228831)
Akash Manna via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 3 23:02:45 PDT 2026
https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/228831
Fixes #140243
An OpenMP loop whose init assigns to a data member of class type, like `for (a = x; ...)` with `I<int> a`, crashed in `OpenMPIterationSpaceChecker::checkAndSetInit`. Such an assignment is an `operator=` call, so it goes through the `CXXOperatorCallExpr` branch, and two of the `setLCDeclAndLB` calls there took the bound from `BO`, the `BinaryOperator` cast that had already failed and is null at that point. The invalid code in the report is not needed: a valid loop over an iterator-typed data member crashed the same way.
#203252 replaced those `BO->getRHS()` uses with `CE->getArg(1)` as part of another fix, so the crash is gone on trunk and no source change is needed. This PR only adds a regression test so the issue can be closed.
>From d6f37f2b959e0735786c3faf0711448c0ad3a719 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Sun, 4 Oct 2026 11:30:47 +0530
Subject: [PATCH] [Clang][OpenMP][NFC] Add test for class-type data members as
loop counters
A loop directive whose init assigns to a data member of class type
crashed in OpenMPIterationSpaceChecker::checkAndSetInit: the
CXXOperatorCallExpr branch read the bound from the null BinaryOperator
cast. #203252 already fixed this by using CE->getArg(1), so only the
regression test is added here.
Fixes #140243
---
.../OpenMP/parallel_for_loop_messages.cpp | 39 +++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git a/clang/test/OpenMP/parallel_for_loop_messages.cpp b/clang/test/OpenMP/parallel_for_loop_messages.cpp
index 0b755f7411748..644dfed66cc79 100644
--- a/clang/test/OpenMP/parallel_for_loop_messages.cpp
+++ b/clang/test/OpenMP/parallel_for_loop_messages.cpp
@@ -629,3 +629,42 @@ void test_loop_firstprivate_lastprivate() {
for (int i = 0; i < 16; ++i)
;
}
+
+namespace GH140243 {
+template <typename T>
+class I { };
+struct R { R () {}; ~R () {}; I<int> r; };
+struct T { T () {}; virtual ~T () {}; I<int> t; };
+// expected-note at +1 {{A defined here}}
+struct A : public R, virtual public T { A () {} I<int> a; void m1 (const I<int> &, const I<int> &); };
+
+// expected-error at +1 {{out-of-line definition of 'm1' does not match any declaration in 'GH140243::A'}}
+void A::m1 (const I<int> &x)
+{
+ int w = 0;
+// expected-error at +5 {{invalid operands to binary expression ('I<int>' and 'int')}}
+// expected-error at +4 {{cannot increment value of type 'I<int>'}}
+// expected-error at +3 {{condition of OpenMP for loop must be a relational comparison}}
+// expected-error at +2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'a'}}
+ #pragma omp parallel for reduction(|:w)
+ for (a = x; A::a < 10; a++)
+ w |= (1 << *A::a); // expected-error {{indirection requires pointer operand ('I<int>' invalid)}}
+}
+
+struct B {
+ GoodIter it;
+ void m1(GoodIter begin, GoodIter end);
+};
+
+void B::m1(GoodIter begin, GoodIter end) {
+#pragma omp parallel for
+ for (it = begin; it < end; ++it)
+ ;
+#pragma omp parallel for
+ for (this->it = begin; B::it < end; ++B::it)
+ ;
+#pragma omp parallel for private(it)
+ for (it = begin; it < end; ++it)
+ ;
+}
+}
More information about the cfe-commits
mailing list