Skip to content

Commit

Permalink
[flang] Handle unsupported warning flags
Browse files Browse the repository at this point in the history
This PR makes flang emit a warning when the user passes an unsupported gfortran warning flag in as a CLI arg.  This PR also checks each `-W` argument instead of just looking at the last one passed in.

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D143301
  • Loading branch information
EthanLuisMcDonough committed Feb 21, 2023
1 parent ae2322a commit ce3a1c5
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 8 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Expand Up @@ -113,6 +113,9 @@ def warn_drv_unsupported_option_for_target : Warning<
def warn_drv_unsupported_option_for_flang : Warning<
"the argument '%0' is not supported for option '%1'. Mapping to '%1%2'">,
InGroup<OptionIgnored>;
def warn_drv_unsupported_diag_option_for_flang : Warning<
"The warning option '-%0' is not supported">,
InGroup<OptionIgnored>;

def err_drv_invalid_thread_model_for_target : Error<
"invalid thread model '%0' in '%1' for this target">;
Expand Down
38 changes: 38 additions & 0 deletions clang/include/clang/Driver/Options.td
Expand Up @@ -235,6 +235,10 @@ def clang_ignored_f_Group : OptionGroup<"<clang ignored f group>">,
def clang_ignored_m_Group : OptionGroup<"<clang ignored m group>">,
Group<m_Group>, Flags<[Ignored]>;

// Unsupported flang groups
def flang_ignored_w_Group : OptionGroup<"<flang ignored W group>">,
Group<W_Group>, Flags<[FlangOnlyOption, Ignored]>;

// Group for clang options in the process of deprecation.
// Please include the version that deprecated the flag as comment to allow
// easier garbage collection.
Expand Down Expand Up @@ -4871,6 +4875,10 @@ multiclass BooleanFFlag<string name> {
def fno_#NAME : Flag<["-"], "fno-"#name>;
}

multiclass FlangIgnoredDiagOpt<string name> {
def unsupported_warning_w#NAME : Flag<["-", "--"], "W"#name>, Group<flang_ignored_w_Group>;
}

defm : BooleanFFlag<"keep-inline-functions">, Group<clang_ignored_gcc_optimization_f_Group>;

def fprofile_dir : Joined<["-"], "fprofile-dir=">, Group<f_Group>;
Expand Down Expand Up @@ -5030,6 +5038,36 @@ defm second_underscore : BooleanFFlag<"second-underscore">, Group<gfortran_Group
defm sign_zero : BooleanFFlag<"sign-zero">, Group<gfortran_Group>;
defm whole_file : BooleanFFlag<"whole-file">, Group<gfortran_Group>;

// -W <arg> options unsupported by the flang compiler
// If any of these options are passed into flang's compiler driver,
// a warning will be raised and the argument will be claimed
defm : FlangIgnoredDiagOpt<"extra">;
defm : FlangIgnoredDiagOpt<"aliasing">;
defm : FlangIgnoredDiagOpt<"ampersand">;
defm : FlangIgnoredDiagOpt<"array-bounds">;
defm : FlangIgnoredDiagOpt<"c-binding-type">;
defm : FlangIgnoredDiagOpt<"character-truncation">;
defm : FlangIgnoredDiagOpt<"conversion">;
defm : FlangIgnoredDiagOpt<"do-subscript">;
defm : FlangIgnoredDiagOpt<"function-elimination">;
defm : FlangIgnoredDiagOpt<"implicit-interface">;
defm : FlangIgnoredDiagOpt<"implicit-procedure">;
defm : FlangIgnoredDiagOpt<"intrinsic-shadow">;
defm : FlangIgnoredDiagOpt<"use-without-only">;
defm : FlangIgnoredDiagOpt<"intrinsics-std">;
defm : FlangIgnoredDiagOpt<"line-truncation">;
defm : FlangIgnoredDiagOpt<"no-align-commons">;
defm : FlangIgnoredDiagOpt<"no-overwrite-recursive">;
defm : FlangIgnoredDiagOpt<"no-tabs">;
defm : FlangIgnoredDiagOpt<"real-q-constant">;
defm : FlangIgnoredDiagOpt<"surprising">;
defm : FlangIgnoredDiagOpt<"underflow">;
defm : FlangIgnoredDiagOpt<"unused-parameter">;
defm : FlangIgnoredDiagOpt<"realloc-lhs">;
defm : FlangIgnoredDiagOpt<"realloc-lhs-all">;
defm : FlangIgnoredDiagOpt<"frontend-loop-interchange">;
defm : FlangIgnoredDiagOpt<"target-lifetime">;

// C++ SYCL options
def fsycl : Flag<["-"], "fsycl">, Flags<[NoXarchOption, CoreOption]>,
Group<sycl_Group>, HelpText<"Enables SYCL kernels compilation for device">;
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Driver/ToolChains/Flang.cpp
Expand Up @@ -329,6 +329,13 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
A->render(Args, CmdArgs);
}

// Remove any unsupported gfortran diagnostic options
for (const Arg *A : Args.filtered(options::OPT_flang_ignored_w_Group)) {
A->claim();
D.Diag(diag::warn_drv_unsupported_diag_option_for_flang)
<< A->getOption().getName();
}

