From a81f4e7bf9d30dd15b07a0ee7a0f7d057569bd5b Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Fri, 21 Apr 2017 07:22:12 -0700 Subject: [PATCH] Remove custom Clang_ast_types module, extend types instead Summary: update-submodule: facebook-clang-plugins Don't replace `clang_ast_types.ml` from fcp. Instead, one can now extend `Clang_ast_t.type_ptr` type! Reviewed By: jberdine Differential Revision: D4922249 fbshipit-source-id: 53b22b0 --- .gitignore | 1 + facebook-clang-plugins | 2 +- infer/src/Makefile | 2 +- infer/src/clang/CProcname.ml | 26 +++++------ infer/src/clang/CType.ml | 8 ++-- infer/src/clang/Capture.re | 2 +- infer/src/clang/ComponentKit.ml | 3 +- infer/src/clang/ast_expressions.ml | 10 ++--- infer/src/clang/ast_expressions.mli | 2 - infer/src/clang/cAst_utils.ml | 37 ++++++++------- infer/src/clang/cAst_utils.mli | 10 ++--- infer/src/clang/cFrontend_config.ml | 4 +- infer/src/clang/cFrontend_config.mli | 2 +- infer/src/clang/cLocation.ml | 8 ++-- infer/src/clang/cMethod_signature.ml | 2 +- infer/src/clang/cMethod_trans.ml | 4 +- infer/src/clang/cTrans.ml | 2 +- infer/src/clang/cType_to_sil_type.ml | 26 +++++------ infer/src/clang/clang_ast_extend.ml | 63 ++++++++++++++++++++++++++ infer/src/clang/clang_ast_types.ml | 65 --------------------------- infer/src/clang/objcCategory_decl.ml | 2 +- infer/src/clang/objcInterface_decl.ml | 4 +- infer/src/clang/objcProtocol_decl.ml | 2 +- 23 files changed, 141 insertions(+), 146 deletions(-) create mode 100644 infer/src/clang/clang_ast_extend.ml delete mode 100644 infer/src/clang/clang_ast_types.ml diff --git a/.gitignore b/.gitignore index 51177fc04..d90a6a2b8 100644 --- a/.gitignore +++ b/.gitignore @@ -128,6 +128,7 @@ buck-out/ /infer/src/clang/clang_ast_v.mli /infer/src/clang/clang_ast_visit.ml /infer/src/clang/clang_ast_main.ml +/infer/src/clang/clang_ast_types.ml /infer/annotations/annot_classes/ /infer/annotations/annotations.jar diff --git a/facebook-clang-plugins b/facebook-clang-plugins index c61920129..ee26293dd 160000 --- a/facebook-clang-plugins +++ b/facebook-clang-plugins @@ -1 +1 @@ -Subproject commit c61920129b7b519a420e8dc648d1319391aaaf73 +Subproject commit ee26293dd046acc5c2dd862d3201aa9f7dace96a diff --git a/infer/src/Makefile b/infer/src/Makefile index 2437d246a..9d5100478 100644 --- a/infer/src/Makefile +++ b/infer/src/Makefile @@ -110,7 +110,7 @@ CLANG_ATDGEN_STUBS = $(addprefix $(CLANG_ATDGEN_STUB_BASE), $(CLANG_ATDGEN_SUFFI FCP_CLANG_AST_PROJ = $(addprefix $(FCP_CLANG_OCAML_BUILD_DIR)/, \ clang_ast_proj.ml clang_ast_proj.mli) FCP_CLANG_AST_MAIN = $(addprefix $(FCP_CLANG_OCAML_DIR)/, \ - clang_ast_visit.ml clang_ast_main.ml) + clang_ast_visit.ml clang_ast_main.ml clang_ast_types.ml) FCP_FILES_TO_MIRROR = $(FCP_CLANG_AST_PROJ) $(FCP_CLANG_AST_MAIN) INFER_CLANG_FCP_MIRRORED_FILES = $(addprefix $(CLANG_SOURCES)/, $(notdir $(FCP_FILES_TO_MIRROR))) diff --git a/infer/src/clang/CProcname.ml b/infer/src/clang/CProcname.ml index 6a4bd4444..6fb8ee70e 100644 --- a/infer/src/clang/CProcname.ml +++ b/infer/src/clang/CProcname.ml @@ -40,9 +40,9 @@ let get_template_info tenv (fdi : Clang_ast_t.function_decl_info) : Typ.template let is_decl_info_generic_model {Clang_ast_t.di_attributes} = let f = function - | Clang_ast_t.AnnotateAttr {ai_parameters=[_; name; _]} - when String.equal name "__infer_generic_model" -> true - | _ -> false in + | Clang_ast_t.AnnotateAttr {ai_parameters=[_; name; _]} + when String.equal name "__infer_generic_model" -> true + | _ -> false in List.exists ~f di_attributes let mk_c_function translation_unit_context ?tenv name function_decl_info_opt = @@ -51,7 +51,7 @@ let mk_c_function translation_unit_context ?tenv name function_decl_info_opt = | Some (decl_info, function_decl_info) -> (match function_decl_info.Clang_ast_t.fdi_storage_class with | Some "static" -> - let file_opt = (fst decl_info.Clang_ast_t.di_source_range).Clang_ast_t.sl_file in + let file_opt = (fst decl_info.Clang_ast_t.di_source_range).Clang_ast_t.sl_file |> Option.map ~f:SourceFile.from_abs_path in let file_to_hex src = SourceFile.to_string src |> Utils.string_crc_hex32 in Option.value_map ~f:file_to_hex ~default:"" file_opt | _ -> "") @@ -64,7 +64,7 @@ let mk_c_function translation_unit_context ?tenv name function_decl_info_opt = | _ -> "" in let template_info, is_generic_model = match function_decl_info_opt, tenv with | Some (decl_info, function_decl_info), Some t -> - get_template_info t function_decl_info, is_decl_info_generic_model decl_info + get_template_info t function_decl_info, is_decl_info_generic_model decl_info | _ -> Typ.NoTemplate, false in let mangled = file ^ mangled_name in if String.is_empty mangled then @@ -85,14 +85,14 @@ let mk_cpp_method ?tenv class_name method_name ?meth_decl mangled = | Some (CXXConversionDecl (di, _, _, fdi, _)) | Some (CXXDestructorDecl (di, _, _, fdi, _)) -> ( let templ_info = match tenv with - | Some t -> get_template_info t fdi - | None -> Typ.NoTemplate in + | Some t -> get_template_info t fdi + | None -> Typ.NoTemplate in let is_gen_model = is_decl_info_generic_model di || - (* read whether parent class is annoatated as generic model *) - di.di_parent_pointer - |> Option.value_map ~f:CAst_utils.get_decl ~default:None - |> Option.map ~f:Clang_ast_proj.get_decl_tuple - |> Option.value_map ~f:is_decl_info_generic_model ~default:false in + (* read whether parent class is annoatated as generic model *) + di.di_parent_pointer + |> Option.value_map ~f:CAst_utils.get_decl ~default:None + |> Option.map ~f:Clang_ast_proj.get_decl_tuple + |> Option.value_map ~f:is_decl_info_generic_model ~default:false in templ_info, is_gen_model ) | _ -> Typ.NoTemplate, false in @@ -102,7 +102,7 @@ let mk_cpp_method ?tenv class_name method_name ?meth_decl mangled = let mk_objc_method class_typename method_name method_kind = Typ.Procname.ObjC_Cpp (Typ.Procname.objc_cpp class_typename method_name method_kind Typ.NoTemplate - ~is_generic_model:false) + ~is_generic_model:false) let block_procname_with_index defining_proc i = Config.anonymous_block_prefix ^ diff --git a/infer/src/clang/CType.ml b/infer/src/clang/CType.ml index a978c0308..d3102f31a 100644 --- a/infer/src/clang/CType.ml +++ b/infer/src/clang/CType.ml @@ -46,12 +46,12 @@ let rec return_type_of_function_type_ptr type_ptr = return_type_of_function_type_ptr in_type_ptr | Some _ -> Logging.err_debug "Warning: Type pointer %s is not a function type." - (Clang_ast_types.type_ptr_to_string type_ptr); - `ErrorType + (Clang_ast_extend.type_ptr_to_string type_ptr); + Clang_ast_extend.ErrorType | None -> Logging.err_debug "Warning: Type pointer %s not found." - (Clang_ast_types.type_ptr_to_string type_ptr); - `ErrorType + (Clang_ast_extend.type_ptr_to_string type_ptr); + Clang_ast_extend.ErrorType let return_type_of_function_type tp = return_type_of_function_type_ptr tp diff --git a/infer/src/clang/Capture.re b/infer/src/clang/Capture.re index d71e9aa54..7ded700fd 100644 --- a/infer/src/clang/Capture.re +++ b/infer/src/clang/Capture.re @@ -72,7 +72,7 @@ let run_clang_frontend ast_source => { switch ast_decl { | Clang_ast_t.TranslationUnitDecl (_, _, _, info) => Config.arc_mode := info.Clang_ast_t.tudi_arc_enabled; - let source_file = info.Clang_ast_t.tudi_input_path; + let source_file = SourceFile.from_abs_path info.Clang_ast_t.tudi_input_path; init_global_state_for_capture_and_linters source_file; let lang = switch info.Clang_ast_t.tudi_input_kind { diff --git a/infer/src/clang/ComponentKit.ml b/infer/src/clang/ComponentKit.ml index 5eae279e8..65723f948 100644 --- a/infer/src/clang/ComponentKit.ml +++ b/infer/src/clang/ComponentKit.ml @@ -27,7 +27,8 @@ let is_in_main_file translation_unit_context an = | None -> false | Some source_file -> - SourceFile.equal source_file translation_unit_context.CFrontend_config.source_file + SourceFile.equal (SourceFile.from_abs_path source_file) + translation_unit_context.CFrontend_config.source_file let is_ck_context (context: CLintersContext.context) an = context.is_ck_translation_unit diff --git a/infer/src/clang/ast_expressions.ml b/infer/src/clang/ast_expressions.ml index 56415d401..50f9eaa32 100644 --- a/infer/src/clang/ast_expressions.ml +++ b/infer/src/clang/ast_expressions.ml @@ -68,7 +68,7 @@ let create_qual_type ?(is_const=false) qt_type_ptr = let new_constant_type_ptr () = let pointer = CAst_utils.get_fresh_pointer () in - `Prebuilt pointer + Clang_ast_extend.Prebuilt pointer (* Whenever new type are added manually to the translation here, *) (* they should be added to the map in cTypes_decl too!! *) @@ -106,19 +106,17 @@ let create_void_unsigned_long_type = let create_void_void_type = new_constant_type_ptr () -let create_class_type typename = `ClassType typename +let create_class_type typename = Clang_ast_extend.ClassType typename let create_class_qual_type ?(is_const=false) typename = create_qual_type ~is_const @@ create_class_type typename let make_objc_class_type class_name = create_class_type (Typ.Name.Objc.from_string class_name) -let create_struct_type struct_name = `StructType struct_name - -let create_pointer_type typ = `PointerOf typ +let create_pointer_type typ = Clang_ast_extend.PointerOf typ let create_pointer_qual_type ~is_const typ = create_qual_type ~is_const @@ create_pointer_type typ -let create_reference_type typ = `ReferenceOf typ +let create_reference_type typ = Clang_ast_extend.ReferenceOf typ let create_integer_literal n = let stmt_info = dummy_stmt_info () in diff --git a/infer/src/clang/ast_expressions.mli b/infer/src/clang/ast_expressions.mli index fdae03d1c..91c289705 100644 --- a/infer/src/clang/ast_expressions.mli +++ b/infer/src/clang/ast_expressions.mli @@ -47,8 +47,6 @@ val create_void_void_type : type_ptr val create_class_type : Typ.Name.t -> type_ptr val create_class_qual_type : ?is_const:bool -> Typ.Name.t -> qual_type -val create_struct_type : Typ.Name.t -> type_ptr - val create_pointer_type : type_ptr -> type_ptr val create_pointer_qual_type : is_const:bool -> type_ptr -> qual_type diff --git a/infer/src/clang/cAst_utils.ml b/infer/src/clang/cAst_utils.ml index fb625a1f7..9da9fb8cc 100644 --- a/infer/src/clang/cAst_utils.ml +++ b/infer/src/clang/cAst_utils.ml @@ -92,7 +92,7 @@ let get_property_of_ivar decl_ptr = let update_sil_types_map type_ptr sil_type = CFrontend_config.sil_types_map := - Clang_ast_types.TypePointerMap.add type_ptr sil_type !CFrontend_config.sil_types_map + Clang_ast_extend.TypePointerMap.add type_ptr sil_type !CFrontend_config.sil_types_map let update_enum_map enum_constant_pointer sil_exp = let open Clang_ast_main in @@ -112,17 +112,20 @@ let get_enum_constant_exp enum_constant_pointer = Clang_ast_main.PointerMap.find enum_constant_pointer !CFrontend_config.enum_map let get_type type_ptr = - try - (* There is chance for success only if type_ptr is in fact clang pointer *) - (let raw_ptr = Clang_ast_types.type_ptr_to_clang_pointer type_ptr in - try - Some (Clang_ast_main.PointerMap.find raw_ptr !CFrontend_config.pointer_type_index) - with Not_found -> Logging.out "type with pointer %d not found\n" raw_ptr; None) - with Clang_ast_types.Not_Clang_Pointer -> - (* otherwise, function fails *) - let type_str = Clang_ast_types.type_ptr_to_string type_ptr in - Logging.out "type %s is not clang pointer\n" type_str; - None + (* There is chance for success only if type_ptr is in fact clang pointer *) + match type_ptr with + | Clang_ast_types.TypePtr.Ptr raw_ptr -> + (try + Some (Clang_ast_main.PointerMap.find raw_ptr !CFrontend_config.pointer_type_index) + with Not_found -> + Logging.out "type with pointer %d not found\n" raw_ptr; + None + ) + | _ -> + (* otherwise, function fails *) + let type_str = Clang_ast_extend.type_ptr_to_string type_ptr in + Logging.out "type %s is not clang pointer\n" type_str; + None let get_desugared_type type_ptr = let typ_opt = get_type type_ptr in @@ -168,8 +171,6 @@ let sil_annot_of_type type_ptr = | _ -> None in mk_annot annot_name_opt -let string_of_type_ptr type_ptr = Clang_ast_j.string_of_type_ptr type_ptr - let name_of_typedef_type_info {Clang_ast_t.tti_decl_ptr} = match get_decl tti_decl_ptr with | Some TypedefDecl (_, name_decl_info, _, _, _) -> @@ -183,17 +184,19 @@ let name_opt_of_typedef_type_ptr type_ptr = | _ -> None let string_of_qual_type {Clang_ast_t.qt_type_ptr; qt_is_const} = - Printf.sprintf "%s%s" (if qt_is_const then "is_const " else "") (string_of_type_ptr qt_type_ptr) + Printf.sprintf "%s%s" + (if qt_is_const then "is_const " else "") + (Clang_ast_extend.type_ptr_to_string qt_type_ptr) let add_type_from_decl_ref type_ptr_to_sil_type tenv decl_ref_opt fail_if_not_found = match decl_ref_opt with (* translate interface first if found *) | Some dr -> - ignore (type_ptr_to_sil_type tenv (`DeclPtr dr.Clang_ast_t.dr_decl_pointer)); + ignore (type_ptr_to_sil_type tenv (Clang_ast_extend.DeclPtr dr.Clang_ast_t.dr_decl_pointer)); | _ -> if fail_if_not_found then assert false else () let add_type_from_decl_ref_list type_ptr_to_sil_type tenv decl_ref_list = let add_elem dr = - ignore (type_ptr_to_sil_type tenv (`DeclPtr dr.Clang_ast_t.dr_decl_pointer)) in + ignore (type_ptr_to_sil_type tenv (Clang_ast_extend.DeclPtr dr.Clang_ast_t.dr_decl_pointer)) in List.iter ~f:add_elem decl_ref_list let get_function_decl_with_body decl_ptr = diff --git a/infer/src/clang/cAst_utils.mli b/infer/src/clang/cAst_utils.mli index 266c211ad..d1c2831f0 100644 --- a/infer/src/clang/cAst_utils.mli +++ b/infer/src/clang/cAst_utils.mli @@ -60,10 +60,6 @@ val get_desugared_type : Clang_ast_t.type_ptr -> Clang_ast_t.c_type option (RecordType, ObjCInterfaceType and None for others *) val get_decl_from_typ_ptr : Clang_ast_t.type_ptr -> Clang_ast_t.decl option -(** returns string representation of type_ptr - NOTE: this doesn't expand type, it only converts type_ptr to string *) -val string_of_type_ptr : Clang_ast_t.type_ptr -> string - val name_of_typedef_type_info : Clang_ast_t.typedef_type_info -> QualifiedCppName.t (** returns name of typedef if type_ptr points to Typedef, None otherwise *) @@ -135,13 +131,13 @@ val get_super_ObjCImplementationDecl : val is_objc_if_descendant : ?blacklist:string list -> Clang_ast_t.decl option -> string list -> bool -val type_ptr_to_objc_interface : Clang_ast_types.t_ptr -> Clang_ast_t.decl option +val type_ptr_to_objc_interface : Clang_ast_t.type_ptr -> Clang_ast_t.decl option -val type_ptr_is_typedef_named : Clang_ast_types.t_ptr -> string -> bool +val type_ptr_is_typedef_named : Clang_ast_t.type_ptr -> string -> bool (** A class method that returns an instance of the class is a factory method. *) val is_objc_factory_method : Clang_ast_t.decl -> Clang_ast_t.decl -> bool val name_of_decl_ref_opt : Clang_ast_t.decl_ref option -> string option -val sil_annot_of_type : Clang_ast_types.t_ptr -> Annot.Item.t +val sil_annot_of_type : Clang_ast_t.type_ptr -> Annot.Item.t diff --git a/infer/src/clang/cFrontend_config.ml b/infer/src/clang/cFrontend_config.ml index b85f02572..9c41f43ed 100644 --- a/infer/src/clang/cFrontend_config.ml +++ b/infer/src/clang/cFrontend_config.ml @@ -100,7 +100,7 @@ let log_out = ref Format.std_formatter let pointer_decl_index = ref Clang_ast_main.PointerMap.empty let pointer_stmt_index = ref Clang_ast_main.PointerMap.empty let pointer_type_index = ref Clang_ast_main.PointerMap.empty -let sil_types_map = ref Clang_ast_types.TypePointerMap.empty +let sil_types_map = ref Clang_ast_extend.TypePointerMap.empty let reset_global_state () = enum_map := Clang_ast_main.PointerMap.empty; @@ -110,4 +110,4 @@ let reset_global_state () = pointer_decl_index := Clang_ast_main.PointerMap.empty; pointer_stmt_index := Clang_ast_main.PointerMap.empty; pointer_type_index := Clang_ast_main.PointerMap.empty; - sil_types_map := Clang_ast_types.TypePointerMap.empty; + sil_types_map := Clang_ast_extend.TypePointerMap.empty; diff --git a/infer/src/clang/cFrontend_config.mli b/infer/src/clang/cFrontend_config.mli index 9903bc15e..225146264 100644 --- a/infer/src/clang/cFrontend_config.mli +++ b/infer/src/clang/cFrontend_config.mli @@ -106,6 +106,6 @@ val pointer_type_index : Clang_ast_t.c_type Clang_ast_main.PointerMap.t ref (** Map from type pointers (clang pointers and types created later by frontend) to sil types Populated during frontend execution when new type is found *) -val sil_types_map : (Typ.t Clang_ast_types.TypePointerMap.t) ref +val sil_types_map : (Typ.t Clang_ast_extend.TypePointerMap.t) ref val reset_global_state : unit -> unit diff --git a/infer/src/clang/cLocation.ml b/infer/src/clang/cLocation.ml index 7c379c1b4..eb1c10e13 100644 --- a/infer/src/clang/cLocation.ml +++ b/infer/src/clang/cLocation.ml @@ -16,8 +16,8 @@ let clang_to_sil_location trans_unit_ctx clang_loc = let line = Option.value ~default:(-1) clang_loc.Clang_ast_t.sl_line in let col = Option.value ~default:(-1) clang_loc.Clang_ast_t.sl_column in let file = - Option.value ~default:trans_unit_ctx.CFrontend_config.source_file - clang_loc.Clang_ast_t.sl_file in + Option.value_map ~default:trans_unit_ctx.CFrontend_config.source_file + ~f:SourceFile.from_abs_path clang_loc.Clang_ast_t.sl_file in Location.{line; col; file} let source_file_in_project source_file = @@ -30,7 +30,7 @@ let source_file_in_project source_file = file_in_project && not (file_should_be_skipped) let should_do_frontend_check trans_unit_ctx (loc_start, _) = - match loc_start.Clang_ast_t.sl_file with + match Option.map ~f:SourceFile.from_abs_path loc_start.Clang_ast_t.sl_file with | Some source_file -> SourceFile.equal source_file trans_unit_ctx.CFrontend_config.source_file || (source_file_in_project source_file && not Config.testing_mode) @@ -42,7 +42,7 @@ let should_do_frontend_check trans_unit_ctx (loc_start, _) = than the source file to avoid conflicts between different versions of the libraries. *) let should_translate trans_unit_ctx (loc_start, loc_end) decl_trans_context ~translate_when_used = let map_file_of pred loc = - match loc.Clang_ast_t.sl_file with + match Option.map ~f:SourceFile.from_abs_path loc.Clang_ast_t.sl_file with | Some f -> pred f | None -> false in diff --git a/infer/src/clang/cMethod_signature.ml b/infer/src/clang/cMethod_signature.ml index 380d4b366..31e107db9 100644 --- a/infer/src/clang/cMethod_signature.ml +++ b/infer/src/clang/cMethod_signature.ml @@ -101,5 +101,5 @@ let ms_to_string ms = IList.to_string (fun (s1, s2) -> (Mangled.to_string s1) ^ ", " ^ (CAst_utils.string_of_qual_type s2)) ms.args - ^ "->" ^ (CAst_utils.string_of_type_ptr ms.ret_type) ^ " " ^ + ^ "->" ^ (Clang_ast_extend.type_ptr_to_string ms.ret_type) ^ " " ^ Clang_ast_j.string_of_source_range ms.loc diff --git a/infer/src/clang/cMethod_trans.ml b/infer/src/clang/cMethod_trans.ml index 4d4221dba..037882ff3 100644 --- a/infer/src/clang/cMethod_trans.ml +++ b/infer/src/clang/cMethod_trans.ml @@ -52,10 +52,10 @@ let get_class_param function_method_decl_info = if (is_instance_method function_method_decl_info) then match function_method_decl_info with | Cpp_Meth_decl_info (_, _, class_decl_ptr, _) -> - let class_type = Ast_expressions.create_qual_type (`DeclPtr class_decl_ptr) in + let class_type = Ast_expressions.create_qual_type (Clang_ast_extend.DeclPtr class_decl_ptr) in [(Mangled.from_string CFrontend_config.this, class_type)] | ObjC_Meth_decl_info (_, class_decl_ptr) -> - let class_type = Ast_expressions.create_qual_type (`DeclPtr class_decl_ptr) in + let class_type = Ast_expressions.create_qual_type (Clang_ast_extend.DeclPtr class_decl_ptr) in [(Mangled.from_string CFrontend_config.self, class_type)] | _ -> [] else [] diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index bb2d7b185..ab69d2292 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -2675,7 +2675,7 @@ struct let child_stmt_info = { (Ast_expressions.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_type_ptr = Ast_expressions.create_pointer_type (`DeclPtr class_ptr) in + let class_type_ptr = Ast_expressions.create_pointer_type (Clang_ast_extend.DeclPtr class_ptr) in let this_res_trans = this_expr_trans trans_state' sil_loc class_type_ptr in let var_res_trans = match ctor_init.Clang_ast_t.xci_subject with | `Delegating _ | `BaseClass _ -> diff --git a/infer/src/clang/cType_to_sil_type.ml b/infer/src/clang/cType_to_sil_type.ml index 324ad24b9..78fa56ced 100644 --- a/infer/src/clang/cType_to_sil_type.ml +++ b/infer/src/clang/cType_to_sil_type.ml @@ -127,8 +127,8 @@ and sil_type_of_c_type translate_decl tenv c_type = and decl_ptr_to_sil_type translate_decl tenv decl_ptr = let open Clang_ast_t in - let typ = `DeclPtr decl_ptr in - try Clang_ast_types.TypePointerMap.find typ !CFrontend_config.sil_types_map + let typ = Clang_ast_extend.DeclPtr decl_ptr in + try Clang_ast_extend.TypePointerMap.find typ !CFrontend_config.sil_types_map with Not_found -> match CAst_utils.get_decl decl_ptr with | Some (CXXRecordDecl _ as d) @@ -151,7 +151,7 @@ and decl_ptr_to_sil_type translate_decl tenv decl_ptr = and clang_type_ptr_to_sil_type translate_decl tenv type_ptr = try - Clang_ast_types.TypePointerMap.find type_ptr !CFrontend_config.sil_types_map + Clang_ast_extend.TypePointerMap.find type_ptr !CFrontend_config.sil_types_map with Not_found -> (match CAst_utils.get_type type_ptr with | Some c_type -> @@ -162,24 +162,24 @@ and clang_type_ptr_to_sil_type translate_decl tenv type_ptr = and prebuilt_type_to_sil_type type_ptr = try - Clang_ast_types.TypePointerMap.find type_ptr !CFrontend_config.sil_types_map + Clang_ast_extend.TypePointerMap.find type_ptr !CFrontend_config.sil_types_map with Not_found -> Logging.out "Prebuilt type %s not found\n" - (Clang_ast_types.type_ptr_to_string type_ptr); + (Clang_ast_extend.type_ptr_to_string type_ptr); assert false and type_ptr_to_sil_type translate_decl tenv type_ptr = match type_ptr with - | `TPtr _ -> clang_type_ptr_to_sil_type translate_decl tenv type_ptr - | `Prebuilt _ -> prebuilt_type_to_sil_type type_ptr - | `PointerOf typ -> + | Clang_ast_types.TypePtr.Ptr _ -> clang_type_ptr_to_sil_type translate_decl tenv type_ptr + | Clang_ast_extend.Prebuilt _ -> prebuilt_type_to_sil_type type_ptr + | Clang_ast_extend.PointerOf typ -> let sil_typ = type_ptr_to_sil_type translate_decl tenv typ in Typ.Tptr (sil_typ, Typ.Pk_pointer) - | `ReferenceOf typ -> + | Clang_ast_extend.ReferenceOf typ -> let sil_typ = type_ptr_to_sil_type translate_decl tenv typ in Typ.Tptr (sil_typ, Typ.Pk_reference) - | `ClassType typename -> + | Clang_ast_extend.ClassType typename -> Typ.Tstruct typename - | `StructType typename -> Typ.Tstruct typename - | `DeclPtr ptr -> decl_ptr_to_sil_type translate_decl tenv ptr - | `ErrorType -> Typ.Tvoid + | Clang_ast_extend.DeclPtr ptr -> decl_ptr_to_sil_type translate_decl tenv ptr + | Clang_ast_extend.ErrorType -> Typ.Tvoid + | _ -> raise (invalid_arg "unknown variant for type_ptr") diff --git a/infer/src/clang/clang_ast_extend.ml b/infer/src/clang/clang_ast_extend.ml new file mode 100644 index 000000000..db9ea08c9 --- /dev/null +++ b/infer/src/clang/clang_ast_extend.ml @@ -0,0 +1,63 @@ +(* + * Copyright (c) 2015 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + *) + +open! IStd + +(* This module adds more variants to some types in AST *) +(* The implementation extends default one from *) +(* facebook-clang-plugins repository *) + + +(* Type pointers *) +type Clang_ast_types.TypePtr.t += + | Prebuilt of int + | PointerOf of Clang_ast_types.TypePtr.t + | ReferenceOf of Clang_ast_types.TypePtr.t + | ClassType of Typ.Name.t + | DeclPtr of int + | ErrorType + +module TypePointerOrd = struct + type t = Clang_ast_types.TypePtr.t + let rec compare a1 a2 = match a1, a2 with + | _ when phys_equal a1 a2 -> 0 + | Clang_ast_types.TypePtr.Ptr a, Clang_ast_types.TypePtr.Ptr b -> Int.compare a b + | Clang_ast_types.TypePtr.Ptr _, _ -> 1 + | _, Clang_ast_types.TypePtr.Ptr _ -> -1 + | Prebuilt a, Prebuilt b -> Int.compare a b + | Prebuilt _, _ -> 1 + | _, Prebuilt _ -> -1 + | PointerOf a, PointerOf b -> compare a b + | PointerOf _, _ -> 1 + | _, PointerOf _ -> -1 + | ReferenceOf a, ReferenceOf b -> compare a b + | ReferenceOf _, _ -> 1 + | _, ReferenceOf _ -> -1 + | ClassType a, ClassType b -> Typ.Name.compare a b + | ClassType _, _ -> 1 + | _, ClassType _ -> -1 + | DeclPtr a, DeclPtr b -> Int.compare a b + | DeclPtr _, _ -> 1 + | _, DeclPtr _ -> -1 + | ErrorType, ErrorType -> 0 + | _ -> raise (invalid_arg ("unexpected type_ptr variants: ")) +end + +module TypePointerMap = Caml.Map.Make(TypePointerOrd) + + +let rec type_ptr_to_string = function + | Clang_ast_types.TypePtr.Ptr raw -> "clang_ptr_" ^ (string_of_int raw) + | Prebuilt raw -> "prebuilt_" ^ (string_of_int raw) + | PointerOf typ -> "pointer_of_" ^ type_ptr_to_string typ + | ReferenceOf typ -> "reference_of_" ^ type_ptr_to_string typ + | ClassType name -> "class_name_" ^ Typ.Name.name name + | DeclPtr raw -> "decl_ptr_" ^ (string_of_int raw) + | ErrorType -> "error_type" + | _ -> "unknown" diff --git a/infer/src/clang/clang_ast_types.ml b/infer/src/clang/clang_ast_types.ml deleted file mode 100644 index d5ca36e7c..000000000 --- a/infer/src/clang/clang_ast_types.ml +++ /dev/null @@ -1,65 +0,0 @@ -(* - * Copyright (c) 2015 - present Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - *) - -open! IStd - -(* This module adds more structure to some fields in AST *) -(* The implementation is replacement of default one from *) -(* facebook-clang-plugins repository *) - - -(* Type pointers *) - -exception Not_Clang_Pointer - -type t_ptr = [ - | `TPtr of int - | `Prebuilt of int - | `PointerOf of t_ptr - | `ReferenceOf of t_ptr - | `ClassType of Typ.Name.t - | `StructType of Typ.Name.t - | `DeclPtr of int - | `ErrorType -] [@@deriving compare] - -module TypePointerOrd = struct - type t = t_ptr [@@deriving compare] -end - -module TypePointerMap = Caml.Map.Make(TypePointerOrd) - -let rec type_ptr_to_string type_ptr = match type_ptr with - | `TPtr raw -> "clang_ptr_" ^ (string_of_int raw) - | `Prebuilt raw -> "prebuilt_" ^ (string_of_int raw) - | `PointerOf typ -> "pointer_of_" ^ type_ptr_to_string typ - | `ReferenceOf typ -> "reference_of_" ^ type_ptr_to_string typ - | `ClassType name -> "class_name_" ^ Typ.Name.name name - | `StructType name -> "struct_name_" ^ Typ.Name.name name - | `DeclPtr raw -> "decl_ptr_" ^ (string_of_int raw) - | `ErrorType -> "error_type" - -let type_ptr_to_clang_pointer type_ptr = match type_ptr with - | `TPtr raw -> raw - | _ -> raise Not_Clang_Pointer - -let pointer_to_type_ptr raw = `TPtr raw - -let type_ptr_to_pointer type_ptr = match type_ptr with - | `TPtr raw -> raw - | _ -> 0 (* invalid pointer *) - - -(* Source files *) - -type src_file = SourceFile.t - -let source_file_of_string = SourceFile.from_abs_path - -let string_of_source_file = SourceFile.to_abs_path diff --git a/infer/src/clang/objcCategory_decl.ml b/infer/src/clang/objcCategory_decl.ml index d0c705d70..f74c91ef7 100644 --- a/infer/src/clang/objcCategory_decl.ml +++ b/infer/src/clang/objcCategory_decl.ml @@ -68,7 +68,7 @@ let get_base_class_name_from_category decl = let process_category type_ptr_to_sil_type tenv class_name decl_info decl_list = let decl_fields = CField_decl.get_fields type_ptr_to_sil_type tenv decl_list in let class_tn_name = Typ.Name.Objc.from_qual_name class_name in - let decl_key = `DeclPtr decl_info.Clang_ast_t.di_pointer in + let decl_key = Clang_ast_extend.DeclPtr decl_info.Clang_ast_t.di_pointer in CAst_utils.update_sil_types_map decl_key (Typ.Tstruct class_tn_name); (match Tenv.lookup tenv class_tn_name with | Some ({ fields } as struct_typ) -> diff --git a/infer/src/clang/objcInterface_decl.ml b/infer/src/clang/objcInterface_decl.ml index f1f6e0c86..c75e8ea30 100644 --- a/infer/src/clang/objcInterface_decl.ml +++ b/infer/src/clang/objcInterface_decl.ml @@ -78,7 +78,7 @@ let add_class_to_tenv type_ptr_to_sil_type tenv decl_info name_info decl_list oc let class_name = CAst_utils.get_qualified_name name_info in Logging.out_debug "ADDING: ObjCInterfaceDecl for '%a'\n" QualifiedCppName.pp class_name; let interface_name = Typ.Name.Objc.from_qual_name class_name in - let decl_key = `DeclPtr decl_info.Clang_ast_t.di_pointer in + let decl_key = Clang_ast_extend.DeclPtr decl_info.Clang_ast_t.di_pointer in CAst_utils.update_sil_types_map decl_key (Typ.Tstruct interface_name); let decl_supers, decl_fields = create_supers_fields type_ptr_to_sil_type tenv decl_list @@ -142,7 +142,7 @@ let interface_impl_declaration type_ptr_to_sil_type tenv decl = let fields = CField_decl.get_fields type_ptr_to_sil_type tenv decl_list in CField_decl.add_missing_fields tenv class_name fields; let class_tn_name = Typ.Name.Objc.from_qual_name class_name in - let decl_key = `DeclPtr decl_info.Clang_ast_t.di_pointer in + let decl_key = Clang_ast_extend.DeclPtr decl_info.Clang_ast_t.di_pointer in let class_typ = Typ.Tstruct class_tn_name in CAst_utils.update_sil_types_map decl_key class_typ; class_typ diff --git a/infer/src/clang/objcProtocol_decl.ml b/infer/src/clang/objcProtocol_decl.ml index 59f24cc68..7f9c9c07a 100644 --- a/infer/src/clang/objcProtocol_decl.ml +++ b/infer/src/clang/objcProtocol_decl.ml @@ -26,7 +26,7 @@ let protocol_decl type_ptr_to_sil_type tenv decl = (* It may turn out that we need a more specific treatment for protocols*) Logging.out_debug "ADDING: ObjCProtocolDecl for '%a'\n" QualifiedCppName.pp name; let protocol_name = Typ.Name.Objc.protocol_from_qual_name name in - let decl_key = `DeclPtr decl_info.Clang_ast_t.di_pointer in + let decl_key = Clang_ast_extend.DeclPtr decl_info.Clang_ast_t.di_pointer in CAst_utils.update_sil_types_map decl_key (Typ.Tstruct protocol_name); ignore( Tenv.mk_struct tenv ~methods:[] protocol_name ); add_protocol_super type_ptr_to_sil_type tenv obj_c_protocol_decl_info;