Skip to content

[flang] Support for warning and error compiler directives. #151510

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
2 changes: 2 additions & 0 deletions flang/include/flang/Parser/dump-parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ class ParseTreeDumper {
NODE(CompilerDirective, NameValue)
NODE(CompilerDirective, Unrecognized)
NODE(CompilerDirective, VectorAlways)
NODE(CompilerDirective, Error)
NODE(CompilerDirective, Warning)
NODE(parser, ComplexLiteralConstant)
NODE(parser, ComplexPart)
NODE(parser, ComponentArraySpec)
Expand Down
10 changes: 8 additions & 2 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3363,15 +3363,21 @@ struct CompilerDirective {
TUPLE_CLASS_BOILERPLATE(AssumeAligned);
std::tuple<common::Indirection<Designator>, uint64_t> t;
};
struct Error {
WRAPPER_CLASS_BOILERPLATE(Error, std::string);
};
struct Warning {
WRAPPER_CLASS_BOILERPLATE(Warning, std::string);
};
EMPTY_CLASS(VectorAlways);
struct NameValue {
TUPLE_CLASS_BOILERPLATE(NameValue);
std::tuple<Name, std::optional<std::uint64_t>> t;
};
EMPTY_CLASS(Unrecognized);
CharBlock source;
std::variant<std::list<IgnoreTKR>, LoopCount, std::list<AssumeAligned>,
VectorAlways, std::list<NameValue>, Unrecognized>
std::variant<std::list<IgnoreTKR>, LoopCount, std::list<AssumeAligned>, Error,
Warning, VectorAlways, std::list<NameValue>, Unrecognized>
u;
};

Expand Down
8 changes: 8 additions & 0 deletions flang/lib/Parser/Fortran-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,8 @@ TYPE_PARSER(construct<StatOrErrmsg>("STAT =" >> statVariable) ||
// !DIR$ LOOP COUNT (n1[, n2]...)
// !DIR$ name[=value] [, name[=value]]...
// !DIR$ <anything else>
// !DIR$ ERROR error-string
// !DIR$ WARNING warning-string
constexpr auto ignore_tkr{
"IGNORE_TKR" >> optionalList(construct<CompilerDirective::IgnoreTKR>(
maybe(parenthesized(many(letter))), name))};
Expand All @@ -1305,11 +1307,17 @@ constexpr auto assumeAligned{"ASSUME_ALIGNED" >>
indirect(designator), ":"_tok >> digitString64))};
constexpr auto vectorAlways{
"VECTOR ALWAYS" >> construct<CompilerDirective::VectorAlways>()};
constexpr auto error{"ERROR" >>
construct<CompilerDirective::Error>(charLiteralConstantWithoutKind)};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the PR comment, these character literals are parenthesized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. updated the PR comment.

constexpr auto warning{"WARNING" >>
construct<CompilerDirective::Warning>(charLiteralConstantWithoutKind)};
TYPE_PARSER(beginDirective >> "DIR$ "_tok >>
sourced((construct<CompilerDirective>(ignore_tkr) ||
construct<CompilerDirective>(loopCount) ||
construct<CompilerDirective>(assumeAligned) ||
construct<CompilerDirective>(vectorAlways) ||
construct<CompilerDirective>(error) ||
construct<CompilerDirective>(warning) ||
construct<CompilerDirective>(
many(construct<CompilerDirective::NameValue>(
name, maybe(("="_tok || ":"_tok) >> digitString64))))) /
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Parser/unparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,8 @@ class UnparseVisitor {
[&](const std::list<CompilerDirective::NameValue> &names) {
Walk("!DIR$ ", names, " ");
},
[&](const CompilerDirective::Error &) { Word("!DIR$ ERROR"); },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will you test this? The directive will emit a fatal error in semantics, and then unpausing won't be run.

[&](const CompilerDirective::Warning &) { Word("!DIR$ WARNING"); },
[&](const CompilerDirective::Unrecognized &) {
Word("!DIR$ ");
Word(x.source.ToString());
Expand Down
7 changes: 6 additions & 1 deletion flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9248,7 +9248,12 @@ void ResolveNamesVisitor::Post(const parser::CompilerDirective &x) {
if (std::holds_alternative<parser::CompilerDirective::VectorAlways>(x.u)) {
return;
}
if (const auto *tkr{
if (const auto *err{std::get_if<parser::CompilerDirective::Error>(&x.u)}) {
Say(err->v, "%s"_err_en_US);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put the else on this line too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. thank you.

else if (const auto *warn{std::get_if<parser::CompilerDirective::Warning>(&x.u)}) {
Say(warn->v, "%s"_warn_en_US);
} else if (const auto *tkr{
std::get_if<std::list<parser::CompilerDirective::IgnoreTKR>>(&x.u)}) {
if (currScope().IsTopLevel() ||
GetProgramUnitContaining(currScope()).kind() !=
Expand Down
8 changes: 8 additions & 0 deletions flang/test/Parser/compiler-directive-error.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
! RUN: not %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s

! Test that error compiler directive issues error
program error
!dir$ error "Error!"
!CHECK: error: Error!
end program

8 changes: 8 additions & 0 deletions flang/test/Parser/compiler-directive-warning.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
! RUN: %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s

! Test that warning compiler directive issues warning
program warn
!dir$ warning "Warning!"
!CHECK: warning: Warning!
end program