Patch to diagnostic library

Gabriel Dos Reis gdr@codesourcery.com
Mon Aug 21 03:23:00 GMT 2000


This patch put in more flexibility in the diagnostic library.  The
default line-wrap cutoff is changed to 80 in the C++ front-end.

Bootstrapped and tested on i686-pc-linux.  Installed.

The next patches on diagnostics will be (hopefully) C++ specific.

-- Gaby
CodeSourcery, LLC		http://www.codesourcery.com

	* diagnostic.c (context_as_prefix): Export.
	(need_error_newline): Remove.
	(lang_diagnostic_starter, lang_diagnostic_finalizer): New objects.
	(error_module_changed, record_last_error_module,
	error_function_changed, record_last_error_function): New functions.
	(initialize_diagnostics): Default intialize
	lang_diagnostic_starter, lang_diagnostic_finalizer.
	(init_output_buffer): Tweak.
	(file_name_as_prefix): New function.
	(announce_function, default_print_error_function,
	report_error_function, set_diagnostic_context): Tweak.

cp/
	* lex.c (lang_init_options): Default diagnostic message maximum
	length to 80, when line-wrapping.

Index: diagnostic.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/diagnostic.h,v
retrieving revision 1.10
diff -p -r1.10 diagnostic.h
*** diagnostic.h	2000/08/20 09:50:12	1.10
--- diagnostic.h	2000/08/21 10:03:17
*************** Boston, MA 02111-1307, USA.  */
*** 27,32 ****
--- 27,35 ----
  /*  Forward declarations.  */
  typedef struct output_buffer output_buffer;
  typedef struct diagnostic_context diagnostic_context;
+ typedef void (*diagnostic_starter_fn) PARAMS ((output_buffer *,
+                                                diagnostic_context *));
+ typedef diagnostic_starter_fn diagnostic_finalizer_fn;
  
  #define DIAGNOSTICS_SHOW_PREFIX_ONCE       0x0
  #define DIAGNOSTICS_SHOW_PREFIX_NEVER      0x1
*************** typedef struct
*** 50,55 ****
--- 53,62 ----
    int ideal_maximum_length;
    /* Nonzero if current PREFIX was emitted at least once.  */
    int emitted_prefix_p;
+ 
+   /* Nonzero means one should emit a newline before outputing anything.  */
+   int need_newline_p;
+ 
    /* Tells how often current PREFIX should be emitted:
       o DIAGNOSTICS_SHOW_PREFIX_NEVER: never - not yet supported;
       o DIAGNOSTICS_SHOW_PREFIX_ONCE: emit current PREFIX only once;
*************** struct output_buffer
*** 79,84 ****
--- 86,92 ----
  
  #define output_buffer_text_cursor(BUFFER) (BUFFER)->state.cursor
  #define output_buffer_format_args(BUFFER) *((BUFFER)->state.format_args)
+ #define output_needs_newline(BUFFER) (BUFFER)->state.need_newline_p
  
  /* This data structure bundles altogether any information relevent to
     the context of a diagnostic message.  */
*************** struct diagnostic_context
*** 137,142 ****
--- 145,153 ----
  
  extern printer_fn lang_printer;
  
+ extern diagnostic_starter_fn lang_diagnostic_starter;
+ extern diagnostic_finalizer_fn lang_diagnostic_finalizer;
+ 
  extern int diagnostic_message_length_per_line;
  
  /* This output buffer is used by front-ends that directly output
*************** void set_message_prefixing_rule PARAMS (
*** 178,182 ****
--- 189,200 ----
  void output_verbatim            PARAMS ((output_buffer *, const char *, ...))
       ATTRIBUTE_PRINTF_2;
  void verbatim PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
+ char *context_as_prefix         PARAMS ((const char *, int, int));
+ char *file_name_as_prefix       PARAMS ((const char *));
+ int error_module_changed        PARAMS ((void));
+ void record_last_error_module   PARAMS ((void));
+ int error_function_changed      PARAMS ((void));
+ void record_last_error_function PARAMS ((void));
+      
  
  #endif /* __GCC_DIAGNOSTIC_H__ */
