diff options
author | Andre Kuhlenschmidt <andre.kuhlenschmidt@gmail.com> | 2025-06-10 06:41:13 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-06-10 06:41:13 -0700 |
commit | bf60aa1c551ef5de62fd1d1cdcbff58cba55cacd (patch) | |
tree | a19845a777fad52816be6e302285c2219462b2e9 /flang/lib/Frontend/CompilerInvocation.cpp | |
parent | c34351c92ae6979ccd549403fe97bc7cc2d1ff5b (diff) | |
download | llvm-bf60aa1c551ef5de62fd1d1cdcbff58cba55cacd.zip llvm-bf60aa1c551ef5de62fd1d1cdcbff58cba55cacd.tar.gz llvm-bf60aa1c551ef5de62fd1d1cdcbff58cba55cacd.tar.bz2 |
[flang][cli] Add diagnostic flags to the CLI (#142022)
This change allows the flang CLI to accept `-W[no-]<feature>` flags matching the clang syntax and enable and disable usage and language feature warnings.
Diffstat (limited to 'flang/lib/Frontend/CompilerInvocation.cpp')
-rw-r--r-- | flang/lib/Frontend/CompilerInvocation.cpp | 64 |
1 files changed, 30 insertions, 34 deletions
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index fb9d0ad..5e156b4 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -20,12 +20,10 @@ #include "flang/Support/Version.h" #include "flang/Tools/TargetSetup.h" #include "flang/Version.inc" -#include "clang/Basic/AllDiagnostics.h" #include "clang/Basic/DiagnosticDriver.h" #include "clang/Basic/DiagnosticOptions.h" #include "clang/Driver/CommonArgs.h" #include "clang/Driver/Driver.h" -#include "clang/Driver/DriverDiagnostic.h" #include "clang/Driver/OptionUtils.h" #include "clang/Driver/Options.h" #include "llvm/ADT/StringRef.h" @@ -36,7 +34,6 @@ #include "llvm/Option/OptTable.h" #include "llvm/Support/CodeGen.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/FileUtilities.h" #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" #include "llvm/Support/raw_ostream.h" @@ -975,10 +972,23 @@ static bool parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args, /// Parses all diagnostics related arguments and populates the variables /// options accordingly. Returns false if new errors are generated. +/// FC1 driver entry point for parsing diagnostic arguments. static bool parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) { unsigned numErrorsBefore = diags.getNumErrors(); + auto &features{res.getFrontendOpts().features}; + // The order of these flags (-pedantic -W<feature> -w) is important and is + // chosen to match clang's behavior. + + // -pedantic + if (args.hasArg(clang::driver::options::OPT_pedantic)) { + features.WarnOnAllNonstandard(); + features.WarnOnAllUsage(); + res.setEnableConformanceChecks(); + res.setEnableUsageChecks(); + } + // -Werror option // TODO: Currently throws a Diagnostic for anything other than -W<error>, // this has to change when other -W<opt>'s are supported. @@ -988,22 +998,26 @@ static bool parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args, 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); + // -W(no-)<feature> + } else if (!features.ApplyCliOption(wArg)) { + const unsigned diagID = diags.getCustomDiagID( + clang::DiagnosticsEngine::Error, "Unknown diagnostic option: -W%0"); + diags.Report(diagID) << wArg; } } } - // Default to off for `flang -fc1`. - res.getFrontendOpts().showColors = - parseShowColorsArgs(args, /*defaultDiagColor=*/false); - - // Honor color diagnostics. - res.getDiagnosticOpts().ShowColors = res.getFrontendOpts().showColors; + // -w + if (args.hasArg(clang::driver::options::OPT_w)) { + features.DisableAllWarnings(); + res.setDisableWarnings(); + } + // Default to off for `flang -fc1`. + bool showColors{parseShowColorsArgs(args, false)}; + diags.getDiagnosticOptions().ShowColors = showColors; + res.getDiagnosticOpts().ShowColors = showColors; + res.getFrontendOpts().showColors = showColors; return diags.getNumErrors() == numErrorsBefore; } @@ -1078,16 +1092,6 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args, Fortran::common::LanguageFeature::OpenACC); } - // -pedantic - if (args.hasArg(clang::driver::options::OPT_pedantic)) { - res.setEnableConformanceChecks(); - res.setEnableUsageChecks(); - } - - // -w - if (args.hasArg(clang::driver::options::OPT_w)) - res.setDisableWarnings(); - // -std=f2018 // TODO: Set proper options when more fortran standards // are supported. @@ -1096,6 +1100,7 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args, // We only allow f2018 as the given standard if (standard == "f2018") { res.setEnableConformanceChecks(); + res.getFrontendOpts().features.WarnOnAllNonstandard(); } else { const unsigned diagID = diags.getCustomDiagID(clang::DiagnosticsEngine::Error, @@ -1702,16 +1707,7 @@ void CompilerInvocation::setFortranOpts() { if (frontendOptions.needProvenanceRangeToCharBlockMappings) fortranOptions.needProvenanceRangeToCharBlockMappings = true; - if (getEnableConformanceChecks()) - fortranOptions.features.WarnOnAllNonstandard(); - - if (getEnableUsageChecks()) - fortranOptions.features.WarnOnAllUsage(); - - if (getDisableWarnings()) { - fortranOptions.features.DisableAllNonstandardWarnings(); - fortranOptions.features.DisableAllUsageWarnings(); - } + fortranOptions.features = frontendOptions.features; } std::unique_ptr<Fortran::semantics::SemanticsContext> |