]> granicus.if.org Git - clang/commitdiff
added preliminary diagnostics in scan-build results to denote whether
authorTed Kremenek <kremenek@apple.com>
Wed, 30 Apr 2008 23:47:44 +0000 (23:47 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 30 Apr 2008 23:47:44 +0000 (23:47 +0000)
a CF memory leak occurred with GC enabled, etc.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@50507 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/ASTConsumers.cpp
Driver/HTMLDiagnostics.cpp
include/clang/Analysis/LocalCheckers.h
include/clang/Analysis/PathDiagnostic.h
include/clang/Analysis/PathSensitive/BugReporter.h
lib/Analysis/BugReporter.cpp
lib/Analysis/CFRefCount.cpp

index c01e4edb0d1d2851e9d347dfc51ebb40c1142f39..59e477ab65e69d3f5873cb9d1559848c30ae4efc 100644 (file)
@@ -813,16 +813,16 @@ public:
   virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) {
     switch (LangOpts.getGCMode()) {
       case LangOptions::NonGC:
-        TFs.push_back(MakeCFRefCountTF(*Ctx, false));
+        TFs.push_back(MakeCFRefCountTF(*Ctx, false, LangOpts));
         break;
         
       case LangOptions::GCOnly:
-        TFs.push_back(MakeCFRefCountTF(*Ctx, true));
+        TFs.push_back(MakeCFRefCountTF(*Ctx, true, LangOpts));
         break;
         
       case LangOptions::HybridGC:
-        TFs.push_back(MakeCFRefCountTF(*Ctx, false));
-        TFs.push_back(MakeCFRefCountTF(*Ctx, true));
+        TFs.push_back(MakeCFRefCountTF(*Ctx, false, LangOpts));
+        TFs.push_back(MakeCFRefCountTF(*Ctx, true, LangOpts));
         break;
     }
   }
index 3e3571c5129348c793e931be611186ccec574f20..cb333b978c159d47a26deee85ac3d2cca1b063b8 100644 (file)
@@ -187,9 +187,16 @@ void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
        << (*D.rbegin()).getLocation().getLogicalColumnNumber()
        << "</a></td></tr>\n"
           "<tr><td class=\"rowname\">Description:</td><td>"
-       << D.getDescription()
-       << "</td></tr>\n</table>\n"
-          "<h3>Annotated Source Code</h3>\n";
+       << D.getDescription() << "</td></tr>\n";
+    
+    // Output any other meta data.
+    
+    for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
+         I!=E; ++I) {
+      os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
+    }
+    
+    os << "</table>\n<h3>Annotated Source Code</h3>\n";    
     
     R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
   }
index bf4f30a337cdc1851dfb402fccfb5de8d284432b..64dc4dd90faa9634dd2adb40d55665ddfd8d6a26 100644 (file)
@@ -24,14 +24,16 @@ class ASTContext;
 class PathDiagnosticClient;
 class GRTransferFuncs;
 class BugType;
-
+class LangOptions;
+  
 void CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags); 
   
 void CheckUninitializedValues(CFG& cfg, ASTContext& Ctx, Diagnostic& Diags,
                               bool FullUninitTaint=false);
   
 GRTransferFuncs* MakeGRSimpleValsTF();
-GRTransferFuncs* MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled); 
+GRTransferFuncs* MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled, 
+                                  const LangOptions& lopts); 
 BugType* MakeDeadStoresChecker();
   
 } // end namespace clang
index f9bd0fd10630bbd873087ac304eeca1007153250..77234e7c4ac67f641fdb937e5f990e68c6087f30 100644 (file)
@@ -65,6 +65,7 @@ class PathDiagnostic {
   std::list<PathDiagnosticPiece*> path;
   unsigned Size;
   std::string Desc;
+  std::vector<std::string> OtherDesc;
 public:
   
   PathDiagnostic() : Size(0) {}
@@ -75,6 +76,13 @@ public:
 
   const std::string& getDescription() const { return Desc; }
   
+  typedef std::vector<std::string>::const_iterator meta_iterator;
+  meta_iterator meta_begin() const { return OtherDesc.begin(); }
+  meta_iterator meta_end() const { return OtherDesc.end(); }
+  void addMeta(const std::string& s) { OtherDesc.push_back(s); }
+  void addMeta(const char* s) { OtherDesc.push_back(s); }
+  
+  
   void push_front(PathDiagnosticPiece* piece) {
     path.push_front(piece);
     ++Size;
index 325e1a750f19c82f4de317662c6613e2482fb6e5..5a8606f071ee620a5dd1a09a0fe93e18331f9b9a 100644 (file)
@@ -41,6 +41,10 @@ public:
   
   virtual const char* getName() const = 0;
   virtual const char* getDescription() const { return getName(); }
+  
+  virtual std::pair<const char**,const char**> getExtraDescriptiveText() {
+    return std::pair<const char**, const char**>(0, 0);
+  }
       
   virtual void EmitWarnings(BugReporter& BR) {}
   virtual void GetErrorNodes(std::vector<ExplodedNode<ValueState>*>& Nodes) {}
@@ -78,6 +82,10 @@ public:
     return getBugType().getDescription();
   }
   
+  virtual std::pair<const char**,const char**> getExtraDescriptiveText() {
+    return getBugType().getExtraDescriptiveText();
+  }
+  
   virtual PathDiagnosticPiece* getEndPath(BugReporter& BR,
                                           ExplodedNode<ValueState>* N) const;
   
index d00d329e956bf10ccc2e9d5e3b25e457f595c52c..465acf03f3cc9f0fe211fe5241f2fb8515e34c30 100644 (file)
@@ -448,6 +448,13 @@ void BugReporter::EmitWarning(BugReport& R) {
 
   llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName()));
   GeneratePathDiagnostic(*D.get(), R);
