Skip to content

[OpenMP] Fix crash on invalid with cancel directive #139577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025

Conversation

AaronBallman
Copy link
Collaborator

If the next token after 'cancel' is a special token, we would trigger an assertion. We should be consuming any token, same as elsewhere in the function.

Note, we could check for an unknown directive and do different error recovery, but that caused too many behavioral changes for other tests in the form of "unexpected tokens ignored" diagnostics that didn't seem like an improvement for the test cases.

Fixes #139360

If the next token after 'cancel' is a special token, we would trigger
an assertion. We should be consuming any token, same as elsewhere in
the function.

Note, we could check for an unknown directive and do different error
recovery, but that caused too many behavioral changes for other tests
in the form of "unexpected tokens ignored" diagnostics that didn't seem
like an improvement for the test cases.

Fixes llvm#139360
@AaronBallman AaronBallman added clang:frontend Language frontend issues, e.g. anything involving "Sema" crash-on-invalid ABI Application Binary Interface clang:openmp OpenMP related changes to Clang clang Clang issues not falling into any other category and removed ABI Application Binary Interface labels May 12, 2025
@llvmbot
Copy link
Member

llvmbot commented May 12, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

If the next token after 'cancel' is a special token, we would trigger an assertion. We should be consuming any token, same as elsewhere in the function.

Note, we could check for an unknown directive and do different error recovery, but that caused too many behavioral changes for other tests in the form of "unexpected tokens ignored" diagnostics that didn't seem like an improvement for the test cases.

Fixes #139360


Full diff: https://github.com/llvm/llvm-project/pull/139577.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Parse/ParseOpenMP.cpp (+1-1)
  • (modified) clang/test/OpenMP/cancel_messages.cpp (+5)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 257c4696de403..14d526a71f458 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -922,6 +922,7 @@ OpenMP Support
   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 a malformed ``cancel`` directive. (#GH139360)
 - 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/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 2508bf5795900..234c26e979ded 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2487,7 +2487,7 @@ StmtResult Parser::ParseOpenMPExecutableDirective(
   } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
     CancelRegion = parseOpenMPDirectiveKind(*this);
     if (Tok.isNot(tok::annot_pragma_openmp_end))
-      ConsumeToken();
+      ConsumeAnyToken();
   }
 
   if (isOpenMPLoopDirective(DKind))
diff --git a/clang/test/OpenMP/cancel_messages.cpp b/clang/test/OpenMP/cancel_messages.cpp
index 0c96beecc04b0..1391578cb0977 100644
--- a/clang/test/OpenMP/cancel_messages.cpp
+++ b/clang/test/OpenMP/cancel_messages.cpp
@@ -93,3 +93,8 @@ label1 : {
   return 0;
 }
 
+namespace GH139360 {
+void f(){
+#pragma omp cancel( // expected-error {{one of 'for', 'parallel', 'sections' or 'taskgroup' is expected}}
+}
+} // namesapce GH139360

@AaronBallman AaronBallman merged commit 70ca3f4 into llvm:main May 12, 2025
15 of 17 checks passed
@AaronBallman AaronBallman deleted the aballman-gh139360 branch May 12, 2025 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:openmp OpenMP related changes to Clang clang Clang issues not falling into any other category crash-on-invalid
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[OpenMP] Assertion `!isTokenSpecial() && "Should consume special tokens with Consume*Token"' failed.
3 participants