From 1c80ba8bb787a2c658e4a71bda31b558a4695b5a Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Thu, 22 Oct 2015 03:10:59 -0700 Subject: [PATCH] simplify code handling default paramters Reviewed By: ddino Differential Revision: D2564783 fb-gh-sync-id: 5cca570 --- infer/src/clang/cMethod_signature.ml | 4 +-- infer/src/clang/cMethod_signature.mli | 4 +-- infer/src/clang/cMethod_trans.ml | 8 ++--- infer/src/clang/cTrans.ml | 5 ++- infer/src/clang/cTrans_models.ml | 8 ++--- infer/src/clang/cTrans_utils.ml | 48 +++++++++++++++------------ infer/src/clang/cTrans_utils.mli | 3 +- 7 files changed, 42 insertions(+), 38 deletions(-) diff --git a/infer/src/clang/cMethod_signature.ml b/infer/src/clang/cMethod_signature.ml index 391dc0dcf..7d0d1aea2 100644 --- a/infer/src/clang/cMethod_signature.ml +++ b/infer/src/clang/cMethod_signature.ml @@ -12,7 +12,7 @@ type method_signature = { _name : Procname.t; - _args : (string * Clang_ast_t.type_ptr * Clang_ast_t.stmt option) list; + _args : (string * Clang_ast_t.type_ptr) list; _ret_type : Clang_ast_t.type_ptr; _attributes : Clang_ast_t.attribute list; _loc : Clang_ast_t.source_range; @@ -65,7 +65,7 @@ let ms_to_string ms = let gen = if ms._is_generated then " (generated)" else "" in "Method " ^ (Procname.to_string ms._name) ^ gen ^ " " ^ IList.to_string - (fun (s1, s2, _) -> s1 ^ ", " ^ (Clang_ast_j.string_of_type_ptr s2)) + (fun (s1, s2) -> s1 ^ ", " ^ (Clang_ast_j.string_of_type_ptr s2)) ms._args ^ "->" ^ (Clang_ast_j.string_of_type_ptr ms._ret_type) ^ " " ^ Clang_ast_j.string_of_source_range ms._loc diff --git a/infer/src/clang/cMethod_signature.mli b/infer/src/clang/cMethod_signature.mli index c49b706cf..14f6de603 100644 --- a/infer/src/clang/cMethod_signature.mli +++ b/infer/src/clang/cMethod_signature.mli @@ -15,7 +15,7 @@ type method_signature val ms_get_name : method_signature -> Procname.t val ms_get_args : method_signature -> - (string * Clang_ast_t.type_ptr * Clang_ast_t.stmt option) list + (string * Clang_ast_t.type_ptr) list val ms_get_ret_type : method_signature -> Clang_ast_t.type_ptr @@ -27,7 +27,7 @@ val ms_is_instance : method_signature -> bool val ms_get_lang : method_signature -> CFrontend_config.lang -val make_ms : Procname.t -> (string * Clang_ast_t.type_ptr * Clang_ast_t.stmt option) list -> +val make_ms : Procname.t -> (string * Clang_ast_t.type_ptr) list -> Clang_ast_t.type_ptr -> Clang_ast_t.attribute list -> Clang_ast_t.source_range -> bool -> bool -> CFrontend_config.lang -> method_signature diff --git a/infer/src/clang/cMethod_trans.ml b/infer/src/clang/cMethod_trans.ml index 09072820b..0b2a2f794 100644 --- a/infer/src/clang/cMethod_trans.ml +++ b/infer/src/clang/cMethod_trans.ml @@ -45,10 +45,10 @@ let get_class_param function_method_decl_info = match function_method_decl_info with | Cpp_Meth_decl_info (_, class_name, _) -> let class_type = Ast_expressions.create_class_type class_name in - [(CFrontend_config.this, class_type, None)] + [(CFrontend_config.this, class_type)] | ObjC_Meth_decl_info (_, class_name) -> let class_type = Ast_expressions.create_class_type class_name in - [(CFrontend_config.self, class_type, None)] + [(CFrontend_config.self, class_type)] | _ -> [] else [] @@ -71,7 +71,7 @@ let get_parameters function_method_decl_info = match par with | Clang_ast_t.ParmVarDecl (decl_info, name_info, type_ptr, var_decl_info) -> let name = name_info.Clang_ast_t.ni_name in - (name, type_ptr, var_decl_info.Clang_ast_t.vdi_init_expr) + (name, type_ptr) | _ -> assert false in let pars = IList.map par_to_ms_par (get_param_decls function_method_decl_info) in @@ -200,7 +200,7 @@ let get_formal_parameters tenv ms = let rec defined_parameters pl = match pl with | [] -> [] - | (name, raw_type, _):: pl' -> + | (name, raw_type):: pl' -> let should_add_pointer name ms = let is_objc_self = name = CFrontend_config.self && CMethod_signature.ms_get_lang ms = CFrontend_config.OBJC in diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index 9b0b124b5..cb75874bd 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -600,7 +600,7 @@ struct (Procname.to_string pn) <> CFrontend_config.builtin_object_size | _ -> true in let params_stmt = if should_translate_args then - CTrans_utils.assign_default_params params_stmt None fun_exp_stmt ~is_cxx_method:false + CTrans_utils.assign_default_params params_stmt fun_exp_stmt else [] in let res_trans_par = let instruction' = exec_with_self_exception (exec_with_lvalue_as_reference instruction) in @@ -678,8 +678,7 @@ struct let callee_pname = match sil_method with | Sil.Const (Sil.Cfun pn) -> pn | _ -> assert false (* method pointer not implemented, this shouldn't happen *) in - let class_name_opt = Some (Procname.c_get_class callee_pname) in - let params_stmt = CTrans_utils.assign_default_params params_stmt class_name_opt fun_exp_stmt ~is_cxx_method:true in + let params_stmt = CTrans_utils.assign_default_params params_stmt fun_exp_stmt in (* As we may have nodes coming from different parameters we need to *) (* call instruction for each parameter and collect the results *) (* afterwards. The 'instructions' function does not do that *) diff --git a/infer/src/clang/cTrans_models.ml b/infer/src/clang/cTrans_models.ml index 192a4c59d..d94119828 100644 --- a/infer/src/clang/cTrans_models.ml +++ b/infer/src/clang/cTrans_models.ml @@ -137,7 +137,7 @@ let get_predefined_ms_stringWithUTF8String class_name method_name mk_procname la && method_name = CFrontend_config.string_with_utf8_m in let id_type = Ast_expressions.create_id_type in get_predefined_ms_method condition class_name method_name Procname.Class_objc_method - mk_procname lang [("x", Ast_expressions.create_char_star_type, None)] id_type [] None + mk_procname lang [("x", Ast_expressions.create_char_star_type)] id_type [] None let get_predefined_ms_retain_release class_name method_name mk_procname lang = let condition = is_retain_or_release method_name in @@ -146,7 +146,7 @@ let get_predefined_ms_retain_release class_name method_name mk_procname lang = then Ast_expressions.create_id_type else Ast_expressions.create_void_type in let class_name = CFrontend_config.nsobject_cl in let class_type = Ast_expressions.create_class_type class_name in - let args = [(CFrontend_config.self, class_type, None)] in + let args = [(CFrontend_config.self, class_type)] in get_predefined_ms_method condition class_name method_name Procname.Instance_objc_method mk_procname lang args return_type [] (get_builtinname method_name) @@ -156,7 +156,7 @@ let get_predefined_ms_autoreleasepool_init class_name method_name mk_procname la && class_name = CFrontend_config.nsautorelease_pool_cl in let class_type = Ast_expressions.create_class_type class_name in get_predefined_ms_method condition class_name method_name Procname.Instance_objc_method - mk_procname lang [(CFrontend_config.self, class_type, None)] + mk_procname lang [(CFrontend_config.self, class_type)] Ast_expressions.create_void_type [] None let get_predefined_ms_nsautoreleasepool_release class_name method_name mk_procname lang = @@ -165,7 +165,7 @@ let get_predefined_ms_nsautoreleasepool_release class_name method_name mk_procna && class_name = CFrontend_config.nsautorelease_pool_cl in let class_type = Ast_expressions.create_class_type class_name in get_predefined_ms_method condition class_name method_name Procname.Instance_objc_method - mk_procname lang [(CFrontend_config.self, class_type, None)] Ast_expressions.create_void_type + mk_procname lang [(CFrontend_config.self, class_type)] Ast_expressions.create_void_type [] (Some SymExec.ModelBuiltins.__objc_release_autorelease_pool) let get_predefined_model_method_signature class_name method_name mk_procname lang = diff --git a/infer/src/clang/cTrans_utils.ml b/infer/src/clang/cTrans_utils.ml index 6df45141f..845df5022 100644 --- a/infer/src/clang/cTrans_utils.ml +++ b/infer/src/clang/cTrans_utils.ml @@ -708,28 +708,34 @@ let rec pointer_of_call_expr stmt = | [stmt] -> pointer_of_call_expr stmt | _ -> None -let assign_default_params params_stmt class_name_opt stmt ~is_cxx_method = +let get_decl_from_call_expr stmt = match pointer_of_call_expr stmt with - | Some pointer -> - (match CMethod_trans.method_signature_of_pointer class_name_opt pointer with - | Some callee_ms -> - (let args = CMethod_signature.ms_get_args callee_ms in - let args = if is_cxx_method then match args with _::tl -> tl | _ -> assert false - else args in - let replace_default_arg param = - match param with - | Clang_ast_t.CXXDefaultArgExpr _, (_, _, Some default_instr) -> - default_instr - | instr, _ -> instr in - try - let params_args = IList.combine params_stmt args in - IList.map replace_default_arg params_args - with Invalid_argument _ -> - (* IList.combine failed because of different list lengths *) - Printing.log_err "Param count doesn't match %s\n" - (Procname.to_string (CMethod_signature.ms_get_name callee_ms)); - params_stmt) - | None -> params_stmt) + | Some pointer -> Ast_utils.get_decl pointer + | _ -> None + + +let assign_default_params params_stmt call_stmt = + let open Clang_ast_t in + match get_decl_from_call_expr call_stmt with + | Some FunctionDecl (_, name_info, _, fdecl_info) + | Some CXXMethodDecl (_, name_info, _, fdecl_info, _) + | Some CXXConstructorDecl (_, name_info, _, fdecl_info, _) -> + (let get_param_default_val param_decl = match param_decl with + | ParmVarDecl (_, _, _, var_decl_info) -> var_decl_info.vdi_init_expr + | _ -> None in + let replace_default_arg param = match param with + | CXXDefaultArgExpr _, Some default_expr -> default_expr + | instr, _ -> instr in + let default_params = IList.map get_param_default_val fdecl_info.Clang_ast_t.fdi_parameters in + try + let param_args = IList.combine params_stmt default_params in + IList.map replace_default_arg param_args + with Invalid_argument _ -> + let name = name_info.ni_name in + (* IList.combine failed because of different list lengths *) + Printing.log_err "Param count doesn't match %s\n" name; + params_stmt) + | Some _ -> params_stmt | None -> params_stmt diff --git a/infer/src/clang/cTrans_utils.mli b/infer/src/clang/cTrans_utils.mli index 1c95e96bd..56f14659b 100644 --- a/infer/src/clang/cTrans_utils.mli +++ b/infer/src/clang/cTrans_utils.mli @@ -219,8 +219,7 @@ val is_logical_negation_of_int : Sil.tenv -> Clang_ast_t.expr_info -> Clang_ast_ val is_dispatch_function : Clang_ast_t.stmt list -> int option -val assign_default_params : Clang_ast_t.stmt list -> string option -> Clang_ast_t.stmt -> - is_cxx_method:bool -> Clang_ast_t.stmt list +val assign_default_params : Clang_ast_t.stmt list -> Clang_ast_t.stmt -> Clang_ast_t.stmt list val is_block_enumerate_function : Clang_ast_t.obj_c_message_expr_info -> bool