From 6fe3edc44efdf5a39fd5408b4a7274ca80990c37 Mon Sep 17 00:00:00 2001 From: Dino Distefano Date: Fri, 26 Oct 2018 07:39:50 -0700 Subject: [PATCH] Extending type matching to BlockExpr Reviewed By: jvillard Differential Revision: D10853084 fbshipit-source-id: 231cf4693 --- infer/src/clang/ctl_parser_types.ml | 4 +- .../linters-for-test-only/componentblock.mm | 109 ++++++++++++++++++ .../objcpp/linters-for-test-only/issues.exp | 4 + 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 infer/tests/codetoanalyze/objcpp/linters-for-test-only/componentblock.mm diff --git a/infer/src/clang/ctl_parser_types.ml b/infer/src/clang/ctl_parser_types.ml index 83c4c0ca5..3f5dc3f31 100644 --- a/infer/src/clang/ctl_parser_types.ml +++ b/infer/src/clang/ctl_parser_types.ml @@ -489,7 +489,9 @@ and c_type_equal c_type abs_ctype = | LValueReferenceType (_, qt), Reference abs_typ | RValueReferenceType (_, qt), Reference abs_typ -> check_type_ptr qt.qt_type_ptr abs_typ - | ObjCObjectPointerType (_, qt), ObjCGenProt _ -> + | BlockPointerType (_, qt), TypeName _ + | FunctionProtoType (_, {fti_return_type= qt}, _), TypeName _ + | ObjCObjectPointerType (_, qt), _ -> check_type_ptr qt.qt_type_ptr abs_ctype | ObjCObjectType _, ObjCGenProt _ -> objc_object_type_equal c_type abs_ctype diff --git a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/componentblock.mm b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/componentblock.mm new file mode 100644 index 000000000..b74bb1f5a --- /dev/null +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/componentblock.mm @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#import + +@interface CKComponent : NSObject + +@end + +@implementation CKComponent + +@end + +typedef CKComponent* (^FBSectionComponentGenerator)(id model, + id context); + +struct TestComponentOptions { + id componentGenerator; +}; + +@interface TestComponent : NSObject + ++ (instancetype)newWithComponentGenerator: + (FBSectionComponentGenerator)generator; ++ (instancetype)newWithOptions:(TestComponentOptions)options; + +@end + +@implementation TestComponent + ++ (instancetype)newWithComponentGenerator: + (FBSectionComponentGenerator)generator { + return [self new]; +} + ++ (instancetype)newWithOptions:(TestComponentOptions)options { + return [self new]; +} + +@end + +__unused static void testBadValDecl() { + NSObject* obj = [NSObject new]; + __unused TestComponent* component = [TestComponent + newWithComponentGenerator:^CKComponent*(id m, id c) { + __unused NSObject* innerObj = obj; + return nil; + }]; +} + +__unused static void testBadStructValDecl() { + NSObject* obj = [NSObject new]; + __unused TestComponent *component = + [TestComponent + newWithOptions:{ + .componentGenerator = ^CKComponent *(id m, id c) { + __unused NSObject *innerObj = obj; + return nil; +} +, +}]; +} + +__unused static void testBadNonValDecl() { + NSObject* obj = [NSObject new]; + __unused TestComponent* component = nil; + component = [TestComponent + newWithComponentGenerator:^CKComponent*(id m, id c) { + __unused id innerObj = obj; + return nil; + }]; +} + +__unused static void testBadStructNonValDecl() { + NSObject* obj = [NSObject new]; + __unused TestComponent* component = nil; + component = + [TestComponent + newWithOptions:{ + .componentGenerator = ^CKComponent *(id m, id c) { + __unused NSObject *innerObj = obj; + return nil; +} +, +}]; +} + +__unused static void testOK() { + __unused TestComponent* component = [TestComponent + newWithComponentGenerator:^CKComponent*(id m, id c) { + __unused id innerObj = m; + return nil; + }]; +} + +__unused static void testOKStruct() { + __unused TestComponent *component = + [TestComponent + newWithOptions:{ + .componentGenerator = ^CKComponent *(id m, id c) { + __unused id innerObj = m; + return nil; +} +, +}]; +} diff --git a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp index a5c787202..398a522dc 100644 --- a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp @@ -26,6 +26,10 @@ codetoanalyze/objcpp/linters-for-test-only/TestStructFieldChecks.mm, Linters_dum codetoanalyze/objcpp/linters-for-test-only/TestStructFieldChecks.mm, buttonComponent, 31, TITLE_NOT_INITIALIZED, no_bucket, WARNING, [] codetoanalyze/objcpp/linters-for-test-only/TestStructFieldChecks.mm, buttonComponent, 33, TITLE_NOT_INITIALIZED, no_bucket, WARNING, [] codetoanalyze/objcpp/linters-for-test-only/TestStructFieldChecks.mm, buttonComponent, 37, TITLE_NOT_INITIALIZED, no_bucket, WARNING, [] +codetoanalyze/objcpp/linters-for-test-only/componentblock.mm, objc_block_2, 48, OBJC_BLOCK_CAPTURING_VALUES, no_bucket, WARNING, [] +codetoanalyze/objcpp/linters-for-test-only/componentblock.mm, objc_block_3, 59, OBJC_BLOCK_CAPTURING_VALUES, no_bucket, WARNING, [] +codetoanalyze/objcpp/linters-for-test-only/componentblock.mm, objc_block_4, 71, OBJC_BLOCK_CAPTURING_VALUES, no_bucket, WARNING, [] +codetoanalyze/objcpp/linters-for-test-only/componentblock.mm, objc_block_5, 83, OBJC_BLOCK_CAPTURING_VALUES, no_bucket, WARNING, [] codetoanalyze/objcpp/linters-for-test-only/hash_test.mm, std::hash_NSObject_*__operator(), 12, DISCOURAGED_HASH_METHOD_INVOCATION, no_bucket, WARNING, [] codetoanalyze/objcpp/linters-for-test-only/stateless.m, Linters_dummy_method, 21, ADHERE_TO_PROTOCOL, no_bucket, WARNING, [] codetoanalyze/objcpp/linters-for-test-only/stateless.m, Linters_dummy_method, 24, ONLY_ONE_CLASS_METHOD, no_bucket, WARNING, []