[cfe-commits] r125427 - in /cfe/trunk: include/clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp test/Analysis/self-init.m
Ted Kremenek
kremenek at apple.com
Fri Feb 11 19:03:54 PST 2011
Author: kremenek
Date: Fri Feb 11 21:03:54 2011
New Revision: 125427
URL: http://llvm.org/viewvc/llvm-project?rev=125427&view=rev
Log:
Weaken the ObjCSelfInitChecker to only warn when one calls an 'init' method within an 'init' method. This is a temporary stop gap to avoid false positives while we investigate how to make it smarter.
Modified:
cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h
cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
cfe/trunk/test/Analysis/self-init.m
Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h?rev=125427&r1=125426&r2=125427&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h Fri Feb 11 21:03:54 2011
@@ -145,6 +145,19 @@
delete (typename data_type::Factory*) Ctx;
}
};
+
+ // Partial specialization for bool.
+ template <> struct GRStatePartialTrait<bool> {
+ typedef bool data_type;
+
+ static inline data_type MakeData(void* const* p) {
+ return (bool) (uintptr_t) p;
+ }
+ static inline void *MakeVoidPtr(data_type d) {
+ return (void*) (uintptr_t) d;
+ }
+ };
+
} // end GR namespace
} // end clang namespace
Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp?rev=125427&r1=125426&r2=125427&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp Fri Feb 11 21:03:54 2011
@@ -109,6 +109,7 @@
} // end anonymous namespace
typedef llvm::ImmutableMap<SymbolRef, unsigned> SelfFlag;
+namespace { struct CalledInit {}; }
namespace clang {
namespace ento {
@@ -119,6 +120,10 @@
return &index;
}
};
+ template <>
+ struct GRStateTrait<CalledInit> : public GRStatePartialTrait<bool> {
+ static void *GDMIndex() { static int index = 0; return &index; }
+ };
}
}
@@ -133,8 +138,8 @@
return getSelfFlags(val, C.getState());
}
-static void addSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
- const GRState *state = C.getState();
+static void addSelfFlag(const GRState *state, SVal val,
+ SelfFlagEnum flag, CheckerContext &C) {
// We tag the symbol that the SVal wraps.
if (SymbolRef sym = val.getAsSymbol())
C.addTransition(state->set<SelfFlag>(sym, getSelfFlags(val, C) | flag));
@@ -161,9 +166,13 @@
const char *errorStr) {
if (!E)
return;
+
+ if (!C.getState()->get<CalledInit>())
+ return;
+
if (!isInvalidSelf(E, C))
return;
-
+
// Generate an error node.
ExplodedNode *N = C.generateSink();
if (!N)
@@ -188,8 +197,14 @@
if (isInitMessage(msg)) {
// Tag the return value as the result of an initializer.
const GRState *state = C.getState();
+
+ // FIXME this really should be context sensitive, where we record
+ // the current stack frame (for IPA). Also, we need to clean this
+ // value out when we return from this method.
+ state = state->set<CalledInit>(true);
+
SVal V = state->getSVal(msg.getOriginExpr());
- addSelfFlag(V, SelfFlag_InitRes, C);
+ addSelfFlag(state, V, SelfFlag_InitRes, C);
return;
}
@@ -262,10 +277,10 @@
I = CE->arg_begin(), E = CE->arg_end(); I != E; ++I) {
SVal argV = state->getSVal(*I);
if (isSelfVar(argV, C)) {
- addSelfFlag(state->getSVal(cast<Loc>(argV)), preCallSelfFlags, C);
+ addSelfFlag(state, state->getSVal(cast<Loc>(argV)), preCallSelfFlags, C);
return;
} else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
- addSelfFlag(state->getSVal(CE), preCallSelfFlags, C);
+ addSelfFlag(state, state->getSVal(CE), preCallSelfFlags, C);
return;
}
}
@@ -277,7 +292,7 @@
// value is the object that 'self' points to.
const GRState *state = C.getState();
if (isSelfVar(location, C))
- addSelfFlag(state->getSVal(cast<Loc>(location)), SelfFlag_Self, C);
+ addSelfFlag(state, state->getSVal(cast<Loc>(location)), SelfFlag_Self, C);
}
// FIXME: A callback should disable checkers at the start of functions.
Modified: cfe/trunk/test/Analysis/self-init.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/self-init.m?rev=125427&r1=125426&r2=125427&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/self-init.m (original)
+++ cfe/trunk/test/Analysis/self-init.m Fri Feb 11 21:03:54 2011
@@ -102,7 +102,7 @@
}
-(id)init6 {
- [NSBundle loadNibNamed:@"Window" owner:myivar]; // expected-warning {{Instance variable used}}
+ [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
return [self initWithSomething:0];
}
@@ -121,17 +121,17 @@
}
-(id)init9 {
- [self doSomething];
- return self; // expected-warning {{Returning 'self'}}
+ [self doSomething];
+ return self; // no-warning
}
-(id)init10 {
- myivar = 0; // expected-warning {{Instance variable used}}
- return self;
+ myivar = 0; // no-warning
+ return self;
}
-(id)init11 {
- return self; // expected-warning {{Returning 'self'}}
+ return self; // no-warning
}
-(id)init12 {
More information about the cfe-commits
mailing list