[cfe-commits] [Patch] Diagnostic group for the static analyzer : -Werror=static-analyzer

Benoit Belley Benoit.Belley at autodesk.com
Thu Aug 26 09:56:44 PDT 2010


Hi Everyone,

The following patch adds a diagnostic group for the static analyzer, namely -Werror=static-analyzer. The effect of the compiler switch is to cause the static analyzer (i.e. the class BugReporter) to report issues as errors instead of warnings. This can be useful when integrating the static analyzer in a build system since the compiler exit status will be 1 if the compiler report an error.

Comment about the change:

- In the BugReporter, it is no longer necessary to quote " characters since we are now using a regular diagnostic instead of a custom one.

- The patch includes a unit test for the -Werror=static-analyzer switch.


Here’s the patch against clang revision 111934:

Index: test/Analysis/warning-as-errors.c
===================================================================
--- test/Analysis/warning-as-errors.c	(revision 0)
+++ test/Analysis/warning-as-errors.c	(revision 0)
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -Werror=static-analyzer %s -analyzer-store=basic -verify
+// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -Werror=static-analyzer %s -analyzer-store=region -verify
+
+// This test case illustrates that the -Werror=static-analyzer command
+// line option can be used to force the static analyzer to emit errors
+// instead of warnings.
+
+char* f(int *p) { 
+  return p; // expected-warning{{incompatible pointer types}}
+}
+
+void g(int *p) {
+  if (!p) *p = 0; // expected-error{{null}}  
+}
+
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td	(revision 111934)
+++ include/clang/Basic/DiagnosticGroups.td	(working copy)
@@ -153,6 +153,9 @@
 def : DiagGroup<"write-strings">;
 def CharSubscript : DiagGroup<"char-subscripts">;
 
+// Static Analyzer warnings
+def StaticAnalyzer : DiagGroup<"static-analyzer">;
+
 // Aggregation warning settings.
 
 // -Widiomatic-parentheses contains warnings about 'idiomatic'
Index: include/clang/Basic/DiagnosticAnalysisKinds.td
===================================================================
--- include/clang/Basic/DiagnosticAnalysisKinds.td	(revision 111934)
+++ include/clang/Basic/DiagnosticAnalysisKinds.td	(working copy)
@@ -12,4 +12,9 @@
 // CHECK: use of uninitialized values
 def warn_uninit_val : Warning<"use of uninitialized variable">;
 
+// Generic error message used by the BugReporter class. The static
+// analyzer group lets the user turn the analyzer warnings into errors
+// using the -Werror=static-analyer command line option.
+def warn_analyzer : Warning<"%0">, InGroup<StaticAnalyzer>;
+
 }
Index: lib/Checker/BugReporter.cpp
===================================================================
--- lib/Checker/BugReporter.cpp	(revision 111934)
+++ lib/Checker/BugReporter.cpp	(working copy)
@@ -21,6 +21,7 @@
 #include "clang/AST/ParentMap.h"
 #include "clang/AST/StmtObjC.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Analysis/AnalysisDiagnostic.h"
 #include "clang/Analysis/ProgramPoint.h"
 #include "clang/Checker/BugReporter/PathDiagnostic.h"
 #include "llvm/Support/raw_ostream.h"
@@ -1813,29 +1814,13 @@
   Diagnostic& Diag = getDiagnostic();
   FullSourceLoc L(R->getLocation(), getSourceManager());
   
-  // Search the description for '%', as that will be interpretted as a
-  // format character by FormatDiagnostics.
   llvm::StringRef desc = R->getShortDescription();
-  unsigned ErrorDiag;
-  {
-    llvm::SmallString<512> TmpStr;
-    llvm::raw_svector_ostream Out(TmpStr);
-    for (llvm::StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
-      if (*I == '%')
-        Out << "%%";
-      else
-        Out << *I;
-    
-    Out.flush();
-    ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
-  }        
-
   switch (End-Beg) {
     default: assert(0 && "Don't handle this many ranges yet!");
-    case 0: Diag.Report(L, ErrorDiag); break;
-    case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
-    case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
-    case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
+    case 0: Diag.Report(L, diag::warn_analyzer) << desc; break;
+    case 1: Diag.Report(L, diag::warn_analyzer) << desc << Beg[0]; break;
+    case 2: Diag.Report(L, diag::warn_analyzer) << desc << Beg[0] << Beg[1]; break;
+    case 3: Diag.Report(L, diag::warn_analyzer) << desc << Beg[0] << Beg[1] << Beg[2]; break;
   }
 
   // Emit a full diagnostic for the path if we have a PathDiagnosticClient.



Benoit 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: static-analyer-diag-group.patch
Type: application/octet-stream
Size: 4036 bytes
Desc: static-analyer-diag-group.patch
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20100826/1a506db1/attachment.obj>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ATT00001..txt
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20100826/1a506db1/attachment.txt>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.gif
Type: image/gif
Size: 651 bytes
Desc: image002.gif
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20100826/1a506db1/attachment.gif>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ATT00002..txt
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20100826/1a506db1/attachment-0001.txt>


More information about the cfe-commits mailing list