Skip to content

Commit 8822006

Browse files
authored
[flang][OpenMP] Prescanning bug with !$ fixed form line continuation (#135416)
The logic for fixed form compiler directive line continuation has a hole that can apply continuation for !$ even if the next line does not begin with a fixed form comment character. Rearrange the nested if statements to enforce that requirement for all compiler directives.
1 parent 13b55ad commit 8822006

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

flang/lib/Parser/prescan.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,22 +1315,22 @@ const char *Prescanner::FixedFormContinuationLine(bool mightNeedSpace) {
13151315
tabInCurrentLine_ = false;
13161316
char col1{*nextLine_};
13171317
if (InCompilerDirective()) {
1318-
if (directiveSentinel_[0] == '$' && directiveSentinel_[1] == '\0') {
1318+
if (!IsFixedFormCommentChar(col1)) {
1319+
return nullptr;
1320+
} else if (directiveSentinel_[0] == '$' && directiveSentinel_[1] == '\0') {
13191321
// !$ OpenMP conditional compilation
13201322
if (preprocessingOnly_) {
13211323
// in -E mode, don't treat "!$ &" as a continuation
13221324
return nullptr;
1323-
} else if (IsFixedFormCommentChar(col1)) {
1324-
if (nextLine_[1] == '$') {
1325-
// accept but do not require a matching sentinel
1326-
if (!(nextLine_[2] == '&' || IsSpaceOrTab(&nextLine_[2]))) {
1327-
return nullptr;
1328-
}
1329-
} else {
1330-
return nullptr; // distinct directive
1325+
} else if (nextLine_[1] == '$') {
1326+
// accept but do not require a matching sentinel
1327+
if (!(nextLine_[2] == '&' || IsSpaceOrTab(&nextLine_[2]))) {
1328+
return nullptr;
13311329
}
1330+
} else {
1331+
return nullptr; // distinct directive
13321332
}
1333-
} else if (IsFixedFormCommentChar(col1)) {
1333+
} else { // all other directives
13341334
int j{1};
13351335
for (; j < 5; ++j) {
13361336
char ch{directiveSentinel_[j - 1]};
@@ -1345,8 +1345,6 @@ const char *Prescanner::FixedFormContinuationLine(bool mightNeedSpace) {
13451345
return nullptr;
13461346
}
13471347
}
1348-
} else {
1349-
return nullptr;
13501348
}
13511349
const char *col6{nextLine_ + 5};
13521350
if (*col6 != '\n' && *col6 != '0' && !IsSpaceOrTab(col6)) {

flang/test/Parser/OpenMP/bug518.f

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
! RUN: %flang_fc1 -E %s 2>&1 | FileCheck %s --strict-whitespace --check-prefix=CHECK-E
2+
! RUN: %flang_fc1 -fopenmp -fdebug-unparse %s 2>&1 | FileCheck %s --check-prefix=CHECK-OMP
3+
! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s --check-prefix=CHECK-NO-OMP
4+
5+
!$ thread = OMP_GET_MAX_THREADS()
6+
7+
!$omp parallel private(ia)
8+
!$ continue
9+
!$omp end parallel
10+
end
11+
12+
!CHECK-E:{{^}}!$ thread = OMP_GET_MAX_THREADS()
13+
!CHECK-E:{{^}}!$omp parallel private(ia)
14+
!CHECK-E:{{^}}!$ continue
15+
!CHECK-E:{{^}}!$omp end parallel
16+
17+
!CHECK-OMP:thread=omp_get_max_threads()
18+
!CHECK-OMP:!$OMP PARALLEL PRIVATE(ia)
19+
!CHECK-OMP: CONTINUE
20+
!CHECK-OMP:!$OMP END PARALLEL
21+
22+
!CHECK-NO-OMP-NOT:thread=
23+
!CHECK-NO-OMP-NOT:!$OMP
24+
!CHECK-NO-OMP:END PROGRAM

0 commit comments

Comments
 (0)