Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -1353,8 +1353,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 @@ -57,6 +57,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. @@ -100,6 +101,12 @@ AddFortranDialectOptions(Args, CmdArgs); + // Silence the warning of argument unused. + 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); @@ -128,7 +135,6 @@ assert(Input.isFilename() && "Invalid input."); 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 @@ -214,7 +214,7 @@ struct FrontendOptions { FrontendOptions() : showHelp(false), showVersion(false), instrumentedParse(false), - needProvenanceRangeToCharBlockMappings(false) {} + showColors(false), needProvenanceRangeToCharBlockMappings(false) {} /// Show the -help text. unsigned showHelp : 1; @@ -225,6 +225,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; } @@ -497,6 +497,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/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)