Index: diagnostic.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/diagnostic.c,v
retrieving revision 1.30
diff -p -r1.30 diagnostic.c
*** diagnostic.c	2000/08/20 09:50:12	1.30
--- diagnostic.c	2000/08/21 10:03:17
*************** static void output_format PARAMS ((outpu
*** 71,77 ****
  static char *vbuild_message_string PARAMS ((const char *, va_list));
  static char *build_message_string PARAMS ((const char *, ...))
       ATTRIBUTE_PRINTF_1;
- static char *context_as_prefix PARAMS ((const char *, int, int));
  static void output_do_printf PARAMS ((output_buffer *, const char *));
  static void format_with_decl PARAMS ((output_buffer *, tree));
  static void file_and_line_for_asm PARAMS ((rtx, const char **, int *));
--- 71,76 ----
*************** static char digit_buffer[128];
*** 121,128 ****
  static output_buffer global_output_buffer;
  output_buffer *diagnostic_buffer = &global_output_buffer;
  
- static int need_error_newline;
- 
  /* Function of last error message;
     more generally, function such that if next error message is in it
     then we don't have to mention the function name.  */
--- 120,125 ----
*************** static int last_error_tick;
*** 137,142 ****
--- 134,143 ----
  void (*print_error_function) PARAMS ((const char *)) =
    default_print_error_function;
  
+ /* Hooks for language specific diagnostic messages pager and finalizer.  */
+ diagnostic_starter_fn lang_diagnostic_starter;
+ diagnostic_finalizer_fn lang_diagnostic_finalizer;
+ 
  /* Maximum characters per line in automatic line wrapping mode.
     Zero means don't wrap lines. */
  
*************** static int current_prefixing_rule;
*** 150,155 ****
--- 151,186 ----
  static int diagnostic_lock;
  
  
+ /* Return truthvalue if current input file is different from the most recent
+    file involved in a diagnostic message.  */
+ int
+ error_module_changed ()
+ {
+   return last_error_tick != input_file_stack_tick;
+ }
+ 
+ /* Remember current file as being the most recent file involved in a
+    diagnostic message.  */
+ void
+ record_last_error_module ()
+ {
+   last_error_tick = input_file_stack_tick;
+ }
+ 
+ /* Same as error_module_changed, but for function.  */
+ int
+ error_function_changed ()
+ {
+   return last_error_function != current_function_decl;
+ }
+ 
+ /* Same as record_last_error_module, but for function.  */
+ void
+ record_last_error_function ()
+ {
+   last_error_function = current_function_decl;
+ }
+ 
  /* Initialize the diagnostic message outputting machinery.  */
  
  void
*************** initialize_diagnostics ()
*** 161,166 ****
--- 192,200 ----
  
    /* Proceed to actual initialization.  */
    default_initialize_buffer (diagnostic_buffer);
+ 
+   lang_diagnostic_starter = default_diagnostic_starter;
+   lang_diagnostic_finalizer = default_diagnostic_finalizer;
  }
  
  void
*************** init_output_buffer (buffer, prefix, maxi
*** 278,283 ****
--- 312,318 ----
       const char *prefix;
       int maximum_length;
  {
+   bzero (buffer, sizeof (output_buffer));
    obstack_init (&buffer->obstack);
    ideal_line_wrap_cutoff (buffer) = maximum_length;
    prefixing_policy (buffer) = current_prefixing_rule;
*************** build_message_string VPARAMS ((const cha
*** 744,754 ****
    return str;
  }
  
- 
  /* Return a malloc'd string describing a location.  The caller is
     responsible for freeing the memory.  */
! 
! static char *
  context_as_prefix (file, line, warn)
       const char *file;
       int line;
--- 779,787 ----
    return str;
  }
  
  /* Return a malloc'd string describing a location.  The caller is
     responsible for freeing the memory.  */
! char *
  context_as_prefix (file, line, warn)
       const char *file;
       int line;
*************** context_as_prefix (file, line, warn)
*** 770,775 ****
--- 803,816 ----
      }
  }
  
+ /* Same as context_as_prefix, but only the source FILE is given.  */
+ char *
+ file_name_as_prefix (f)
+      const char *f;
+ {
+   return build_message_string ("%s: ", f);
+ }
+ 
  /* Format a MESSAGE into BUFFER.  Automatically wrap lines.  */
  
  static void
*************** announce_function (decl)
*** 1150,1157 ****
        else
          verbatim (" %s", (*decl_printable_name) (decl, 2));
        fflush (stderr);
!       need_error_newline = 1;
!       last_error_function = current_function_decl;
      }
  }
  
--- 1191,1198 ----
        else
          verbatim (" %s", (*decl_printable_name) (decl, 2));
        fflush (stderr);
!       output_needs_newline (diagnostic_buffer) = 1;
!       record_last_error_function ();
      }
  }
  
