From 54c35bc5c64bf2adcf58b2d5435c39ba230d50e8 Mon Sep 17 00:00:00 2001 From: Nikos Gorogiannis Date: Tue, 3 Mar 2020 11:36:18 -0800 Subject: [PATCH] [java] improve function figuring out package in jclasspath Summary: This function uses uncompiled regexps plus it forgets to close the file when it finds a package declaration. Fix by using Core operations on strings. Reviewed By: artempyanykh Differential Revision: D20192957 fbshipit-source-id: 317caacea --- infer/src/java/jClasspath.ml | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/infer/src/java/jClasspath.ml b/infer/src/java/jClasspath.ml index 7c0ba4940..cbc5573ba 100644 --- a/infer/src/java/jClasspath.ml +++ b/infer/src/java/jClasspath.ml @@ -64,23 +64,19 @@ type t = string * file_entry String.Map.t * JBasics.ClassSet.t Only the case where the package is declared in a single line is supported *) let read_package_declaration source_file = let path = SourceFile.to_abs_path source_file in - let file_in = In_channel.create path in - let remove_trailing_semicolon = Str.replace_first (Str.regexp ";") "" in - let empty_package = "" in - let rec loop () = - match remove_trailing_semicolon (In_channel.input_line_exn file_in) with - | exception End_of_file -> - In_channel.close file_in ; empty_package - | line -> ( - match Str.split (Str.regexp "[ \t]+") line with - | [] -> - (loop [@tailcall]) () - | [hd; package] when String.equal hd "package" -> - package - | _ -> - (loop [@tailcall]) () ) + let process_line line = + String.strip line |> String.lsplit2 ~on:';' |> Option.map ~f:fst + |> Option.bind ~f:(String.chop_prefix ~prefix:"package") + |> Option.map ~f:String.strip + in + let rec loop file_in = + match In_channel.input_line file_in with + | None -> + None + | Some line -> ( + match process_line line with Some package -> Some package | None -> loop file_in ) in - loop () + Utils.with_file_in path ~f:loop |> Option.value ~default:"" let add_source_file path map =