[cfe-dev] -Wfatal-errors

Christian Adåker cadaker at gmail.com
Mon Dec 21 12:11:45 PST 2009


On Mon, Dec 21, 2009 at 9:17 AM, Chris Lattner <clattner at apple.com> wrote:
>
> On Dec 18, 2009, at 2:11 AM, Christian Adåker wrote:
>
>> Hi.
>>
>> I was looking through the TODO file for some simple way of
>> contributing to clang, and saw a note about the -Wfatal-errors flag.
>> It turned out to be pretty straightforward to fix (by looking at
>> -Werror), but I thought I'd send the patch here for comments in case
>> I've missed something.
>
> I don't think this is quite right: -pedantic-errors can map to errors as well in the MAP_IGNORE case right above it.  It's close though!

Oops, that's right. Fixed. However, -Werror doesn't affect the
-pedantic warnings in that way. Shouldn't they be similar?

Also, I've implemented -Wfatal-errors=foo. However, I'm not quite sure
how -Wno-fatal-errors=foo should work. In this patch, it always turns
foo into an error, even if -Werror is not in effect. Is that ok?

//Christian
-------------- next part --------------
Index: include/clang/Basic/Diagnostic.h
===================================================================
--- include/clang/Basic/Diagnostic.h	(revision 91836)
+++ include/clang/Basic/Diagnostic.h	(working copy)
@@ -76,7 +76,10 @@
 
       /// Map this diagnostic to "warning", but make it immune to -Werror.  This
       /// happens when you specify -Wno-error=foo.
-      MAP_WARNING_NO_WERROR = 5
+      MAP_WARNING_NO_WERROR = 5,
+      /// Map this diagnostic to "error", but make it immune to -Wfatal-errors.
+      /// This happens for -Wno-fatal-errors=foo.
+      MAP_ERROR_NO_WFATAL = 6
     };
   }
 
@@ -178,6 +181,7 @@
   unsigned char AllExtensionsSilenced; // Used by __extension__
   bool IgnoreAllWarnings;        // Ignore all warnings: -w
   bool WarningsAsErrors;         // Treat warnings like errors:
+  bool ErrorsAsFatal;            // Treat errors like fatal errors.
   bool SuppressSystemWarnings;   // Suppress warnings in system headers.
   bool SuppressAllDiagnostics;   // Suppress all diagnostics.
   ExtensionHandling ExtBehavior; // Map extensions onto warnings or errors?
@@ -260,6 +264,11 @@
   void setWarningsAsErrors(bool Val) { WarningsAsErrors = Val; }
   bool getWarningsAsErrors() const { return WarningsAsErrors; }
 
+  /// setErrorsAsFatal - When set to true, any error reported is made a
+  /// fatal error.
+  void setErrorsAsFatal(bool Val) { ErrorsAsFatal = Val; }
+  bool getErrorsAsFatal() const { return ErrorsAsFatal; }
+
   /// setSuppressSystemWarnings - When set to true mask warnings that
   /// come from system headers.
   void setSuppressSystemWarnings(bool Val) { SuppressSystemWarnings = Val; }
Index: lib/Frontend/Warnings.cpp
===================================================================
--- lib/Frontend/Warnings.cpp	(revision 91836)
+++ lib/Frontend/Warnings.cpp	(working copy)
@@ -47,8 +47,6 @@
   else
     Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore);
 
-  // FIXME: -Wfatal-errors / -Wfatal-errors=foo
-
   for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
     const std::string &Opt = Opts.Warnings[i];
     const char *OptStart = &Opt[0];
@@ -98,6 +96,31 @@
       OptStart = Specifier;
     }
 
+    // -Wfatal-errors is yet another special case.
+    if (OptEnd-OptStart >= 12 && memcmp(OptStart, "fatal-errors", 12) == 0) {
+      const char* Specifier = 0;
+      if (OptEnd-OptStart != 12) {
+        if ((OptStart[12] != '=' && OptStart[12] != '-') ||
+            OptEnd-OptStart == 13) {
+          fprintf(stderr,
+                  "warning: unknown -Wfatal-errors warning specifier: -W%s\n",
+                  Opt.c_str());
+          continue;
+        }
+        Specifier = OptStart + 13;
+      }
+
+      if (Specifier == 0) {
+        Diags.setErrorsAsFatal(isPositive);
+        continue;
+      }
+
+      // -Wfatal-errors=foo maps foo to Fatal, -Wno-fatal-errors=foo
+      // maps it to Error.
+      Mapping = isPositive ? diag::MAP_FATAL : diag::MAP_ERROR_NO_WFATAL;
+      OptStart = Specifier;
+    }
+
     if (Diags.setDiagnosticGroupMapping(OptStart, Mapping))
       Diags.Report(diag::warn_unknown_warning_option) << ("-W" + Opt);
   }
Index: lib/Basic/Diagnostic.cpp
===================================================================
--- lib/Basic/Diagnostic.cpp	(revision 91836)
+++ lib/Basic/Diagnostic.cpp	(working copy)
@@ -203,6 +203,7 @@
   AllExtensionsSilenced = 0;
   IgnoreAllWarnings = false;
   WarningsAsErrors = false;
+  ErrorsAsFatal = false;
   SuppressSystemWarnings = false;
   SuppressAllDiagnostics = false;
   ExtBehavior = Ext_Ignore;
@@ -326,9 +327,13 @@
       return Diagnostic::Ignored;
     Result = Diagnostic::Warning;
     if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
+    if (Result == Diagnostic::Error && ErrorsAsFatal)
+      Result = Diagnostic::Fatal;
     break;
   case diag::MAP_ERROR:
     Result = Diagnostic::Error;
+    if (ErrorsAsFatal)
+      Result = Diagnostic::Fatal;
     break;
   case diag::MAP_FATAL:
     Result = Diagnostic::Fatal;
@@ -349,6 +354,8 @@
 
     if (WarningsAsErrors)
       Result = Diagnostic::Error;
+    if (Result == Diagnostic::Error && ErrorsAsFatal)
+      Result = Diagnostic::Fatal;
     break;
 
   case diag::MAP_WARNING_NO_WERROR:
@@ -361,6 +368,12 @@
       return Diagnostic::Ignored;
 
     break;
+
+  case diag::MAP_ERROR_NO_WFATAL:
+    // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
+    // unaffected by -Wfatal-errors.
+    Result = Diagnostic::Error;
+    break;
   }
 
   // Okay, we're about to return this as a "diagnostic to emit" one last check:


More information about the cfe-dev mailing list