Index: gcc/diagnostic.def
===================================================================
--- gcc/diagnostic.def	(revision 197267)
+++ gcc/diagnostic.def	(working copy)
@@ -20,28 +20,28 @@ along with GCC; see the file COPYING3.
    assign this kind to an actual diagnostic, we only use this in
    variables that can hold a kind, to mean they have yet to have a
    kind specified.  I.e. they're uninitialized.  Within the diagnostic
    machinery, this kind also means "don't change the existing kind",
    meaning "no change is specified".  */
-DEFINE_DIAGNOSTIC_KIND (DK_UNSPECIFIED, "")
+DEFINE_DIAGNOSTIC_KIND (DK_UNSPECIFIED, "", "")
 
 /* If a diagnostic is set to DK_IGNORED, it won't get reported at all.
    This is used by the diagnostic machinery when it wants to disable a
    diagnostic without disabling the option which causes it.  */
-DEFINE_DIAGNOSTIC_KIND (DK_IGNORED, "")
+DEFINE_DIAGNOSTIC_KIND (DK_IGNORED, "", "")
 
 /* The remainder are real diagnostic types.  */
-DEFINE_DIAGNOSTIC_KIND (DK_FATAL, "fatal error: ")
-DEFINE_DIAGNOSTIC_KIND (DK_ICE, "internal compiler error: ")
-DEFINE_DIAGNOSTIC_KIND (DK_ERROR, "error: ")
-DEFINE_DIAGNOSTIC_KIND (DK_SORRY, "sorry, unimplemented: ")
-DEFINE_DIAGNOSTIC_KIND (DK_WARNING, "warning: ")
-DEFINE_DIAGNOSTIC_KIND (DK_ANACHRONISM, "anachronism: ")
-DEFINE_DIAGNOSTIC_KIND (DK_NOTE, "note: ")
-DEFINE_DIAGNOSTIC_KIND (DK_DEBUG, "debug: ")
+DEFINE_DIAGNOSTIC_KIND (DK_FATAL, "fatal error: ", COLOR_BOLD ";" COLOR_FG_RED)
+DEFINE_DIAGNOSTIC_KIND (DK_ICE, "internal compiler error: ", COLOR_BOLD ";" COLOR_FG_RED)
+DEFINE_DIAGNOSTIC_KIND (DK_ERROR, "error: ", COLOR_BOLD ";" COLOR_FG_RED)
+DEFINE_DIAGNOSTIC_KIND (DK_SORRY, "sorry, unimplemented: ", COLOR_BOLD ";" COLOR_FG_RED)
+DEFINE_DIAGNOSTIC_KIND (DK_WARNING, "warning: ", COLOR_BOLD ";" COLOR_FG_MAGENTA)
+DEFINE_DIAGNOSTIC_KIND (DK_ANACHRONISM, "anachronism: ", COLOR_BOLD ";" COLOR_FG_MAGENTA)
+DEFINE_DIAGNOSTIC_KIND (DK_NOTE, "note: ", COLOR_BOLD ";" COLOR_FG_GREEN)
+DEFINE_DIAGNOSTIC_KIND (DK_DEBUG, "debug: ", COLOR_BOLD ";" COLOR_FG_GREEN)
 /* These two would be re-classified as DK_WARNING or DK_ERROR, so the
 prefix does not matter.  */
-DEFINE_DIAGNOSTIC_KIND (DK_PEDWARN, "pedwarn: ")
-DEFINE_DIAGNOSTIC_KIND (DK_PERMERROR, "permerror: ")
+DEFINE_DIAGNOSTIC_KIND (DK_PEDWARN, "pedwarn: ", "")
+DEFINE_DIAGNOSTIC_KIND (DK_PERMERROR, "permerror: ", "")
 /* This one is just for counting DK_WARNING promoted to DK_ERROR
    due to -Werror and -Werror=warning.  */
-DEFINE_DIAGNOSTIC_KIND (DK_WERROR, "error: ")
+DEFINE_DIAGNOSTIC_KIND (DK_WERROR, "error: ", "")
Index: gcc/diagnostic.c
===================================================================
--- gcc/diagnostic.c	(revision 197267)
+++ gcc/diagnostic.c	(working copy)
@@ -29,10 +29,11 @@ along with GCC; see the file COPYING3.
 #include "demangle.h"
 #include "input.h"
 #include "intl.h"
 #include "backtrace.h"
 #include "diagnostic.h"
