Make source file baby parser fails silently

Summary:
The java source file parser should refuse to run on non
.java file. Also, as we expect some autogenerated source files to
break Java official syntax, we catch parsing error silently and
cancel location recording for them.

Reviewed By: jvillard

Differential Revision: D21089587

fbshipit-source-id: 35f1a1e28
master
David Pichardie 5 years ago committed by Facebook GitHub Bot
parent d30b0959a1
commit ccb2d23c5b

@ -372,30 +372,33 @@ and skip_comments action = parse
(** We scan source file [file] and record location of each class declaration *) (** We scan source file [file] and record location of each class declaration *)
let collect_class_location (program:JClasspath.program) (file:SourceFile.t) = let collect_class_location (program:JClasspath.program) (file:SourceFile.t) =
let cin = In_channel.create (SourceFile.to_abs_path file) in let path = SourceFile.to_abs_path file in
let stack = [] in if String.is_suffix path ~suffix:".java" then (
let record_location ~classname ~col ~line = let cin = In_channel.create path in
let loc : Location.t = { line; col; file } in let stack = [] in
let cn : JBasics.class_name = JBasics.make_cn classname in let record_location ~classname ~col ~line =
Logging.debug Capture Verbose "set_java_location %s with location %a@." let loc : Location.t = { line; col; file } in
(JBasics.cn_name cn) Location.pp_file_pos loc; let cn : JBasics.class_name = JBasics.make_cn classname in
JClasspath.set_java_location program cn loc in Logging.debug Capture Verbose "set_java_location %s with location %a@."
try ( (JBasics.cn_name cn) Location.pp_file_pos loc;
class_scan { record_location; stack; } (from_channel cin) ; JClasspath.set_java_location program cn loc in
In_channel.close cin ) try (
with class_scan { record_location; stack; } (from_channel cin) ;
| Failure s -> In_channel.close cin )
raise with
(Failure | Failure s ->
(Printf.sprintf "Error parsing source file %s\n%s" (SourceFile.to_abs_path file) s)) Logging.debug Capture Verbose "Error parsing source file %s\n%s"
| Missing_opening_bracket -> (SourceFile.to_abs_path file) s;
raise In_channel.close cin
(Failure (Printf.sprintf "Missing opening bracket error while parsing source file %s\n" | Missing_opening_bracket ->
(SourceFile.to_abs_path file))) Logging.debug Capture Verbose
| Missing_opening_parenthesis -> "Missing opening bracket error while parsing source file %s\n"
raise (SourceFile.to_abs_path file);
(Failure In_channel.close cin
(Printf.sprintf "Missing opening parenthesis error while parsing source file %s\n" | Missing_opening_parenthesis ->
(SourceFile.to_abs_path file))) Logging.debug Capture Verbose
"Missing opening parenthesis error while parsing source file %s\n"
(SourceFile.to_abs_path file);
In_channel.close cin
)
} }

Loading…
Cancel
Save