Changeset 1a69a90


Ignore:
Timestamp:
Nov 21, 2019, 1:03:17 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
49d3128
Parents:
665f432
Message:

Added --colors command line arguments to have more control on whether or not to print errors with colors

Location:
src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Common/SemanticError.cc

    r665f432 r1a69a90  
    149149// Helpers
    150150namespace ErrorHelpers {
     151        Colors colors = Colors::Auto;
     152
     153        static inline bool with_colors() {
     154                return colors == Colors::Auto ? isatty( STDERR_FILENO ) : bool(colors);
     155        }
     156
    151157        const std::string & error_str() {
    152                 static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
     158                static std::string str = with_colors() ? "\e[31merror:\e[39m " : "error: ";
    153159                return str;
    154160        }
    155161
    156162        const std::string & warning_str() {
    157                 static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: ";
     163                static std::string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: ";
    158164                return str;
    159165        }
    160166
    161167        const std::string & bold_ttycode() {
    162                 static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : "";
     168                static std::string str = with_colors() ? "\e[1m" : "";
    163169                return str;
    164170        }
    165171
    166172        const std::string & reset_font_ttycode() {
    167                 static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";
     173                static std::string str = with_colors() ? "\e[0m" : "";
    168174                return str;
    169175        }
  • src/Common/SemanticError.h

    r665f432 r1a69a90  
    9797// Helpers
    9898namespace ErrorHelpers {
     99        enum class Colors {
     100                Never = false,
     101                Always = true,
     102                Auto,
     103        };
     104
     105        extern Colors colors;
     106
    99107        const std::string & error_str();
    100108        const std::string & warning_str();
  • src/main.cc

    r665f432 r1a69a90  
    407407
    408408
    409 static const char optstring[] = ":hlLmNnpP:S:tgwW:D:";
     409static const char optstring[] = ":c:ghlLmNnpP:S:twW:D:";
    410410
    411411enum { PreludeDir = 128 };
    412412static struct option long_opts[] = {
     413        { "colors", required_argument, nullptr, 'c' },
     414        { "gdb", no_argument, nullptr, 'g' },
    413415        { "help", no_argument, nullptr, 'h' },
    414416        { "libcfa", no_argument, nullptr, 'l' },
     
    422424        { "statistics", required_argument, nullptr, 'S' },
    423425        { "tree", no_argument, nullptr, 't' },
    424         { "gdb", no_argument, nullptr, 'g' },
    425426        { "", no_argument, nullptr, 0 },                                        // -w
    426427        { "", no_argument, nullptr, 0 },                                        // -W
     
    430431
    431432static const char * description[] = {
     433        "Use color in diagnostics. WHEN is ‘never’, ‘always’, or ‘auto’.",// -c
     434        "wait for gdb to attach",                                                                       // -g
    432435        "print help message",                                                           // -h
    433436        "generate libcfa.c",                                                            // -l
     
    441444        "<option-list> enable profiling information:\n          counters,heap,time,all,none", // -S
    442445        "building cfa standard lib",                                                                    // -t
    443         "wait for gdb to attach",                                                                       // -g
    444446        "",                                                                                                     // -w
    445447        "",                                                                                                     // -W
     
    512514        while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) {
    513515                switch ( c ) {
     516                  case 'c':                                                                             // diagnostic colors
     517                        if ( strcmp( optarg, "always" ) == 0 ) {
     518                                ErrorHelpers::colors = ErrorHelpers::Colors::Always;
     519                        } else if ( strcmp( optarg, "never" ) == 0 ) {
     520                                ErrorHelpers::colors = ErrorHelpers::Colors::Never;
     521                        } else if ( strcmp( optarg, "auto" ) == 0 ) {
     522                                ErrorHelpers::colors = ErrorHelpers::Colors::Auto;
     523                        } // if
     524                        break;
    514525                  case 'h':                                                                             // help message
    515526                        usage( argv );                                                          // no return
Note: See TracChangeset for help on using the changeset viewer.