+#include "diagnostic-color.h"
 
 #define pedantic_warning_kind(DC)			\
   ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
 #define permissive_error_option(DC) ((DC)->opt_permissive)
@@ -130,10 +131,11 @@ diagnostic_initialize (diagnostic_contex
   context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
   for (i = 0; i < n_opts; i++)
     context->classify_diagnostic[i] = DK_UNSPECIFIED;
   context->show_caret = false;
   diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
+  context->show_color = false;
   context->show_option_requested = false;
   context->abort_on_error = false;
   context->show_column = false;
   context->pedantic_errors = false;
   context->permissive = false;
@@ -208,27 +210,43 @@ diagnostic_set_info (diagnostic_info *di
 char *
 diagnostic_build_prefix (diagnostic_context *context,
 			 const diagnostic_info *diagnostic)
 {
   static const char *const diagnostic_kind_text[] = {
-#define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
+#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
 #include "diagnostic.def"
 #undef DEFINE_DIAGNOSTIC_KIND
     "must-not-happen"
   };
   const char *text = _(diagnostic_kind_text[diagnostic->kind]);
+
+  static const char *const diagnostic_kind_color[] = {
+#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
+#include "diagnostic.def"
+#undef DEFINE_DIAGNOSTIC_KIND
+    "must-not-happen"
+  };
+
+  char buffer[256];
+
+  if (context->show_color) {
+    colorize_text (buffer, 256, diagnostic_kind_color[diagnostic->kind], text);
+  } else {
+    strncpy (buffer, text, 256);
+  }
+
   expanded_location s = expand_location_to_spelling_point (diagnostic->location);
   if (diagnostic->override_column)
     s.column = diagnostic->override_column;
   gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
-
+  
   return
     (s.file == NULL
-     ? build_message_string ("%s: %s", progname, text)
+     ? build_message_string ("%s: %s", progname, buffer)
      : context->show_column
-     ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
-     : build_message_string ("%s:%d: %s", s.file, s.line, text));
+     ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, buffer)
+     : build_message_string ("%s:%d: %s", s.file, s.line, buffer));
 }
 
 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
    MAX_WIDTH by some margin, then adjust the start of the line such
    that the COLUMN is smaller than MAX_WIDTH minus the margin.  The
Index: gcc/diagnostic.h
===================================================================
--- gcc/diagnostic.h	(revision 197267)
+++ gcc/diagnostic.h	(working copy)
@@ -100,10 +100,13 @@ struct diagnostic_context
 
   /* True if we should print the source line with a caret indicating
      the location.  */
   bool show_caret;
 
+  /* True if we should colorize the output.  */
+  bool show_color;
+
   /* Maximum width of the source line printed.  */
   int caret_max_width;
 
   /* True if we should print the command line option which controls
      each diagnostic, if known.  */
