Skip to content

Commit ab6e881

Browse files
committed
This commit adds the support for warning and error compiler directives.
The warning directive !$dir warning("warning message") can be used to issue warning in the codepath. Similarly, the directive !$dir error("error message") can be used to issue error in the codepath.
1 parent eb96c8c commit ab6e881

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ class ParseTreeDumper {
208208
NODE(CompilerDirective, NameValue)
209209
NODE(CompilerDirective, Unrecognized)
210210
NODE(CompilerDirective, VectorAlways)
211+
NODE(CompilerDirective, Error)
212+
NODE(CompilerDirective, Warning)
211213
NODE(parser, ComplexLiteralConstant)
212214
NODE(parser, ComplexPart)
213215
NODE(parser, ComponentArraySpec)

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,15 +3363,21 @@ struct CompilerDirective {
33633363
TUPLE_CLASS_BOILERPLATE(AssumeAligned);
33643364
std::tuple<common::Indirection<Designator>, uint64_t> t;
33653365
};
3366+
struct Error {
3367+
WRAPPER_CLASS_BOILERPLATE(Error, std::string);
3368+
};
3369+
struct Warning {
3370+
WRAPPER_CLASS_BOILERPLATE(Warning, std::string);
3371+
};
33663372
EMPTY_CLASS(VectorAlways);
33673373
struct NameValue {
33683374
TUPLE_CLASS_BOILERPLATE(NameValue);
33693375
std::tuple<Name, std::optional<std::uint64_t>> t;
33703376
};
33713377
EMPTY_CLASS(Unrecognized);
33723378
CharBlock source;
3373-
std::variant<std::list<IgnoreTKR>, LoopCount, std::list<AssumeAligned>,
3374-
VectorAlways, std::list<NameValue>, Unrecognized>
3379+
std::variant<std::list<IgnoreTKR>, LoopCount, std::list<AssumeAligned>, Error,
3380+
Warning, VectorAlways, std::list<NameValue>, Unrecognized>
33753381
u;
33763382
};
33773383

flang/lib/Parser/Fortran-parsers.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,8 @@ TYPE_PARSER(construct<StatOrErrmsg>("STAT =" >> statVariable) ||
12941294
// !DIR$ LOOP COUNT (n1[, n2]...)
12951295
// !DIR$ name[=value] [, name[=value]]...
12961296
// !DIR$ <anything else>
1297+
// !DIR$ ERROR error-string
1298+
// !DIR$ WARNING warning-string
12971299
constexpr auto ignore_tkr{
12981300
"IGNORE_TKR" >> optionalList(construct<CompilerDirective::IgnoreTKR>(
12991301
maybe(parenthesized(many(letter))), name))};
@@ -1305,11 +1307,17 @@ constexpr auto assumeAligned{"ASSUME_ALIGNED" >>
13051307
indirect(designator), ":"_tok >> digitString64))};
13061308
constexpr auto vectorAlways{
13071309
"VECTOR ALWAYS" >> construct<CompilerDirective::VectorAlways>()};
1310+
constexpr auto error{"ERROR" >>
1311+
construct<CompilerDirective::Error>(charLiteralConstantWithoutKind)};
1312+
constexpr auto warning{"WARNING" >>
1313+
construct<CompilerDirective::Warning>(charLiteralConstantWithoutKind)};
13081314
TYPE_PARSER(beginDirective >> "DIR$ "_tok >>
13091315
sourced((construct<CompilerDirective>(ignore_tkr) ||
13101316
construct<CompilerDirective>(loopCount) ||
13111317
construct<CompilerDirective>(assumeAligned) ||
13121318
construct<CompilerDirective>(vectorAlways) ||
1319+
construct<CompilerDirective>(error) ||
1320+
construct<CompilerDirective>(warning) ||
13131321
construct<CompilerDirective>(
13141322
many(construct<CompilerDirective::NameValue>(
13151323
name, maybe(("="_tok || ":"_tok) >> digitString64))))) /

flang/lib/Parser/unparse.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,8 @@ class UnparseVisitor {
18471847
[&](const std::list<CompilerDirective::NameValue> &names) {
18481848
Walk("!DIR$ ", names, " ");
18491849
},
1850+
[&](const CompilerDirective::Error &) { Word("!DIR$ ERROR"); },
1851+
[&](const CompilerDirective::Warning &) { Word("!DIR$ WARNING"); },
18501852
[&](const CompilerDirective::Unrecognized &) {
18511853
Word("!DIR$ ");
18521854
Word(x.source.ToString());

flang/lib/Semantics/resolve-names.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9248,7 +9248,12 @@ void ResolveNamesVisitor::Post(const parser::CompilerDirective &x) {
92489248
if (std::holds_alternative<parser::CompilerDirective::VectorAlways>(x.u)) {
92499249
return;
92509250
}
9251-
if (const auto *tkr{
9251+
if (const auto *err{std::get_if<parser::CompilerDirective::Error>(&x.u)}) {
9252+
Say(err->v, "%s"_err_en_US);
9253+
}
9254+
else if (const auto *warn{std::get_if<parser::CompilerDirective::Warning>(&x.u)}) {
9255+
Say(warn->v, "%s"_warn_en_US);
9256+
} else if (const auto *tkr{
92529257
std::get_if<std::list<parser::CompilerDirective::IgnoreTKR>>(&x.u)}) {
92539258
if (currScope().IsTopLevel() ||
92549259
GetProgramUnitContaining(currScope()).kind() !=
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
! RUN: not %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s
2+
3+
! Test that error compiler directive issues error
4+
program warn
5+
!dir$ error "Error!"
6+
!CHECK: error: Error!
7+
end program
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
! RUN: %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s
2+
3+
! Test that warning compiler directive issues warning
4+
program warn
5+
!dir$ warning "Warning!"
6+
!CHECK: warning: Warning!
7+
end program
8+

0 commit comments

Comments
 (0)