From cb805882c260defb16069a4eb27db987430faf12 Mon Sep 17 00:00:00 2001 From: Qianyi Shu Date: Mon, 15 Jun 2020 02:12:25 -0700 Subject: [PATCH] [cost] add objc test case for NSInteger and NSString Summary: Add objc test case for ```NSInteger``` and ```NSString```. The test cases are adapted from java test case: ```IntTest.java```, ```StringBuilder.java```, and ```StringTest.java```. Inspection of the record will be done later. Reviewed By: ezgicicek Differential Revision: D21994620 fbshipit-source-id: 0c1d7b34e --- .../objc/performance/NSInteger.m | 12 +++ .../codetoanalyze/objc/performance/NSString.m | 89 +++++++++++++++++++ .../objc/performance/cost-issues.exp | 15 ++++ .../codetoanalyze/objc/performance/issues.exp | 14 +++ 4 files changed, 130 insertions(+) create mode 100644 infer/tests/codetoanalyze/objc/performance/NSInteger.m create mode 100644 infer/tests/codetoanalyze/objc/performance/NSString.m diff --git a/infer/tests/codetoanalyze/objc/performance/NSInteger.m b/infer/tests/codetoanalyze/objc/performance/NSInteger.m new file mode 100644 index 000000000..2ffe0655e --- /dev/null +++ b/infer/tests/codetoanalyze/objc/performance/NSInteger.m @@ -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. + */ +#import + +void nsinteger_value_linear(NSInteger integer) { + for (int count = 0; count < integer; count++) { + } +} diff --git a/infer/tests/codetoanalyze/objc/performance/NSString.m b/infer/tests/codetoanalyze/objc/performance/NSString.m new file mode 100644 index 000000000..4ae87c07a --- /dev/null +++ b/infer/tests/codetoanalyze/objc/performance/NSString.m @@ -0,0 +1,89 @@ +/* + * 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 + +NSString* mId; +NSCharacterSet* characterSet; + +NSString* string_by_appending_same_string_linear_FN(NSString* s) { + NSString* str = [s stringByAppendingString:@"me"]; + return str; +} + +NSString* string_by_appending_string_linear_FN(NSString* s, NSString* m) { + NSString* str = [s stringByAppendingString:m]; + return str; +} + +NSUInteger rangeof_character_from_set_linear_FN(NSString* m) { + return [m rangeOfString:@"_"].location; +} + +NSUInteger rangeof_string_quadratic_FN(NSString* m, NSString* n) { + return [m rangeOfString:n].location; +} + +NSString* substring_from_index_linear_FN() { + NSUInteger index = rangeof_character_from_set_linear_FN(mId); + return [mId substringToIndex:index]; +} + +NSString* has_prefix_constant() { + NSString* s = @""; + return [s hasPrefix:s] ? [s substringFromIndex:1] : s; +} + +void component_seperated_by_string_linear_FP(NSString* m) { + NSArray* arrayOfComponents = [m componentsSeparatedByString:@","]; + for (int i = 0; i < arrayOfComponents.count; i++) { + } +} + +void call_component_separated_by_string_constant_FP() { + NSString* s = @"hello"; + component_seperated_by_string_linear_FP(s); +} + +void init_with_bytes_linear_FP(const void* bytes, + NSUInteger length, + NSStringEncoding encoding) { + NSString* s = [[NSString alloc] initWithBytes:bytes + length:length + encoding:encoding]; + for (int i = 0; i < s.length; i++) { + } +} + +void init_with_string_constant_FP() { + NSString* s = @"abcd"; + NSString* str = [[NSString alloc] initWithString:s]; + for (int i = 0; i < str.length; i++) { + } +} + +void init_with_string_linear_FP(NSString* s) { + NSString* str = [[NSString alloc] initWithString:s]; + for (int i = 0; i < str.length; i++) { + } +} + +void call_init_with_string_constant_FP() { + NSString* s = [[NSString alloc] init]; + init_with_string_linear_FP(s); +} + +void substring_no_end_linear_FP(NSString* s, int x) { + NSString* sub = [s substringFromIndex:x]; + for (int i = 0; i < sub.length; i++) { + } +} + +void replace_linear_FP(NSString* s) { + NSString* r = [s stringByReplacingOccurrencesOfString:@"." withString:@","]; + for (int i = 0; i < r.length; i++) { + } +} diff --git a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp index c8169d287..41bd01b57 100644 --- a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp @@ -1,3 +1,18 @@ +codetoanalyze/objc/performance/NSInteger.m, nsinteger_value_linear, 3 + 3 ⋅ integer + 2 ⋅ (1+max(0, integer)), OnUIThread:false, [{1+max(0, integer)},Loop at line 10, column 3,{integer},Loop at line 10, column 3] +codetoanalyze/objc/performance/NSString.m, call_component_separated_by_string_constant_FP, ⊤, OnUIThread:false, [Call to component_seperated_by_string_linear_FP,Unbounded loop,Loop at line 42, column 3] +codetoanalyze/objc/performance/NSString.m, call_init_with_string_constant_FP, ⊤, OnUIThread:false, [Call to init_with_string_linear_FP,Unbounded loop,Loop at line 70, column 3] +codetoanalyze/objc/performance/NSString.m, component_seperated_by_string_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 42, column 3] +codetoanalyze/objc/performance/NSString.m, has_prefix_constant, 15, OnUIThread:false, [] +codetoanalyze/objc/performance/NSString.m, init_with_bytes_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 57, column 3] +codetoanalyze/objc/performance/NSString.m, init_with_string_constant_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 64, column 3] +codetoanalyze/objc/performance/NSString.m, init_with_string_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 70, column 3] +codetoanalyze/objc/performance/NSString.m, rangeof_character_from_set_linear_FN, 6, OnUIThread:false, [] +codetoanalyze/objc/performance/NSString.m, rangeof_string_quadratic_FN, 6, OnUIThread:false, [] +codetoanalyze/objc/performance/NSString.m, replace_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 87, column 3] +codetoanalyze/objc/performance/NSString.m, string_by_appending_same_string_linear_FN, 7, OnUIThread:false, [] +codetoanalyze/objc/performance/NSString.m, string_by_appending_string_linear_FN, 7, OnUIThread:false, [] +codetoanalyze/objc/performance/NSString.m, substring_from_index_linear_FN, 13, OnUIThread:false, [] +codetoanalyze/objc/performance/NSString.m, substring_no_end_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 81, column 3] codetoanalyze/objc/performance/araii.m, Araii.dealloc, 4, OnUIThread:false, [] codetoanalyze/objc/performance/araii.m, Araii.initWithBuffer, 15, OnUIThread:false, [] codetoanalyze/objc/performance/araii.m, memory_leak_raii_main, 18, OnUIThread:false, [] diff --git a/infer/tests/codetoanalyze/objc/performance/issues.exp b/infer/tests/codetoanalyze/objc/performance/issues.exp index 200eda799..100e99491 100644 --- a/infer/tests/codetoanalyze/objc/performance/issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/issues.exp @@ -1,3 +1,17 @@ +codetoanalyze/objc/performance/NSString.m, call_component_separated_by_string_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Call to component_seperated_by_string_linear_FP,Unbounded loop,Loop at line 42, column 3] +codetoanalyze/objc/performance/NSString.m, call_init_with_string_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Call to init_with_string_linear_FP,Unbounded loop,Loop at line 70, column 3] +codetoanalyze/objc/performance/NSString.m, component_seperated_by_string_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 42, column 3] +codetoanalyze/objc/performance/NSString.m, component_seperated_by_string_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSArray.count,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/objc/performance/NSString.m, init_with_bytes_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 57, column 3] +codetoanalyze/objc/performance/NSString.m, init_with_bytes_linear_FP, 6, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/objc/performance/NSString.m, init_with_string_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 64, column 3] +codetoanalyze/objc/performance/NSString.m, init_with_string_constant_FP, 3, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/objc/performance/NSString.m, init_with_string_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 70, column 3] +codetoanalyze/objc/performance/NSString.m, init_with_string_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/objc/performance/NSString.m, replace_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 87, column 3] +codetoanalyze/objc/performance/NSString.m, replace_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/objc/performance/NSString.m, substring_no_end_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 81, column 3] +codetoanalyze/objc/performance/NSString.m, substring_no_end_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/compound_loop_guard.m, compound_while, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] codetoanalyze/objc/performance/compound_loop_guard.m, nested_while_and_or_constant, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] codetoanalyze/objc/performance/compound_loop_guard.m, nested_while_and_or_constant, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here]