[cfe-commits] r163760 - in /cfe/trunk: lib/Analysis/UninitializedValues.cpp test/SemaObjC/uninit-variables.m

Ted Kremenek kremenek at apple.com
Wed Sep 12 17:21:35 PDT 2012


Author: kremenek
Date: Wed Sep 12 19:21:35 2012
New Revision: 163760

URL: http://llvm.org/viewvc/llvm-project?rev=163760&view=rev
Log:
Teach -Wuninitialized to recognize common "noreturn" idioms in
Objective-C related to NSException.

Fixes <rdar://problem/12287498>

I debated whether or not this logic should be sunk into the CFG
itself.  It's not clear if we should, as different analyses may
wish to have different policies.  We can re-evaluate this in the
future.

Modified:
    cfe/trunk/lib/Analysis/UninitializedValues.cpp
    cfe/trunk/test/SemaObjC/uninit-variables.m

Modified: cfe/trunk/lib/Analysis/UninitializedValues.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/UninitializedValues.cpp?rev=163760&r1=163759&r2=163760&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/UninitializedValues.cpp (original)
+++ cfe/trunk/lib/Analysis/UninitializedValues.cpp Wed Sep 12 19:21:35 2012
@@ -22,6 +22,7 @@
 #include "clang/Analysis/AnalysisContext.h"
 #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
 #include "clang/Analysis/Analyses/UninitializedValues.h"
+#include "clang/Analysis/DomainSpecific/ObjCNoReturn.h"
 #include "llvm/Support/SaveAndRestore.h"
 
 using namespace clang;
@@ -412,6 +413,7 @@
   const CFGBlock *block;
   AnalysisDeclContext ∾
   const ClassifyRefs &classification;
+  ObjCNoReturn objCNoRet;
   UninitVariablesHandler *handler;
 
 public:
@@ -420,16 +422,18 @@
                     const ClassifyRefs &classification,
                     UninitVariablesHandler *handler)
     : vals(vals), cfg(cfg), block(block), ac(ac),
-      classification(classification), handler(handler) {}
+      classification(classification), objCNoRet(ac.getASTContext()),
+      handler(handler) {}
 
   void reportUse(const Expr *ex, const VarDecl *vd);
 
-  void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS);
+  void VisitBinaryOperator(BinaryOperator *bo);
   void VisitBlockExpr(BlockExpr *be);
   void VisitCallExpr(CallExpr *ce);
-  void VisitDeclStmt(DeclStmt *ds);
   void VisitDeclRefExpr(DeclRefExpr *dr);
-  void VisitBinaryOperator(BinaryOperator *bo);
+  void VisitDeclStmt(DeclStmt *ds);
+  void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS);
+  void VisitObjCMessageExpr(ObjCMessageExpr *ME);
 
   bool isTrackedVar(const VarDecl *vd) {
     return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl()));
@@ -689,6 +693,14 @@
   }
 }
 
+void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) {
+  // If the Objective-C message expression is an implicit no-return that
+  // is not modeled in the CFG, set the tracked dataflow values to Unknown.
+  if (objCNoRet.isImplicitNoReturn(ME)) {
+    vals.setAllScratchValues(Unknown);
+  }
+}
+
 //------------------------------------------------------------------------====//
 // High-level "driver" logic for uninitialized values analysis.
 //====------------------------------------------------------------------------//

Modified: cfe/trunk/test/SemaObjC/uninit-variables.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/uninit-variables.m?rev=163760&r1=163759&r2=163760&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/uninit-variables.m (original)
+++ cfe/trunk/test/SemaObjC/uninit-variables.m Wed Sep 12 19:21:35 2012
@@ -1,5 +1,16 @@
 // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -fsyntax-only -fblocks %s -verify
 
+#include <stdarg.h>
+
+ at interface NSObject {} @end
+ at class NSString;
+
+ at interface NSException
++ (void)raise:(NSString *)name format:(NSString *)format, ...;
++ (void)raise:(NSString *)name format:(NSString *)format arguments:(va_list)argList;
+- (void)raise;
+ at end
+
 // Duplicated from uninit-variables.c.
 // Test just to ensure the analysis is working.
 int test1() {
@@ -25,3 +36,21 @@
   }
 }
 
+int test_abort_on_exceptions(int y, NSException *e, NSString *s, int *z, ...) {
+  int x; // expected-note {{initialize the variable 'x' to silence this warning}}
+  if (y == 1) {
+    va_list alist;
+    va_start(alist, z);
+    [NSException raise:@"Blah" format:@"Blah %@" arguments:alist];
+    return x;
+  }
+  else if (y == 2) {
+	[NSException raise:@"Blah" format:s];
+	return x;  
+  }
+  else if (y == 3) {
+	[e raise];
+	return x;
+  }
+  return x; // expected-warning {{variable 'x' is uninitialized when used here}}
+}





More information about the cfe-commits mailing list