Skip to content

[flang] Accept a compiler directive sentinel after a semicolon #96966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ bool Prescanner::MustSkipToEndOfLine() const {
if (inFixedForm_ && column_ > fixedFormColumnLimit_ && !tabInCurrentLine_) {
return true; // skip over ignored columns in right margin (73:80)
} else if (*at_ == '!' && !inCharLiteral_) {
return true; // inline comment goes to end of source line
return !IsCompilerDirectiveSentinel(at_);
} else {
return false;
}
Expand Down Expand Up @@ -1345,32 +1345,12 @@ Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {

std::optional<Prescanner::LineClassification>
Prescanner::IsFreeFormCompilerDirectiveLine(const char *start) const {
char sentinel[8];
const char *p{SkipWhiteSpace(start)};
if (*p++ != '!') {
return std::nullopt;
}
for (std::size_t j{0}; j + 1 < sizeof sentinel; ++p, ++j) {
if (*p == '\n') {
break;
}
if (*p == ' ' || *p == '\t' || *p == '&') {
if (j == 0) {
break;
}
sentinel[j] = '\0';
p = SkipWhiteSpace(p + 1);
if (*p == '!') {
break;
}
if (const char *sp{IsCompilerDirectiveSentinel(sentinel, j)}) {
std::size_t offset = p - start;
return {LineClassification{
LineClassification::Kind::CompilerDirective, offset, sp}};
}
break;
if (const char *p{SkipWhiteSpace(start)}; p && *p++ == '!') {
if (auto maybePair{IsCompilerDirectiveSentinel(p)}) {
auto offset{static_cast<std::size_t>(maybePair->second - start)};
return {LineClassification{LineClassification::Kind::CompilerDirective,
offset, maybePair->first}};
}
sentinel[j] = ToLowerCaseLetter(*p);
}
return std::nullopt;
}
Expand Down Expand Up @@ -1415,6 +1395,28 @@ const char *Prescanner::IsCompilerDirectiveSentinel(CharBlock token) const {
return end > p && IsCompilerDirectiveSentinel(p, end - p) ? p : nullptr;
}

std::optional<std::pair<const char *, const char *>>
Prescanner::IsCompilerDirectiveSentinel(const char *p) const {
char sentinel[8];
for (std::size_t j{0}; j + 1 < sizeof sentinel && *p != '\n'; ++p, ++j) {
if (*p == ' ' || *p == '\t' || *p == '&') {
if (j > 0) {
sentinel[j] = '\0';
p = SkipWhiteSpace(p + 1);
if (*p != '!') {
if (const char *sp{IsCompilerDirectiveSentinel(sentinel, j)}) {
return std::make_pair(sp, p);
}
}
}
break;
} else {
sentinel[j] = ToLowerCaseLetter(*p);
}
}
return std::nullopt;
}

constexpr bool IsDirective(const char *match, const char *dir) {
for (; *match; ++match) {
if (*match != ToLowerCaseLetter(*dir++)) {
Expand Down
3 changes: 3 additions & 0 deletions flang/lib/Parser/prescan.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class Prescanner {

const char *IsCompilerDirectiveSentinel(const char *, std::size_t) const;
const char *IsCompilerDirectiveSentinel(CharBlock) const;
// 'first' is the sentinel, 'second' is beginning of payload
std::optional<std::pair<const char *, const char *>>
IsCompilerDirectiveSentinel(const char *p) const;

template <typename... A> Message &Say(A &&...a) {
return messages_.Say(std::forward<A>(a)...);
Expand Down
11 changes: 4 additions & 7 deletions flang/lib/Parser/token-sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,17 @@ TokenSequence &TokenSequence::ClipComment(
if (std::size_t blanks{tok.CountLeadingBlanks()};
blanks < tok.size() && tok[blanks] == '!') {
// Retain active compiler directive sentinels (e.g. "!dir$")
for (std::size_t k{j + 1}; k < tokens && tok.size() < blanks + 5; ++k) {
for (std::size_t k{j + 1}; k < tokens && tok.size() <= blanks + 5; ++k) {
if (tok.begin() + tok.size() == TokenAt(k).begin()) {
tok.ExtendToCover(TokenAt(k));
} else {
break;
}
}
bool isSentinel{false};
if (tok.size() == blanks + 5) {
char sentinel[4];
for (int k{0}; k < 4; ++k) {
sentinel[k] = ToLowerCaseLetter(tok[blanks + k + 1]);
}
isSentinel = prescanner.IsCompilerDirectiveSentinel(sentinel, 4);
if (tok.size() > blanks + 5) {
isSentinel = prescanner.IsCompilerDirectiveSentinel(&tok[blanks + 1])
.has_value();
}
if (isSentinel) {
} else if (skipFirst) {
Expand Down
7 changes: 7 additions & 0 deletions flang/test/Preprocessing/sentinel-after-semi.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
! RUN: %flang_fc1 -fdebug-unparse -fopenacc %s 2>&1 | FileCheck %s
! CHECK: !$ACC DECLARE COPYIN(var)
#define ACCDECLARE(var) integer :: var; \
!$acc declare copyin(var)
program main
ACCDECLARE(var)
end program
Loading