Author: Aaron Ballman
Date: 2025-05-12T08:04:22-04:00
New Revision: d5974524de6d7bccb95a1b3f074d1b6b76b60497

URL: 
https://github.com/llvm/llvm-project/commit/d5974524de6d7bccb95a1b3f074d1b6b76b60497
DIFF: 
https://github.com/llvm/llvm-project/commit/d5974524de6d7bccb95a1b3f074d1b6b76b60497.diff

LOG: [OpenMP] Fix crash with invalid argument to simd collapse (#139313)

Same as with other recent crash fixes, this is checking whether the
argument expression contains errors or not.

Fixes #138493

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaOpenMP.cpp
    clang/test/OpenMP/simd_collapse_messages.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e362ec595a3bb..257c4696de403 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -920,6 +920,8 @@ OpenMP Support
   ``partial`` was an invalid expression. (#GH139267)
 - Fixed a crashing bug with ``omp tile sizes`` if the argument to ``sizes`` was
   an invalid expression. (#GH139073)
+- Fixed a crashing bug with ``omp simd collapse`` if the argument to
+  ``collapse`` was an invalid expression. (#GH138493)
 - Fixed a crashing bug with ``omp distribute dist_schedule`` if the argument to
   ``dist_schedule`` was not strictly positive. (#GH139266)
 

diff  --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 15d568fcb9cc4..9d02594afd551 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -9648,6 +9648,13 @@ checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr 
*CollapseLoopCountExpr,
                 DSAStackTy &DSA,
                 SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA,
                 OMPLoopBasedDirective::HelperExprs &Built) {
+  // If either of the loop expressions exist and contain errors, we bail out
+  // early because diagnostics have already been emitted and we can't reliably
+  // check more about the loop.
+  if ((CollapseLoopCountExpr && CollapseLoopCountExpr->containsErrors()) ||
+      (OrderedLoopCountExpr && OrderedLoopCountExpr->containsErrors()))
+    return 0;
+
   unsigned NestedLoopCount = 1;
   bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) &&
                                     
!isOpenMPLoopTransformationDirective(DKind);

diff  --git a/clang/test/OpenMP/simd_collapse_messages.cpp 
b/clang/test/OpenMP/simd_collapse_messages.cpp
index 1ce3bef3535ce..d74033dcddc8d 100644
--- a/clang/test/OpenMP/simd_collapse_messages.cpp
+++ b/clang/test/OpenMP/simd_collapse_messages.cpp
@@ -97,3 +97,12 @@ int main(int argc, char **argv) {
   return tmain<int, char, 1, 0>(argc, argv);
 }
 
+namespace GH138493 {
+void f(void) {
+  // This would previously crash when processing an invalid expression as an
+  // argument to collapse.
+#pragma omp simd collapse(a) // expected-error {{use of undeclared identifier 
'a'}}
+  for (int i = 0; i < 10; i++)
+    ;
+}
+} // namespace GH138493


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to