@@ -60,25 +60,31 @@ let load_and_preprocess ~all_cppflags filename =
6060 (* TODO: extract o_file *)
6161 let command = reroot command in
6262 let preprocess_command = Str. replace_first command_program_regexp (" \\ 1 " ^ String. join " " (List. map Filename. quote all_cppflags) ^ " -E" ) command in
63- let preprocess_command = Str. replace_first command_o_regexp (" -o " ^ preprocessed_file) preprocess_command in
6463 if preprocess_command = command then (* easier way to check if match was found (and replaced) *)
65- failwith " CompilationDatabase.preprocess: no -o argument found for " ^ file
64+ failwith ( " CompilationDatabase.preprocess: no program found for " ^ file)
6665 else
67- preprocess_command
66+ let preprocess_command_o = Str. replace_first command_o_regexp (" -o " ^ preprocessed_file) preprocess_command in
67+ if preprocess_command_o = preprocess_command then (* easier way to check if match was found (and replaced) *)
68+ preprocess_command ^ " -o " ^ preprocessed_file
69+ else
70+ preprocess_command_o
6871 | None , Some arguments ->
69- let arguments = List. map reroot arguments in
70- begin match List. findi (fun i e -> e = " -o" ) arguments with
72+ let arguments_program, arguments =
73+ match List. map reroot arguments with
74+ | arguments_program :: arguments -> arguments_program, arguments
75+ | _ -> failwith (" CompilationDatabase.preprocess: no program found for " ^ file)
76+ in
77+ let preprocess_arguments =
78+ match List. findi (fun i e -> e = " -o" ) arguments with
7179 | (o_i , _ ) ->
7280 begin match List. split_at o_i arguments with
73- | (arguments_program :: arguments_init , _ :: o_file :: arguments_tl ) ->
74- let preprocess_arguments = all_cppflags @ " -E" :: arguments_init @ " -o" :: preprocessed_file :: arguments_tl in
75- Filename. quote_command arguments_program preprocess_arguments
76- | _ ->
77- failwith " CompilationDatabase.preprocess: no -o argument value found for " ^ file
81+ | (arguments_init , _ :: o_file :: arguments_tl ) -> arguments_init @ " -o" :: preprocessed_file :: arguments_tl
82+ | _ -> failwith (" CompilationDatabase.preprocess: no argument found for -o option for " ^ file)
7883 end
79- | exception Not_found ->
80- failwith " CompilationDatabase.preprocess: no -o argument found for " ^ file
81- end
84+ | exception Not_found -> arguments @ " -o" :: [preprocessed_file]
85+ in
86+ let preprocess_arguments = all_cppflags @ " -E" :: preprocess_arguments in
87+ Filename. quote_command arguments_program preprocess_arguments
8288 | Some _ , Some _ ->
8389 failwith " CompilationDatabase.preprocess: both command and arguments specified for " ^ file
8490 | None , None ->
@@ -89,6 +95,6 @@ let load_and_preprocess ~all_cppflags filename =
8995 Printf. printf " Preprocessing %s\n to %s\n using %s\n in %s\n " file preprocessed_file preprocess_command cwd;
9096 system ~cwd preprocess_command; (* command/arguments might have paths relative to directory *)
9197 Some preprocessed_file
92- in
98+ in
9399 parse_file filename
94100 |> BatList. filter_map preprocess
0 commit comments