Index: cfe/trunk/include/clang/StaticAnalyzer/Core/RetainSummaryManager.h =================================================================== --- cfe/trunk/include/clang/StaticAnalyzer/Core/RetainSummaryManager.h +++ cfe/trunk/include/clang/StaticAnalyzer/Core/RetainSummaryManager.h @@ -677,6 +677,9 @@ // Function returns the first argument. Identity, + // Function returns "this" argument. + IdentityThis, + // Function either returns zero, or the input parameter. IdentityOrZero }; Index: cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp =================================================================== --- cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp +++ cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp @@ -849,7 +849,6 @@ //===----------------------------------------------------------------------===// bool RetainCountChecker::evalCall(const CallExpr *CE, CheckerContext &C) const { - // Get the callee. We're only interested in simple C functions. ProgramStateRef state = C.getState(); const FunctionDecl *FD = C.getCalleeDecl(CE); if (!FD) @@ -874,18 +873,27 @@ // Bind the return value. if (BSmr == BehaviorSummary::Identity || - BSmr == BehaviorSummary::IdentityOrZero) { - SVal RetVal = state->getSVal(CE->getArg(0), LCtx); + BSmr == BehaviorSummary::IdentityOrZero || + BSmr == BehaviorSummary::IdentityThis) { + + const Expr *BindReturnTo = + (BSmr == BehaviorSummary::IdentityThis) + ? cast(CE)->getImplicitObjectArgument() + : CE->getArg(0); + SVal RetVal = state->getSVal(BindReturnTo, LCtx); // If the receiver is unknown or the function has // 'rc_ownership_trusted_implementation' annotate attribute, conjure a // return value. + // FIXME: this branch is very strange. if (RetVal.isUnknown() || (hasTrustedImplementationAnnotation && !ResultTy.isNull())) { SValBuilder &SVB = C.getSValBuilder(); RetVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, ResultTy, C.blockCount()); } + + // Bind the value. state = state->BindExpr(CE, LCtx, RetVal, /*Invalidate=*/false); if (BSmr == BehaviorSummary::IdentityOrZero) { Index: cfe/trunk/lib/StaticAnalyzer/Core/RetainSummaryManager.cpp =================================================================== --- cfe/trunk/lib/StaticAnalyzer/Core/RetainSummaryManager.cpp +++ cfe/trunk/lib/StaticAnalyzer/Core/RetainSummaryManager.cpp @@ -152,6 +152,10 @@ return S == "safeMetaCast"; } +static bool isOSObjectThisCast(StringRef S) { + return S == "metaCast"; +} + static bool isOSIteratorSubclass(const Decl *D) { return isSubclass(D, "OSIterator"); } @@ -219,13 +223,13 @@ const CXXRecordDecl *PD = RetTy->getPointeeType()->getAsCXXRecordDecl(); if (PD && isOSObjectSubclass(PD)) { if (const IdentifierInfo *II = FD->getIdentifier()) { - if (isOSObjectDynamicCast(II->getName())) + StringRef FuncName = II->getName(); + if (isOSObjectDynamicCast(FuncName) || isOSObjectThisCast(FuncName)) return getDefaultSummary(); // All objects returned with functions *not* starting with // get, or iterators, are returned at +1. - if ((!II->getName().startswith("get") && - !II->getName().startswith("Get")) || + if ((!FuncName.startswith("get") && !FuncName.startswith("Get")) || isOSIteratorSubclass(PD)) { return getOSSummaryCreateRule(FD); } else { @@ -703,8 +707,13 @@ // the input was non-zero), // or that it returns zero (when the cast failed, or the input // was zero). - if (TrackOSObjects && isOSObjectDynamicCast(FName)) { - return BehaviorSummary::IdentityOrZero; + if (TrackOSObjects) { + if (isOSObjectDynamicCast(FName) && FD->param_size() >= 1) { + return BehaviorSummary::IdentityOrZero; + } else if (isOSObjectThisCast(FName) && isa(FD) && + !cast(FD)->isStatic()) { + return BehaviorSummary::IdentityThis; + } } const FunctionDecl* FDD = FD->getDefinition(); Index: cfe/trunk/test/Analysis/os_object_base.h =================================================================== --- cfe/trunk/test/Analysis/os_object_base.h +++ cfe/trunk/test/Analysis/os_object_base.h @@ -23,6 +23,8 @@ static OSMetaClassBase *safeMetaCast(const OSMetaClassBase *inst, const OSMetaClass *meta); + OSMetaClassBase *metaCast(const char *toMeta); + virtual void retain() const; virtual void release() const; virtual void free(); Index: cfe/trunk/test/Analysis/osobject-retain-release.cpp =================================================================== --- cfe/trunk/test/Analysis/osobject-retain-release.cpp +++ cfe/trunk/test/Analysis/osobject-retain-release.cpp @@ -54,6 +54,9 @@ OtherStruct(OSArray *arr); }; +bool test_meta_cast_no_leak(OSMetaClassBase *arg) { + return arg && arg->metaCast("blah") != nullptr; +} void escape(void *); void escape_with_source(void *p) {}