From e411db6f825da28147f02cc654c3833a3a2fad09 Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Tue, 19 Nov 2019 04:57:17 -0800 Subject: [PATCH] [AL] Make the transition Parameters work with function calls Summary: The transition HOLDS-NEXT WITH-TRANSITION Parameters was only implemented for ObjC methods, now it also works on function calls. Reviewed By: jvillard Differential Revision: D18572760 fbshipit-source-id: 06e615cbe --- infer/src/al/cTL.ml | 4 +++- .../linters-for-test-only/CFStringExample.m | 24 +++++++++++++++++++ .../al_definitions/linters_example.al | 9 +++++++ .../objc/linters-for-test-only/issues.exp | 6 +++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 infer/tests/codetoanalyze/objc/linters-for-test-only/CFStringExample.m diff --git a/infer/src/al/cTL.ml b/infer/src/al/cTL.ml index 887611a10..8a305e20f 100644 --- a/infer/src/al/cTL.ml +++ b/infer/src/al/cTL.ml @@ -806,7 +806,9 @@ let transition_via_parameters an = match an with | Decl (ObjCMethodDecl (_, _, omdi)) -> List.map ~f:(fun d -> Decl d) omdi.omdi_parameters - | Stmt (ObjCMessageExpr (_, stmt_list, _, _)) -> + | Stmt (ObjCMessageExpr (_, stmt_list, _, _)) + | Stmt (CallExpr (_, _ :: stmt_list, _)) + | Stmt (CXXMemberCallExpr (_, stmt_list, _)) -> List.map ~f:(fun stmt -> Stmt stmt) stmt_list | _ -> [] diff --git a/infer/tests/codetoanalyze/objc/linters-for-test-only/CFStringExample.m b/infer/tests/codetoanalyze/objc/linters-for-test-only/CFStringExample.m new file mode 100644 index 000000000..e07afb3a7 --- /dev/null +++ b/infer/tests/codetoanalyze/objc/linters-for-test-only/CFStringExample.m @@ -0,0 +1,24 @@ +/* + * 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 + +static CFStringRef CreateFoo() { + return CFStringCreateWithCString(NULL, "Hello", kCFStringEncodingUTF8); +} + +static void doSomething(CFStringRef s) { CFShow(s); } + +static void doSomethingElse(int x, CFStringRef s) { CFShow(s); } + +static void Foo() {} + +int main() { + doSomething(CreateFoo()); + doSomethingElse(0, CreateFoo()); + Foo(); + return 0; +} diff --git a/infer/tests/codetoanalyze/objc/linters-for-test-only/al_definitions/linters_example.al b/infer/tests/codetoanalyze/objc/linters-for-test-only/al_definitions/linters_example.al index bd7cf7ebb..9b0c42779 100644 --- a/infer/tests/codetoanalyze/objc/linters-for-test-only/al_definitions/linters_example.al +++ b/infer/tests/codetoanalyze/objc/linters-for-test-only/al_definitions/linters_example.al @@ -888,3 +888,12 @@ DEFINE-CHECKER IVAR_CAPTURED_IN_OBJC_BLOCK = { SET severity = "ERROR"; SET mode = "ON"; }; + +DEFINE-CHECKER CALLS_TO_FUNCTIONS_WITH_CREATE_FUNCTION_PARAMETERS = { +LET is_create_method_parameter = HOLDS-NEXT WITH-TRANSITION Parameters + (call_function(REGEXP(".*Create.*")) AND has_type("REGEXP('C.*Ref')")); +SET report_when = + WHEN + is_create_method_parameter + HOLDS-IN-NODE CallExpr; +}; \ No newline at end of file diff --git a/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp b/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp index 673c2c5ad..6795e9661 100644 --- a/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp +++ b/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp @@ -1,3 +1,9 @@ +codetoanalyze/objc/linters-for-test-only/CFStringExample.m, main, 19, ALL_PATH_NO_FILTER_EXAMPLE, no_bucket, WARNING, [] +codetoanalyze/objc/linters-for-test-only/CFStringExample.m, main, 19, BLACKLIST_PATH_EXAMPLE, no_bucket, WARNING, [] +codetoanalyze/objc/linters-for-test-only/CFStringExample.m, main, 19, FILTER_BY_ALL_PATH_EXAMPLE, no_bucket, WARNING, [] +codetoanalyze/objc/linters-for-test-only/CFStringExample.m, main, 19, WHITE_BLACKLIST_PATH_EXAMPLE, no_bucket, WARNING, [] +codetoanalyze/objc/linters-for-test-only/CFStringExample.m, main, 20, CALLS_TO_FUNCTIONS_WITH_CREATE_FUNCTION_PARAMETERS, no_bucket, WARNING, [] +codetoanalyze/objc/linters-for-test-only/CFStringExample.m, main, 21, CALLS_TO_FUNCTIONS_WITH_CREATE_FUNCTION_PARAMETERS, no_bucket, WARNING, [] codetoanalyze/objc/linters-for-test-only/CallingAMethodWithSelf.m, CallingAMethodWithSelf::methodThatShallBeOkaySuper, 35, TEST_IS_RECEIVER_SUPER, no_bucket, WARNING, [] codetoanalyze/objc/linters-for-test-only/CallingAMethodWithSelf.m, CallingAMethodWithSelf::methodThatShallComplain, 38, TEST_IS_METHOD_EXPOSED, no_bucket, WARNING, [] codetoanalyze/objc/linters-for-test-only/CallingAMethodWithSelf.m, CallingAMethodWithSelf::methodThatShallComplain, 39, TEST_IF_VIEW_METHOD_IS_NOT_CALLED_WITH_SUPER, no_bucket, WARNING, []