Skip to content

[flang] Ignore empty keyword macros before directives #130333

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
Mar 10, 2025
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
1 change: 1 addition & 0 deletions flang/include/flang/Parser/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Preprocessor {
void Define(const std::string &macro, const std::string &value);
void Undefine(std::string macro);
bool IsNameDefined(const CharBlock &);
bool IsNameDefinedEmpty(const CharBlock &);
bool IsFunctionLikeDefinition(const CharBlock &);
bool AnyDefinitions() const { return !definitions_.empty(); }
bool InConditional() const { return !ifStack_.empty(); }
Expand Down
9 changes: 9 additions & 0 deletions flang/lib/Parser/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,15 @@ bool Preprocessor::IsNameDefined(const CharBlock &token) {
return definitions_.find(token) != definitions_.end();
}

bool Preprocessor::IsNameDefinedEmpty(const CharBlock &token) {
if (auto it{definitions_.find(token)}; it != definitions_.end()) {
const Definition &def{it->second};
return !def.isFunctionLike() && def.replacement().SizeInChars() == 0;
} else {
return false;
}
}

bool Preprocessor::IsFunctionLikeDefinition(const CharBlock &token) {
auto it{definitions_.find(token)};
return it != definitions_.end() && it->second.isFunctionLike();
Expand Down
41 changes: 32 additions & 9 deletions flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,8 @@ void Prescanner::Statement() {
if (inFixedForm_) {
CHECK(IsFixedFormCommentChar(*at_));
} else {
while (int n{IsSpaceOrTab(at_)}) {
at_ += n, ++column_;
}
at_ += line.payloadOffset;
column_ += line.payloadOffset;
CHECK(*at_ == '!');
}
std::optional<int> condOffset;
Expand Down Expand Up @@ -597,6 +596,30 @@ const char *Prescanner::SkipWhiteSpace(const char *p) {
return p;
}

const char *Prescanner::SkipWhiteSpaceIncludingEmptyMacros(
const char *p) const {
while (true) {
if (int n{IsSpaceOrTab(p)}) {
p += n;
} else if (preprocessor_.AnyDefinitions() && IsLegalIdentifierStart(*p)) {
// Skip keyword macros with empty definitions
const char *q{p + 1};
while (IsLegalInIdentifier(*q)) {
++q;
}
if (preprocessor_.IsNameDefinedEmpty(
CharBlock{p, static_cast<std::size_t>(q - p)})) {
p = q;
} else {
break;
}
} else {
break;
}
}
return p;
}

const char *Prescanner::SkipWhiteSpaceAndCComments(const char *p) const {
while (true) {
if (int n{IsSpaceOrTab(p)}) {
Expand Down Expand Up @@ -1463,18 +1486,18 @@ Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
*sp = '\0';
if (const char *ss{IsCompilerDirectiveSentinel(
sentinel, static_cast<std::size_t>(sp - sentinel))}) {
std::size_t payloadOffset = p - start;
return {LineClassification{
LineClassification::Kind::CompilerDirective, payloadOffset, ss}};
return {
LineClassification{LineClassification::Kind::CompilerDirective, 0, ss}};
}
return std::nullopt;
}

std::optional<Prescanner::LineClassification>
Prescanner::IsFreeFormCompilerDirectiveLine(const char *start) const {
if (const char *p{SkipWhiteSpace(start)}; p && *p++ == '!') {
if (const char *p{SkipWhiteSpaceIncludingEmptyMacros(start)};
p && *p++ == '!') {
if (auto maybePair{IsCompilerDirectiveSentinel(p)}) {
auto offset{static_cast<std::size_t>(maybePair->second - start)};
auto offset{static_cast<std::size_t>(p - start - 1)};
return {LineClassification{LineClassification::Kind::CompilerDirective,
offset, maybePair->first}};
}
Expand Down Expand Up @@ -1529,7 +1552,7 @@ Prescanner::IsCompilerDirectiveSentinel(const char *p) const {
if (int n{*p == '&' ? 1 : IsSpaceOrTab(p)}) {
if (j > 0) {
sentinel[j] = '\0';
p = SkipWhiteSpace(p + n);
p = SkipWhiteSpaceIncludingEmptyMacros(p + n);
if (*p != '!') {
if (const char *sp{IsCompilerDirectiveSentinel(sentinel, j)}) {
return std::make_pair(sp, p);
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Parser/prescan.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class Prescanner {
void SkipCComments();
void SkipSpaces();
static const char *SkipWhiteSpace(const char *);
const char *SkipWhiteSpaceIncludingEmptyMacros(const char *) const;
const char *SkipWhiteSpaceAndCComments(const char *) const;
const char *SkipCComment(const char *) const;
bool NextToken(TokenSequence &);
Expand Down
5 changes: 5 additions & 0 deletions flang/test/Preprocessing/bug126459.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
! RUN: %flang -E -fopenmp %s 2>&1 | FileCheck %s
!CHECK: NDIR=0
#define BLANKMACRO
BLANKMACRO !$ NDIR=0
end