@@ -293,6 +296,8 @@ void diagnostic_set_caret_max_width (dia
 
 
 /* Pure text formatting support functions.  */
 extern char *file_name_as_prefix (const char *);
 
+extern bool colorize_init (void);
+
 #endif /* ! GCC_DIAGNOSTIC_H */
Index: gcc/diagnostic-core.h
===================================================================
--- gcc/diagnostic-core.h	(revision 197267)
+++ gcc/diagnostic-core.h	(working copy)
@@ -26,11 +26,11 @@ along with GCC; see the file COPYING3.
 #include "bversion.h"
 
 /* Constants used to discriminate diagnostics.  */
 typedef enum
 {
-#define DEFINE_DIAGNOSTIC_KIND(K, msgid) K,
+#define DEFINE_DIAGNOSTIC_KIND(K, msgid, C) K,
 #include "diagnostic.def"
 #undef DEFINE_DIAGNOSTIC_KIND
   DK_LAST_DIAGNOSTIC_KIND,
   /* This is used for tagging pragma pops in the diagnostic
      classification history chain.  */
Index: gcc/toplev.c
===================================================================
--- gcc/toplev.c	(revision 197267)
+++ gcc/toplev.c	(working copy)
@@ -1102,10 +1102,12 @@ general_init (const char *argv0)
      finalizer -- for tokens resulting from macro expansion.  */
   diagnostic_finalizer (global_dc) = virt_loc_aware_diagnostic_finalizer;
 
   global_dc->show_caret
     = global_options_init.x_flag_diagnostics_show_caret;
+  global_dc->show_color
+    = global_options_init.x_flag_diagnostics_show_color;
   global_dc->show_option_requested
     = global_options_init.x_flag_diagnostics_show_option;
   global_dc->show_column
     = global_options_init.x_flag_show_column;
   global_dc->internal_error = plugins_internal_error_function;
Index: gcc/dwarf2out.c
===================================================================
--- gcc/dwarf2out.c	(revision 197267)
+++ gcc/dwarf2out.c	(working copy)
@@ -18826,10 +18826,11 @@ gen_producer_string (void)
       case OPT_gno_record_gcc_switches:
       case OPT__output_pch_:
       case OPT_fdiagnostics_show_location_:
       case OPT_fdiagnostics_show_option:
       case OPT_fdiagnostics_show_caret:
+      case OPT_fdiagnostics_show_color:
       case OPT_fverbose_asm:
       case OPT____:
       case OPT__sysroot_:
       case OPT_nostdinc:
       case OPT_nostdinc__:
Index: gcc/opts.c
===================================================================
--- gcc/opts.c	(revision 197267)
+++ gcc/opts.c	(working copy)
@@ -1495,10 +1495,17 @@ common_handle_option (struct gcc_options
  
     case OPT_fdiagnostics_show_caret:
       dc->show_caret = value;
       break;
 
+    case OPT_fdiagnostics_show_color:
+      if (value)
+        dc->show_color = colorize_init ();
+      else 
+        dc->show_color = false;
+      break;
+
     case OPT_fdiagnostics_show_option:
       dc->show_option_requested = value;
       break;
 
     case OPT_fdump_:
Index: gcc/diagnostic-color.c
===================================================================
--- gcc/diagnostic-color.c	(revision 0)
+++ gcc/diagnostic-color.c	(revision 0)
@@ -0,0 +1,133 @@
+/* Output colorization.
+   Copyright 2011-2013 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+   02110-1301, USA.  */
+
+#include "diagnostic-color.h"
+
+/* Select Graphic Rendition (SGR, "\33[...m") strings.  */
+/* Also Erase in Line (EL) to Right ("\33[K") by default.  */
+/*    Why have EL to Right after SGR?
+         -- The behavior of line-wrapping when at the bottom of the
+            terminal screen and at the end of the current line is often
+            such that a new line is introduced, entirely cleared with
+            the current background color which may be different from the
+            default one (see the boolean back_color_erase terminfo(5)
+            capability), thus scrolling the display by one line.
+            The end of this new line will stay in this background color
+            even after reverting to the default background color with
+            "\33[m', unless it is explicitly cleared again with "\33[K"
+            (which is the behavior the user would instinctively expect
+            from the whole thing).  There may be some unavoidable
+            background-color flicker at the end of this new line because
+            of this (when timing with the monitor's redraw is just right).
+         -- The behavior of HT (tab, "\t") is usually the same as that of
+            Cursor Forward Tabulation (CHT) with a default parameter
+            of 1 ("\33[I"), i.e., it performs pure movement to the next
+            tab stop, without any clearing of either content or screen
+            attributes (including background color); try
+               printf 'asdfqwerzxcv\rASDF\tZXCV\n'
+            in a bash(1) shell to demonstrate this.  This is not what the
+            user would instinctively expect of HT (but is ok for CHT).
+            The instinctive behavior would include clearing the terminal
+            cells that are skipped over by HT with blank cells in the
+            current screen attributes, including background color;
+            the boolean dest_tabs_magic_smso terminfo(5) capability
+            indicates this saner behavior for HT, but only some rare
+            terminals have it (although it also indicates a special
+            glitch with standout mode in the Teleray terminal for which
+            it was initially introduced).  The remedy is to add "\33K"
+            after each SGR sequence, be it START (to fix the behavior
+            of any HT after that before another SGR) or END (to fix the
+            behavior of an HT in default background color that would
+            follow a line-wrapping at the bottom of the screen in another
+            background color, and to complement doing it after START).
+            Piping grep's output through a pager such as less(1) avoids
+            any HT problems since the pager performs tab expansion.
+
+      Generic disadvantages of this remedy are:
+         -- Some very rare terminals might support SGR but not EL (nobody
+            will use "grep --color" on a terminal that does not support
+            SGR in the first place).
+         -- Having these extra control sequences might somewhat complicate
+            the task of any program trying to parse "grep --color"
+            output in order to extract structuring information from it.
+      A specific disadvantage to doing it after SGR START is:
+         -- Even more possible background color flicker (when timing
+            with the monitor's redraw is just right), even when not at the
+            bottom of the screen.
+      There are no additional disadvantages specific to doing it after
+      SGR END.
+
+      It would be impractical for GNU grep to become a full-fledged
+      terminal program linked against ncurses or the like, so it will
+      not detect terminfo(5) capabilities.  */
+static const char *sgr_start = "\33[%sm\33[K";
+static const char *sgr_end   = "\33[m\33[K";
+
+int
+colorize_start (char *buffer, size_t n, char const *sgr_seq)
+{
+  return snprintf(buffer, n, sgr_start, sgr_seq);
+}
+
+int
+colorize_stop (char *buffer, size_t n)
+{
+  return snprintf(buffer, n, sgr_end);
+}
+
+int
+colorize_text(char *buffer, size_t n, const char * sgr_seq, const char * text)
+{
+  int num;
+  num = colorize_start(buffer, n, sgr_seq);
+  num += snprintf(buffer + num, n - num, text);
+  num += colorize_stop(buffer + num, n - num);
+  return num;
+}
+
+#if defined(_WIN32)
+int
+should_colorize (void)
+{
+}
+
+bool colorize_init(void)
+{
+  return false;
+}
+#else
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/* Return non-zero if we should highlight matches in output to file
+   descriptor FD.  */
+int
+should_colorize (void)
+{
+  char const *t = getenv ("TERM");
+  return t && strcmp (t, "dumb") != 0;
+}
+
+bool colorize_init(void)
+{
+  return isatty (STDOUT_FILENO) && should_colorize ();
+}
+#endif
Index: gcc/diagnostic-color.h
===================================================================
--- gcc/diagnostic-color.h	(revision 0)
+++ gcc/diagnostic-color.h	(revision 0)
@@ -0,0 +1,119 @@
+/* Copyright (C) 2013 Free Software Foundation, Inc.
+   Contributed by Manuel Lopez-Ibanez <manu@gcc.gnu.org>
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+/* Based on code from: */
+/* grep.c - main driver file for grep.
+   Copyright (C) 1992, 1997-2002, 2004-2013 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+   02110-1301, USA.  
+
+   Written July 1992 by Mike Haertel.  */
+
+#ifndef GCC_DIAGNOSTIC_COLOR_H
+#define GCC_DIAGNOSTIC_COLOR_H
+
+#include <stdio.h>
+
+/* The context and logic for choosing default --color screen attributes
+   (foreground and background colors, etc.) are the following.
+      -- There are eight basic colors available, each with its own
+         nominal luminosity to the human eye and foreground/background
+         codes (black [0 %, 30/40], blue [11 %, 34/44], red [30 %, 31/41],
+         magenta [41 %, 35/45], green [59 %, 32/42], cyan [70 %, 36/46],
+         yellow [89 %, 33/43], and white [100 %, 37/47]).
+      -- Sometimes, white as a background is actually implemented using
+         a shade of light gray, so that a foreground white can be visible
+         on top of it (but most often not).
+      -- Sometimes, black as a foreground is actually implemented using
+         a shade of dark gray, so that it can be visible on top of a
+         background black (but most often not).
+      -- Sometimes, more colors are available, as extensions.
+      -- Other attributes can be selected/deselected (bold [1/22],
+         underline [4/24], standout/inverse [7/27], blink [5/25], and
+         invisible/hidden [8/28]).  They are sometimes implemented by
+         using colors instead of what their names imply; e.g., bold is
+         often achieved by using brighter colors.  In practice, only bold
+         is really available to us, underline sometimes being mapped by
+         the terminal to some strange color choice, and standout best
+         being left for use by downstream programs such as less(1).
+      -- We cannot assume that any of the extensions or special features
+         are available for the purpose of choosing defaults for everyone.
+      -- The most prevalent default terminal backgrounds are pure black
+         and pure white, and are not necessarily the same shades of
+         those as if they were selected explicitly with SGR sequences.
+         Some terminals use dark or light pictures as default background,
+         but those are covered over by an explicit selection of background
+         color with an SGR sequence; their users will appreciate their
+         background pictures not be covered like this, if possible.
+      -- Some uses of colors attributes is to make some output items
+         more understated (e.g., context lines); this cannot be achieved
+         by changing the background color.
+      -- For these reasons, the grep color defaults should strive not
+         to change the background color from its default, unless it's
+         for a short item that should be highlighted, not understated.
+      -- The grep foreground color defaults (without an explicitly set
+         background) should provide enough contrast to be readable on any
+         terminal with either a black (dark) or white (light) background.
+         This only leaves red, magenta, green, and cyan (and their bold
+         counterparts) and possibly bold blue.  */
+/* The color strings used for matched text.
+   The user can overwrite them using the deprecated
+   environment variable GREP_COLOR or the new GREP_COLORS.  */
+#define COLOR_NONE             "00" 
+#define COLOR_BOLD             "01" 
+#define COLOR_UNDERSCORE       "04" 
+#define COLOR_BLINK            "05" 
+#define COLOR_REVERSE          "07" 
+#define COLOR_FG_BLACK         "30" 
+#define COLOR_FG_RED           "31" 
+#define COLOR_FG_GREEN         "32" 
+#define COLOR_FG_YELLOW        "33" 
+#define COLOR_FG_BLUE          "34" 
+#define COLOR_FG_MAGENTA       "35" 
+#define COLOR_FG_CYAN          "36" 
+#define COLOR_FG_WHITE         "37" 
+#define COLOR_BG_BLACK         "40" 
+#define COLOR_BG_RED           "41" 
+#define COLOR_BG_GREEN         "42" 
+#define COLOR_BG_YELLOW        "43" 
+#define COLOR_BG_BLUE          "44" 
+#define COLOR_BG_MAGENTA       "45" 
+#define COLOR_BG_CYAN          "46" 
+#define COLOR_BG_WHITE         "47"  
+
+extern int colorize_start (char *buffer, size_t n, char const *sgr_seq);
+extern int colorize_stop (char *buffer, size_t n);
+extern int colorize_text(char *buffer, size_t n, const char * sgr_seq, const char * text);
+extern bool colorize_init (void);
+
+
+#endif /* ! GCC_DIAGNOSTIC_COLOR_H */
Index: gcc/common.opt
===================================================================
--- gcc/common.opt	(revision 197267)
+++ gcc/common.opt	(working copy)
@@ -1021,10 +1021,14 @@ Enum(diagnostic_prefixing_rule) String(e
 
 fdiagnostics-show-caret
 Common Var(flag_diagnostics_show_caret) Init(1)
 Show the source line with a caret indicating the column
 
+fdiagnostics-show-color
+Common Var(flag_diagnostics_show_color) Init(0)
+Colorize diagnostics
+
 fdiagnostics-show-option
 Common Var(flag_diagnostics_show_option) Init(1)
 Amend appropriate diagnostic messages with the command line option that controls them
 
 fdisable-
Index: gcc/Makefile.in
===================================================================
--- gcc/Makefile.in	(revision 197267)
+++ gcc/Makefile.in	(working copy)
@@ -1463,11 +1463,11 @@ OBJS = \
 	$(EXTRA_OBJS) \
 	$(host_hook_obj)
 
 # Objects in libcommon.a, potentially used by all host binaries and with
 # no target dependencies.
-OBJS-libcommon = diagnostic.o pretty-print.o intl.o input.o version.o
+OBJS-libcommon = diagnostic.o diagnostic-color.o pretty-print.o intl.o input.o version.o
 
 # Objects in libcommon-target.a, used by drivers and by the core
 # compiler and containing target-dependent code.
 OBJS-libcommon-target = $(common_out_object_file) prefix.o params.o \
 	opts.o opts-common.o options.o vec.o hooks.o common/common-targhooks.o \
@@ -2666,11 +2666,12 @@ fold-const.o : fold-const.c $(CONFIG_H)
    $(TREE_H) $(FLAGS_H) $(DIAGNOSTIC_CORE_H) $(HASH_TABLE_H) $(EXPR_H) \
    $(RTL_H) $(GGC_H) $(TM_P_H) langhooks.h $(MD5_H) intl.h $(TARGET_H) \
    $(GIMPLE_H) realmpfr.h $(TREE_FLOW_H)
 diagnostic.o : diagnostic.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
    version.h $(DEMANGLE_H) $(INPUT_H) intl.h $(BACKTRACE_H) $(DIAGNOSTIC_H) \
-   diagnostic.def
+   diagnostic.def diagnostic-color.h
+diagnostic-color.o : diagnostic-color.c $(CONFIG_H) $(SYSTEM_H) coretypes.h 
 opts.o : opts.c $(OPTS_H) $(OPTIONS_H) $(DIAGNOSTIC_CORE_H) $(CONFIG_H) $(SYSTEM_H) \
    coretypes.h $(DUMPFILE_H) $(TM_H) \
    $(DIAGNOSTIC_H) insn-attr-common.h intl.h $(COMMON_TARGET_H) \
    $(FLAGS_H) $(PARAMS_H) opts-diagnostic.h
 opts-global.o : opts-global.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
