Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -1354,9 +1354,9 @@ 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 : Flag<["-"], "fdiagnostics-color">, Group, Flags<[CoreOption]>, Alias; -def : Flag<["-"], "fno-diagnostics-color">, Group, Flags<[CoreOption]>, Alias; + [CoreOption, FlangOption, FC1Option]>; +def : Flag<["-"], "fdiagnostics-color">, Group, Flags<[CoreOption, FlangOption, FC1Option]>, Alias; +def : Flag<["-"], "fno-diagnostics-color">, Group, Flags<[CoreOption, FlangOption, FC1Option]>, Alias; def fdiagnostics_color_EQ : Joined<["-"], "fdiagnostics-color=">, Group; def fansi_escape_codes : Flag<["-"], "fansi-escape-codes">, Group, Flags<[CoreOption, CC1Option]>, HelpText<"Use ANSI escape codes for diagnostics">, Index: clang/lib/Driver/ToolChains/Flang.h =================================================================== --- clang/lib/Driver/ToolChains/Flang.h +++ clang/lib/Driver/ToolChains/Flang.h @@ -39,6 +39,15 @@ /// \param [out] CmdArgs The list of output command arguments void AddPreprocessingOptions(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const; + + /// Extract diagnostics options from the driver arguments and add them to the + /// command arguments. + /// + /// \param [in] Args The list of input driver arguments + /// \param [out] CmdArgs The list of output command arguments + void AddDiagnosticsOptions(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs) const; + /// Extract other compilation options from the driver arguments and add them /// to the command arguments. /// Index: clang/lib/Driver/ToolChains/Flang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Flang.cpp +++ clang/lib/Driver/ToolChains/Flang.cpp @@ -43,6 +43,13 @@ options::OPT_I, options::OPT_cpp, options::OPT_nocpp}); } +void Flang::AddDiagnosticsOptions(const ArgList &Args, + ArgStringList &CmdArgs) const { + Args.AddAllArgs(CmdArgs, + {options::OPT_fcolor_diagnostics, + options::OPT_fno_color_diagnostics}); +} + void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const { Args.AddAllArgs(CmdArgs, {options::OPT_module_dir, options::OPT_fdebug_module_writer, @@ -100,6 +107,8 @@ AddFortranDialectOptions(Args, CmdArgs); + AddDiagnosticsOptions(Args, CmdArgs); + // Add other compile options AddOtherOptions(Args, CmdArgs); 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) {} + needProvenanceRangeToCharBlockMappings(false), showColors(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 @@ -54,9 +54,10 @@ // Deserialization (from args) //===----------------------------------------------------------------------===// static bool parseShowColorsArgs( - const llvm::opt::ArgList &args, bool defaultColor) { + const llvm::opt::ArgList &args, bool defaultColor = true) { // Color diagnostics default to auto ("on" if terminal supports) in the driver - // but default to off in cc1, needing an explicit OPT_fdiagnostics_color. + // `flang-new` but default to off in `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/Frontend/TextDiagnosticPrinter.cpp =================================================================== --- flang/lib/Frontend/TextDiagnosticPrinter.cpp +++ flang/lib/Frontend/TextDiagnosticPrinter.cpp @@ -46,14 +46,16 @@ // We only emit diagnostics in contexts that lack valid source locations. assert(!info.getLocation().isValid() && - "Diagnostics with valid source location are not supported"); + "Diagnostics with valid source location are not supported"); - Fortran::frontend::TextDiagnostic::printDiagnosticLevel(os, level, - diagOpts->ShowColors); + clang::DiagnosticsEngine &diagsEngine = + const_cast(*info.getDiags()); + Fortran::frontend::TextDiagnostic::printDiagnosticLevel( + os, level, diagsEngine.getShowColors()); Fortran::frontend::TextDiagnostic::printDiagnosticMessage( os, /*IsSupplemental=*/level == clang::DiagnosticsEngine::Note, - diagMessageStream.str(), diagOpts->ShowColors); + diagMessageStream.str(), diagsEngine.getShowColors()); os.flush(); return; Index: flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp =================================================================== --- flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -158,6 +158,11 @@ return false; } + // Honor color diagnostics. + if (flang->getFrontendOpts().showColors) { + flang->getDiagnostics().setShowColors(true); + } + // 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) @@ -119,6 +120,7 @@ ! HELP-FC1-NEXT: -fno-analyzed-objects-for-unparse ! HELP-FC1-NEXT: Do not use the analyzed objects when unparsing ! HELP-FC1-NEXT: -fno-automatic Implies the SAVE attribute for non-automatic local objects in subprograms unless RECURSIVE +! HELP-FC1-NEXT: -fno-color-diagnostics Disable colors in diagnostics ! HELP-FC1-NEXT: -fno-reformat Dump the cooked character stream in -E mode ! HELP-FC1-NEXT: -fopenacc Enable OpenACC ! HELP-FC1-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code.