Patchwork fix PR 44782, implement -fmax-errors for C-family languages

login
register
mail settings
Submitter Nathan Froyd
Date Nov. 9, 2010, 2:19 p.m.
Message ID <20101109141929.GB7991@nightcrawler>
Download mbox | patch
Permalink /patch/70550/
State New
Headers show

Comments

Nathan Froyd - Nov. 9, 2010, 2:19 p.m.
The patch below implements -fmax-errors for the C family of languages,
as requested by PR 44782.  I chose -fmax-errors rather than
-ferror-limit for compatibility with the Fortran front end's option.  I
suppose if people wanted, we could add -ferror-limit as an undocumented
alias.

Needs C approval for the c-family changes and a diagnostic maintainer's
approval for diagnostic.* changes.

Tested on x86_64-unknown-linux-gnu.  OK to commit?

-Nathan

gcc/
	PR c/44782
	* diagnostic.h (struct diagnostic_context): Add max_errors field.
	* diagnostic.c (diagnostic_initialize): Initialize it.
	(diagnostic_action_after_output): Exit if more than max_errors
	have been output.
	* doc/invoke.texi (Warning Options): Add -fmax-errors.
	(-fmax-errors): Document.

gcc/c-family/
	PR c/44782
	* c.opt (fmax-errors=): New option.
	* c-opts.c (c_common_handle_option) [OPT_fmax_errors_]: Handle it.

gcc/testsuite/
	PR c/44782
	* c-c++-common/fmax-errors.c: New test.
Joseph S. Myers - Nov. 9, 2010, 3:28 p.m.
On Tue, 9 Nov 2010, Nathan Froyd wrote:

> The patch below implements -fmax-errors for the C family of languages,
> as requested by PR 44782.  I chose -fmax-errors rather than
> -ferror-limit for compatibility with the Fortran front end's option.  I
> suppose if people wanted, we could add -ferror-limit as an undocumented
> alias.
> 
> Needs C approval for the c-family changes and a diagnostic maintainer's
> approval for diagnostic.* changes.

I don't think this should be a C-family option; just like -fshow-column, 
for example, it should be a generic option in common.opt.  If some 
languages' diagnostics aren't affected by it because they don't go through 
the common machinery, that is a problem with those languages (and maybe a 
consequence of missing features in the generic diagnostic code that need 
implementing before those languages can switch to it).
Gabriel Dos Reis - Nov. 10, 2010, 12:26 a.m.
On Tue, Nov 9, 2010 at 9:28 AM, Joseph S. Myers <joseph@codesourcery.com> wrote:
> On Tue, 9 Nov 2010, Nathan Froyd wrote:
>
>> The patch below implements -fmax-errors for the C family of languages,
>> as requested by PR 44782.  I chose -fmax-errors rather than
>> -ferror-limit for compatibility with the Fortran front end's option.  I
>> suppose if people wanted, we could add -ferror-limit as an undocumented
>> alias.
>>
>> Needs C approval for the c-family changes and a diagnostic maintainer's
>> approval for diagnostic.* changes.
>
> I don't think this should be a C-family option; just like -fshow-column,
> for example, it should be a generic option in common.opt.  If some
> languages' diagnostics aren't affected by it because they don't go through
> the common machinery, that is a problem with those languages (and maybe a
> consequence of missing features in the generic diagnostic code that need
> implementing before those languages can switch to it).

agreed.

-- Gaby

