Skip to content

Commit 46ffacc

Browse files
authored
[flang][OpenMP] Skip invalid conditional compilation sentinels (#126282)
In fixed form, an initial line can have an OpenMP conditional compilation sentinel only if columns 3 through 5 have only white space or numbers. Fixes #89560
1 parent e657b96 commit 46ffacc

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

flang/lib/Parser/prescan.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,21 @@ Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
14321432
}
14331433
*sp++ = ToLowerCaseLetter(*p);
14341434
}
1435+
// A fixed form OpenMP conditional compilation sentinel must satisfy the
1436+
// following criteria, for initial lines:
1437+
// - Columns 3 through 5 must have only white space or numbers.
1438+
// - Column 6 must be space or zero.
1439+
if (column == 3 && sentinel[0] == '$') {
1440+
const char *q{p};
1441+
for (int col{3}; col < 6; ++col, ++q) {
1442+
if (!IsSpaceOrTab(q) && !IsDecimalDigit(*q)) {
1443+
return std::nullopt;
1444+
}
1445+
}
1446+
if (*q != ' ' && *q != '0') {
1447+
return std::nullopt;
1448+
}
1449+
}
14351450
if (column == 6) {
14361451
if (*p == '0') {
14371452
++p;

flang/test/Parser/OpenMP/sentinels.f

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,34 @@ subroutine sub(a, b)
3939
C $ This is a comment line
4040
c $ his is a comment line
4141
* $ This is a comment line
42+
43+
! Test non-space/non-number char in columns 3-5, for initial lines.
44+
! CHECK-NOT: "comment"
45+
c$x PRINT *, "comment"
46+
c$ + PRINT *, "comment"
47+
c$ * PRINT *, "comment"
48+
49+
! Test non-space/non-number char in columns 3-5, for continuation lines.
50+
! CHECK: "msg1"
51+
! CHECK-NOT: "comment"
52+
c$ x PRINT *, "comment"
53+
c$1 & , "comment"
54+
c$ x & , "comment"
55+
c$ +& , "comment"
56+
57+
c$ PRINT *, "msg1"
58+
c$1 & , "comment"
59+
c$ x & , "comment"
60+
c$ +& , "comment"
61+
62+
! Test valid chars in initial and continuation lines.
63+
! CHECK: "msg2"
64+
! CHECK-SAME: "msg3"
65+
c$ 20 PRINT *, "msg2"
66+
c$ & , "msg3"
67+
68+
! CHECK: "msg4"
69+
! CHECK-SAME: "msg5"
70+
c$ 0PRINT *, "msg4",
71+
c$ + "msg5"
4272
end

0 commit comments

Comments
 (0)