Skip to content

Commit

Permalink
[Flang][Driver] Add warning support for invalid R_Group options
Browse files Browse the repository at this point in the history
With the R_Group options, invalid values e.g. '-Rpa' will not emit
a warning like clang. This patch enables warning reporting, as
well as suggestions on what option the user intended to select.

Depends on D158174 and D158436. The former, adds backend
support to R_Group options while the latter, implements
regex support with some tests refactoring that cause a merge
conflict.

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D158593
  • Loading branch information
victorkingi committed Aug 31, 2023
1 parent ac2d265 commit e8af247
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
19 changes: 19 additions & 0 deletions flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ createFrontendAction(CompilerInstance &ci) {
llvm_unreachable("Invalid program action!");
}

static void emitUnknownDiagWarning(clang::DiagnosticsEngine &diags,
clang::diag::Flavor flavor,
llvm::StringRef prefix,
llvm::StringRef opt) {
llvm::StringRef suggestion =
clang::DiagnosticIDs::getNearestOption(flavor, opt);
diags.Report(clang::diag::warn_unknown_diag_option)
<< (flavor == clang::diag::Flavor::WarningOrError ? 0 : 1)
<< (prefix.str() += std::string(opt)) << !suggestion.empty()
<< (prefix.str() += std::string(suggestion));
}

// Remarks are ignored by default in Diagnostic.td, hence, we have to
// enable them here before execution. Clang follows same idea using
// ProcessWarningOptions in Warnings.cpp
Expand All @@ -123,6 +135,13 @@ updateDiagEngineForOptRemarks(clang::DiagnosticsEngine &diagsEng,
if (!isPositive)
remarkOpt = remarkOpt.substr(3);

// Verify that this is a valid optimization remarks option
if (diagIDs->getDiagnosticsInGroup(flavor, remarkOpt, diags)) {
emitUnknownDiagWarning(diagsEng, flavor, isPositive ? "-R" : "-Rno-",
remarkOpt);
return;
}

diagsEng.setSeverityForGroup(flavor, remarkOpt,
isPositive ? clang::diag::Severity::Remark
: clang::diag::Severity::Ignored);
Expand Down
7 changes: 7 additions & 0 deletions flang/test/Driver/optimization-remark-invalid.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
! Check error on invalid regex -Rpass message is emitted
! RUN: not %flang %s -O1 -Rpass=[ -c 2>&1 | FileCheck %s --check-prefix=REGEX-INVALID

! Check "unknown remark option" warning
! RUN: %flang %s -O1 -R -c 2>&1 | FileCheck %s --check-prefix=WARN

! Check "unknown remark option" warning with suggestion
! RUN: %flang %s -O1 -Rpas -c 2>&1 | FileCheck %s --check-prefix=WARN-SUGGEST

! REGEX-INVALID: error: in pattern '-Rpass=[': brackets ([ ]) not balanced
! WARN: warning: unknown remark option '-R'
! WARN-SUGGEST: warning: unknown remark option '-Rpas'; did you mean '-Rpass'?

program forttest
end program forttest

0 comments on commit e8af247

Please sign in to comment.