[comp-db] simple refactor

Summary:
- Remove some unnecessary mutation in compilation database management.
- Move capture code to CaptureCompilationDatabase module.
- Freeze `Config.clang_compilation_dbs` and remove mutations outside `Config`.

Reviewed By: martintrojer

Differential Revision: D24445244

fbshipit-source-id: f97dda522
master
Nikos Gorogiannis 4 years ago committed by Facebook GitHub Bot
parent 2bdc6b892b
commit adcad19ee1

@ -2721,6 +2721,8 @@ and checkers = List.map !all_checkers ~f:(fun (checker, _, var) -> (checker, !va
and clang_biniou_file = !clang_biniou_file and clang_biniou_file = !clang_biniou_file
and clang_compilation_dbs = !clang_compilation_dbs
and clang_compound_literal_init_limit = !clang_compound_literal_init_limit and clang_compound_literal_init_limit = !clang_compound_literal_init_limit
and clang_extra_flags = !clang_extra_flags and clang_extra_flags = !clang_extra_flags

@ -648,4 +648,4 @@ val scuba_execution_id : Int64.t option
(** {2 Global variables with initial values specified by command-line options} *) (** {2 Global variables with initial values specified by command-line options} *)
val clang_compilation_dbs : [`Escaped of string | `Raw of string] list ref val clang_compilation_dbs : [`Escaped of string | `Raw of string] list

@ -157,3 +157,16 @@ let capture_files_in_database ~changed_files compilation_database =
fun source_file -> SourceFile.Set.mem source_file changed_files_set fun source_file -> SourceFile.Set.mem source_file changed_files_set
in in
run_compilation_database compilation_database filter_changed run_compilation_database compilation_database filter_changed
let capture ~changed_files ~db_files =
let root = Config.project_root in
let clang_compilation_dbs =
List.map db_files ~f:(function
| `Escaped fname ->
`Escaped (Utils.filename_to_absolute ~root fname)
| `Raw fname ->
`Raw (Utils.filename_to_absolute ~root fname) )
in
let compilation_database = CompilationDatabase.from_json_files clang_compilation_dbs in
capture_files_in_database ~changed_files compilation_database

@ -7,11 +7,6 @@
open! IStd open! IStd
val capture_files_in_database :
changed_files:SourceFile.Set.t option -> CompilationDatabase.t -> unit
(** Run the capture of the files for which we have compilation commands in the database and in
[changed_files], if specified. *)
val get_compilation_database_files_buck : val get_compilation_database_files_buck :
BuckMode.clang_compilation_db_deps -> prog:string -> args:string list -> [> `Raw of string] list BuckMode.clang_compilation_db_deps -> prog:string -> args:string list -> [> `Raw of string] list
(** Get the compilation database files that contain the compilation given by the buck command. It (** Get the compilation database files that contain the compilation given by the buck command. It
@ -22,3 +17,10 @@ val get_compilation_database_files_xcodebuild :
prog:string -> args:string list -> [> `Escaped of string] list prog:string -> args:string list -> [> `Escaped of string] list
(** Get the compilation database files that contain the compilation given by the xcodebuild command, (** Get the compilation database files that contain the compilation given by the xcodebuild command,
using xcpretty. *) using xcpretty. *)
val capture :
changed_files:SourceFile.Set.t option
-> db_files:[< `Escaped of string | `Raw of string] list
-> unit
(** Run the capture of the files for which we have compilation commands in [db_files] and
[changed_files], if specified. *)

@ -10,14 +10,12 @@ module L = Logging
type compilation_data = {directory: string; executable: string; escaped_arguments: string list} type compilation_data = {directory: string; executable: string; escaped_arguments: string list}
type t = compilation_data SourceFile.Map.t ref type t = compilation_data SourceFile.Map.t
let empty () = ref SourceFile.Map.empty let empty = SourceFile.Map.empty
let get_size database = SourceFile.Map.cardinal !database
let filter_compilation_data database ~f = let filter_compilation_data database ~f =
SourceFile.Map.filter (fun s _ -> f s) !database |> SourceFile.Map.bindings SourceFile.Map.filter (fun s _ -> f s) database |> SourceFile.Map.bindings
let parse_command_and_arguments command_and_arguments = let parse_command_and_arguments command_and_arguments =
@ -49,7 +47,7 @@ let decode_json_file (database : t) json_format =
let exit_format_error error = let exit_format_error error =
L.(die ExternalError) ("Json file doesn't have the expected format: " ^^ error) L.(die ExternalError) ("Json file doesn't have the expected format: " ^^ error)
in in
let parse_command json = let parse_command db json =
let directory = ref None in let directory = ref None in
let file = ref None in let file = ref None in
let command = ref None in let command = ref None in
@ -126,21 +124,20 @@ let decode_json_file (database : t) json_format =
let compilation_data = {directory; executable; escaped_arguments} in let compilation_data = {directory; executable; escaped_arguments} in
let abs_file = if Filename.is_relative file then directory ^/ file else file in let abs_file = if Filename.is_relative file then directory ^/ file else file in
let source_file = SourceFile.from_abs_path abs_file in let source_file = SourceFile.from_abs_path abs_file in
database := SourceFile.Map.add source_file compilation_data !database SourceFile.Map.add source_file compilation_data db
| _ -> | _ ->
exit_format_error "Compilation database entry is not an object: %s" exit_format_error "Compilation database entry is not an object: %s"
(Yojson.Basic.to_string json) (Yojson.Basic.to_string json)
in in
match Yojson.Basic.from_file json_path with match Yojson.Basic.from_file json_path with
| `List commands -> | `List commands ->
List.iter ~f:parse_command commands List.fold ~init:database ~f:parse_command commands
| _ as json -> | _ as json ->
exit_format_error "Compilation database is not a list of commands: %s" exit_format_error "Compilation database is not a list of commands: %s"
(Yojson.Basic.to_string json) (Yojson.Basic.to_string json)
let from_json_files db_json_files = let from_json_files db_json_files =
let db = empty () in let db = List.fold ~init:empty ~f:decode_json_file db_json_files in
List.iter ~f:(decode_json_file db) db_json_files ; L.(debug Capture Quiet) "created database with %d entries@\n" (SourceFile.Map.cardinal db) ;
L.(debug Capture Quiet) "created database with %d entries@\n" (get_size db) ;
db db

@ -104,18 +104,6 @@ let check_xcpretty () =
@." @."
let capture_with_compilation_database db_files =
let root = Config.project_root in
Config.clang_compilation_dbs :=
List.map db_files ~f:(function
| `Escaped fname ->
`Escaped (Utils.filename_to_absolute ~root fname)
| `Raw fname ->
`Raw (Utils.filename_to_absolute ~root fname) ) ;
let compilation_database = CompilationDatabase.from_json_files !Config.clang_compilation_dbs in
CaptureCompilationDatabase.capture_files_in_database compilation_database
let capture ~changed_files = function let capture ~changed_files = function
| Analyze -> | Analyze ->
() ()
@ -130,10 +118,10 @@ let capture ~changed_files = function
BuckGenrule.capture CombinedGenrule build_cmd BuckGenrule.capture CombinedGenrule build_cmd
| BuckCompilationDB {deps; prog; args} -> | BuckCompilationDB {deps; prog; args} ->
L.progress "Capturing using Buck's compilation database...@." ; L.progress "Capturing using Buck's compilation database...@." ;
let json_cdb = let db_files =
CaptureCompilationDatabase.get_compilation_database_files_buck deps ~prog ~args CaptureCompilationDatabase.get_compilation_database_files_buck deps ~prog ~args
in in
capture_with_compilation_database ~changed_files json_cdb CaptureCompilationDatabase.capture ~changed_files ~db_files
| BuckGenrule {prog} -> | BuckGenrule {prog} ->
L.progress "Capturing for Buck genrule compatibility...@." ; L.progress "Capturing for Buck genrule compatibility...@." ;
JMain.from_arguments prog JMain.from_arguments prog
@ -148,7 +136,7 @@ let capture ~changed_files = function
Clang.capture compiler ~prog ~args Clang.capture compiler ~prog ~args
| ClangCompilationDB {db_files} -> | ClangCompilationDB {db_files} ->
L.progress "Capturing using compilation database...@." ; L.progress "Capturing using compilation database...@." ;
capture_with_compilation_database ~changed_files db_files CaptureCompilationDatabase.capture ~changed_files ~db_files
| Gradle {prog; args} -> | Gradle {prog; args} ->
L.progress "Capturing in gradle mode...@." ; L.progress "Capturing in gradle mode...@." ;
Gradle.capture ~prog ~args Gradle.capture ~prog ~args
@ -167,10 +155,10 @@ let capture ~changed_files = function
| XcodeXcpretty {prog; args} -> | XcodeXcpretty {prog; args} ->
L.progress "Capturing using xcodebuild and xcpretty...@." ; L.progress "Capturing using xcodebuild and xcpretty...@." ;
check_xcpretty () ; check_xcpretty () ;
let json_cdb = let db_files =
CaptureCompilationDatabase.get_compilation_database_files_xcodebuild ~prog ~args CaptureCompilationDatabase.get_compilation_database_files_xcodebuild ~prog ~args
in in
capture_with_compilation_database ~changed_files json_cdb CaptureCompilationDatabase.capture ~changed_files ~db_files
(* shadowed for tracing *) (* shadowed for tracing *)
@ -363,9 +351,9 @@ let assert_supported_build_system build_system =
let mode_of_build_command build_cmd (buck_mode : BuckMode.t option) = let mode_of_build_command build_cmd (buck_mode : BuckMode.t option) =
match build_cmd with match build_cmd with
| [] -> | [] ->
if not (List.is_empty !Config.clang_compilation_dbs) then ( if not (List.is_empty Config.clang_compilation_dbs) then (
assert_supported_mode `Clang "clang compilation database" ; assert_supported_mode `Clang "clang compilation database" ;
ClangCompilationDB {db_files= !Config.clang_compilation_dbs} ) ClangCompilationDB {db_files= Config.clang_compilation_dbs} )
else Analyze else Analyze
| prog :: args -> ( | prog :: args -> (
let build_system = let build_system =

Loading…
Cancel
Save