Skip to content

Commit 6a7044a

Browse files
authored
[flang] Improve OpenACC SELF clause parser (#135883)
The current parser can fail on "self(x * 2)" by recognizing just "x" as a one-element list of object names and then failing at a higher level because it never reached the right parenthesis. Add lookahead checks and error recovery. Fixes #135810.
1 parent b4ff435 commit 6a7044a

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

flang/lib/Parser/openacc-parsers.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@ TYPE_PARSER(construct<AccDefaultClause>(
126126
// SELF clause is either a simple optional condition for compute construct
127127
// or a synonym of the HOST clause for the update directive 2.14.4 holding
128128
// an object list.
129-
TYPE_PARSER(construct<AccSelfClause>(Parser<AccObjectList>{}) ||
130-
construct<AccSelfClause>(scalarLogicalExpr))
129+
TYPE_PARSER(
130+
construct<AccSelfClause>(Parser<AccObjectList>{}) / lookAhead(")"_tok) ||
131+
construct<AccSelfClause>(scalarLogicalExpr / lookAhead(")"_tok)) ||
132+
construct<AccSelfClause>(
133+
recovery(fail<std::optional<ScalarLogicalExpr>>(
134+
"logical expression or object list expected"_err_en_US),
135+
SkipTo<')'>{} >> pure<std::optional<ScalarLogicalExpr>>())))
131136

132137
// Modifier for copyin, copyout, cache and create
133138
TYPE_PARSER(construct<AccDataModifier>(
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
! RUN: %python %S/../test_errors.py %s %flang -fopenacc
2+
integer function square(x, y)
3+
implicit none
4+
integer, intent(in) :: x, y
5+
!$acc parallel self(x * 2 > x) ! ok
6+
!$acc end parallel
7+
!ERROR: Must have LOGICAL type, but is INTEGER(4)
8+
!$acc parallel self(x * 2)
9+
!$acc end parallel
10+
!ERROR: SELF clause on the PARALLEL directive only accepts optional scalar logical expression
11+
!$acc parallel self(x, y)
12+
!$acc end parallel
13+
square = x * x
14+
end function square
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
! RUN: %python %S/../test_errors.py %s %flang -fopenacc
2+
integer function square(x)
3+
implicit none
4+
integer, intent(in) :: x
5+
!ERROR: logical expression or object list expected
6+
!$acc parallel self(,)
7+
!$acc end parallel
8+
!ERROR: logical expression or object list expected
9+
!$acc parallel self(.true., )
10+
!$acc end parallel
11+
square = x * x
12+
end function square

0 commit comments

Comments
 (0)