From: George Karpenkov Date: Wed, 31 Oct 2018 17:38:46 +0000 (+0000) Subject: [analyzer] Re-add custom OSIterator rule for RetainCountChecker X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=abe726a8c420d79d8904b9f90d35f0516c7c5d4f;p=clang [analyzer] Re-add custom OSIterator rule for RetainCountChecker Turns out the rule is quite ubiquitous. Revert of https://reviews.llvm.org/D53628 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@345747 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Core/RetainSummaryManager.cpp b/lib/StaticAnalyzer/Core/RetainSummaryManager.cpp index b0e26bae96..2fb0d754d9 100644 --- a/lib/StaticAnalyzer/Core/RetainSummaryManager.cpp +++ b/lib/StaticAnalyzer/Core/RetainSummaryManager.cpp @@ -69,6 +69,10 @@ static bool isOSObjectDynamicCast(StringRef S) { return S == "safeMetaCast"; } +static bool isOSIteratorSubclass(const Decl *D) { + return isSubclass(D, "OSIterator"); +} + static bool hasRCAnnotation(const Decl *D, StringRef rcAnnotation) { for (const auto *Ann : D->specific_attrs()) { if (Ann->getAnnotation() == rcAnnotation) @@ -235,11 +239,12 @@ RetainSummaryManager::generateSummary(const FunctionDecl *FD, if (isOSObjectDynamicCast(II->getName())) return getDefaultSummary(); - // All objects returned with functions starting with "get" are getters. - if (II->getName().startswith("get")) { - return getOSSummaryGetRule(FD); - } else { + // All objects returned with functions *not* starting with + // get, or iterators, are returned at +1. + if (!II->getName().startswith("get") || isOSIteratorSubclass(PD)) { return getOSSummaryCreateRule(FD); + } else { + return getOSSummaryGetRule(FD); } } } diff --git a/test/Analysis/osobject-retain-release.cpp b/test/Analysis/osobject-retain-release.cpp index 47601b0bad..71f912961d 100644 --- a/test/Analysis/osobject-retain-release.cpp +++ b/test/Analysis/osobject-retain-release.cpp @@ -23,6 +23,9 @@ struct OSObject { static const OSMetaClass * const metaClass; }; +struct OSIterator : public OSObject { +}; + struct OSArray : public OSObject { unsigned int getCount(); @@ -33,6 +36,8 @@ struct OSArray : public OSObject { return nullptr; } + OSIterator * getIterator(); + static OS_RETURNS_NOT_RETAINED OSArray *MaskedGetter(); static OS_RETURNS_RETAINED OSArray *getOoopsActuallyCreate(); @@ -49,6 +54,11 @@ struct OSMetaClassBase { static OSObject *safeMetaCast(const OSObject *inst, const OSMetaClass *meta); }; +void check_custom_iterator_rule(OSArray *arr) { + OSIterator *it = arr->getIterator(); + it->release(); +} + void check_no_invalidation() { OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to function 'withCapacity' returns an OSObject of type struct OSArray * with a +1 retain count}} OtherStruct::doNothingToArray(arr);