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 


Attachment: static-analyer-diag-group.patch
Description: static-analyer-diag-group.patch


 
Benoit Belley
Sr Principal Developer
M&E-Product Development Group    
 
Autodesk Canada Inc. 
10 Rue Duke
Montreal, Quebec  H3C 2L7 
Canada
 
Direct 514 954-7154
 
 

<<attachment: image002.gif>>

 

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to