Patch

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 8f60834..1120298 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -525,6 +525,10 @@  c_common_handle_option (size_t scode, const char *arg, int value,
       global_dc->warning_as_error_requested = value;
       break;
 
+    case OPT_fmax_errors_:
+      global_dc->max_errors = value;
+      break;
+
     case OPT_Wformat:
       set_Wformat (value);
       break;
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 01ce661..77b0343 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -821,6 +821,10 @@  flax-vector-conversions
 C ObjC C++ ObjC++ Var(flag_lax_vector_conversions)
 Allow implicit conversions between vectors with differing numbers of subparts and/or differing element types.
 
+fmax-errors=
+C ObjC C++ ObjC++ Joined RejectNegative UInteger
+-fmax-errors=<number>	Specify maximum number of errors to report
+
 fms-extensions
 C ObjC C++ ObjC++ Var(flag_ms_extensions)
 Don't warn about uses of Microsoft extensions
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index 40ddc23..d297cdd 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -109,6 +109,7 @@  diagnostic_initialize (diagnostic_context *context, int n_opts)
   context->fatal_errors = false;
   context->dc_inhibit_warnings = false;
   context->dc_warn_system_headers = false;
+  context->max_errors = 0;
   context->internal_error = NULL;
   diagnostic_starter (context) = default_diagnostic_starter;
   diagnostic_finalizer (context) = default_diagnostic_finalizer;
@@ -219,6 +220,17 @@  diagnostic_action_after_output (diagnostic_context *context,
 	  diagnostic_finish (context);
 	  exit (FATAL_EXIT_CODE);
 	}
+      if (context->max_errors != 0
+	  && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
+			  + diagnostic_kind_count (context, DK_SORRY))
+	      >= context->max_errors))
+	{
+	  fnotice (stderr,
+		   "compilation terminated due to -fmax-errors=%u.\n",
+		   context->max_errors);
+	  diagnostic_finish (context);
+	  exit (FATAL_EXIT_CODE);
+	}
       break;
 
     case DK_ICE:
diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h
index 99671c6..8074354 100644
--- a/gcc/diagnostic.h
+++ b/gcc/diagnostic.h
@@ -128,6 +128,9 @@  struct diagnostic_context
   /* True if warnings should be given in system headers.  */
   bool dc_warn_system_headers;
 
+  /* Maximum number of errors to report.  */
+  unsigned int max_errors;
+
   /* This function is called before any message is printed out.  It is
      responsible for preparing message prefix and such.  For example, it
      might say:
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index d3b702b..57014c0 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -229,7 +229,8 @@  Objective-C and Objective-C++ Dialects}.
 
 @item Warning Options
 @xref{Warning Options,,Options to Request or Suppress Warnings}.
-@gccoptlist{-fsyntax-only  -pedantic  -pedantic-errors @gol
+@gccoptlist{-fsyntax-only  fmax-errors=@var{n}  -pedantic @gol
+-pedantic-errors @gol
 -w  -Wextra  -Wall  -Waddress  -Waggregate-return  -Warray-bounds @gol
 -Wno-attributes -Wno-builtin-macro-redefined @gol
 -Wc++-compat -Wc++0x-compat -Wcast-align  -Wcast-qual  @gol
@@ -2775,6 +2776,15 @@  warnings but control the kinds of diagnostics produced by GCC.
 @opindex fsyntax-only
 Check the code for syntax errors, but don't do anything beyond that.
 
+@item -fmax-errors=@var{n}
+@opindex fmax-errors
+Limits the maximum number of error messages to @var{n}, at which point
+GCC bails out rather than attempting to continue processing the source
+code.  If @var{n} is 0 (the default), there is no limit on the number
+of error messages produced.  If @option{-Wfatal-errors} is also
+specified, then @option{-Wfatal-errors} takes precedence over this
+option.
+
 @item -w
 @opindex w
 Inhibit all warning messages.

diff --git a/gcc/testsuite/c-c++-common/fmax-errors.c b/gcc/testsuite/c-c++-common/fmax-errors.c
new file mode 100644
index 0000000..1ef78eb
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/fmax-errors.c
@@ -0,0 +1,11 @@ 
+/* PR c/44782 */
+/* { dg-do compile } */
+/* { dg-options "-fmax-errors=3" } */
+
+void foo (unsigned int i, unsigned int j)
+{
+  (i) ();			/* { dg-error "" } */
+  (j) ();			/* { dg-error "" } */
+  (i+j) ();			/* { dg-error "" } */
+  (i*j) ();			/* no error here due to -fmax-errors */
+} /* { dg-prune-output "compilation terminated" } */