diff --git a/Makefile b/Makefile index 252002861..a4bf97a7b 100644 --- a/Makefile +++ b/Makefile @@ -168,6 +168,7 @@ DIRECT_TESTS += \ java_infer \ java_litho-required-props \ java_performance \ + java_performance-exclusive \ java_purity \ java_quandary \ java_racerd \ diff --git a/infer/man/man1/infer-full.txt b/infer/man/man1/infer-full.txt index 5795200a5..42786cf41 100644 --- a/infer/man/man1/infer-full.txt +++ b/infer/man/man1/infer-full.txt @@ -1402,6 +1402,10 @@ INTERNAL OPTIONS --icfg-dotty-outfile-reset Cancel the effect of --icfg-dotty-outfile. + --no-inclusive-cost + Deactivates: Computes the inclusive cost (Conversely: + --inclusive-cost) + --incremental-analysis Activates: [EXPERIMENTAL] Use incremental analysis for changed files. Not compatible with --reanalyze and --continue-analysis. diff --git a/infer/src/base/Config.ml b/infer/src/base/Config.ml index a572e94ae..b31983d20 100644 --- a/infer/src/base/Config.ml +++ b/infer/src/base/Config.ml @@ -1402,6 +1402,10 @@ and icfg_dotty_outfile = options that would generate icfg file otherwise" +and inclusive_cost = + CLOpt.mk_bool ~long:"inclusive-cost" ~default:true "Computes the inclusive cost" + + and iphoneos_target_sdk_version = CLOpt.mk_string_opt ~long:"iphoneos-target-sdk-version" ~in_help:InferCommand.[(Capture, manual_clang_linters)] @@ -2752,6 +2756,8 @@ and hoisting_report_only_expensive = !hoisting_report_only_expensive and icfg_dotty_outfile = !icfg_dotty_outfile +and inclusive_cost = !inclusive_cost + and iphoneos_target_sdk_version = !iphoneos_target_sdk_version and iphoneos_target_sdk_version_path_regex = diff --git a/infer/src/base/Config.mli b/infer/src/base/Config.mli index fd5185cd9..c4b170745 100644 --- a/infer/src/base/Config.mli +++ b/infer/src/base/Config.mli @@ -373,6 +373,8 @@ val infer_is_javac : bool val implicit_sdk_root : string option +val inclusive_cost : bool + val inferconfig_file : string option val inferconfig_dir : string option diff --git a/infer/src/cost/cost.ml b/infer/src/cost/cost.ml index 4f98d5d2c..2961b115a 100644 --- a/infer/src/cost/cost.ml +++ b/infer/src/cost/cost.ml @@ -60,7 +60,8 @@ module InstrBasicCost = struct let get_instr_cost_record tenv extras instr_node instr = match instr with - | Sil.Call (ret, Exp.Const (Const.Cfun callee_pname), params, _, _) -> + | Sil.Call (ret, Exp.Const (Const.Cfun callee_pname), params, _, _) when Config.inclusive_cost + -> let {inferbo_invariant_map; integer_type_widths; get_callee_summary_and_formals} = extras in let operation_cost = match @@ -94,6 +95,8 @@ module InstrBasicCost = struct if is_allocation_function callee_pname then CostDomain.plus CostDomain.unit_cost_allocation operation_cost else operation_cost + | Sil.Call (_, Exp.Const (Const.Cfun _), _, _, _) -> + CostDomain.zero_record | Sil.Load {id= lhs_id} when Ident.is_none lhs_id -> (* dummy deref inserted by frontend--don't count as a step. In JDK 11, dummy deref disappears and causes cost differences diff --git a/infer/tests/codetoanalyze/java/performance-exclusive/ExclusiveTest.java b/infer/tests/codetoanalyze/java/performance-exclusive/ExclusiveTest.java new file mode 100644 index 000000000..c16d24433 --- /dev/null +++ b/infer/tests/codetoanalyze/java/performance-exclusive/ExclusiveTest.java @@ -0,0 +1,22 @@ +/* + * 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. + */ +class ExclusiveTest { + + void linear(int x) { + for (int i = 0; i < x; i++) {} + } + + void call_linear_exclusive_constant(int x) { + linear(x); + } + + void call_linear_exclusive_linear(int x) { + for (int i = 0; i < x; i++) { + linear(i); + } + } +} diff --git a/infer/tests/codetoanalyze/java/performance-exclusive/Makefile b/infer/tests/codetoanalyze/java/performance-exclusive/Makefile new file mode 100644 index 000000000..f2abd5648 --- /dev/null +++ b/infer/tests/codetoanalyze/java/performance-exclusive/Makefile @@ -0,0 +1,12 @@ +# 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. + +TESTS_DIR = ../../.. + +INFER_OPTIONS = --cost-only --no-inclusive-cost --bufferoverrun --debug-exceptions --use-cost-threshold +INFERPRINT_OPTIONS = --issues-tests +SOURCES = $(wildcard *.java) + +include $(TESTS_DIR)/javac.make diff --git a/infer/tests/codetoanalyze/java/performance-exclusive/issues.exp b/infer/tests/codetoanalyze/java/performance-exclusive/issues.exp new file mode 100644 index 000000000..12771889f --- /dev/null +++ b/infer/tests/codetoanalyze/java/performance-exclusive/issues.exp @@ -0,0 +1,2 @@ +codetoanalyze/java/performance-exclusive/ExclusiveTest.java, ExclusiveTest.call_linear_exclusive_linear(int):void, 0, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 2 + 7 ⋅ x, O(x), degree = 1,{x},Loop at line 18] +codetoanalyze/java/performance-exclusive/ExclusiveTest.java, ExclusiveTest.linear(int):void, 0, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 2 + 5 ⋅ x, O(x), degree = 1,{x},Loop at line 10]