Skip to content

Commit

Permalink
[flang] Accept more unrecognized !DIR$ compiler directives (#85829)
Browse files Browse the repository at this point in the history
When encountering an unparsable !DIR$ compiler directive line, accept it
as a whole source line and emit a warning that it is unrecognizable.

Fixes #59107,
#82212, and
#82654.
  • Loading branch information
klausler committed Mar 26, 2024
1 parent 4998587 commit 6e261d9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
1 change: 1 addition & 0 deletions flang/include/flang/Parser/dump-parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class ParseTreeDumper {
NODE(CompilerDirective, LoopCount)
NODE(CompilerDirective, AssumeAligned)
NODE(CompilerDirective, NameValue)
NODE(CompilerDirective, Unrecognized)
NODE(parser, ComplexLiteralConstant)
NODE(parser, ComplexPart)
NODE(parser, ComponentArraySpec)
Expand Down
12 changes: 12 additions & 0 deletions flang/include/flang/Parser/parse-tree-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,18 @@ template <typename M> void Walk(CompilerDirective &x, M &mutator) {
}
}
template <typename V>
void Walk(const CompilerDirective::Unrecognized &x, V &visitor) {
if (visitor.Pre(x)) {
visitor.Post(x);
}
}
template <typename M>
void Walk(CompilerDirective::Unrecognized &x, M &mutator) {
if (mutator.Pre(x)) {
mutator.Post(x);
}
}
template <typename V>
void Walk(const OmpLinearClause::WithModifier &x, V &visitor) {
if (visitor.Pre(x)) {
Walk(x.modifier, visitor);
Expand Down
6 changes: 4 additions & 2 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3298,7 +3298,8 @@ struct StmtFunctionStmt {
// Compiler directives
// !DIR$ IGNORE_TKR [ [(tkrdmac...)] name ]...
// !DIR$ LOOP COUNT (n1[, n2]...)
// !DIR$ name...
// !DIR$ name[=value] [, name[=value]]... = can be :
// !DIR$ <anything else>
struct CompilerDirective {
UNION_CLASS_BOILERPLATE(CompilerDirective);
struct IgnoreTKR {
Expand All @@ -3316,9 +3317,10 @@ struct CompilerDirective {
TUPLE_CLASS_BOILERPLATE(NameValue);
std::tuple<Name, std::optional<std::uint64_t>> t;
};
struct Unrecognized {};
CharBlock source;
std::variant<std::list<IgnoreTKR>, LoopCount, std::list<AssumeAligned>,
std::list<NameValue>>
std::list<NameValue>, Unrecognized>
u;
};

Expand Down
30 changes: 17 additions & 13 deletions flang/lib/Parser/Fortran-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,24 +1261,28 @@ TYPE_PARSER(construct<StatOrErrmsg>("STAT =" >> statVariable) ||
// Directives, extensions, and deprecated statements
// !DIR$ IGNORE_TKR [ [(tkrdmac...)] name ]...
// !DIR$ LOOP COUNT (n1[, n2]...)
// !DIR$ name...
// !DIR$ name[=value] [, name[=value]]...
// !DIR$ <anything else>
constexpr auto ignore_tkr{
"DIR$ IGNORE_TKR" >> optionalList(construct<CompilerDirective::IgnoreTKR>(
maybe(parenthesized(many(letter))), name))};
"IGNORE_TKR" >> optionalList(construct<CompilerDirective::IgnoreTKR>(
maybe(parenthesized(many(letter))), name))};
constexpr auto loopCount{
"DIR$ LOOP COUNT" >> construct<CompilerDirective::LoopCount>(
parenthesized(nonemptyList(digitString64)))};
constexpr auto assumeAligned{"DIR$ ASSUME_ALIGNED" >>
"LOOP COUNT" >> construct<CompilerDirective::LoopCount>(
parenthesized(nonemptyList(digitString64)))};
constexpr auto assumeAligned{"ASSUME_ALIGNED" >>
optionalList(construct<CompilerDirective::AssumeAligned>(
indirect(designator), ":"_tok >> digitString64))};
TYPE_PARSER(beginDirective >>
sourced(construct<CompilerDirective>(ignore_tkr) ||
construct<CompilerDirective>(loopCount) ||
construct<CompilerDirective>(assumeAligned) ||
TYPE_PARSER(beginDirective >> "DIR$ "_tok >>
sourced((construct<CompilerDirective>(ignore_tkr) ||
construct<CompilerDirective>(loopCount) ||
construct<CompilerDirective>(assumeAligned) ||
construct<CompilerDirective>(
many(construct<CompilerDirective::NameValue>(
name, maybe(("="_tok || ":"_tok) >> digitString64))))) /
endOfStmt ||
construct<CompilerDirective>(
"DIR$" >> many(construct<CompilerDirective::NameValue>(name,
maybe(("="_tok || ":"_tok) >> digitString64))))) /
endOfStmt)
SkipTo<'\n'>{} >> pure<CompilerDirective::Unrecognized>()) /
endOfStmt))

TYPE_PARSER(extension<LanguageFeature::CrayPointer>(
"nonstandard usage: based POINTER"_port_en_US,
Expand Down
4 changes: 4 additions & 0 deletions flang/lib/Parser/unparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,10 @@ class UnparseVisitor {
[&](const std::list<CompilerDirective::NameValue> &names) {
Walk("!DIR$ ", names, " ");
},
[&](const CompilerDirective::Unrecognized &) {
Word("!DIR$ ");
Word(x.source.ToString());
},
},
x.u);
Put('\n');
Expand Down
4 changes: 4 additions & 0 deletions flang/test/Parser/unrecognized-dir.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
! RUN: %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s
!CHECK: warning: Compiler directive was ignored
!DIR$ Not a recognized directive
end

0 comments on commit 6e261d9

Please sign in to comment.