*************** void
*** 1162,1168 ****
  default_print_error_function (file)
    const char *file;
  {
!   if (last_error_function != current_function_decl)
      {
        char *prefix = file ? build_message_string ("%s: ", file) : NULL;
        output_state os;
--- 1203,1209 ----
  default_print_error_function (file)
    const char *file;
  {
!   if (error_function_changed ())
      {
        char *prefix = file ? build_message_string ("%s: ", file) : NULL;
        output_state os;
*************** default_print_error_function (file)
*** 1187,1193 ****
                 (*decl_printable_name) (current_function_decl, 2));
  	}
  
!       last_error_function = current_function_decl;
        output_to_stream (diagnostic_buffer, stderr);
        diagnostic_buffer->state = os;
        free ((char*) prefix);
--- 1228,1234 ----
                 (*decl_printable_name) (current_function_decl, 2));
  	}
  
!       record_last_error_function ();
        output_to_stream (diagnostic_buffer, stderr);
        diagnostic_buffer->state = os;
        free ((char*) prefix);
*************** report_error_function (file)
*** 1204,1217 ****
  {
    struct file_stack *p;
  
!   if (need_error_newline)
      {
        verbatim ("\n");
!       need_error_newline = 0;
      }
  
    if (input_file_stack && input_file_stack->next != 0
!       && input_file_stack_tick != last_error_tick)
      {
        for (p = input_file_stack->next; p; p = p->next)
  	if (p == input_file_stack->next)
--- 1245,1258 ----
  {
    struct file_stack *p;
  
!   if (output_needs_newline (diagnostic_buffer))
      {
        verbatim ("\n");
!       output_needs_newline (diagnostic_buffer) = 0;
      }
  
    if (input_file_stack && input_file_stack->next != 0
!       && error_function_changed ())
      {
        for (p = input_file_stack->next; p; p = p->next)
  	if (p == input_file_stack->next)
*************** report_error_function (file)
*** 1219,1225 ****
  	else
  	  verbatim (",\n                 from %s:%d", p->name, p->line);
        verbatim (":\n");
!       last_error_tick = input_file_stack_tick;
      }
  
    (*print_error_function) (input_filename);
--- 1260,1266 ----
  	else
  	  verbatim (",\n                 from %s:%d", p->name, p->line);
        verbatim (":\n");
!       record_last_error_function ();
      }
  
    (*print_error_function) (input_filename);
*************** See %s for instructions.",
*** 1616,1623 ****
  
  /* Setup DC for reporting a diagnostic MESSAGE (an error of a WARNING),
     using arguments pointed to by ARGS_PTR, issued at a location specified
!    by FILE and LINE.  Front-ends may override the defaut diagnostic pager
!    and finalizer *after* this subroutine completes.  */
  void
  set_diagnostic_context (dc, message, args_ptr, file, line, warn)
       diagnostic_context *dc;
--- 1657,1663 ----
  
  /* Setup DC for reporting a diagnostic MESSAGE (an error of a WARNING),
     using arguments pointed to by ARGS_PTR, issued at a location specified
!    by FILE and LINE.  */
  void
  set_diagnostic_context (dc, message, args_ptr, file, line, warn)
       diagnostic_context *dc;
*************** set_diagnostic_context (dc, message, arg
*** 1633,1640 ****
    diagnostic_file_location (dc) = file;
    diagnostic_line_location (dc) = line;
    diagnostic_is_warning (dc) = warn;
!   diagnostic_starter (dc) = default_diagnostic_starter;
!   diagnostic_finalizer (dc) = default_diagnostic_finalizer;
  }
  
  static void
--- 1673,1680 ----
    diagnostic_file_location (dc) = file;
    diagnostic_line_location (dc) = line;
    diagnostic_is_warning (dc) = warn;
!   diagnostic_starter (dc) = lang_diagnostic_starter;
!   diagnostic_finalizer (dc) = lang_diagnostic_finalizer;
  }
  
  static void
Index: cp/lex.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/lex.c,v
retrieving revision 1.211
diff -p -r1.211 lex.c
*** lex.c	2000/07/27 04:49:17	1.211
--- lex.c	2000/08/21 10:03:19
*************** lang_init_options ()
*** 385,392 ****
    flag_exceptions = 1;
    /* Mark as "unspecified".  */
    flag_bounds_check = -1;
!   /* By default wrap lines at 72 characters.  */
!   diagnostic_message_length_per_line = 72;
    /* By default, emit location information once for every
       diagnostic message.  */
    set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);
--- 385,393 ----
    flag_exceptions = 1;
    /* Mark as "unspecified".  */
    flag_bounds_check = -1;
!   /* By default wrap lines at 80 characters.  Is getenv ("COLUMNS")
!      preferable?  */
!   diagnostic_message_length_per_line = 80;
    /* By default, emit location information once for every
       diagnostic message.  */
    set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);



More information about the Gcc-patches mailing list