diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1355,8 +1355,11 @@ Flags<[CC1Option]>, MetaVarName<"">, Values<".,latest">, HelpText<"Attempt to match the ABI of Clang ">; def fclasspath_EQ : Joined<["-"], "fclasspath=">, Group; -defm color_diagnostics : OptInCC1FFlag<"color-diagnostics", "Enable", "Disable", " colors in diagnostics", - [CoreOption, FlangOption]>; +def fcolor_diagnostics : Flag<["-"], "fcolor-diagnostics">, Group, + Flags<[CoreOption, CC1Option, FlangOption, FC1Option]>, + HelpText<"Enable colors in diagnostics">; +def fno_color_diagnostics : Flag<["-"], "fno-color-diagnostics">, Group, + Flags<[CoreOption, FlangOption]>, HelpText<"Disable colors in diagnostics">; def : Flag<["-"], "fdiagnostics-color">, Group, Flags<[CoreOption]>, Alias; def : Flag<["-"], "fno-diagnostics-color">, Group, Flags<[CoreOption]>, Alias; def fdiagnostics_color_EQ : Joined<["-"], "fdiagnostics-color=">, Group; diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -65,6 +65,7 @@ const llvm::Triple &Triple = TC.getEffectiveTriple(); const std::string &TripleStr = Triple.getTriple(); + const Driver &D = TC.getDriver(); ArgStringList CmdArgs; // Invoke ourselves in -fc1 mode. @@ -108,6 +109,14 @@ AddFortranDialectOptions(Args, CmdArgs); + // Color diagnostics are parsed by the driver directly from argv and later + // re-parsed to construct this job; claim any possible color diagnostic here + // to avoid warn_drv_unused_argument. + Args.getLastArg(options::OPT_fcolor_diagnostics, + options::OPT_fno_color_diagnostics); + if (D.getDiags().getDiagnosticOptions().ShowColors) + CmdArgs.push_back("-fcolor-diagnostics"); + // Add other compile options AddOtherOptions(Args, CmdArgs); @@ -139,7 +148,6 @@ CmdArgs.push_back(Input.getFilename()); - const auto& D = C.getDriver(); // TODO: Replace flang-new with flang once the new driver replaces the // throwaway driver const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC)); diff --git a/flang/include/flang/Frontend/CompilerInvocation.h b/flang/include/flang/Frontend/CompilerInvocation.h --- a/flang/include/flang/Frontend/CompilerInvocation.h +++ b/flang/include/flang/Frontend/CompilerInvocation.h @@ -30,8 +30,7 @@ /// When errors are encountered, return false and, if Diags is non-null, /// report the error(s). bool parseDiagnosticArgs(clang::DiagnosticOptions &opts, - llvm::opt::ArgList &args, - bool defaultDiagColor = true); + llvm::opt::ArgList &args); class CompilerInvocationBase { public: diff --git a/flang/include/flang/Frontend/FrontendOptions.h b/flang/include/flang/Frontend/FrontendOptions.h --- a/flang/include/flang/Frontend/FrontendOptions.h +++ b/flang/include/flang/Frontend/FrontendOptions.h @@ -219,7 +219,7 @@ struct FrontendOptions { FrontendOptions() : showHelp(false), showVersion(false), instrumentedParse(false), - needProvenanceRangeToCharBlockMappings(false) {} + showColors(false), needProvenanceRangeToCharBlockMappings(false) {} /// Show the -help text. unsigned showHelp : 1; @@ -230,6 +230,9 @@ /// Instrument the parse to get a more verbose log unsigned instrumentedParse : 1; + /// Enable color diagnostics. + unsigned showColors : 1; + /// Enable Provenance to character-stream mapping. Allows e.g. IDEs to find /// symbols based on source-code location. This is not needed in regular /// compilation. diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -53,10 +53,11 @@ //===----------------------------------------------------------------------===// // Deserialization (from args) //===----------------------------------------------------------------------===// -static bool parseShowColorsArgs( - const llvm::opt::ArgList &args, bool defaultColor) { - // Color diagnostics default to auto ("on" if terminal supports) in the driver - // but default to off in cc1, needing an explicit OPT_fdiagnostics_color. +static bool parseShowColorsArgs(const llvm::opt::ArgList &args, + bool defaultColor = true) { + // Color diagnostics default to auto ("on" if terminal supports) in the + // compiler driver `flang-new` but default to off in the frontend driver + // `flang-new -fc1`, needing an explicit OPT_fdiagnostics_color. // Support both clang's -f[no-]color-diagnostics and gcc's // -f[no-]diagnostics-colors[=never|always|auto]. enum { @@ -88,9 +89,8 @@ } bool Fortran::frontend::parseDiagnosticArgs(clang::DiagnosticOptions &opts, - llvm::opt::ArgList &args, - bool defaultDiagColor) { - opts.ShowColors = parseShowColorsArgs(args, defaultDiagColor); + llvm::opt::ArgList &args) { + opts.ShowColors = parseShowColorsArgs(args); return true; } @@ -502,6 +502,10 @@ } } + // Default to off for `flang-new -fc1`. + res.getFrontendOpts().showColors = + parseShowColorsArgs(args, /*defaultDiagColor=*/false); + return diags.getNumErrors() == numErrorsBefore; } diff --git a/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp --- a/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -158,6 +158,9 @@ return false; } + // Honor color diagnostics. + flang->getDiagnosticOpts().ShowColors = flang->getFrontendOpts().showColors; + // Create and execute the frontend action. std::unique_ptr act(createFrontendAction(*flang)); if (!act) diff --git a/flang/test/Driver/color-diagnostics-forwarding.f90 b/flang/test/Driver/color-diagnostics-forwarding.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Driver/color-diagnostics-forwarding.f90 @@ -0,0 +1,21 @@ +! Test that flang-new forwards -f{no-}color-diagnostics options to +! flang-new -fc1 as expected. + +! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 -fcolor-diagnostics \ +! RUN: | FileCheck %s --check-prefix=CHECK-CD +! CHECK-CD: "-fc1"{{.*}} "-fcolor-diagnostics" + +! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 -fno-color-diagnostics \ +! RUN: | FileCheck %s --check-prefix=CHECK-NCD +! CHECK-NCD-NOT: "-fc1"{{.*}} "-fcolor-diagnostics" + +! Check that the last flag wins. +! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 \ +! RUN: -fno-color-diagnostics -fcolor-diagnostics \ +! RUN: | FileCheck %s --check-prefix=CHECK-NCD_CD_S +! CHECK-NCD_CD_S: "-fc1"{{.*}} "-fcolor-diagnostics" + +! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 \ +! RUN: -fcolor-diagnostics -fno-color-diagnostics \ +! RUN: | FileCheck %s --check-prefix=CHECK-CD_NCD_S +! CHECK-CD_NCD_S-NOT: "-fc1"{{.*}} "-fcolor-diagnostics" diff --git a/flang/test/Driver/color-diagnostics.f90 b/flang/test/Driver/color-diagnostics.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Driver/color-diagnostics.f90 @@ -0,0 +1,23 @@ +! Test the behaviors of -f{no-}color-diagnostics. +! Windows command prompt doesn't support ANSI escape sequences. +! REQUIRES: shell + +! RUN: not %flang %s -fcolor-diagnostics 2>&1 \ +! RUN: | FileCheck %s --check-prefix=CHECK_CD +! RUN: not %flang %s -fno-color-diagnostics 2>&1 \ +! RUN: | FileCheck %s --check-prefix=CHECK_NCD +! RUN: not %flang_fc1 %s -fcolor-diagnostics 2>&1 \ +! RUN: | FileCheck %s --check-prefix=CHECK_CD +! RUN: not %flang_fc1 %s -fno-color-diagnostics 2>&1 \ +! RUN: | FileCheck %s --check-prefix=UNSUPPORTED_OPTION +! RUN: not %flang_fc1 %s 2>&1 | FileCheck %s --check-prefix=CHECK_NCD + +! CHECK_CD: {{.*}}[0;1;31merror: {{.*}}[0m{{.*}}[1mSemantic errors in {{.*}}color-diagnostics.f90{{.*}}[0m + +! CHECK_NCD: Semantic errors in {{.*}}color-diagnostics.f90 + +! UNSUPPORTED_OPTION: error: unknown argument: '-fno-color-diagnostics' + +program m + integer :: i = k +end diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90 --- a/flang/test/Driver/driver-help.f90 +++ b/flang/test/Driver/driver-help.f90 @@ -83,6 +83,7 @@ ! HELP-FC1-NEXT: -falternative-parameter-statement ! HELP-FC1-NEXT: Enable the old style PARAMETER statement ! HELP-FC1-NEXT: -fbackslash Specify that backslash in string introduces an escape character +! HELP-FC1-NEXT: -fcolor-diagnostics Enable colors in diagnostics ! HELP-FC1-NEXT: -fdebug-dump-all Dump symbols and the parse tree after the semantic checks ! HELP-FC1-NEXT: -fdebug-dump-parse-tree-no-sema ! HELP-FC1-NEXT: Dump the parse tree (skips the semantic checks)