Simplify DB initialization

Summary:
Simplify DB initialization by removing some unit functions, since
Config values are now already initialized at module load time.

Reviewed By: akotulski

Differential Revision: D3522728

fbshipit-source-id: ac93d30
master
Josh Berdine 9 years ago committed by Facebook Github Bot
parent 1219ebf33a
commit 843d380d10

@ -143,12 +143,12 @@ let expand_type tenv typ =>
let tenv_serializer: Serialization.serializer t = Serialization.create_serializer Serialization.tenv_key; let tenv_serializer: Serialization.serializer t = Serialization.create_serializer Serialization.tenv_key;
let global_tenv: Lazy.t (option t) = let global_tenv: Lazy.t (option t) =
lazy (Serialization.from_file tenv_serializer (DB.global_tenv_fname ())); lazy (Serialization.from_file tenv_serializer DB.global_tenv_fname);
/** Load a type environment from a file */ /** Load a type environment from a file */
let load_from_file (filename: DB.filename) :option t => let load_from_file (filename: DB.filename) :option t =>
if (filename == DB.global_tenv_fname ()) { if (filename == DB.global_tenv_fname) {
Lazy.force global_tenv Lazy.force global_tenv
} else { } else {
Serialization.from_file tenv_serializer filename Serialization.from_file tenv_serializer filename

@ -121,25 +121,24 @@ let source_dir_get_internal_file source_dir extension =
let fname = source_dir_name ^ extension in let fname = source_dir_name ^ extension in
Filename.concat source_dir fname Filename.concat source_dir fname
let captured_dir () = let captured_dir =
Filename.concat Config.results_dir Config.captured_dir_name Filename.concat Config.results_dir Config.captured_dir_name
let sources_dir () = let sources_dir =
Filename.concat Config.results_dir Config.sources_dir_name Filename.concat Config.results_dir Config.sources_dir_name
(** get the source directory corresponding to a source file *) (** get the source directory corresponding to a source file *)
let source_dir_from_source_file source_file = let source_dir_from_source_file source_file =
Filename.concat (captured_dir ()) (source_file_encoding source_file) Filename.concat captured_dir (source_file_encoding source_file)
(** get the path to the copy of the source file to be stored in the results directory *) (** get the path to the copy of the source file to be stored in the results directory *)
let source_file_in_resdir source_file = let source_file_in_resdir source_file =
Filename.concat (sources_dir ()) (source_file_encoding source_file) Filename.concat sources_dir (source_file_encoding source_file)
(** Find the source directories in the results dir *) (** Find the source directories in the results dir *)
let find_source_dirs () = let find_source_dirs () =
let source_dirs = ref [] in let source_dirs = ref [] in
let capt_dir = captured_dir () in let files_in_results_dir = Array.to_list (Sys.readdir captured_dir) in
let files_in_results_dir = Array.to_list (Sys.readdir capt_dir) in
let add_cg_files_from_dir dir = let add_cg_files_from_dir dir =
let files = Array.to_list (Sys.readdir dir) in let files = Array.to_list (Sys.readdir dir) in
IList.iter (fun fname -> IList.iter (fun fname ->
@ -147,7 +146,7 @@ let find_source_dirs () =
if Filename.check_suffix path ".cg" then source_dirs := dir :: !source_dirs) if Filename.check_suffix path ".cg" then source_dirs := dir :: !source_dirs)
files in files in
IList.iter (fun fname -> IList.iter (fun fname ->
let dir = Filename.concat capt_dir fname in let dir = Filename.concat captured_dir fname in
if Sys.is_directory dir then add_cg_files_from_dir dir) if Sys.is_directory dir then add_cg_files_from_dir dir)
files_in_results_dir; files_in_results_dir;
IList.rev !source_dirs IList.rev !source_dirs
@ -303,12 +302,12 @@ module Results_dir = struct
filename_from_base base path filename_from_base base path
(** directory of spec files *) (** directory of spec files *)
let specs_dir () = path_to_filename Abs_root [Config.specs_dir_name] let specs_dir = path_to_filename Abs_root [Config.specs_dir_name]
(** initialize the results directory *) (** initialize the results directory *)
let init () = let init () =
create_dir Config.results_dir; create_dir Config.results_dir;
create_dir (specs_dir ()); create_dir specs_dir;
create_dir (path_to_filename Abs_root [Config.attributes_dir_name]); create_dir (path_to_filename Abs_root [Config.attributes_dir_name]);
create_dir (path_to_filename Abs_root [Config.sources_dir_name]); create_dir (path_to_filename Abs_root [Config.sources_dir_name]);
create_dir (path_to_filename Abs_root [Config.captured_dir_name]); create_dir (path_to_filename Abs_root [Config.captured_dir_name]);
@ -316,9 +315,8 @@ module Results_dir = struct
create_dir (path_to_filename Abs_source_dir []) create_dir (path_to_filename Abs_source_dir [])
let clean_specs_dir () = let clean_specs_dir () =
let specs_dir_path = specs_dir () in create_dir specs_dir; (* create dir just in case it doesn't exist to avoid errors *)
create_dir specs_dir_path; (* create dir just in case it doesn't exist to avoid errors *) let files_to_remove = Array.map (Filename.concat specs_dir) (Sys.readdir specs_dir) in
let files_to_remove = Array.map (Filename.concat specs_dir_path) (Sys.readdir specs_dir_path) in
Array.iter Sys.remove files_to_remove Array.iter Sys.remove files_to_remove
(** create a file at the given path, creating any missing directories *) (** create a file at the given path, creating any missing directories *)
@ -345,9 +343,9 @@ type origin =
| Spec_lib | Spec_lib
| Models | Models
let global_tenv_fname () = let global_tenv_fname =
let basename = Config.global_tenv_filename in let basename = Config.global_tenv_filename in
filename_concat (captured_dir ()) basename filename_concat captured_dir basename
let is_source_file path = let is_source_file path =
IList.exists IList.exists

@ -51,7 +51,7 @@ module Results_dir : sig
val path_to_filename : path_kind -> path -> filename val path_to_filename : path_kind -> path -> filename
(** directory of spec files *) (** directory of spec files *)
val specs_dir : unit -> filename val specs_dir : filename
(** Initialize the results directory *) (** Initialize the results directory *)
val init : unit -> unit val init : unit -> unit
@ -106,11 +106,6 @@ val source_file_to_string : source_file -> string
(** convert a string obtained by source_file_to_string to a source file *) (** convert a string obtained by source_file_to_string to a source file *)
val source_file_from_string : string -> source_file val source_file_from_string : string -> source_file
exception No_project_root
(** get the project root when it exists or raise No_project_root otherwise *)
val project_root : unit -> string
(** get the full path of a source file, raise No_project_root exception when used with a relative source file and no project root specified *) (** get the full path of a source file, raise No_project_root exception when used with a relative source file and no project root specified *)
val source_file_to_abs_path : source_file -> string val source_file_to_abs_path : source_file -> string
@ -125,7 +120,7 @@ type source_dir
val source_dir_compare : source_dir -> source_dir -> int val source_dir_compare : source_dir -> source_dir -> int
(** get the absolute path to the sources dir *) (** get the absolute path to the sources dir *)
val sources_dir : unit -> string val sources_dir : string
(** expose the source dir as a string *) (** expose the source dir as a string *)
val source_dir_to_string : source_dir -> string val source_dir_to_string : source_dir -> string
@ -140,7 +135,7 @@ val source_dir_from_source_file : source_file -> source_dir
val source_file_in_resdir : source_file -> filename val source_file_in_resdir : source_file -> filename
(** directory where the results of the capture phase are stored *) (** directory where the results of the capture phase are stored *)
val captured_dir : unit -> filename val captured_dir : filename
(** create the directory containing the file bane *) (** create the directory containing the file bane *)
val filename_create_dir : filename -> unit val filename_create_dir : filename -> unit
@ -165,7 +160,7 @@ val read_file_with_lock : string -> string -> bytes option
val update_file_with_lock : string -> string -> (bytes -> bytes) -> unit val update_file_with_lock : string -> string -> (bytes -> bytes) -> unit
(** get the path of the global type environment (only used in Java) *) (** get the path of the global type environment (only used in Java) *)
val global_tenv_fname : unit -> filename val global_tenv_fname : filename
(** Check if a path is a Java, C, C++ or Objectve C source file according to the file extention *) (** Check if a path is a Java, C, C++ or Objectve C source file according to the file extention *)
val is_source_file: string -> bool val is_source_file: string -> bool

@ -31,7 +31,7 @@ let tenv_filename file_base =
if Sys.file_exists (DB.filename_to_string per_source_tenv_filename) then if Sys.file_exists (DB.filename_to_string per_source_tenv_filename) then
per_source_tenv_filename per_source_tenv_filename
else else
DB.global_tenv_fname () DB.global_tenv_fname
(** create a new file_data *) (** create a new file_data *)
let new_file_data source nLOC cg_fname = let new_file_data source nLOC cg_fname =

@ -36,7 +36,7 @@ let load_specfiles () =
let all_filepaths = IList.map (fun fname -> Filename.concat dir fname) all_filenames in let all_filepaths = IList.map (fun fname -> Filename.concat dir fname) all_filenames in
IList.filter is_specs_file all_filepaths in IList.filter is_specs_file all_filepaths in
let specs_dirs = let specs_dirs =
let result_specs_dir = DB.filename_to_string (DB.Results_dir.specs_dir ()) in let result_specs_dir = DB.filename_to_string DB.Results_dir.specs_dir in
result_specs_dir :: Config.specs_library in result_specs_dir :: Config.specs_library in
IList.flatten (IList.map specs_files_in_dir specs_dirs) IList.flatten (IList.map specs_files_in_dir specs_dirs)

@ -195,8 +195,7 @@ let inhabit_call (procname, receiver) cfg env =
let create_dummy_harness_file harness_name = let create_dummy_harness_file harness_name =
let dummy_file_name = let dummy_file_name =
let dummy_file_dir = let dummy_file_dir =
let sources_dir = DB.sources_dir () in if Sys.file_exists DB.sources_dir then DB.sources_dir
if Sys.file_exists sources_dir then sources_dir
else Filename.get_temp_dir_name () in else Filename.get_temp_dir_name () in
let file_str = let file_str =
Procname.java_get_class_name Procname.java_get_class_name

@ -92,35 +92,31 @@ let capture_libs linereader program tenv =
(* load a stored global tenv if the file is found, and create a new one otherwise *) (* load a stored global tenv if the file is found, and create a new one otherwise *)
let load_tenv () = let load_tenv () =
let tenv_filename = DB.global_tenv_fname () in if DB.file_exists DB.global_tenv_fname then
let tenv =
if DB.file_exists tenv_filename then
begin begin
match Tenv.load_from_file tenv_filename with match Tenv.load_from_file DB.global_tenv_fname with
| None -> | None ->
Tenv.create () Tenv.create ()
| Some _ when Config.analyze_models -> | Some _ when Config.analyze_models ->
let error_msg = let error_msg =
"Unexpected tenv file " "Unexpected tenv file "
^ (DB.filename_to_string tenv_filename) ^ (DB.filename_to_string DB.global_tenv_fname)
^ " found while generating the models" in ^ " found while generating the models" in
failwith error_msg failwith error_msg
| Some tenv -> | Some tenv ->
tenv tenv
end end
else else
Tenv.create () in Tenv.create ()
tenv
(* Store to a file the type environment containing all the types required to perform the analysis *) (* Store to a file the type environment containing all the types required to perform the analysis *)
let save_tenv tenv = let save_tenv tenv =
if not Config.analyze_models then JTransType.add_models_types tenv; if not Config.analyze_models then JTransType.add_models_types tenv;
let tenv_filename = DB.global_tenv_fname () in
(* TODO: this prevents per compilation step incremental analysis at this stage *) (* TODO: this prevents per compilation step incremental analysis at this stage *)
if DB.file_exists tenv_filename then DB.file_remove tenv_filename; if DB.file_exists DB.global_tenv_fname then DB.file_remove DB.global_tenv_fname;
JUtils.log "writing new tenv %s@." (DB.filename_to_string tenv_filename); JUtils.log "writing new tenv %s@." (DB.filename_to_string DB.global_tenv_fname);
Tenv.store_to_file tenv_filename tenv Tenv.store_to_file DB.global_tenv_fname tenv
(* The program is loaded and translated *) (* The program is loaded and translated *)

@ -44,9 +44,8 @@ let store_icfg cg cfg =
end end
let store_tenv tenv = let store_tenv tenv =
let tenv_filename = DB.global_tenv_fname () in if DB.file_exists DB.global_tenv_fname then DB.file_remove DB.global_tenv_fname;
if DB.file_exists tenv_filename then DB.file_remove tenv_filename; Tenv.store_to_file DB.global_tenv_fname tenv
Tenv.store_to_file tenv_filename tenv
let () = let () =
begin match Config.source_file with begin match Config.source_file with

Loading…
Cancel
Save