From f893ea1592a4df9cba8e63d7fee1600eeda51a2a Mon Sep 17 00:00:00 2001 From: George Karpenkov Date: Fri, 30 Nov 2018 02:17:44 +0000 Subject: [PATCH] [analyzer] Add the type of the leaked object to the diagnostic message If the object is a temporary, and there is no variable it binds to, let's at least print out the object name in order to help differentiate it from other temporaries. rdar://45175098 Differential Revision: https://reviews.llvm.org/D55033 llvm-svn: 347943 --- .../RetainCountDiagnostics.cpp | 23 +++++++++++++++---- .../RetainCountDiagnostics.h | 4 +++- .../Inputs/expected-plists/objc-arc.m.plist | 6 ++--- .../retain-release-path-notes.m.plist | 6 ++--- clang/test/Analysis/objc-radar17039661.m | 6 ++--- .../test/Analysis/osobject-retain-release.cpp | 18 +++++++++------ .../test/Analysis/retain-release-path-notes.m | 2 +- .../retaincountchecker-compoundregion.m | 4 ++-- 8 files changed, 45 insertions(+), 24 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp b/clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp index 608ed88eb036..8758091a9b9b 100644 --- a/clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp @@ -28,6 +28,17 @@ static bool isNumericLiteralExpression(const Expr *E) { isa(E); } +/// If type represents a pointer to CXXRecordDecl, +/// and is not a typedef, return the decl name. +/// Otherwise, return the serialization of type. +static StringRef getPrettyTypeName(QualType QT) { + QualType PT = QT->getPointeeType(); + if (!PT.isNull() && !QT->getAs()) + if (const auto *RD = PT->getAsCXXRecordDecl()) + return RD->getName(); + return QT.getAsString(); +} + /// Write information about the type state change to {@code os}, /// return whether the note should be generated. static bool shouldGenerateNote(llvm::raw_string_ostream &os, @@ -193,7 +204,7 @@ CFRefReportVisitor::VisitNode(const ExplodedNode *N, << Sym->getType().getAsString() << " with a "; } else if (CurrV.getObjKind() == RetEffect::OS) { os << " returns an OSObject of type " - << Sym->getType().getAsString() << " with a "; + << getPrettyTypeName(Sym->getType()) << " with a "; } else if (CurrV.getObjKind() == RetEffect::Generalized) { os << " returns an object of type " << Sym->getType().getAsString() << " with a "; @@ -432,7 +443,7 @@ CFRefLeakReportVisitor::getEndPath(BugReporterContext &BRC, if (RegionDescription) { os << "object allocated and stored into '" << *RegionDescription << '\''; } else { - os << "allocated object"; + os << "allocated object of type " << getPrettyTypeName(Sym->getType()); } // Get the retain count. @@ -472,10 +483,10 @@ CFRefLeakReportVisitor::getEndPath(BugReporterContext &BRC, " Foundation"; } } - } - else + } else { os << " is not referenced later in this execution path and has a retain " "count of +" << RV->getCount(); + } return std::make_shared(L, os.str()); } @@ -555,6 +566,10 @@ void CFRefLeakReport::createDescription(CheckerContext &Ctx, FullSourceLoc SL(AllocStmt->getBeginLoc(), Ctx.getSourceManager()); os << " (allocated on line " << SL.getSpellingLineNumber() << ")"; } + } else { + + // If we can't figure out the name, just supply the type information. + os << " of type " << getPrettyTypeName(Sym->getType()); } } diff --git a/clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.h b/clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.h index 58abd670395f..a25cc12fb093 100644 --- a/clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.h +++ b/clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.h @@ -141,12 +141,14 @@ public: }; class CFRefReport : public BugReport { +protected: + SymbolRef Sym; public: CFRefReport(CFRefBug &D, const LangOptions &LOpts, const SummaryLogTy &Log, ExplodedNode *n, SymbolRef sym, bool registerVisitor = true) - : BugReport(D, D.getDescription(), n) { + : BugReport(D, D.getDescription(), n), Sym(sym) { if (registerVisitor) addVisitor(llvm::make_unique(sym, Log)); } diff --git a/clang/test/Analysis/Inputs/expected-plists/objc-arc.m.plist b/clang/test/Analysis/Inputs/expected-plists/objc-arc.m.plist index 977827d5adce..3f9d63e7a635 100644 --- a/clang/test/Analysis/Inputs/expected-plists/objc-arc.m.plist +++ b/clang/test/Analysis/Inputs/expected-plists/objc-arc.m.plist @@ -1727,12 +1727,12 @@ depth0 extended_message - Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1 + Object leaked: allocated object of type CFStringRef is not referenced later in this execution path and has a retain count of +1 message - Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1 + Object leaked: allocated object of type CFStringRef is not referenced later in this execution path and has a retain count of +1 - descriptionPotential leak of an object + descriptionPotential leak of an object of type CFStringRef categoryMemory (Core Foundation/Objective-C) typeLeak check_nameosx.cocoa.RetainCount diff --git a/clang/test/Analysis/Inputs/expected-plists/retain-release-path-notes.m.plist b/clang/test/Analysis/Inputs/expected-plists/retain-release-path-notes.m.plist index a2f971bc5e11..f7ff277b544a 100644 --- a/clang/test/Analysis/Inputs/expected-plists/retain-release-path-notes.m.plist +++ b/clang/test/Analysis/Inputs/expected-plists/retain-release-path-notes.m.plist @@ -3834,12 +3834,12 @@ depth0 extended_message - Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1 + Object leaked: allocated object of type MyObj * is not referenced later in this execution path and has a retain count of +1 message - Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1 + Object leaked: allocated object of type MyObj * is not referenced later in this execution path and has a retain count of +1 - descriptionPotential leak of an object + descriptionPotential leak of an object of type MyObj * categoryMemory (Core Foundation/Objective-C) typeLeak check_nameosx.cocoa.RetainCount diff --git a/clang/test/Analysis/objc-radar17039661.m b/clang/test/Analysis/objc-radar17039661.m index 0d2ddbfc3060..5f7c531eaeb7 100644 --- a/clang/test/Analysis/objc-radar17039661.m +++ b/clang/test/Analysis/objc-radar17039661.m @@ -1315,12 +1315,12 @@ void runTest() { // CHECK: // CHECK: depth4 // CHECK: extended_message -// CHECK: Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1 +// CHECK: Object leaked: allocated object of type NSNumber * is not referenced later in this execution path and has a retain count of +1 // CHECK: message -// CHECK: Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1 +// CHECK: Object leaked: allocated object of type NSNumber * is not referenced later in this execution path and has a retain count of +1 // CHECK: // CHECK: -// CHECK: descriptionPotential leak of an object +// CHECK: descriptionPotential leak of an object of type NSNumber * // CHECK: categoryMemory (Core Foundation/Objective-C) // CHECK: typeLeak // CHECK: location diff --git a/clang/test/Analysis/osobject-retain-release.cpp b/clang/test/Analysis/osobject-retain-release.cpp index e02feef6fe45..d53ebbab5f09 100644 --- a/clang/test/Analysis/osobject-retain-release.cpp +++ b/clang/test/Analysis/osobject-retain-release.cpp @@ -61,13 +61,13 @@ void check_custom_iterator_rule(OSArray *arr) { } 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}} + OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to function 'withCapacity' returns an OSObject of type OSArray with a +1 retain count}} OtherStruct::doNothingToArray(arr); } // expected-warning{{Potential leak of an object stored into 'arr'}} // expected-note@-1{{Object leaked}} void check_no_invalidation_other_struct() { - OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to function 'withCapacity' returns an OSObject of type struct OSArray * with a +1 retain count}} + OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to function 'withCapacity' returns an OSObject of type OSArray with a +1 retain count}} OtherStruct other(arr); // expected-warning{{Potential leak}} // expected-note@-1{{Object leaked}} } @@ -94,7 +94,8 @@ struct ArrayOwner : public OSObject { }; OSArray *generateArray() { - return OSArray::withCapacity(10); // expected-note{{Call to function 'withCapacity' returns an OSObject of type struct OSArray * with a +1 retain count}} + return OSArray::withCapacity(10); // expected-note{{Call to function 'withCapacity' returns an OSObject of type OSArray with a +1 retain count}} + // expected-note@-1{{Call to function 'withCapacity' returns an OSObject of type OSArray with a +1 retain count}} } unsigned int check_leak_good_error_message() { @@ -109,7 +110,10 @@ unsigned int check_leak_good_error_message() { } unsigned int check_leak_msg_temporary() { - return generateArray()->getCount(); + return generateArray()->getCount(); // expected-warning{{Potential leak of an object}} + // expected-note@-1{{Calling 'generateArray'}} + // expected-note@-2{{Returning from 'generateArray'}} + // expected-note@-3{{Object leaked: allocated object of type OSArray is not referenced later in this execution path and has a retain count of +1}} } void check_confusing_getters() { @@ -178,14 +182,14 @@ void check_dynamic_cast_null_check() { } void use_after_release() { - OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to function 'withCapacity' returns an OSObject of type struct OSArray * with a +1 retain count}} + OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to function 'withCapacity' returns an OSObject of type OSArray with a +1 retain count}} arr->release(); // expected-note{{Object released}} arr->getCount(); // expected-warning{{Reference-counted object is used after it is released}} // expected-note@-1{{Reference-counted object is used after it is released}} } void potential_leak() { - OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to function 'withCapacity' returns an OSObject of type struct OSArray * with a +1 retain count}} + OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to function 'withCapacity' 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}} arr->getCount(); @@ -236,7 +240,7 @@ unsigned int no_warn_ok_release(ArrayOwner *owner) { } unsigned int warn_on_overrelease_with_unknown_source(ArrayOwner *owner) { - OSArray *arr = owner->getArraySourceUnknown(); // expected-note{{function call returns an OSObject of type struct OSArray * with a +0 retain count}} + OSArray *arr = owner->getArraySourceUnknown(); // expected-note{{function call returns an OSObject of type OSArray with a +0 retain count}} arr->release(); // expected-warning{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} // expected-note@-1{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} return arr->getCount(); diff --git a/clang/test/Analysis/retain-release-path-notes.m b/clang/test/Analysis/retain-release-path-notes.m index 68967f56f64e..2ade99d883a2 100644 --- a/clang/test/Analysis/retain-release-path-notes.m +++ b/clang/test/Analysis/retain-release-path-notes.m @@ -227,7 +227,7 @@ static int Cond; // expected-note@-1 {{Method returns an instance of MyObj with a +1 retain count}} // expected-note@-2 {{Calling 'initX'}} // expected-note@-3 {{Returning from 'initX'}} - // expected-note@-4 {{Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1}} + // expected-note@-4 {{Object leaked: allocated object of type MyObj * is not referenced later in this execution path and has a retain count of +1}} // initI is inlined because the allocation happens within initY id y = [[MyObj alloc] initY]; // expected-note@-1 {{Calling 'initY'}} diff --git a/clang/test/Analysis/retaincountchecker-compoundregion.m b/clang/test/Analysis/retaincountchecker-compoundregion.m index d9967b7fabfc..ce1f7a9aa18d 100644 --- a/clang/test/Analysis/retaincountchecker-compoundregion.m +++ b/clang/test/Analysis/retaincountchecker-compoundregion.m @@ -19,7 +19,7 @@ void foo(CFAllocatorRef allocator) { int width = 0; int height = 0; CFTypeRef* values = (CFTypeRef[]){ - CFNumberCreate(allocator, kCFNumberSInt32Type, &width), //expected-warning-re{{Potential leak of an object{{$}}}} - CFNumberCreate(allocator, kCFNumberSInt32Type, &height), //expected-warning-re{{Potential leak of an object{{$}}}} + CFNumberCreate(allocator, kCFNumberSInt32Type, &width), //expected-warning{{Potential leak of an object of type CFNumberRef}} + CFNumberCreate(allocator, kCFNumberSInt32Type, &height), //expected-warning{{Potential leak of an object of type CFNumberRef}} }; }