[Config] support arbitrary named symbol lists

Reviewed By: jvillard

Differential Revision: D13903005

fbshipit-source-id: b1cf9b4e5
master
David Lively 6 years ago committed by Facebook Github Bot
parent 03d73c7941
commit c5890238f0

@ -68,6 +68,9 @@ OPTIONS
Activates: Enable --cost and disable all other checkers
(Conversely: --no-cost-only)
--custom-symbols json
Specify named lists of symbols available to rules (default: [])
--debug,-g
Activates: Debug mode (also sets --debug-level 2,
--developer-mode, --no-filtering, --print-buckets, --print-types,

@ -186,6 +186,10 @@ OPTIONS
compare against, assuming we are on the current version already.
See also infer-diff(1).
--custom-symbols json
Specify named lists of symbols available to rules (default: [])
See also infer-analyze(1).
--no-cxx
Deactivates: Analyze C++ methods (Conversely: --cxx)
See also infer-capture(1).

@ -186,6 +186,10 @@ OPTIONS
compare against, assuming we are on the current version already.
See also infer-diff(1).
--custom-symbols json
Specify named lists of symbols available to rules (default: [])
See also infer-analyze(1).
--no-cxx
Deactivates: Analyze C++ methods (Conversely: --cxx)
See also infer-capture(1).

@ -469,6 +469,25 @@ let exe_usage =
(Option.value_map ~default:"" ~f:(( ^ ) " ") exe_command_name)
let get_symbol_string json_obj =
match json_obj with
| `String sym_regexp_str ->
sym_regexp_str
| _ ->
L.(die UserError) "each --custom-symbols element should be list of symbol *strings*"
let get_symbols_regexp json_obj =
let sym_regexp_strs =
match json_obj with
| `List json_objs ->
List.map ~f:get_symbol_string json_objs
| _ ->
L.(die UserError) "each --custom-symbols element should be a *list* of strings"
in
Str.regexp ("\\(" ^ String.concat ~sep:"\\|" sym_regexp_strs ^ "\\)")
(** Command Line options *)
(* HOWTO define a new command line and config file option.
@ -968,6 +987,12 @@ and cxx =
"Analyze C++ methods"
and custom_symbols =
CLOpt.mk_json ~long:"custom-symbols"
~in_help:InferCommand.[(Analyze, manual_generic)]
"Specify named lists of symbols available to rules"
and ( bo_debug
, developer_mode
, debug
@ -2917,6 +2942,19 @@ and stats_report = !stats_report
and subtype_multirange = !subtype_multirange
and custom_symbols =
(* Convert symbol lists to regexps just once, here *)
match !custom_symbols with
| `Assoc sym_lists ->
List.Assoc.map ~f:get_symbols_regexp sym_lists
| `List [] ->
[]
| _ ->
L.(die UserError)
"--custom-symbols must be dictionary of symbol lists not %s"
(Yojson.Basic.to_string !custom_symbols)
and symops_per_iteration = !symops_per_iteration
and keep_going = !keep_going
@ -3015,3 +3053,11 @@ let java_package_is_external package =
| _ ->
List.exists external_java_packages ~f:(fun (prefix : string) ->
String.is_prefix package ~prefix )
let is_in_custom_symbols list_name symbol =
match List.Assoc.find ~equal:String.equal custom_symbols list_name with
| Some regexp ->
Str.string_match regexp symbol 0
| None ->
false

@ -702,4 +702,7 @@ val java_package_is_external : string -> bool
(** Check if a Java package is external to the repository *)
val quandaryBO_filtered_issues : IssueType.t list
(* List of issues that are enabled by QuandaryBO but should not be in the final report.json *)
(** List of issues that are enabled by QuandaryBO but should not be in the final report.json *)
val is_in_custom_symbols : string -> string -> bool
(** Does named symbol match any prefix in the named custom symbol list? *)

@ -1420,6 +1420,14 @@ let has_cxx_fully_qualified_name an qual_name_re =
ALVar.compare_str_with_alexp fully_qualified_name qual_name_re
let has_cxx_fully_qualified_name_in_custom_symbols an list_name =
match Ctl_parser_types.ast_node_cxx_fully_qualified_name an with
| "" ->
false
| fully_qualified_name ->
Config.is_in_custom_symbols list_name fully_qualified_name
let is_cxx_method_overriding an qual_name_re =
let rec overrides_named (decl_refs : Clang_ast_t.decl_ref list) (qnre : ALVar.alexp) =
List.exists

@ -492,6 +492,11 @@ val has_cxx_fully_qualified_name : Ctl_parser_types.ast_node -> ALVar.alexp -> b
* matching the given regexp
*)
val has_cxx_fully_qualified_name_in_custom_symbols : Ctl_parser_types.ast_node -> string -> bool
(** true iff node has C++ fully qualified name (w/class and namespace)
* matching a prefix on the given named custom symbol list
*)
val is_in_source_file : Ctl_parser_types.ast_node -> ALVar.alexp -> bool
(**
* True iff the source file path of the given node matches the given regexp or string.

@ -975,6 +975,12 @@ let rec eval_Atomic pred_name_ args an lcxt =
CPredicates.declaration_has_name an decl_name
| "has_cxx_fully_qualified_name", [qual_name_re], an ->
CPredicates.has_cxx_fully_qualified_name an qual_name_re
| "has_cxx_fully_qualified_name_in_custom_symbols", [list_name], an -> (
match list_name with
| ALVar.Const s ->
CPredicates.has_cxx_fully_qualified_name_in_custom_symbols an s
| _ ->
assert false )
| "declaration_ref_name", [decl_name], an ->
CPredicates.declaration_ref_name an decl_name
| "decl_unavailable_in_supported_ios_sdk", [], an ->

@ -5,8 +5,8 @@
TESTS_DIR = ../../..
CLANG_OPTIONS = -std=c++11 -c
INFER_OPTIONS = --no-capture --linters-only --cxx --linters-def-file linters_example.al --project-root $(TESTS_DIR)
CLANG_OPTIONS = -std=c++11 -c
INFER_OPTIONS = --no-capture --linters-only --cxx --linters-def-file linters_example.al --project-root $(TESTS_DIR) --custom-symbols '{ "test": [ "::AllowedSym" ] }'
INFERPRINT_OPTIONS = --issues-tests
SOURCES = \

@ -26,3 +26,5 @@ codetoanalyze/cpp/linters-for-test-only/test_overrides.cpp, Linters_dummy_method
codetoanalyze/cpp/linters-for-test-only/test_partial_spec_template.cpp, Foo::is_negative_impl_type-parameter-0-0,_false__check, 16, FIND_NODES_WITH_CXX_FULL_NAME, no_bucket, WARNING, []
codetoanalyze/cpp/linters-for-test-only/test_partial_spec_template.cpp, Linters_dummy_method, 9, FIND_NODES_WITH_CXX_FULL_NAME, no_bucket, WARNING, []
codetoanalyze/cpp/linters-for-test-only/test_partial_spec_template.cpp, Linters_dummy_method, 14, FIND_NODES_WITH_CXX_FULL_NAME, no_bucket, WARNING, []
codetoanalyze/cpp/linters-for-test-only/test_symbol_lists.cpp, foo, 13, FIND_LISTED_SYMBOLS, no_bucket, WARNING, []
codetoanalyze/cpp/linters-for-test-only/test_symbol_lists.cpp, foo, 14, FIND_LISTED_SYMBOLS, no_bucket, WARNING, []

@ -48,3 +48,8 @@ DEFINE-CHECKER FIND_CXX_METHODS_FROM_HEADER_FILE = {
HOLDS-IN-NODE CXXMethodDecl;
SET message = "found C++ method %name% in %source_file%";
};
DEFINE-CHECKER FIND_LISTED_SYMBOLS = {
SET report_when = NOT is_decl() AND has_cxx_fully_qualified_name_in_custom_symbols("test");
SET message = "found usage of %cxx_fully_qualified_name%";
};

@ -0,0 +1,16 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
extern void AllowedSymbol1();
extern void AllowedSymbol2();
extern void DisallowedSymbol();
void foo() {
AllowedSymbol1();
AllowedSymbol2();
DisallowedSymbol();
}
Loading…
Cancel
Save