From b99b653ffab3cea130c1b0b99b362da14b0c1f2e Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Wed, 13 Sep 2017 09:17:59 -0700 Subject: [PATCH] [linters] Fix the computing of the successors of method declarations Reviewed By: ddino Differential Revision: D5814233 fbshipit-source-id: 84fae71 --- infer/src/backend/InferPrint.ml | 12 ++++ infer/src/clang/cAst_utils.ml | 3 + infer/src/clang/cFrontend_errors.ml | 5 +- infer/src/clang/ctl_parser_types.ml | 19 +++++- .../TestParameterMemModels.mm | 62 +++++++++++++++++++ .../objcpp/linters-for-test-only/issues.exp | 6 ++ .../linters-for-test-only/linters_example.al | 10 +++ 7 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParameterMemModels.mm diff --git a/infer/src/backend/InferPrint.ml b/infer/src/backend/InferPrint.ml index 20d47aa01..bf308351c 100644 --- a/infer/src/backend/InferPrint.ml +++ b/infer/src/backend/InferPrint.ml @@ -67,6 +67,18 @@ let get_bug_hash (kind: string) (type_str: string) (procedure_id: string) (filen (node_key: int) (error_desc: Localise.error_desc) = let qualifier_tag_call_procedure = Localise.error_desc_get_tag_call_procedure error_desc in let qualifier_tag_value = Localise.error_desc_get_tag_value error_desc in + Logging.progress + "\nCreating hash from kind=%s, type_str=%s, \ + procedure_id=%s, filename=%s, node_key=%d, \ + qualifier_tag_call_procedure=%s, qualifier_tag_value=%s @." + kind + type_str + procedure_id + filename + node_key + qualifier_tag_call_procedure + qualifier_tag_value + ; Hashtbl.hash ( kind , type_str diff --git a/infer/src/clang/cAst_utils.ml b/infer/src/clang/cAst_utils.ml index 60210ff5f..b7f77f665 100644 --- a/infer/src/clang/cAst_utils.ml +++ b/infer/src/clang/cAst_utils.ml @@ -282,6 +282,9 @@ let generate_key_decl decl = let name = full_name_of_decl_opt (Some decl) in Buffer.add_string buffer (string_of_int (get_tag decl)) ; Buffer.add_string buffer (QualifiedCppName.to_qual_string name) ; + Logging.(debug Capture Medium) + "Generated key for decl with name %s and tag %d is %s" (QualifiedCppName.to_qual_string name) + (get_tag decl) (Buffer.contents buffer) ; Buffer.contents buffer let rec get_super_if decl = diff --git a/infer/src/clang/cFrontend_errors.ml b/infer/src/clang/cFrontend_errors.ml index 2b0636edd..07e3e212b 100644 --- a/infer/src/clang/cFrontend_errors.ml +++ b/infer/src/clang/cFrontend_errors.ml @@ -431,10 +431,7 @@ let log_frontend_issue translation_unit_context method_decl_opt key (issue_desc: let exn = Exceptions.Frontend_warning ((issue_desc.id, issue_desc.name), err_desc, __POS__) in let trace = [Errlog.make_trace_element 0 issue_desc.loc "" []] in let err_kind = issue_desc.severity in - let method_name = - CAst_utils.full_name_of_decl_opt method_decl_opt |> QualifiedCppName.to_qual_string - in - let key = Hashtbl.hash (key ^ method_name) in + let key = Hashtbl.hash key in Reporting.log_issue_from_errlog err_kind errlog exn ~loc:issue_desc.loc ~ltr:trace ~node_id:(0, key) ?linters_def_file ?doc_url:issue_desc.doc_url diff --git a/infer/src/clang/ctl_parser_types.ml b/infer/src/clang/ctl_parser_types.ml index f3095fa03..c48172e3d 100644 --- a/infer/src/clang/ctl_parser_types.ml +++ b/infer/src/clang/ctl_parser_types.ml @@ -132,7 +132,24 @@ let get_successor_decls_of_stmt stmt = -> [] let get_successor_decls_of_decl decl = - match Clang_ast_proj.get_decl_context_tuple decl with Some (decls, _) -> decls | None -> [] + let open Clang_ast_t in + match Clang_ast_proj.get_decl_context_tuple decl with + | Some (decls, _) + -> decls + | None -> + match decl with + | FunctionDecl (_, _, _, fdi) + | CXXMethodDecl (_, _, _, fdi, _) + | CXXConstructorDecl (_, _, _, fdi, _) + | CXXConversionDecl (_, _, _, fdi, _) + | CXXDestructorDecl (_, _, _, fdi, _) + -> fdi.Clang_ast_t.fdi_parameters + | ObjCMethodDecl (_, _, mdi) + -> mdi.Clang_ast_t.omdi_parameters + | BlockDecl (_, block_decl_info) + -> block_decl_info.Clang_ast_t.bdi_parameters + | _ + -> [] let get_successor_stmts_of_decl decl = let open Clang_ast_t in diff --git a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParameterMemModels.mm b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParameterMemModels.mm new file mode 100644 index 000000000..5a6cd951c --- /dev/null +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParameterMemModels.mm @@ -0,0 +1,62 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#import + +class FBMemImage; + +class FBMemQuery; + +@interface FBMemModelUsed1 : NSObject + +// NEW_COMPONENT_USING_MEM_MODEL bug ++ (instancetype)newImage:(FBMemImage*)image; +// no NEW_COMPONENT_USING_MEM_MODEL bug ++ (instancetype)newInt:(int)size; +// no NEW_COMPONENT_USING_MEM_MODEL bug ++ (int)newReturnInt:(FBMemImage*)size; +// NEW_COMPONENT_USING_MEM_MODEL bug ++ (instancetype)newMultiParams:(int)size + image:(FBMemImage*)image + query:(FBMemQuery*)query; + +@end + +@implementation FBMemModelUsed1 + +// NEW_COMPONENT_USING_MEM_MODEL bug ++ (instancetype)newImage:(FBMemImage*)image { + return nil; +} + +// no NEW_COMPONENT_USING_MEM_MODEL bug ++ (instancetype)newInt:(int)size { + return nil; +} + +// no NEW_COMPONENT_USING_MEM_MODEL bug ++ (int)newReturnInt:(FBMemImage*)size { + return 0; +} + +// NEW_COMPONENT_USING_MEM_MODEL bug ++ (instancetype)newMultiParams:(int)size + image:(FBMemImage*)image + query:(FBMemQuery*)query { + return nil; +} + +@end + +@interface FBMemModelUsed2 : NSObject + ++ (instancetype)newQuery:(FBMemQuery*)query; + +@end + +@implementation FBMemModelUsed2 + ++ (instancetype)newQuery:(FBMemQuery*)query { + return nil; +} + +@end 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 964ccca49..67fbc0d75 100644 --- a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp @@ -4,6 +4,12 @@ codetoanalyze/objcpp/linters-for-test-only/ReferenceTest.mm, ReferenceTest_newWi codetoanalyze/objcpp/linters-for-test-only/ReferenceTest.mm, ReferenceTest_newWithActionRef:, 40, TEST_REFERENCE, [] codetoanalyze/objcpp/linters-for-test-only/ReferenceTest.mm, ReferenceTest_newWithConstAction:, 21, TEST_REFERENCE, [] codetoanalyze/objcpp/linters-for-test-only/ReferenceTest.mm, ReferenceTest_newWithConstAction:, 36, TEST_REFERENCE, [] +codetoanalyze/objcpp/linters-for-test-only/TestParameterMemModels.mm, FBMemModelUsed1_newImage:, 12, NEW_COMPONENT_USING_MEM_MODEL, [] +codetoanalyze/objcpp/linters-for-test-only/TestParameterMemModels.mm, FBMemModelUsed1_newImage:, 27, NEW_COMPONENT_USING_MEM_MODEL, [] +codetoanalyze/objcpp/linters-for-test-only/TestParameterMemModels.mm, FBMemModelUsed1_newMultiParams:image:query:, 20, NEW_COMPONENT_USING_MEM_MODEL, [] +codetoanalyze/objcpp/linters-for-test-only/TestParameterMemModels.mm, FBMemModelUsed1_newMultiParams:image:query:, 44, NEW_COMPONENT_USING_MEM_MODEL, [] +codetoanalyze/objcpp/linters-for-test-only/TestParameterMemModels.mm, FBMemModelUsed2_newQuery:, 52, NEW_COMPONENT_USING_MEM_MODEL, [] +codetoanalyze/objcpp/linters-for-test-only/TestParameterMemModels.mm, FBMemModelUsed2_newQuery:, 58, NEW_COMPONENT_USING_MEM_MODEL, [] codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, anotherButtonComponent, 41, PARAMETER_TRANS_TYPE, [] codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, anotherButtonComponent, 41, TEST_PARAMETER_LABEL, [] codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, anotherButtonComponent, 41, TEST_PARAMETER_LABEL_REGEXP, [] diff --git a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/linters_example.al b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/linters_example.al index 70aa89718..89d85abf5 100644 --- a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/linters_example.al +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/linters_example.al @@ -139,3 +139,13 @@ DEFINE-CHECKER TEST_PARAMETER_SELECTOR_BY_TYPE = { HOLDS-IN-NODE ObjCMessageExpr; SET message = "Do not construct the Component action with a selector only...`"; }; + +DEFINE-CHECKER NEW_COMPONENT_USING_MEM_MODEL = { + SET report_when = + WHEN method_return_type("instancetype") + AND HOLDS-NEXT WITH-TRANSITION Parameters (has_type("REGEXP('FBMem.*')*")) + AND declaration_has_name(REGEXP("^new.*:$")) + HOLDS-IN-NODE ObjCMethodDecl; + + SET message = "Consider using fragment models instead."; +};