|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 - present Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +open! Utils; |
| 11 | + |
| 12 | +let module F = Format; |
| 13 | + |
| 14 | +let module L = Logging; |
| 15 | + |
| 16 | + |
| 17 | +/** Module to manage the table of attributes. */ |
| 18 | +let serializer: Serialization.serializer ProcAttributes.t = Serialization.create_serializer Serialization.attributes_key; |
| 19 | + |
| 20 | +let attributes_filename pname => { |
| 21 | + let pname_file = Procname.to_filename pname; |
| 22 | + pname_file ^ ".attr" |
| 23 | +}; |
| 24 | + |
| 25 | + |
| 26 | +/** path to the .attr file for the given procedure in the current results directory */ |
| 27 | +let res_dir_attr_filename pname => { |
| 28 | + let attr_fname = attributes_filename pname; |
| 29 | + let bucket_dir = { |
| 30 | + let base = Filename.chop_extension attr_fname; |
| 31 | + let len = String.length base; |
| 32 | + if (len < 2) { |
| 33 | + Filename.current_dir_name |
| 34 | + } else { |
| 35 | + String.sub base (len - 2) 2 |
| 36 | + } |
| 37 | + }; |
| 38 | + let filename = |
| 39 | + DB.Results_dir.path_to_filename |
| 40 | + DB.Results_dir.Abs_root [Config.attributes_dir_name, bucket_dir, attr_fname]; |
| 41 | + DB.filename_create_dir filename; |
| 42 | + filename |
| 43 | +}; |
| 44 | + |
| 45 | +let store_attributes proc_attributes => { |
| 46 | + let proc_name = proc_attributes.ProcAttributes.proc_name; |
| 47 | + let attributes_file = res_dir_attr_filename proc_name; |
| 48 | + let should_write = |
| 49 | + /* only overwrite defined procedures */ |
| 50 | + proc_attributes.ProcAttributes.is_defined || not (DB.file_exists attributes_file); |
| 51 | + if should_write { |
| 52 | + Serialization.to_file serializer attributes_file proc_attributes |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +let load_attributes proc_name => { |
| 57 | + let attributes_file = res_dir_attr_filename proc_name; |
| 58 | + Serialization.from_file serializer attributes_file |
| 59 | +}; |
| 60 | + |
| 61 | + |
| 62 | +/** Given a procdesure name, find the file where it is defined and */ |
| 63 | +/** its corresponding type environment */ |
| 64 | +let find_tenv_from_class_of_proc procname => |
| 65 | + switch (load_attributes procname) { |
| 66 | + | None => None |
| 67 | + | Some attrs => |
| 68 | + let source_file = attrs.ProcAttributes.loc.Location.file; |
| 69 | + let source_dir = DB.source_dir_from_source_file source_file; |
| 70 | + let tenv_fname = DB.source_dir_get_internal_file source_dir ".tenv"; |
| 71 | + Tenv.load_from_file tenv_fname |
| 72 | + }; |
| 73 | + |
| 74 | + |
| 75 | +/** Given an ObjC class c, extract the type from the tenv where the class was */ |
| 76 | +/** defined. We do this by adding a method that is unique to each class, and then */ |
| 77 | +/** finding the tenv that corresponds to the class definition. */ |
| 78 | +let get_correct_type_from_objc_class_name c => { |
| 79 | + let class_method = Procname.get_default_objc_class_method (Mangled.to_string c); |
| 80 | + switch (find_tenv_from_class_of_proc class_method) { |
| 81 | + | None => None |
| 82 | + | Some tenv => |
| 83 | + let type_name = Typename.TN_csu (Csu.Class Csu.Objc) c; |
| 84 | + Option.map (fun st => Sil.Tstruct st) (Tenv.lookup tenv type_name) |
| 85 | + } |
| 86 | +}; |
0 commit comments