From 10ca0c32691d82ffad2929bcc102a33d3f4ae3d2 Mon Sep 17 00:00:00 2001 From: Dino Distefano Date: Tue, 14 May 2019 07:57:33 -0700 Subject: [PATCH] Fix detection of return type for instancetype when using NS_ASSUME_NONNUL Reviewed By: jvillard Differential Revision: D15317686 fbshipit-source-id: a13520499 --- infer/src/clang/cType_to_sil_type.ml | 5 ++-- infer/src/clang/ctl_parser_types.ml | 10 +++++++ .../objc/linters-for-test-only/issues.exp | 3 +++ .../linters-for-test-only/ns_assume_nonnull.m | 26 +++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 infer/tests/codetoanalyze/objc/linters-for-test-only/ns_assume_nonnull.m diff --git a/infer/src/clang/cType_to_sil_type.ml b/infer/src/clang/cType_to_sil_type.ml index fdfcb570e..8ed4280ba 100644 --- a/infer/src/clang/cType_to_sil_type.ml +++ b/infer/src/clang/cType_to_sil_type.ml @@ -100,10 +100,11 @@ let rec build_array_type translate_decl tenv (qual_type : Clang_ast_t.qual_type) and type_desc_of_attr_type translate_decl tenv type_info attr_info = - match type_info.Clang_ast_t.ti_desugared_type with + let open Clang_ast_t in + match type_info.ti_desugared_type with | Some type_ptr -> ( match CAst_utils.get_type type_ptr with - | Some (Clang_ast_t.ObjCObjectPointerType (_, qual_type)) -> + | Some (ObjCObjectPointerType (_, qual_type)) -> let typ = qual_type_to_sil_type translate_decl tenv qual_type in Typ.Tptr (typ, pointer_attribute_of_objc_attribute attr_info) | _ -> diff --git a/infer/src/clang/ctl_parser_types.ml b/infer/src/clang/ctl_parser_types.ml index 03caae476..c471634e4 100644 --- a/infer/src/clang/ctl_parser_types.ml +++ b/infer/src/clang/ctl_parser_types.ml @@ -511,6 +511,12 @@ and c_type_equal c_type abs_ctype = match (c_type, abs_ctype) with | BuiltinType (_, bi), BuiltIn abi -> builtin_equal bi abi + | BuiltinType (_, `ObjCId), TypeName ae when ALVar.compare_str_with_alexp "instancetype" ae -> + (* This is a special case where coming from an AttributedType with {ati_attr_kind=`Nonnull} where the + compiler change 'instancetype' to ObjCId *) + L.(debug Linters Medium) + "@\n Special Case when comaping BuiltInType(ObjcId) and TypeName(instancetype)\n" ; + true | TypedefType (_, tdi), BuiltIn _ -> check_type_ptr tdi.tti_child_type.qt_type_ptr abs_ctype | PointerType _, BuiltIn _ | PointerType _, Pointer _ | ObjCObjectPointerType _, Pointer _ -> @@ -522,6 +528,8 @@ and c_type_equal c_type abs_ctype = | FunctionProtoType (_, {fti_return_type= qt}, _), TypeName _ | ObjCObjectPointerType (_, qt), _ -> check_type_ptr qt.qt_type_ptr abs_ctype + | ObjCObjectType (_, ooti), TypeName _ -> + check_type_ptr ooti.ooti_base_type abs_ctype | ObjCObjectType _, ObjCGenProt _ -> objc_object_type_equal c_type abs_ctype | ObjCInterfaceType (_, pointer), TypeName ae -> @@ -554,6 +562,8 @@ and c_type_equal c_type abs_ctype = | TypedefType (ti, _), ObjCGenProt _ | AttributedType (ti, _), Pointer _ -> ( match ti.ti_desugared_type with Some dt -> check_type_ptr dt abs_ctype | None -> false ) + | AttributedType (ti, _), TypeName _ -> ( + match ti.ti_desugared_type with Some dt -> check_type_ptr dt abs_ctype | None -> false ) | _, _ -> display_equality_warning () ; false 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 6034c7d06..41b50577d 100644 --- a/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp +++ b/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp @@ -241,6 +241,9 @@ codetoanalyze/objc/linters-for-test-only/implicit_cast.c, main, 9, TEST_IMPLICIT codetoanalyze/objc/linters-for-test-only/implicit_cast.c, main, 9, TEST_VAR_TYPE_CHECK, no_bucket, WARNING, [] codetoanalyze/objc/linters-for-test-only/namespace.mm, Linters_dummy_method, 7, TEST_DEFINE_NAMESPACE, no_bucket, WARNING, [] codetoanalyze/objc/linters-for-test-only/namespace.mm, Linters_dummy_method, 15, TEST_USING_NAMESPACE, no_bucket, WARNING, [] +codetoanalyze/objc/linters-for-test-only/ns_assume_nonnull.m, Blah::someMethod, 14, TEST_INSTANCE_TYPE, no_bucket, WARNING, [] +codetoanalyze/objc/linters-for-test-only/ns_assume_nonnull.m, Blah::someMethod, 20, TEST_INSTANCE_TYPE, no_bucket, WARNING, [] +codetoanalyze/objc/linters-for-test-only/ns_assume_nonnull.m, Blah::someMethod, 20, TEST_IS_METHOD_EXPOSED, no_bucket, WARNING, [] codetoanalyze/objc/linters-for-test-only/protocols.m, Foo::newWithA, 22, TEST_INSTANCE_TYPE, no_bucket, WARNING, [] codetoanalyze/objc/linters-for-test-only/protocols.m, Foo::newWithA, 22, TEST_PROTOCOL_TYPE_INHERITANCE, no_bucket, WARNING, [] codetoanalyze/objc/linters-for-test-only/protocols.m, Foo::newWithB, 23, TEST_INSTANCE_TYPE, no_bucket, WARNING, [] diff --git a/infer/tests/codetoanalyze/objc/linters-for-test-only/ns_assume_nonnull.m b/infer/tests/codetoanalyze/objc/linters-for-test-only/ns_assume_nonnull.m new file mode 100644 index 000000000..2908755e8 --- /dev/null +++ b/infer/tests/codetoanalyze/objc/linters-for-test-only/ns_assume_nonnull.m @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019-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 + +NS_ASSUME_NONNULL_BEGIN + +@interface Blah : NSObject + ++ (instancetype)someMethod; + +@end + +@implementation Blah + ++ (instancetype)someMethod { + return nil; +} + +@end + +NS_ASSUME_NONNULL_END