From 8522bc263ed120fed75fa459ee5b81c9a2c78126 Mon Sep 17 00:00:00 2001 From: George Karpenkov Date: Fri, 18 Jan 2019 03:12:48 +0000 Subject: [PATCH] [analyzer] [RetainCountChecker] Produce a correct message when OSTypeAlloc is used Differential Revision: https://reviews.llvm.org/D56820 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@351509 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../RetainCountDiagnostics.cpp | 28 ++++++++++++++++++- test/Analysis/os_object_base.h | 2 +- test/Analysis/osobject-retain-release.cpp | 7 +++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp b/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp index cda1a928de..85b97ac9c0 100644 --- a/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp +++ b/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp @@ -132,6 +132,32 @@ static Optional findArgIdxOfSymbol(ProgramStateRef CurrSt, return None; } +Optional findMetaClassAlloc(const Expr *Callee) { + if (const auto *ME = dyn_cast(Callee)) { + if (ME->getMemberDecl()->getNameAsString() != "alloc") + return None; + const Expr *This = ME->getBase()->IgnoreParenImpCasts(); + if (const auto *DRE = dyn_cast(This)) { + const ValueDecl *VD = DRE->getDecl(); + if (VD->getNameAsString() != "metaClass") + return None; + + if (const auto *RD = dyn_cast(VD->getDeclContext())) + return RD->getNameAsString(); + + } + } + return None; +} + +std::string findAllocatedObjectName(const Stmt *S, + QualType QT) { + if (const auto *CE = dyn_cast(S)) + if (auto Out = findMetaClassAlloc(CE->getCallee())) + return *Out; + return getPrettyTypeName(QT); +} + static void generateDiagnosticsForCallLike(ProgramStateRef CurrSt, const LocationContext *LCtx, const RefVal &CurrV, SymbolRef &Sym, @@ -189,7 +215,7 @@ static void generateDiagnosticsForCallLike(ProgramStateRef CurrSt, os << "a Core Foundation object of type '" << Sym->getType().getAsString() << "' with a "; } else if (CurrV.getObjKind() == ObjKind::OS) { - os << "an OSObject of type '" << getPrettyTypeName(Sym->getType()) + os << "an OSObject of type '" << findAllocatedObjectName(S, Sym->getType()) << "' with a "; } else if (CurrV.getObjKind() == ObjKind::Generalized) { os << "an object of type '" << Sym->getType().getAsString() diff --git a/test/Analysis/os_object_base.h b/test/Analysis/os_object_base.h index 997d0d2958..e388dddd58 100644 --- a/test/Analysis/os_object_base.h +++ b/test/Analysis/os_object_base.h @@ -47,7 +47,7 @@ struct OSObject : public OSMetaClassBase { }; struct OSMetaClass : public OSMetaClassBase { - virtual OSObject * alloc(); + virtual OSObject * alloc() const; virtual ~OSMetaClass(){} }; diff --git a/test/Analysis/osobject-retain-release.cpp b/test/Analysis/osobject-retain-release.cpp index af40a55aef..3aaaab3bef 100644 --- a/test/Analysis/osobject-retain-release.cpp +++ b/test/Analysis/osobject-retain-release.cpp @@ -627,3 +627,10 @@ void test_smart_ptr_no_leak() { } obj->release(); } + +void test_ostypealloc_correct_diagnostic_name() { + OSArray *arr = OSTypeAlloc(OSArray); // expected-note{{Call to method 'OSMetaClass::alloc' returns an OSObject of type 'OSArray' with a +1 retain count}} + arr->retain(); // expected-note{{Reference count incremented. The object now has a +2 retain count}} + arr->release(); // expected-note{{Reference count decremented. The object now has a +1 retain count}} +} // expected-note{{Object leaked: object allocated and stored into 'arr' is not referenced later in this execution path and has a retain count of +1}} + // expected-warning@-1{{Potential leak of an object stored into 'arr'}} -- 2.40.0