[flang] Treat conditional comments as comments (#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.
This commit is contained in:
Peter Klausler 2025-10-31 10:28:04 -07:00 committed by GitHub
parent 2abcb19af2
commit 77cd34eedf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 3 deletions

View file

@ -1642,6 +1642,17 @@ Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
// This is a Continuation line, not an initial directive line.
return std::nullopt;
}
++column, ++p;
}
if (isOpenMPConditional) {
for (; column <= fixedFormColumnLimit_; ++column, ++p) {
if (IsSpaceOrTab(p)) {
} else if (*p == '!') {
return std::nullopt; // !$ ! is a comment, not a directive
} else {
break;
}
}
}
if (const char *ss{IsCompilerDirectiveSentinel(
sentinel, static_cast<std::size_t>(sp - sentinel))}) {
@ -1657,8 +1668,17 @@ Prescanner::IsFreeFormCompilerDirectiveLine(const char *start) const {
p && *p++ == '!') {
if (auto maybePair{IsCompilerDirectiveSentinel(p)}) {
auto offset{static_cast<std::size_t>(p - start - 1)};
return {LineClassification{LineClassification::Kind::CompilerDirective,
offset, maybePair->first}};
const char *sentinel{maybePair->first};
if ((sentinel[0] == '$' && sentinel[1] == '\0') || sentinel[1] == '@') {
if (const char *comment{IsFreeFormComment(maybePair->second)}) {
if (*comment == '!') {
// Conditional line comment - treat as comment
return std::nullopt;
}
}
}
return {LineClassification{
LineClassification::Kind::CompilerDirective, offset, sentinel}};
}
}
return std::nullopt;

View file

@ -18,7 +18,6 @@ c$ !1 B
*$1 continue
end
!PREPRO:!$ &
!PREPRO: continue
!PREPRO: k=0
!PREPRO: k=0

View file

@ -0,0 +1,5 @@
!RUN: %flang_fc1 -fopenmp -fdebug-unparse %s 2>&1 | FileCheck %s
!CHECK: END
!CHECK-NOT: error:
end
c$ !

View file

@ -0,0 +1,5 @@
!RUN: %flang_fc1 -fopenmp -fdebug-unparse %s 2>&1 | FileCheck %s
!CHECK: END
!CHECK-NOT: error:
end
!$ !