Skip to content

Commit 0eb8a92

Browse files
authored
[OpenMP] Fix tentative parsing crash with metadirective (#139901)
There were two crashes that have the same root cause: not correctly handling unexpected tokens. In one case, we were failing to return early which caused us to parse a paren as a regular token instead of a special token, causing an assertion. The other case was failing to commit or revert the tentative parse action when not getting a paren when one was expected. Fixes #139665
1 parent c5229e9 commit 0eb8a92

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,10 @@ OpenMP Support
946946
- Fixed a crashing bug with a malformed ``cancel`` directive. (#GH139360)
947947
- Fixed a crashing bug with ``omp distribute dist_schedule`` if the argument to
948948
``dist_schedule`` was not strictly positive. (#GH139266)
949+
- Fixed two crashing bugs with a malformed ``metadirective`` directive. One was
950+
a crash if the next token after ``metadirective`` was a paren, bracket, or
951+
brace. The other was if the next token after the meta directive was not an
952+
open parenthesis. (#GH139665)
949953

950954
Improvements
951955
^^^^^^^^^^^^

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,8 +2666,12 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
26662666
? OMPC_unknown
26672667
: getOpenMPClauseKind(PP.getSpelling(Tok));
26682668
// Check if the clause is unrecognized.
2669-
if (CKind == OMPC_unknown)
2669+
if (CKind == OMPC_unknown) {
26702670
Diag(Tok, diag::err_omp_expected_clause) << "metadirective";
2671+
TPA.Revert();
2672+
SkipUntil(tok::annot_pragma_openmp_end);
2673+
return Directive;
2674+
}
26712675
if (getLangOpts().OpenMP < 52 && CKind == OMPC_otherwise)
26722676
Diag(Tok, diag::err_omp_unexpected_clause)
26732677
<< getOpenMPClauseName(CKind) << "metadirective";
@@ -2678,8 +2682,11 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
26782682

26792683
// Parse '('.
26802684
if (T.expectAndConsume(diag::err_expected_lparen_after,
2681-
getOpenMPClauseName(CKind).data()))
2685+
getOpenMPClauseName(CKind).data())) {
2686+
TPA.Revert();
2687+
SkipUntil(tok::annot_pragma_openmp_end);
26822688
return Directive;
2689+
}
26832690

26842691
OMPTraitInfo &TI = Actions.getASTContext().getNewOMPTraitInfo();
26852692
if (CKind == OMPC_when) {

clang/test/OpenMP/metadirective_messages.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ void foo() {
4949
;
5050
#endif
5151
}
52+
53+
namespace GH139665 {
54+
void f(){
55+
#pragma omp metadirective( // expected-error {{expected at least one clause on '#pragma omp metadirective' directive}}
56+
}
57+
58+
void g() {
59+
#pragma omp metadirective align // expected-error {{expected '(' after 'align'}}
60+
}
61+
} // namespace GH139665

0 commit comments

Comments
 (0)