[warning control] New #pragma GCC diagnostic

DJ Delorie dj@redhat.com
Fri Jan 20 20:50:00 GMT 2006


> * Have -Werror act like a -Werror=* wildcard.

First one.  I didn't preset the classify_diagnostics[] array because
that interferes with the -Werror= options, as they're handled in the
order they're encountered.  But, -Werror uses the kind-changing logic
that -Werror= uses, resulting in consistent diagnostics (i.e. errors
always say "error:", warnings always say "warning:").

	* diagnostic.c (diagnostic_count_diagnostic): Move -Werror logic to...
	(diagnostic_report_diagnostic) ... here, and turn them into real errors.

	* gcc.dg/Werror-1.c, gcc.dg/Werror-5.c, gcc.dg/Werror-7.c,
	gcc.dg/Werror-10.c, gcc.dg/Werror-11.c: Adjust expectations.

Index: diagnostic.c
===================================================================
--- diagnostic.c	(revision 110036)
+++ diagnostic.c	(working copy)
@@ -205,24 +205,9 @@ diagnostic_count_diagnostic (diagnostic_
       if (!diagnostic_report_warnings_p ())
         return false;
 
-      /* -Werror can reclassify warnings as errors, but
-	 classify_diagnostic can reclassify it back to a warning.  The
-	 second part of this test detects that case.  */
-      if (!context->warning_as_error_requested
-	  || (context->classify_diagnostic[diagnostic->option_index]
-	      == DK_WARNING))
-        {
-          ++diagnostic_kind_count (context, DK_WARNING);
-          break;
-        }
-      else if (context->issue_warnings_are_errors_message)
-        {
-	  pp_verbatim (context->printer,
-                       "%s: warnings being treated as errors\n", progname);
-          context->issue_warnings_are_errors_message = false;
-        }
+      ++diagnostic_kind_count (context, DK_WARNING);
+      break;
 
-      /* And fall through.  */
     case DK_ERROR:
       ++diagnostic_kind_count (context, DK_ERROR);
       break;
@@ -362,6 +347,8 @@ void
 diagnostic_report_diagnostic (diagnostic_context *context,
 			      diagnostic_info *diagnostic)
 {
+  int maybe_print_warnings_as_errors_message = 0;
+
   if (context->lock > 0)
     {
       /* If we're reporting an ICE in the middle of some other error,
@@ -373,6 +360,17 @@ diagnostic_report_diagnostic (diagnostic
 	error_recursion (context);
     }
 
+  /* If the user requested that warnings be treated as errors, so be
+     it.  Note that we do this before the next block so that
+     individual warnings can be overridden back to warnings with
+     -Wno-error=*.  */
+  if (context->warning_as_error_requested
+      && diagnostic->kind == DK_WARNING)
+    {
+      diagnostic->kind = DK_ERROR;
+      maybe_print_warnings_as_errors_message = 1;
+    }
+
   if (diagnostic->option_index)
     {
       /* This tests if the user provided the appropriate -Wfoo or
@@ -382,13 +380,25 @@ diagnostic_report_diagnostic (diagnostic
       /* This tests if the user provided the appropriate -Werror=foo
 	 option.  */
       if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
-	diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
+	{
+	  diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
+	  maybe_print_warnings_as_errors_message = 0;
+	}
       /* This allows for future extenions, like temporarily disabling
 	 warnings for ranges of source code.  */
       if (diagnostic->kind == DK_IGNORED)
 	return;
     }
 
+  /* If we changed the kind due to -Werror, and didn't override it, we
+     need to print this message.  */
+  if (maybe_print_warnings_as_errors_message)
+    {
+      pp_verbatim (context->printer,
+		   "%s: warnings being treated as errors\n", progname);
+      context->issue_warnings_are_errors_message = false;
+    }
+
   context->lock++;
 
   if (diagnostic_count_diagnostic (context, diagnostic))
Index: testsuite/gcc.dg/Werror-10.c
===================================================================
--- testsuite/gcc.dg/Werror-10.c	(revision 110036)
+++ testsuite/gcc.dg/Werror-10.c	(working copy)
@@ -6,7 +6,7 @@
 
 #pragma GCC diagnostic error "-Walways-true"
 
-void __attribute__((dj)) bar() { }	/* { dg-warning "warning: .* attribute directive ignored" } */
+void __attribute__((dj)) bar() { }	/* { dg-error "error: .* attribute directive ignored" } */
 
 int i;
 
Index: testsuite/gcc.dg/Werror-1.c
===================================================================
--- testsuite/gcc.dg/Werror-1.c	(revision 110036)
+++ testsuite/gcc.dg/Werror-1.c	(working copy)
@@ -9,7 +9,7 @@
 
 #pragma GCC diagnostic error "-Walways-true"
 
-void __attribute__((dj)) bar() { }	/* { dg-warning "warning: .* attribute directive ignored" } */
+void __attribute__((dj)) bar() { }	/* { dg-error "error: .* attribute directive ignored" } */
 
 int i;
 
Index: testsuite/gcc.dg/Werror-11.c
===================================================================
--- testsuite/gcc.dg/Werror-11.c	(revision 110036)
+++ testsuite/gcc.dg/Werror-11.c	(working copy)
@@ -6,7 +6,7 @@
 
 #pragma GCC diagnostic warning "-Walways-true"
 
-void __attribute__((dj)) bar() { }	/* { dg-warning "warning: .* attribute directive ignored" } */
+void __attribute__((dj)) bar() { }	/* { dg-error "error: .* attribute directive ignored" } */
 
 int i;
 
Index: testsuite/gcc.dg/Werror-5.c
===================================================================
--- testsuite/gcc.dg/Werror-5.c	(revision 110036)
+++ testsuite/gcc.dg/Werror-5.c	(working copy)
@@ -4,13 +4,13 @@
 
 /* Make sure -Werror turns warnings in to errors.  */
 
-void __attribute__((dj)) bar() { }	/* { dg-warning "warning: .* attribute directive ignored" } */
+void __attribute__((dj)) bar() { }	/* { dg-error "error: .* attribute directive ignored" } */
 
 int i;
 
 void
 foo ()
 {
-  if (&i)	/* { dg-warning "warning: .* will always evaluate as 'true'" } */
+  if (&i)	/* { dg-error "error: .* will always evaluate as 'true'" } */
     grill ();
 }
Index: testsuite/gcc.dg/Werror-7.c
===================================================================
--- testsuite/gcc.dg/Werror-7.c	(revision 110036)
+++ testsuite/gcc.dg/Werror-7.c	(working copy)
@@ -4,7 +4,7 @@
 
 /* Make sure -Wno-error= overrides -Werror.  */
 
-void __attribute__((dj)) bar() { }	/* { dg-warning "warning: .* attribute directive ignored" } */
+void __attribute__((dj)) bar() { }	/* { dg-error "error: .* attribute directive ignored" } */
 
 int i;
 



More information about the Gcc-patches mailing list