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 *)
let collect_class_location (program:JClasspath.program) (file:SourceFile.t) =
let cin = In_channel.create (SourceFile.to_abs_path file) in
let stack = [] in
let record_location ~classname ~col ~line =
let loc : Location.t = { line; col; file } in
let cn : JBasics.class_name = JBasics.make_cn classname in
Logging.debug Capture Verbose "set_java_location %s with location %a@."
(JBasics.cn_name cn) Location.pp_file_pos loc;
JClasspath.set_java_location program cn loc in
try (
class_scan { record_location; stack; } (from_channel cin) ;
In_channel.close cin )
with
| Failure s ->
raise
(Failure
(Printf.sprintf "Error parsing source file %s\n%s" (SourceFile.to_abs_path file) s))
| Missing_opening_bracket ->
raise
(Failure (Printf.sprintf "Missing opening bracket error while parsing source file %s\n"
(SourceFile.to_abs_path file)))
| Missing_opening_parenthesis ->
raise
(Failure
(Printf.sprintf "Missing opening parenthesis error while parsing source file %s\n"
(SourceFile.to_abs_path file)))
let path = SourceFile.to_abs_path file in
if String.is_suffix path ~suffix:".java" then (
let cin = In_channel.create path in
let stack = [] in
let record_location ~classname ~col ~line =
let loc : Location.t = { line; col; file } in
let cn : JBasics.class_name = JBasics.make_cn classname in
Logging.debug Capture Verbose "set_java_location %s with location %a@."
(JBasics.cn_name cn) Location.pp_file_pos loc;
JClasspath.set_java_location program cn loc in
try (
class_scan { record_location; stack; } (from_channel cin) ;
In_channel.close cin )
with
| Failure s ->
Logging.debug Capture Verbose "Error parsing source file %s\n%s"
(SourceFile.to_abs_path file) s;
In_channel.close cin
| Missing_opening_bracket ->
Logging.debug Capture Verbose
"Missing opening bracket error while parsing source file %s\n"
(SourceFile.to_abs_path file);
In_channel.close cin
| Missing_opening_parenthesis ->
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