From 03b330214faba34c64d479b99f303fba11ac3bb2 Mon Sep 17 00:00:00 2001 From: Sungkeun Cho Date: Tue, 22 Sep 2020 10:11:37 -0700 Subject: [PATCH] [cost] Add FN test: saving lambda in object field Summary: This diff adds a test, in which autoreleasing lambda is written in an object field, then called later. The test is not analyzed precisely because it cannot follow that a lambda value is written to a field of an object as of now. Reviewed By: ezgicicek Differential Revision: D23843089 fbshipit-source-id: 62ba153aa --- .../objc/autoreleasepool/arc_enumerator.m | 58 +++++++++++++++++++ .../objc/autoreleasepool/cost-issues.exp | 7 +++ 2 files changed, 65 insertions(+) create mode 100644 infer/tests/codetoanalyze/objc/autoreleasepool/arc_enumerator.m diff --git a/infer/tests/codetoanalyze/objc/autoreleasepool/arc_enumerator.m b/infer/tests/codetoanalyze/objc/autoreleasepool/arc_enumerator.m new file mode 100644 index 000000000..b9057f0b7 --- /dev/null +++ b/infer/tests/codetoanalyze/objc/autoreleasepool/arc_enumerator.m @@ -0,0 +1,58 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#import +#import "no_arc_callee.h" + +typedef BOOL (^Filter)(id); + +@interface MyEnumerator : NSObject +@end + +@implementation MyEnumerator +NSEnumerator* _enumerator; +Filter _filter; + +- (instancetype)initWithArray:(NSArray*)array filter:(Filter)filter { + _enumerator = [array objectEnumerator]; + _filter = filter; + return self; +} + +- (id)nextObject { + id obj; + while (obj = [_enumerator nextObject]) { + if (_filter(obj)) { + return obj; + } + } + return NULL; +} + +@end + +@interface ArcEnumerator : NSObject +@end + +@implementation ArcEnumerator + +- (MyEnumerator*)makeMyEnumerator_zero:(NSArray*)x { + return [[MyEnumerator alloc] initWithArray:x + filter:^BOOL(NSObject* o) { + NoArcCallee* dummy = + [NoArcCallee giveMeObject]; + return true; + }]; +} + +- (void)callMyEnumerator_linear_FN:(NSArray*)x { + MyEnumerator* enumerator = [self makeMyEnumerator_zero:x]; + id s; + while (s = [enumerator nextObject]) { + } +} + +@end diff --git a/infer/tests/codetoanalyze/objc/autoreleasepool/cost-issues.exp b/infer/tests/codetoanalyze/objc/autoreleasepool/cost-issues.exp index 9d50c254a..d04802891 100644 --- a/infer/tests/codetoanalyze/objc/autoreleasepool/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/autoreleasepool/cost-issues.exp @@ -24,6 +24,13 @@ codetoanalyze/objc/autoreleasepool/arc_caller.m, ArcCaller.callGiveMeObject_line codetoanalyze/objc/autoreleasepool/arc_caller.m, ArcCaller.callMutableCopyObject_zero:x:, 0, OnUIThread:false, [] codetoanalyze/objc/autoreleasepool/arc_caller.m, ArcCaller.callNewObject_zero:, 0, OnUIThread:false, [] codetoanalyze/objc/autoreleasepool/arc_caller.m, ArcCaller.dealloc, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/arc_enumerator.m, ArcEnumerator.callMyEnumerator_linear_FN:, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/arc_enumerator.m, ArcEnumerator.dealloc, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/arc_enumerator.m, ArcEnumerator.makeMyEnumerator_zero:, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/arc_enumerator.m, MyEnumerator.dealloc, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/arc_enumerator.m, MyEnumerator.initWithArray:filter:, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/arc_enumerator.m, MyEnumerator.nextObject, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/arc_enumerator.m, objc_blockArcEnumerator.makeMyEnumerator_zero:_1, 1, OnUIThread:false, [autorelease,Call to NoArcCallee.giveMeObject,Modeled call to NSObject.autorelease] codetoanalyze/objc/autoreleasepool/arc_keyed_unarchiver.m, ArcKeyedUnarchiver.callInitForReadingFromData_constant:data:, 1, OnUIThread:false, [autorelease,Modeled call to NSKeyedUnarchiver.initForReadingFromData:error:] codetoanalyze/objc/autoreleasepool/arc_keyed_unarchiver.m, ArcKeyedUnarchiver.callInitForReadingWithData_constant:data:, 1, OnUIThread:false, [autorelease,Modeled call to NSKeyedUnarchiver.initForReadingWithData:] codetoanalyze/objc/autoreleasepool/arc_keyed_unarchiver.m, ArcKeyedUnarchiver.callUnarchivedObjectOfClass_constant:data:, 1, OnUIThread:false, [autorelease,Modeled call to NSKeyedUnarchiver.unarchivedObjectOfClass:fromData:error:]