Skip to content

Commit 77cd34e

Browse files
authored
[flang] Treat conditional comments as comments (llvm#165881)
An OpenMP, OpenACC, or CUDA conditional line should be treated as a comment when that's what its payload contains, not as a conditional source line that will confuse the parser when it is indeed just a comment.
1 parent 2abcb19 commit 77cd34e

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

flang/lib/Parser/prescan.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,17 @@ Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
16421642
// This is a Continuation line, not an initial directive line.
16431643
return std::nullopt;
16441644
}
1645+
++column, ++p;
1646+
}
1647+
if (isOpenMPConditional) {
1648+
for (; column <= fixedFormColumnLimit_; ++column, ++p) {
1649+
if (IsSpaceOrTab(p)) {
1650+
} else if (*p == '!') {
1651+
return std::nullopt; // !$ ! is a comment, not a directive
1652+
} else {
1653+
break;
1654+
}
1655+
}
16451656
}
16461657
if (const char *ss{IsCompilerDirectiveSentinel(
16471658
sentinel, static_cast<std::size_t>(sp - sentinel))}) {
@@ -1657,8 +1668,17 @@ Prescanner::IsFreeFormCompilerDirectiveLine(const char *start) const {
16571668
p && *p++ == '!') {
16581669
if (auto maybePair{IsCompilerDirectiveSentinel(p)}) {
16591670
auto offset{static_cast<std::size_t>(p - start - 1)};
1660-
return {LineClassification{LineClassification::Kind::CompilerDirective,
1661-
offset, maybePair->first}};
1671+
const char *sentinel{maybePair->first};
1672+
if ((sentinel[0] == '$' && sentinel[1] == '\0') || sentinel[1] == '@') {
1673+
if (const char *comment{IsFreeFormComment(maybePair->second)}) {
1674+
if (*comment == '!') {
1675+
// Conditional line comment - treat as comment
1676+
return std::nullopt;
1677+
}
1678+
}
1679+
}
1680+
return {LineClassification{
1681+
LineClassification::Kind::CompilerDirective, offset, sentinel}};
16621682
}
16631683
}
16641684
return std::nullopt;

flang/test/Preprocessing/bug136845.F

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*$1 continue
1919
end
2020

21-
!PREPRO:!$ &
2221
!PREPRO: continue
2322
!PREPRO: k=0
2423
!PREPRO: k=0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
!RUN: %flang_fc1 -fopenmp -fdebug-unparse %s 2>&1 | FileCheck %s
2+
!CHECK: END
3+
!CHECK-NOT: error:
4+
end
5+
c$ !
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
!RUN: %flang_fc1 -fopenmp -fdebug-unparse %s 2>&1 | FileCheck %s
2+
!CHECK: END
3+
!CHECK-NOT: error:
4+
end
5+
!$ !

0 commit comments

Comments
 (0)