Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ 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; Index: clang/lib/Driver/ToolChains/Flang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Flang.cpp +++ 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)); Index: flang/include/flang/Frontend/CompilerInvocation.h =================================================================== --- flang/include/flang/Frontend/CompilerInvocation.h +++ 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: Index: flang/include/flang/Frontend/FrontendOptions.h =================================================================== --- flang/include/flang/Frontend/FrontendOptions.h +++ 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. Index: flang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- flang/lib/Frontend/CompilerInvocation.cpp +++ 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; } Index: flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp =================================================================== --- flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ 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) Index: flang/test/Driver/color-diagnostics.f90 =================================================================== --- /dev/null +++ flang/test/Driver/color-diagnostics.f90 @@ -0,0 +1,23 @@ +! Test the color and bold diagnostics. +! REQUIRES: shell + +! RUN: not %flang %s -fcolor-diagnostics 2>&1 \ +! RUN: | FileCheck %s --check-prefix=COMPILER_DRIVER_DC +! RUN: not %flang %s -fno-color-diagnostics 2>&1 \ +! RUN: | FileCheck %s --check-prefix=COMPILER_DRIVER_NDC +! RUN: not %flang_fc1 %s -fcolor-diagnostics 2>&1 \ +! RUN: | FileCheck %s --check-prefix=FRONTEND_DRIVER_DC +! RUN: not %flang_fc1 %s -fno-color-diagnostics 2>&1 \ +! RUN: | FileCheck %s --check-prefix=FRONTEND_DRIVER_NDC + +! COMPILER_DRIVER_DC: {{.*}}[0;1;31merror: {{.*}}[0m{{.*}}[1mSemantic errors in {{.*}}color-diagnostics.f90{{.*}}[0m + +! COMPILER_DRIVER_NDC: Semantic errors in {{.*}}color-diagnostics.f90 + +! FRONTEND_DRIVER_DC: {{.*}}[0;1;31merror: {{.*}}[0m{{.*}}[1mSemantic errors in {{.*}}color-diagnostics.f90{{.*}}[0m + +! FRONTEND_DRIVER_NDC: error: unknown argument: '-fno-color-diagnostics' + +program m + integer :: i = k +end Index: flang/test/Driver/driver-help.f90 =================================================================== --- flang/test/Driver/driver-help.f90 +++ 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) Index: flang/test/Driver/frontend-forwarding.f90 =================================================================== --- flang/test/Driver/frontend-forwarding.f90 +++ flang/test/Driver/frontend-forwarding.f90 @@ -18,3 +18,22 @@ ! CHECK: "-fdefault-real-8" ! CHECK: "-flarge-sizes" ! CHECK: "-mllvm" "-print-before-all" + +! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 -fcolor-diagnostics \ +! RUN: | FileCheck %s --check-prefix=CHECK-DC +! CHECK-DC: "-fcolor-diagnostics" + +! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 -fno-color-diagnostics \ +! RUN: | FileCheck %s --check-prefix=CHECK-NDC +! CHECK-NDC-NOT: "-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-NDC_DC_S +! CHECK-NDC_DC_S: "-fcolor-diagnostics" + +! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 \ +! RUN: -fcolor-diagnostics -fno-color-diagnostics \ +! RUN: | FileCheck %s --check-prefix=CHECK-DC_NDC_S +! CHECK-DC_NDC_S-NOT: "-fcolor-diagnostics"