From 6ff1a42b6ad973a3c76a050860a2a32b4667a3f4 Mon Sep 17 00:00:00 2001 From: Sungkeun Cho Date: Wed, 9 Sep 2020 01:41:39 -0700 Subject: [PATCH] [cost] Add tests run with ARC/non-ARC sources Summary: This diff adds some tests run with ARC/non-ARC compiled objective-c sources. Reviewed By: ezgicicek Differential Revision: D23542135 fbshipit-source-id: 8e089666b --- infer/tests/clang-arc-no-arc.make | 21 ++++++++ .../objc/autoreleasepool/Makefile | 4 +- .../objc/autoreleasepool/arc_caller.m | 53 +++++++++++++++++++ .../objc/autoreleasepool/cost-issues.exp | 13 +++++ .../objc/autoreleasepool/no_arc_callee.h | 21 ++++++++ .../objc/autoreleasepool/no_arc_callee.m | 35 ++++++++++++ 6 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 infer/tests/clang-arc-no-arc.make create mode 100644 infer/tests/codetoanalyze/objc/autoreleasepool/arc_caller.m create mode 100644 infer/tests/codetoanalyze/objc/autoreleasepool/no_arc_callee.h create mode 100644 infer/tests/codetoanalyze/objc/autoreleasepool/no_arc_callee.m diff --git a/infer/tests/clang-arc-no-arc.make b/infer/tests/clang-arc-no-arc.make new file mode 100644 index 000000000..ae7852c3e --- /dev/null +++ b/infer/tests/clang-arc-no-arc.make @@ -0,0 +1,21 @@ +# 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. + +ROOT_DIR = $(TESTS_DIR)/../.. + +CLEAN_EXTRA += duplicates.txt + +OBJECTS = $(foreach source,$(filter %.c %.cpp %.m %.mm,$(SOURCES)),$(basename $(source)).o) + +include $(TESTS_DIR)/infer.make +include $(TESTS_DIR)/clang-base.make + +infer-out$(TEST_SUFFIX)/report.json: $(CLANG_DEPS) $(SOURCES) $(HEADERS) $(TESTS_DIR)/.inferconfig $(MAKEFILE_LIST) + $(QUIET)$(call silent_on_success,Testing infer/clang in $(TEST_REL_DIR),\ + $(INFER_BIN) capture --results-dir $(@D) --dump-duplicate-symbols $(INFER_OPTIONS) \ + -- clang -fno-objc-arc $(CLANG_OPTIONS) $(SOURCES_NO_ARC) && \ + $(INFER_BIN) capture --continue --results-dir $(@D) --dump-duplicate-symbols $(INFER_OPTIONS) \ + -- clang -fobjc-arc $(CLANG_OPTIONS) $(SOURCES_ARC) && \ + $(INFER_BIN) analyze --results-dir $(@D) --dump-duplicate-symbols $(INFER_OPTIONS)) diff --git a/infer/tests/codetoanalyze/objc/autoreleasepool/Makefile b/infer/tests/codetoanalyze/objc/autoreleasepool/Makefile index 16634c4bd..0c7c19cbf 100644 --- a/infer/tests/codetoanalyze/objc/autoreleasepool/Makefile +++ b/infer/tests/codetoanalyze/objc/autoreleasepool/Makefile @@ -11,8 +11,10 @@ INFERPRINT_OPTIONS = --issues-tests INFERPRINT_COST_OPTIONS = --cost-tests-only-autoreleasepool --cost-issues-tests SOURCES = $(wildcard *.m) +SOURCES_ARC = $(wildcard arc_*.m) +SOURCES_NO_ARC = $(filter-out $(SOURCES_ARC),$(SOURCES)) -include $(TESTS_DIR)/clang.make +include $(TESTS_DIR)/clang-arc-no-arc.make include $(TESTS_DIR)/objc.make include $(TESTS_DIR)/cost.make diff --git a/infer/tests/codetoanalyze/objc/autoreleasepool/arc_caller.m b/infer/tests/codetoanalyze/objc/autoreleasepool/arc_caller.m new file mode 100644 index 000000000..c657ab5bb --- /dev/null +++ b/infer/tests/codetoanalyze/objc/autoreleasepool/arc_caller.m @@ -0,0 +1,53 @@ +/* + * 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" + +@interface ArcCaller : NSObject +@end + +@implementation ArcCaller + +- (void)callAllocObject_zero:(int)n { + for (int i = 0; i < n; i++) { + NoArcCallee* obj = [NoArcCallee allocObject]; + } +} + +- (void)callNewObject_zero:(int)n { + for (int i = 0; i < n; i++) { + NoArcCallee* obj = [NoArcCallee newObject]; + } +} + +- (void)callCopyObject_zero:(int)n x:(NoArcCallee*)x { + for (int i = 0; i < n; i++) { + NoArcCallee* obj = [NoArcCallee copyObject:x]; + } +} + +- (void)callMutableCopyObject_zero:(int)n x:(NoArcCallee*)x { + for (int i = 0; i < n; i++) { + NoArcCallee* obj = [NoArcCallee mutableCopyObject:x]; + } +} + +- (void)callGiveMeObject_linear:(int)n { + for (int i = 0; i < n; i++) { + NoArcCallee* obj = [NoArcCallee giveMeObject]; + } +} + +- (void)callGiveMeObject_autoreleasepool_zero:(int)n { + for (int i = 0; i < n; i++) { + @autoreleasepool { + NoArcCallee* obj = [NoArcCallee giveMeObject]; + } + } +} + +@end diff --git a/infer/tests/codetoanalyze/objc/autoreleasepool/cost-issues.exp b/infer/tests/codetoanalyze/objc/autoreleasepool/cost-issues.exp index 22df3c619..859323a7a 100644 --- a/infer/tests/codetoanalyze/objc/autoreleasepool/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/autoreleasepool/cost-issues.exp @@ -1,3 +1,10 @@ +codetoanalyze/objc/autoreleasepool/arc_caller.m, ArcCaller.callAllocObject_zero:, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/arc_caller.m, ArcCaller.callCopyObject_zero:x:, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/arc_caller.m, ArcCaller.callGiveMeObject_autoreleasepool_zero:, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/arc_caller.m, ArcCaller.callGiveMeObject_linear:, n, OnUIThread:false, [{n},Loop] +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/basic.m, Basic.autoreleased_in_autoreleasepool_zero:, 0, OnUIThread:false, [] codetoanalyze/objc/autoreleasepool/basic.m, Basic.autoreleased_in_loop_linear:, n, OnUIThread:false, [{n},Loop] codetoanalyze/objc/autoreleasepool/basic.m, Basic.autoreleased_in_loop_nested_zero:, 0, OnUIThread:false, [] @@ -8,3 +15,9 @@ codetoanalyze/objc/autoreleasepool/basic.m, Basic.call_no_autorelease_zero, 0, codetoanalyze/objc/autoreleasepool/basic.m, Basic.dealloc, 0, OnUIThread:false, [] codetoanalyze/objc/autoreleasepool/basic.m, Basic.loop_in_autoreleasepool_zero:, 0, OnUIThread:false, [] codetoanalyze/objc/autoreleasepool/basic.m, Basic.no_autoreleased_in_loop_zero:, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/no_arc_callee.m, NoArcCallee.allocObject, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/no_arc_callee.m, NoArcCallee.copyObject:, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/no_arc_callee.m, NoArcCallee.dealloc, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/no_arc_callee.m, NoArcCallee.giveMeObject, 1, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/no_arc_callee.m, NoArcCallee.mutableCopyObject:, 0, OnUIThread:false, [] +codetoanalyze/objc/autoreleasepool/no_arc_callee.m, NoArcCallee.newObject, 0, OnUIThread:false, [] diff --git a/infer/tests/codetoanalyze/objc/autoreleasepool/no_arc_callee.h b/infer/tests/codetoanalyze/objc/autoreleasepool/no_arc_callee.h new file mode 100644 index 000000000..288b7fc65 --- /dev/null +++ b/infer/tests/codetoanalyze/objc/autoreleasepool/no_arc_callee.h @@ -0,0 +1,21 @@ +/* + * 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 + +@interface NoArcCallee : NSObject + ++ (NoArcCallee*)allocObject; + ++ (NoArcCallee*)newObject; + ++ (NoArcCallee*)copyObject:(NoArcCallee*)obj; + ++ (NoArcCallee*)mutableCopyObject:(NoArcCallee*)obj; + ++ (NoArcCallee*)giveMeObject; + +@end diff --git a/infer/tests/codetoanalyze/objc/autoreleasepool/no_arc_callee.m b/infer/tests/codetoanalyze/objc/autoreleasepool/no_arc_callee.m new file mode 100644 index 000000000..50ab1bf76 --- /dev/null +++ b/infer/tests/codetoanalyze/objc/autoreleasepool/no_arc_callee.m @@ -0,0 +1,35 @@ +/* + * 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" + +@implementation NoArcCallee + ++ (NoArcCallee*)allocObject { + return [NoArcCallee alloc]; +} + +/* Since the checker cares about calling `autorelease` only, we define the + following functions as returning a new object simply. */ + ++ (NoArcCallee*)newObject { + return [[NoArcCallee alloc] init]; +} + ++ (NoArcCallee*)copyObject:(NoArcCallee*)obj { + return [[NoArcCallee alloc] init]; +} + ++ (NoArcCallee*)mutableCopyObject:(NoArcCallee*)obj { + return [[NoArcCallee alloc] init]; +} + ++ (NoArcCallee*)giveMeObject { + return [[[NoArcCallee alloc] init] autorelease]; +} + +@end