Skip to content

Commit 6e261d9

Browse files
authored
[flang] Accept more unrecognized !DIR$ compiler directives (#85829)
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.
1 parent 4998587 commit 6e261d9

File tree

6 files changed

+42
-15
lines changed

6 files changed

+42
-15
lines changed

flang/include/flang/Parser/dump-parse-tree.h

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ class ParseTreeDumper {
207207
NODE(CompilerDirective, LoopCount)
208208
NODE(CompilerDirective, AssumeAligned)
209209
NODE(CompilerDirective, NameValue)
210+
NODE(CompilerDirective, Unrecognized)
210211
NODE(parser, ComplexLiteralConstant)
211212
NODE(parser, ComplexPart)
212213
NODE(parser, ComponentArraySpec)

flang/include/flang/Parser/parse-tree-visitor.h

+12
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,18 @@ template <typename M> void Walk(CompilerDirective &x, M &mutator) {
861861
}
862862
}
863863
template <typename V>
864+
void Walk(const CompilerDirective::Unrecognized &x, V &visitor) {
865+
if (visitor.Pre(x)) {
866+
visitor.Post(x);
867+
}
868+
}
869+
template <typename M>
870+
void Walk(CompilerDirective::Unrecognized &x, M &mutator) {
871+
if (mutator.Pre(x)) {
872+
mutator.Post(x);
873+
}
874+
}
875+
template <typename V>
864876
void Walk(const OmpLinearClause::WithModifier &x, V &visitor) {
865877
if (visitor.Pre(x)) {
866878
Walk(x.modifier, visitor);

flang/include/flang/Parser/parse-tree.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -3298,7 +3298,8 @@ struct StmtFunctionStmt {
32983298
// Compiler directives
32993299
// !DIR$ IGNORE_TKR [ [(tkrdmac...)] name ]...
33003300
// !DIR$ LOOP COUNT (n1[, n2]...)
3301-
// !DIR$ name...
3301+
// !DIR$ name[=value] [, name[=value]]... = can be :
3302+
// !DIR$ <anything else>
33023303
struct CompilerDirective {
33033304
UNION_CLASS_BOILERPLATE(CompilerDirective);
33043305
struct IgnoreTKR {
@@ -3316,9 +3317,10 @@ struct CompilerDirective {
33163317
TUPLE_CLASS_BOILERPLATE(NameValue);
33173318
std::tuple<Name, std::optional<std::uint64_t>> t;
33183319
};
3320+
struct Unrecognized {};
33193321
CharBlock source;
33203322
std::variant<std::list<IgnoreTKR>, LoopCount, std::list<AssumeAligned>,
3321-
std::list<NameValue>>
3323+
std::list<NameValue>, Unrecognized>
33223324
u;
33233325
};
33243326

flang/lib/Parser/Fortran-parsers.cpp

+17-13
Original file line numberDiff line numberDiff line change
@@ -1261,24 +1261,28 @@ TYPE_PARSER(construct<StatOrErrmsg>("STAT =" >> statVariable) ||
12611261
// Directives, extensions, and deprecated statements
12621262
// !DIR$ IGNORE_TKR [ [(tkrdmac...)] name ]...
12631263
// !DIR$ LOOP COUNT (n1[, n2]...)
1264-
// !DIR$ name...
1264+
// !DIR$ name[=value] [, name[=value]]...
1265+
// !DIR$ <anything else>
12651266
constexpr auto ignore_tkr{
1266-
"DIR$ IGNORE_TKR" >> optionalList(construct<CompilerDirective::IgnoreTKR>(
1267-
maybe(parenthesized(many(letter))), name))};
1267+
"IGNORE_TKR" >> optionalList(construct<CompilerDirective::IgnoreTKR>(
1268+
maybe(parenthesized(many(letter))), name))};
12681269
constexpr auto loopCount{
1269-
"DIR$ LOOP COUNT" >> construct<CompilerDirective::LoopCount>(
1270-
parenthesized(nonemptyList(digitString64)))};
1271-
constexpr auto assumeAligned{"DIR$ ASSUME_ALIGNED" >>
1270+
"LOOP COUNT" >> construct<CompilerDirective::LoopCount>(
1271+
parenthesized(nonemptyList(digitString64)))};
1272+
constexpr auto assumeAligned{"ASSUME_ALIGNED" >>
12721273
optionalList(construct<CompilerDirective::AssumeAligned>(
12731274
indirect(designator), ":"_tok >> digitString64))};
1274-
TYPE_PARSER(beginDirective >>
1275-
sourced(construct<CompilerDirective>(ignore_tkr) ||
1276-
construct<CompilerDirective>(loopCount) ||
1277-
construct<CompilerDirective>(assumeAligned) ||
1275+
TYPE_PARSER(beginDirective >> "DIR$ "_tok >>
1276+
sourced((construct<CompilerDirective>(ignore_tkr) ||
1277+
construct<CompilerDirective>(loopCount) ||
1278+
construct<CompilerDirective>(assumeAligned) ||
1279+
construct<CompilerDirective>(
1280+
many(construct<CompilerDirective::NameValue>(
1281+
name, maybe(("="_tok || ":"_tok) >> digitString64))))) /
1282+
endOfStmt ||
12781283
construct<CompilerDirective>(
1279-
"DIR$" >> many(construct<CompilerDirective::NameValue>(name,
1280-
maybe(("="_tok || ":"_tok) >> digitString64))))) /
1281-
endOfStmt)
1284+
SkipTo<'\n'>{} >> pure<CompilerDirective::Unrecognized>()) /
1285+
endOfStmt))
12821286

12831287
TYPE_PARSER(extension<LanguageFeature::CrayPointer>(
12841288
"nonstandard usage: based POINTER"_port_en_US,

flang/lib/Parser/unparse.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,10 @@ class UnparseVisitor {
18271827
[&](const std::list<CompilerDirective::NameValue> &names) {
18281828
Walk("!DIR$ ", names, " ");
18291829
},
1830+
[&](const CompilerDirective::Unrecognized &) {
1831+
Word("!DIR$ ");
1832+
Word(x.source.ToString());
1833+
},
18301834
},
18311835
x.u);
18321836
Put('\n');
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
! RUN: %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s
2+
!CHECK: warning: Compiler directive was ignored
3+
!DIR$ Not a recognized directive
4+
end

0 commit comments

Comments
 (0)