diff --git a/infer/src/clang/ast_expressions.ml b/infer/src/clang/ast_expressions.ml index cdd42bedd..958d9c241 100644 --- a/infer/src/clang/ast_expressions.ml +++ b/infer/src/clang/ast_expressions.ml @@ -12,15 +12,6 @@ open! IStd module L = Logging -let dummy_source_range () = - let dummy_source_loc = {Clang_ast_t.sl_file= None; sl_line= None; sl_column= None} in - (dummy_source_loc, dummy_source_loc) - - -let dummy_stmt_info () = - {Clang_ast_t.si_pointer= CAst_utils.get_fresh_pointer (); si_source_range= dummy_source_range ()} - - let stmt_info_with_fresh_pointer stmt_info = { Clang_ast_t.si_pointer= CAst_utils.get_fresh_pointer () ; si_source_range= stmt_info.Clang_ast_t.si_source_range } @@ -60,7 +51,7 @@ let create_class_qual_type ?quals typename = let create_integer_literal n = - let stmt_info = dummy_stmt_info () in + let stmt_info = CAst_utils.dummy_stmt_info () in let expr_info = {Clang_ast_t.ei_qual_type= create_int_type; ei_value_kind= `RValue; ei_object_kind= `Ordinary} in @@ -102,12 +93,6 @@ let create_nil stmt_info = create_implicit_cast_expr stmt_info [paren_expr] create_id_type `NullToPointer -let dummy_stmt () = - let pointer = CAst_utils.get_fresh_pointer () in - let source_range = dummy_source_range () in - Clang_ast_t.NullStmt ({Clang_ast_t.si_pointer= pointer; si_source_range= source_range}, []) - - let make_expr_info qt vk objc_kind = {Clang_ast_t.ei_qual_type= qt; ei_value_kind= vk; ei_object_kind= objc_kind} diff --git a/infer/src/clang/ast_expressions.mli b/infer/src/clang/ast_expressions.mli index b76e13dac..d1b769ad0 100644 --- a/infer/src/clang/ast_expressions.mli +++ b/infer/src/clang/ast_expressions.mli @@ -12,12 +12,6 @@ open Clang_ast_t (** This module creates extra ast constructs that are needed for the translation *) -val dummy_stmt : unit -> stmt - -val dummy_source_range : unit -> source_range - -val dummy_stmt_info : unit -> stmt_info - val create_class_qual_type : ?quals:Typ.type_quals -> Typ.Name.t -> qual_type val create_pointer_qual_type : ?quals:Typ.type_quals -> qual_type -> qual_type diff --git a/infer/src/clang/cAst_utils.ml b/infer/src/clang/cAst_utils.ml index ef08f4780..15f128e6f 100644 --- a/infer/src/clang/cAst_utils.ml +++ b/infer/src/clang/cAst_utils.ml @@ -62,6 +62,21 @@ let get_fresh_pointer () = internal_pointer +let dummy_source_range () = + let dummy_source_loc = {Clang_ast_t.sl_file= None; sl_line= None; sl_column= None} in + (dummy_source_loc, dummy_source_loc) + + +let dummy_stmt_info () = + {Clang_ast_t.si_pointer= get_fresh_pointer (); si_source_range= dummy_source_range ()} + + +let dummy_stmt () = + let pointer = get_fresh_pointer () in + let source_range = dummy_source_range () in + Clang_ast_t.NullStmt ({Clang_ast_t.si_pointer= pointer; si_source_range= source_range}, []) + + let type_from_unary_expr_or_type_trait_expr_info info = match info.Clang_ast_t.uttei_qual_type with Some tp -> Some tp | None -> None @@ -72,14 +87,16 @@ let get_decl_opt decl_ptr_opt = match decl_ptr_opt with Some decl_ptr -> get_decl decl_ptr | None -> None -let get_stmt stmt_ptr = +let get_stmt stmt_ptr source_range = let stmt = Int.Table.find ClangPointers.pointer_stmt_table stmt_ptr in - if Option.is_none stmt then L.internal_error "stmt with pointer %d not found@\n" stmt_ptr ; + if Option.is_none stmt then + CFrontend_config.incorrect_assumption __POS__ source_range "stmt with pointer %d not found@\n" + stmt_ptr ; stmt -let get_stmt_opt stmt_ptr_opt = - match stmt_ptr_opt with Some stmt_ptr -> get_stmt stmt_ptr | None -> None +let get_stmt_opt stmt_ptr_opt source_range = + match stmt_ptr_opt with Some stmt_ptr -> get_stmt stmt_ptr source_range | None -> None let get_decl_opt_with_decl_ref decl_ref_opt = @@ -123,7 +140,9 @@ let get_type type_ptr = match type_ptr with | Clang_ast_types.TypePtr.Ptr raw_ptr -> let typ = Int.Table.find ClangPointers.pointer_type_table raw_ptr in - if Option.is_none typ then L.internal_error "type with pointer %d not found@\n" raw_ptr ; + if Option.is_none typ then + CFrontend_config.incorrect_assumption __POS__ (dummy_source_range ()) + "type with pointer %d not found@\n" raw_ptr ; typ | _ -> (* otherwise, function fails *) @@ -145,7 +164,7 @@ let get_desugared_type type_ptr = let get_decl_from_typ_ptr typ_ptr = let typ_opt = get_desugared_type typ_ptr in - let typ = match typ_opt with Some t -> t | _ -> assert false in + let typ = match typ_opt with Some t -> t | None -> assert false in match (typ : Clang_ast_t.c_type) with | RecordType (_, decl_ptr) | ObjCInterfaceType (_, decl_ptr) -> get_decl decl_ptr diff --git a/infer/src/clang/cAst_utils.mli b/infer/src/clang/cAst_utils.mli index e178db29e..63197b216 100644 --- a/infer/src/clang/cAst_utils.mli +++ b/infer/src/clang/cAst_utils.mli @@ -11,6 +11,12 @@ open! IStd (** Functions for transformations of ast nodes *) +val dummy_stmt : unit -> Clang_ast_t.stmt + +val dummy_source_range : unit -> Clang_ast_t.source_range + +val dummy_stmt_info : unit -> Clang_ast_t.stmt_info + val get_fresh_pointer : unit -> Clang_ast_t.pointer val type_from_unary_expr_or_type_trait_expr_info : @@ -20,9 +26,10 @@ val get_decl : Clang_ast_t.pointer -> Clang_ast_t.decl option val get_decl_opt : Clang_ast_t.pointer option -> Clang_ast_t.decl option -val get_stmt : Clang_ast_t.pointer -> Clang_ast_t.stmt option +val get_stmt : Clang_ast_t.pointer -> Clang_ast_t.source_range -> Clang_ast_t.stmt option -val get_stmt_opt : Clang_ast_t.pointer option -> Clang_ast_t.stmt option +val get_stmt_opt : + Clang_ast_t.pointer option -> Clang_ast_t.source_range -> Clang_ast_t.stmt option val get_decl_opt_with_decl_ref : Clang_ast_t.decl_ref option -> Clang_ast_t.decl option diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index a65afdb3b..0413a0fa2 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -2275,19 +2275,20 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s (* For OpaqueValueExpr we return the translation generated from its source expression*) - and opaqueValueExpr_trans trans_state opaque_value_expr_info = + and opaqueValueExpr_trans trans_state opaque_value_expr_info source_range = L.(debug Capture Verbose) " priority node free = '%s'@\n@." (string_of_bool (PriorityNode.is_priority_free trans_state)) ; match trans_state.opaque_exp with | Some exp -> {empty_res_trans with exps= [exp]} - | _ -> + | None -> match opaque_value_expr_info.Clang_ast_t.ovei_source_expr with | Some stmt -> instruction trans_state stmt - | _ -> - assert false + | None -> + CFrontend_config.incorrect_assumption __POS__ source_range + "Expected source expression for OpaqueValueExpr" (* NOTE: This translation has several hypothesis. Need to be verified when we have more*) @@ -2672,9 +2673,12 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s let sil_loc = CLocation.get_sil_location stmt_info context in let trans_state_pri = PriorityNode.try_claim_priority_node trans_state stmt_info in let is_dyn_array = cxx_new_expr_info.Clang_ast_t.xnei_is_array in + let source_range = stmt_info.Clang_ast_t.si_source_range in let size_exp_opt, res_trans_size = if is_dyn_array then - match CAst_utils.get_stmt_opt cxx_new_expr_info.Clang_ast_t.xnei_array_size_expr with + match + CAst_utils.get_stmt_opt cxx_new_expr_info.Clang_ast_t.xnei_array_size_expr source_range + with | Some stmt -> ( let trans_state_size = {trans_state_pri with succ_nodes= []} in @@ -2689,12 +2693,16 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s else (None, empty_res_trans) in let placement_args = - List.filter_map ~f:CAst_utils.get_stmt cxx_new_expr_info.Clang_ast_t.xnei_placement_args + List.filter_map + ~f:(fun i -> CAst_utils.get_stmt i source_range) + cxx_new_expr_info.Clang_ast_t.xnei_placement_args in let trans_state_placement = {trans_state_pri with succ_nodes= []} in let res_trans_placement = instructions trans_state_placement placement_args in let res_trans_new = cpp_new_trans sil_loc typ size_exp_opt res_trans_placement.exps in - let stmt_opt = CAst_utils.get_stmt_opt cxx_new_expr_info.Clang_ast_t.xnei_initializer_expr in + let stmt_opt = + CAst_utils.get_stmt_opt cxx_new_expr_info.Clang_ast_t.xnei_initializer_expr source_range + in let trans_state_init = {trans_state_pri with succ_nodes= []} in let var_exp_typ = match res_trans_new.exps with @@ -3085,8 +3093,9 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s objCPropertyRefExpr_trans trans_state stmt_list | CXXThisExpr (stmt_info, _, expr_info) -> cxxThisExpr_trans trans_state stmt_info expr_info - | OpaqueValueExpr (_, _, _, opaque_value_expr_info) -> + | OpaqueValueExpr (stmt_info, _, _, opaque_value_expr_info) -> opaqueValueExpr_trans trans_state opaque_value_expr_info + stmt_info.Clang_ast_t.si_source_range | PseudoObjectExpr (_, stmt_list, _) -> pseudoObjectExpr_trans trans_state stmt_list | UnaryExprOrTypeTraitExpr (_, _, expr_info, ei) -> @@ -3360,10 +3369,10 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s true in (* its pointer will be used in PriorityNode *) - let this_stmt_info = Ast_expressions.dummy_stmt_info () in + let this_stmt_info = CAst_utils.dummy_stmt_info () in (* this will be used to avoid creating node in init_expr_trans *) let child_stmt_info = - {(Ast_expressions.dummy_stmt_info ()) with Clang_ast_t.si_source_range= source_range} + {(CAst_utils.dummy_stmt_info ()) with Clang_ast_t.si_source_range= source_range} in let trans_state' = PriorityNode.try_claim_priority_node trans_state this_stmt_info in let class_qual_type = diff --git a/infer/src/clang/cTrans_models.ml b/infer/src/clang/cTrans_models.ml index 615e23738..7ecf70cb6 100644 --- a/infer/src/clang/cTrans_models.ml +++ b/infer/src/clang/cTrans_models.ml @@ -76,7 +76,7 @@ let get_predefined_ms_method condition class_name method_name method_kind mk_pro in let ms = CMethod_signature.make_ms procname arguments return_type attributes - (Ast_expressions.dummy_source_range ()) + (CAst_utils.dummy_source_range ()) false lang None None None `None in Some ms diff --git a/infer/src/clang/cTrans_utils.ml b/infer/src/clang/cTrans_utils.ml index 56409c69c..444c24b7e 100644 --- a/infer/src/clang/cTrans_utils.ml +++ b/infer/src/clang/cTrans_utils.ml @@ -449,7 +449,15 @@ let dereference_var_sil (exp, typ) sil_loc = assigned to it *) let dereference_value_from_result sil_loc trans_result ~strip_pointer = let obj_sil, class_typ = extract_exp_from_list trans_result.exps "" in - let typ_no_ptr = match class_typ.Typ.desc with Tptr (typ, _) -> typ | _ -> assert false in + let typ_no_ptr = + match class_typ.Typ.desc with + | Tptr (typ, _) -> + typ + | _ -> + CFrontend_config.incorrect_assumption __POS__ + (CAst_utils.dummy_source_range ()) + "Expected pointer type but found type %a" (Typ.pp_full Pp.text) class_typ + in let cast_typ = if strip_pointer then typ_no_ptr else class_typ in let cast_inst, cast_exp = dereference_var_sil (obj_sil, cast_typ) sil_loc in {trans_result with instrs= trans_result.instrs @ cast_inst; exps= [(cast_exp, cast_typ)]} @@ -570,7 +578,7 @@ let is_superinstance mei = let is_null_stmt s = match s with Clang_ast_t.NullStmt _ -> true | _ -> false let extract_stmt_from_singleton stmt_list warning_string = - extract_item_from_singleton stmt_list warning_string (Ast_expressions.dummy_stmt ()) + extract_item_from_singleton stmt_list warning_string (CAst_utils.dummy_stmt ()) module Self = struct