Skip to content

[flang] Fixes in preprocessing conditional compilation code when preceded by macros #129015

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions flang/include/flang/Parser/parsing.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/Support/raw_ostream.h"
#include <optional>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -66,6 +67,7 @@ class Parsing {
void DumpProvenance(llvm::raw_ostream &) const;
void DumpParsingLog(llvm::raw_ostream &) const;
void Parse(llvm::raw_ostream &debugOutput);
const char *IsCompilerDirectiveSentinel(const char *, std::size_t) const;
void ClearLog();

void EmitMessage(llvm::raw_ostream &o, const char *at,
Expand All @@ -87,6 +89,7 @@ class Parsing {
std::optional<Program> parseTree_;
ParsingLog log_;
Preprocessor preprocessor_{allCooked_.allSources()};
std::unordered_set<std::string> compilerDirectiveSentinels_;
};
} // namespace Fortran::parser
#endif // FORTRAN_PARSER_PARSING_H_
10 changes: 9 additions & 1 deletion flang/lib/Parser/parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
if (options.showColors) {
allSources.setShowColors(/*showColors=*/true);
}
compilerDirectiveSentinels_ = prescanner.getCompilerDirectiveSentinels();
return sourceFile;
}

Expand Down Expand Up @@ -153,7 +154,8 @@ void Parsing::EmitPreprocessedSource(
return ch;
}};

if (ch == '!' && lineWasBlankBefore) {
if (ch == '!' && lineWasBlankBefore &&
IsCompilerDirectiveSentinel((const char *)&atChar + 1, 4)) {
// Other comment markers (C, *, D) in original fixed form source
// input card column 1 will have been deleted or normalized to !,
// which signifies a comment (directive) in both source forms.
Expand Down Expand Up @@ -258,6 +260,12 @@ void Parsing::Parse(llvm::raw_ostream &out) {
finalRestingPlace_ = parseState.GetLocation();
}

const char *Parsing::IsCompilerDirectiveSentinel(
const char *sentinel, std::size_t len) const {
const auto iter{compilerDirectiveSentinels_.find(std::string(sentinel, len))};
return iter == compilerDirectiveSentinels_.end() ? nullptr : iter->c_str();
}

void Parsing::ClearLog() { log_.clear(); }

} // namespace Fortran::parser
2 changes: 1 addition & 1 deletion flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,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 !IsCompilerDirectiveSentinel(at_);
return !IsCompilerDirectiveSentinel(at_ + 1);
} else {
return false;
}
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 @@ -90,6 +90,9 @@ class Prescanner {
template <typename... A> Message &Say(A &&...a) {
return messages_.Say(std::forward<A>(a)...);
}
std::unordered_set<std::string> getCompilerDirectiveSentinels() {
return compilerDirectiveSentinels_;
}

private:
struct LineClassification {
Expand Down
8 changes: 8 additions & 0 deletions flang/test/Preprocessing/bug126459.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
! RUN: %flang -fopenmp -E %s 2>&1 | FileCheck %s
!CHECK: !$ NDIR=0
program main
integer NDIR
#define BLANKMACRO

BLANKMACRO !$ NDIR=0
end program main