[cfe-commits] r170059 - in /cfe/trunk: lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp test/Analysis/self-init.m

Anna Zaks ganna at apple.com
Wed Dec 12 16:42:19 PST 2012


Author: zaks
Date: Wed Dec 12 18:42:19 2012
New Revision: 170059

URL: http://llvm.org/viewvc/llvm-project?rev=170059&view=rev
Log:
[analyzer] Fix a self-init checker false positive.

This is a Band-Aid fix to a false positive, where we complain about not
initializing self to [super init], where self is not coming from the
init method, but is coming from the caller to init.

The proper solution would be to associate the self and it's state with
the enclosing init.

Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
    cfe/trunk/test/Analysis/self-init.m

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp?rev=170059&r1=170058&r2=170059&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp Wed Dec 12 18:42:19 2012
@@ -123,9 +123,10 @@
 static void addSelfFlag(ProgramStateRef state, SVal val,
                         SelfFlagEnum flag, CheckerContext &C) {
   // We tag the symbol that the SVal wraps.
-  if (SymbolRef sym = val.getAsSymbol())
+  if (SymbolRef sym = val.getAsSymbol()) {
     state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag);
-  C.addTransition(state);
+    C.addTransition(state);
+  }
 }
 
 static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
@@ -303,6 +304,10 @@
 void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad,
                                         const Stmt *S,
                                         CheckerContext &C) const {
+  if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
+        C.getCurrentAnalysisDeclContext()->getDecl())))
+    return;
+
   // Tag the result of a load from 'self' so that we can easily know that the
   // value is the object that 'self' points to.
   ProgramStateRef state = C.getState();

Modified: cfe/trunk/test/Analysis/self-init.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/self-init.m?rev=170059&r1=170058&r2=170059&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/self-init.m (original)
+++ cfe/trunk/test/Analysis/self-init.m Wed Dec 12 18:42:19 2012
@@ -281,3 +281,28 @@
 }
 @end
 
+// Test for radar://12838705.
+ at interface ABCClass : NSObject
+ at property (nonatomic, strong) NSString *foo;
+ at property (nonatomic, strong) NSString *bar;
+ at property (nonatomic, strong) NSString *baz;
+ at end
+
+ at implementation ABCClass
+ at synthesize foo = foo_;
+ at synthesize bar = bar_;
+ at synthesize baz = baz_;
+
+- (id)initWithABC:(ABCClass *)abc {
+  self = [super init];
+  baz_ = abc->baz_;
+  return self;
+}
+
+- (ABCClass *)abcWithFoo:(NSString *)foo {
+  ABCClass *copy = [[ABCClass alloc] initWithABC:self];
+  return copy;
+}
+
+ at end
+





More information about the cfe-commits mailing list