// Optimization level for CodeGen.
if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
if (A->getOption().matches(options::OPT_O4)) {
Expand Down
4 changes: 4 additions & 0 deletions flang/docs/FlangDriver.md
Expand Up @@ -581,6 +581,10 @@ floating point and so always acts as though these flags were specified.
GCC/GFortran will also set flush-to-zero mode: linking `crtfastmath.o`, the same
as Flang.

The only GCC/GFortran warning option currently supported is `-Werror`. Passing
any unsupported GCC/GFortran warning flags into Flang's compiler driver will
result in warnings being emitted.

### Comparison with nvfortran
nvfortran defines `-fast` as
`-O2 -Munroll=c:1 -Mnoframe -Mlre -Mpre -Mvect=simd -Mcache_align -Mflushz -Mvect`.
Expand Down
19 changes: 11 additions & 8 deletions flang/lib/Frontend/CompilerInvocation.cpp
Expand Up @@ -610,14 +610,17 @@ static bool parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
// TODO: Currently throws a Diagnostic for anything other than -W<error>,
// this has to change when other -W<opt>'s are supported.
if (args.hasArg(clang::driver::options::OPT_W_Joined)) {
if (args.getLastArgValue(clang::driver::options::OPT_W_Joined)
.equals("error")) {
res.setWarnAsErr(true);
} else {
const unsigned diagID =
diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
"Only `-Werror` is supported currently.");
diags.Report(diagID);
const auto &wArgs =
args.getAllArgValues(clang::driver::options::OPT_W_Joined);
for (const auto &wArg : wArgs) {
if (wArg == "error") {
res.setWarnAsErr(true);
} else {
const unsigned diagID =
diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
"Only `-Werror` is supported currently.");
diags.Report(diagID);
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions flang/test/Driver/w-arg-unsupported.f90
@@ -0,0 +1,37 @@
! RUN: %flang -std=f2018 -Wextra -Waliasing -Wampersand -Warray-bounds -Wc-binding-type \
! RUN: -Wcharacter-truncation -Wconversion -Wdo-subscript -Wfunction-elimination \
! RUN: -Wimplicit-interface -Wimplicit-procedure -Wintrinsic-shadow -Wuse-without-only \
! RUN: -Wintrinsics-std -Wline-truncation -Wno-align-commons -Wno-overwrite-recursive \
! RUN: -Wno-tabs -Wreal-q-constant -Wsurprising -Wunderflow -Wunused-parameter \
! RUN: -Wrealloc-lhs -Wrealloc-lhs-all -Wfrontend-loop-interchange -Wtarget-lifetime %s \
! RUN: 2>&1 | FileCheck %s

! CHECK: The warning option '-Wextra' is not supported
! CHECK-NEXT: The warning option '-Waliasing' is not supported
! CHECK-NEXT: The warning option '-Wampersand' is not supported
! CHECK-NEXT: The warning option '-Warray-bounds' is not supported
! CHECK-NEXT: The warning option '-Wc-binding-type' is not supported
! CHECK-NEXT: The warning option '-Wcharacter-truncation' is not supported
! CHECK-NEXT: The warning option '-Wconversion' is not supported
! CHECK-NEXT: The warning option '-Wdo-subscript' is not supported
! CHECK-NEXT: The warning option '-Wfunction-elimination' is not supported
! CHECK-NEXT: The warning option '-Wimplicit-interface' is not supported
! CHECK-NEXT: The warning option '-Wimplicit-procedure' is not supported
! CHECK-NEXT: The warning option '-Wintrinsic-shadow' is not supported
! CHECK-NEXT: The warning option '-Wuse-without-only' is not supported
! CHECK-NEXT: The warning option '-Wintrinsics-std' is not supported
! CHECK-NEXT: The warning option '-Wline-truncation' is not supported
! CHECK-NEXT: The warning option '-Wno-align-commons' is not supported
! CHECK-NEXT: The warning option '-Wno-overwrite-recursive' is not supported
! CHECK-NEXT: The warning option '-Wno-tabs' is not supported
! CHECK-NEXT: The warning option '-Wreal-q-constant' is not supported
! CHECK-NEXT: The warning option '-Wsurprising' is not supported
! CHECK-NEXT: The warning option '-Wunderflow' is not supported
! CHECK-NEXT: The warning option '-Wunused-parameter' is not supported
! CHECK-NEXT: The warning option '-Wrealloc-lhs' is not supported
! CHECK-NEXT: The warning option '-Wrealloc-lhs-all' is not supported
! CHECK-NEXT: The warning option '-Wfrontend-loop-interchange' is not supported
! CHECK-NEXT: The warning option '-Wtarget-lifetime' is not supported

program m
end program
13 changes: 13 additions & 0 deletions flang/test/Driver/werror-all.f90
@@ -0,0 +1,13 @@
! Ensures that -Werror is read regardless of whether or not other -W
! flags are present in the CLI args

! RUN: not %flang -std=f2018 -Werror -Wextra %s 2>&1 | FileCheck %s --check-prefix=WRONG
! RUN: %flang -std=f2018 -Wextra -Wall %s 2>&1 | FileCheck %s --check-prefix=CHECK-OK

! WRONG: Semantic errors in
! CHECK-OK: FORALL index variable

program werror_check_all
integer :: a(3)
forall (j=1:n) a(i) = 1
end program werror_check_all
11 changes: 11 additions & 0 deletions flang/test/Driver/wextra-ok.f90
@@ -0,0 +1,11 @@
! Ensure that supplying -Wextra into flang-new does not raise error
! The first check should be changed if -Wextra is implemented

! RUN: %flang -std=f2018 -Wextra %s 2>&1 | FileCheck %s --check-prefix=CHECK-OK
! RUN: not %flang -std=f2018 -Wblah -Wextra %s 2>&1 | FileCheck %s --check-prefix=WRONG

! CHECK-OK: The warning option '-Wextra' is not supported
! WRONG: Only `-Werror` is supported currently.

program wextra_ok
end program wextra_ok

0 comments on commit ce3a1c5

Please sign in to comment.