+  
+  // Get the meta data.
+  
+  std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
+  
+  for (const char** s = Meta.first; s != Meta.second; ++s)
+    D->addMeta(*s);
 
   // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
   
index 9ed4f1db2934115c156ae325db922e9d48d11680..c5f582fdbe7f170a9fa68ba897075a1b1a9e9a8e 100644 (file)
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "GRSimpleVals.h"
+#include "clang/Basic/LangOptions.h"
 #include "clang/Analysis/PathSensitive/ValueState.h"
 #include "clang/Analysis/PathDiagnostic.h"
 #include "clang/Analysis/LocalCheckers.h"
@@ -624,7 +625,8 @@ private:
   // Instance variables.
   
   CFRefSummaryManager Summaries;  
-  const bool          GCEnabled;  
+  const bool          GCEnabled; 
+  const LangOptions&  LOpts;
   RefBFactoryTy       RefBFactory;
      
   UseAfterReleasesTy UseAfterReleases;
@@ -670,9 +672,10 @@ private:
   
 public:
   
-  CFRefCount(ASTContext& Ctx, bool gcenabled)
+  CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
     : Summaries(Ctx, gcenabled),
       GCEnabled(gcenabled),
+      LOpts(lopts),
       RetainSelector(GetUnarySelector("retain", Ctx)),
       ReleaseSelector(GetUnarySelector("release", Ctx)) {}
   
@@ -687,6 +690,9 @@ public:
     return &Printer;
   }
   
+  bool isGCEnabled() const { return GCEnabled; }
+  const LangOptions& getLangOptions() const { return LOpts; }
+  
   // Calls.
   
   virtual void EvalCall(ExplodedNodeSet<ValueState>& Dst,
@@ -1375,6 +1381,8 @@ namespace {
     
   public:
     CFRefBug(CFRefCount& tf) : TF(tf) {}
+    
+    CFRefCount& getTF() { return TF; }   
   };
   
   class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
@@ -1390,7 +1398,6 @@ namespace {
     }
     
     virtual void EmitWarnings(BugReporter& BR);
-    
   };
   
   class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
@@ -1432,11 +1439,12 @@ namespace {
   class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
     SymbolID Sym;
   public:
-    CFRefReport(BugType& D, ExplodedNode<ValueState> *n, SymbolID sym)
+    CFRefReport(CFRefBug& D, ExplodedNode<ValueState> *n, SymbolID sym)
       : RangedBugReport(D, n), Sym(sym) {}
         
     virtual ~CFRefReport() {}
     
+    virtual std::pair<const char**,const char**> getExtraDescriptiveText();
     
     virtual PathDiagnosticPiece* VisitNode(ExplodedNode<ValueState>* N,
                                            ExplodedNode<ValueState>* PrevN,
@@ -1454,6 +1462,43 @@ void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
   Eng.Register(new Leak(*this));
 }
 
+
+static const char* Msgs[] = {
+  "Code is compiled in garbage collection only mode"  // GC only
+  "  (the bug occurs with garbage collection enabled).",
+  
+  "Code is compiled without garbage collection.", // No GC.
+  
+  "Code is compiled for use with and without garbage collection (GC)."
+  "  The bug occurs with GC enabled.", // Hybrid, with GC.
+  
+  "Code is compiled for use with and without garbage collection (GC)."
+  "  The bug occurs in non-GC mode."  // Hyrbird, without GC/
+};
+
+std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
+  CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
+
+  switch (TF.getLangOptions().getGCMode()) {
+    default:
+      assert(false);
+      
+    case LangOptions::NonGC:
+      assert (!TF.isGCEnabled());
+      return std::make_pair(&Msgs[0], &Msgs[0]+1);
+    
+    case LangOptions::GCOnly:
+      assert (TF.isGCEnabled());
+      return std::make_pair(&Msgs[1], &Msgs[1]+1);
+    
+    case LangOptions::HybridGC:
+      if (TF.isGCEnabled())
+        return std::make_pair(&Msgs[2], &Msgs[2]+1);
+      else
+        return std::make_pair(&Msgs[3], &Msgs[3]+1);
+  }
+}
+
 PathDiagnosticPiece* CFRefReport::VisitNode(ExplodedNode<ValueState>* N,
                                             ExplodedNode<ValueState>* PrevN,
                                             ExplodedGraph<ValueState>& G,
@@ -1618,6 +1663,7 @@ void Leak::GetErrorNodes(std::vector<ExplodedNode<ValueState>*>& Nodes) {
 // Transfer function creation for external clients.
 //===----------------------------------------------------------------------===//
 
-GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled) {
-  return new CFRefCount(Ctx, GCEnabled);
+GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
+                                         const LangOptions& lopts) {
+  return new CFRefCount(Ctx, GCEnabled, lopts);
 }