From 671d9fa291d2c001809d850fa6019666b06d7060 Mon Sep 17 00:00:00 2001 From: Martin Trojer Date: Mon, 2 Nov 2020 00:33:13 -0800 Subject: [PATCH] Add --clang-yojson-file flag Summary: : New flag (equivalent to --clang-biniou-file) to pass in AST to frontend. Passing in json files has the big advantage of emitting line numbers on frontend AST errors. Reviewed By: jvillard Differential Revision: D23814358 fbshipit-source-id: 0ad0452ff --- infer/man/man1/infer-capture.txt | 9 ++++++++- infer/man/man1/infer-full.txt | 13 +++++++++++-- infer/man/man1/infer.txt | 10 ++++++++-- infer/src/base/Config.ml | 24 ++++++++++++++++++++++-- infer/src/base/Config.mli | 2 +- infer/src/clang/Capture.ml | 24 ++++++++++++++---------- 6 files changed, 64 insertions(+), 18 deletions(-) diff --git a/infer/man/man1/infer-capture.txt b/infer/man/man1/infer-capture.txt index afbbf31f3..be0ad46c3 100644 --- a/infer/man/man1/infer-capture.txt +++ b/infer/man/man1/infer-capture.txt @@ -223,7 +223,9 @@ CLANG LINTERS OPTIONS --no-linters-validate-syntax-only) CLANG OPTIONS --clang-biniou-file file - Specify a file containing the AST of the program, in biniou format + Specify a file containing the AST of the program, in biniou + format. Please note you still need to provide a compilation + command. --clang-blacklisted-flags +string Clang flags to filter out @@ -236,6 +238,11 @@ CLANG OPTIONS arrays) is not done element by element but using a builtin function that each analysis has to model. + --clang-yojson-file file + Specify a file containing the AST of the program, in yojson + format. Please note you still need to provide a compilation + command. + --compilation-database +path File that contain compilation commands (can be specified multiple times) diff --git a/infer/man/man1/infer-full.txt b/infer/man/man1/infer-full.txt index 3b36f46a4..5553eead1 100644 --- a/infer/man/man1/infer-full.txt +++ b/infer/man/man1/infer-full.txt @@ -220,8 +220,9 @@ OPTIONS relative to project root or be absolute See also infer-analyze(1). --clang-biniou-file file - Specify a file containing the AST of the program, in biniou format - See also infer-capture(1). + Specify a file containing the AST of the program, in biniou + format. Please note you still need to provide a compilation + command. See also infer-capture(1). --clang-blacklisted-flags +string Clang flags to filter out See also infer-capture(1). @@ -234,6 +235,11 @@ OPTIONS arrays) is not done element by element but using a builtin function that each analysis has to model. See also infer-analyze(1) and infer-capture(1). + --clang-yojson-file file + Specify a file containing the AST of the program, in yojson + format. Please note you still need to provide a compilation + command. See also infer-capture(1). + --compilation-database +path File that contain compilation commands (can be specified multiple times) See also infer-capture(1). @@ -1371,6 +1377,9 @@ INTERNAL OPTIONS --clang-libcxx-include-to-override-regex-reset Cancel the effect of --clang-libcxx-include-to-override-regex. + --clang-yojson-file-reset + Cancel the effect of --clang-yojson-file. + --classpath string Specify the Java classpath diff --git a/infer/man/man1/infer.txt b/infer/man/man1/infer.txt index 415383180..d18358298 100644 --- a/infer/man/man1/infer.txt +++ b/infer/man/man1/infer.txt @@ -220,8 +220,9 @@ OPTIONS relative to project root or be absolute See also infer-analyze(1). --clang-biniou-file file - Specify a file containing the AST of the program, in biniou format - See also infer-capture(1). + Specify a file containing the AST of the program, in biniou + format. Please note you still need to provide a compilation + command. See also infer-capture(1). --clang-blacklisted-flags +string Clang flags to filter out See also infer-capture(1). @@ -234,6 +235,11 @@ OPTIONS arrays) is not done element by element but using a builtin function that each analysis has to model. See also infer-analyze(1) and infer-capture(1). + --clang-yojson-file file + Specify a file containing the AST of the program, in yojson + format. Please note you still need to provide a compilation + command. See also infer-capture(1). + --compilation-database +path File that contain compilation commands (can be specified multiple times) See also infer-capture(1). diff --git a/infer/src/base/Config.ml b/infer/src/base/Config.ml index 2a7b10f7c..94848e39a 100644 --- a/infer/src/base/Config.ml +++ b/infer/src/base/Config.ml @@ -824,7 +824,9 @@ and check_version = and clang_biniou_file = CLOpt.mk_path_opt ~long:"clang-biniou-file" ~in_help:InferCommand.[(Capture, manual_clang)] - ~meta:"file" "Specify a file containing the AST of the program, in biniou format" + ~meta:"file" + "Specify a file containing the AST of the program, in biniou format. Please note you still \ + need to provide a compilation command." and clang_compound_literal_init_limit = @@ -894,6 +896,14 @@ and clang_libcxx_include_to_override_regex = $(b,-I /path/to/infer/facebook-clang-plugins/clang/install/include/c++/v1)." +and clang_yojson_file = + CLOpt.mk_path_opt ~long:"clang-yojson-file" + ~in_help:InferCommand.[(Capture, manual_clang)] + ~meta:"file" + "Specify a file containing the AST of the program, in yojson format. Please note you still \ + need to provide a compilation command." + + and classpath = CLOpt.mk_string_opt ~long:"classpath" "Specify the Java classpath" and compilation_database = @@ -2737,7 +2747,17 @@ and check_version = !check_version and checkers = List.map !all_checkers ~f:(fun (checker, _, var) -> (checker, !var)) -and clang_biniou_file = !clang_biniou_file +and clang_ast_file = + match (!clang_biniou_file, !clang_yojson_file) with + | Some _, Some _ -> + L.die UserError "Please provide only one of --clang-biniou-file and --clang-yojson-file" + | Some b, _ -> + Some (`Biniou b) + | _, Some y -> + Some (`Yojson y) + | _ -> + None + and clang_compilation_dbs = !clang_compilation_dbs diff --git a/infer/src/base/Config.mli b/infer/src/base/Config.mli index 6ea8197e7..039640ca2 100644 --- a/infer/src/base/Config.mli +++ b/infer/src/base/Config.mli @@ -200,7 +200,7 @@ val changed_files_index : string option val check_version : string option -val clang_biniou_file : string option +val clang_ast_file : [`Biniou of string | `Yojson of string] option val clang_compound_literal_init_limit : int diff --git a/infer/src/clang/Capture.ml b/infer/src/clang/Capture.ml index 05e46052b..68277ab8b 100644 --- a/infer/src/clang/Capture.ml +++ b/infer/src/clang/Capture.ml @@ -14,9 +14,13 @@ let debug_mode = Config.debug_mode || Config.frontend_stats (** This function reads the json file in fname, validates it, and encodes in the AST data structure defined in Clang_ast_t. *) -let validate_decl_from_file fname = - Atdgen_runtime.Util.Biniou.from_file ~len:CFrontend_config.biniou_buffer_size - Clang_ast_b.read_decl fname +let validate_decl_from_file file = + match file with + | `Biniou fname -> + Atdgen_runtime.Util.Biniou.from_file ~len:CFrontend_config.biniou_buffer_size + Clang_ast_b.read_decl fname + | `Yojson fname -> + Atdgen_runtime.Util.Json.from_file Clang_ast_j.read_decl fname let validate_decl_from_channel chan = @@ -38,9 +42,9 @@ let run_clang_frontend ast_source = in let ast_decl = match ast_source with - | `File path -> - validate_decl_from_file path - | `Pipe chan -> + | `File file -> + validate_decl_from_file file + | `BiniouPipe chan -> validate_decl_from_channel chan in let trans_unit_ctx = @@ -76,9 +80,9 @@ let run_clang_frontend ast_source = in let pp_ast_filename fmt ast_source = match ast_source with - | `File path -> + | `File (`Biniou path) | `File (`Yojson path) -> Format.pp_print_string fmt path - | `Pipe _ -> + | `BiniouPipe _ -> Format.fprintf fmt "stdin of %a" SourceFile.pp trans_unit_ctx.CFrontend_config.source_file in ClangPointers.populate_all_tables ast_decl ; @@ -176,12 +180,12 @@ let cc1_capture clang_cmd = L.(debug Capture Quiet) "@\n Skip compilation and analysis of source file %s@\n@\n" source_path ; () ) else - match Config.clang_biniou_file with + match Config.clang_ast_file with | Some fname -> run_and_validate_clang_frontend (`File fname) | None -> run_plugin_and_frontend source_path - (fun chan_in -> run_and_validate_clang_frontend (`Pipe chan_in)) + (fun chan_in -> run_and_validate_clang_frontend (`BiniouPipe chan_in)) clang_cmd