From e0ce8c4392cfc32ac0f2339f41a1c7648261bc18 Mon Sep 17 00:00:00 2001 From: David Lively Date: Thu, 25 Apr 2019 13:09:09 -0700 Subject: [PATCH] Add --annotation-reachability-cxx-sources override option Reviewed By: jeremydubreil Differential Revision: D15063925 fbshipit-source-id: cbee2ef7d --- infer/man/man1/infer-analyze.txt | 4 ++++ infer/man/man1/infer-full.txt | 4 ++++ infer/man/man1/infer.txt | 4 ++++ infer/src/base/Config.ml | 8 ++++++++ infer/src/base/Config.mli | 2 ++ infer/src/checkers/annotationReachability.ml | 19 +++++++++++++++---- .../sources-override/Makefile | 18 ++++++++++++++++++ .../sources-override/issues.exp | 2 ++ 8 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 infer/tests/codetoanalyze/cpp/annotation-reachability/sources-override/Makefile create mode 100644 infer/tests/codetoanalyze/cpp/annotation-reachability/sources-override/issues.exp diff --git a/infer/man/man1/infer-analyze.txt b/infer/man/man1/infer-analyze.txt index c6ef40ab1..de37bc671 100644 --- a/infer/man/man1/infer-analyze.txt +++ b/infer/man/man1/infer-analyze.txt @@ -352,6 +352,10 @@ CLANG OPTIONS go through a symbol starting with "Trusted::". (default: []) + --annotation-reachability-cxx-sources json + Override sources in all cxx annotation reachability specs with the + given sources spec (default: []) + --cxx-scope-guards json Specify scope guard classes that can be read only by destructors without being reported as dead stores. (default: []) diff --git a/infer/man/man1/infer-full.txt b/infer/man/man1/infer-full.txt index 0b4df6a86..1e5cd3827 100644 --- a/infer/man/man1/infer-full.txt +++ b/infer/man/man1/infer-full.txt @@ -96,6 +96,10 @@ OPTIONS (default: []) See also infer-analyze(1). + --annotation-reachability-cxx-sources json + Override sources in all cxx annotation reachability specs with the + given sources spec (default: []) See also infer-analyze(1). + --annotation-reachability-only Activates: Enable --annotation-reachability and disable all other checkers (Conversely: --no-annotation-reachability-only) diff --git a/infer/man/man1/infer.txt b/infer/man/man1/infer.txt index 31b6b29db..360cdc398 100644 --- a/infer/man/man1/infer.txt +++ b/infer/man/man1/infer.txt @@ -96,6 +96,10 @@ OPTIONS (default: []) See also infer-analyze(1). + --annotation-reachability-cxx-sources json + Override sources in all cxx annotation reachability specs with the + given sources spec (default: []) See also infer-analyze(1). + --annotation-reachability-only Activates: Enable --annotation-reachability and disable all other checkers (Conversely: --no-annotation-reachability-only) diff --git a/infer/src/base/Config.ml b/infer/src/base/Config.ml index 790c9eafa..a61f6749a 100644 --- a/infer/src/base/Config.ml +++ b/infer/src/base/Config.ml @@ -769,6 +769,12 @@ This will cause us to create a new ISOLATED_REACHING_CONNECT issue for every fun |} +and annotation_reachability_cxx_sources = + CLOpt.mk_json ~long:"annotation-reachability-cxx-sources" + ~in_help:InferCommand.[(Analyze, manual_clang)] + {|Override sources in all cxx annotation reachability specs with the given sources spec|} + + and annotation_reachability_custom_pairs = CLOpt.mk_json ~long:"annotation-reachability-custom-pairs" ~in_help:InferCommand.[(Analyze, manual_java)] @@ -2552,6 +2558,8 @@ and annotation_reachability = !annotation_reachability and annotation_reachability_cxx = !annotation_reachability_cxx +and annotation_reachability_cxx_sources = !annotation_reachability_cxx_sources + and annotation_reachability_custom_pairs = !annotation_reachability_custom_pairs and append_buck_flavors = !append_buck_flavors diff --git a/infer/src/base/Config.mli b/infer/src/base/Config.mli index 6818f0468..d699e0729 100644 --- a/infer/src/base/Config.mli +++ b/infer/src/base/Config.mli @@ -227,6 +227,8 @@ val annotation_reachability : bool val annotation_reachability_cxx : Yojson.Basic.json +val annotation_reachability_cxx_sources : Yojson.Basic.json + val annotation_reachability_custom_pairs : Yojson.Basic.json val anon_args : string list diff --git a/infer/src/checkers/annotationReachability.ml b/infer/src/checkers/annotationReachability.ml index 91e9d4a01..ff5c7e80b 100644 --- a/infer/src/checkers/annotationReachability.ml +++ b/infer/src/checkers/annotationReachability.ml @@ -287,6 +287,8 @@ module CxxAnnotationSpecs = struct let option_name = "--annotation-reachability-cxx" + let src_option_name = "--annotation-reachability-cxx-sources" + let cxx_string_of_pname pname = let chop_prefix s = String.chop_prefix s ~prefix:Config.clang_inner_destructor_prefix |> Option.value ~default:s @@ -299,7 +301,7 @@ module CxxAnnotationSpecs = struct ^ "()" - let spec_from_config spec_name spec_cfg = + let spec_from_config spec_name spec_cfg source_overrides = let src = option_name ^ " -> " ^ spec_name in let make_pname_pred entry ~src : Typ.Procname.t -> bool = let symbols = U.yojson_lookup entry "symbols" ~src ~f:U.string_list_of_yojson ~default:[] in @@ -316,8 +318,12 @@ module CxxAnnotationSpecs = struct | _, _ -> fun pname -> sym_pred pname || path_pred pname in - let sources = U.yojson_lookup spec_cfg "sources" ~src ~f:U.assoc_of_yojson ~default:[] in - let sources_src = src ^ " -> sources" in + let sources, sources_src = + if List.length source_overrides > 0 then (source_overrides, src_option_name) + else + ( U.yojson_lookup spec_cfg "sources" ~src ~f:U.assoc_of_yojson ~default:[] + , src ^ " -> sources" ) + in let src_name = spec_name ^ "-source" in let src_desc = U.yojson_lookup sources "desc" ~src:sources_src ~f:U.string_of_yojson ~default:src_name @@ -392,11 +398,16 @@ module CxxAnnotationSpecs = struct U.assoc_of_yojson Config.annotation_reachability_cxx ~src:option_name + let annotation_reachability_cxx_sources = + U.assoc_of_yojson Config.annotation_reachability_cxx_sources ~src:src_option_name + + let from_config () : 'AnnotationSpec list = List.map ~f:(fun (spec_name, spec_cfg) -> let src = option_name ^ " -> " ^ spec_name in - spec_from_config spec_name (U.assoc_of_yojson spec_cfg ~src) ) + spec_from_config spec_name (U.assoc_of_yojson spec_cfg ~src) + annotation_reachability_cxx_sources ) annotation_reachability_cxx end diff --git a/infer/tests/codetoanalyze/cpp/annotation-reachability/sources-override/Makefile b/infer/tests/codetoanalyze/cpp/annotation-reachability/sources-override/Makefile new file mode 100644 index 000000000..4a0f28665 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/annotation-reachability/sources-override/Makefile @@ -0,0 +1,18 @@ +# 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. + +TESTS_DIR = ../../../.. + +# see explanations in cpp/errors/Makefile for the custom isystem +CLANG_OPTIONS = -x c++ -std=c++11 -nostdinc++ -isystem$(ROOT_DIR) -isystem$(CLANG_INCLUDES)/c++/v1/ -c +INFER_OPTIONS = --annotation-reachability-only --debug-exceptions --project-root $(TESTS_DIR) --annotation-reachability-cxx '{ "TEST_ANNOT_REACH": { "sources": { "symbols": [ "CheckFrom::" ] }, "sinks": { "overrides": { "symbols": [ "Good::"], "paths": [ "codetoanalyze/cpp/annotation-reachability/reachability-approved.cpp" ] },"symbols": [ "Danger::", "death" ] } } }' --annotation-reachability-cxx-sources '{ "symbols": [ "CheckFrom::danger_via", "CheckFrom::death_via" ] }' +INFERPRINT_OPTIONS = --issues-tests + + +SOURCES = $(wildcard ../*.cpp) + +include $(TESTS_DIR)/clang.make + +infer-out/report.json: $(MAKEFILE_LIST) diff --git a/infer/tests/codetoanalyze/cpp/annotation-reachability/sources-override/issues.exp b/infer/tests/codetoanalyze/cpp/annotation-reachability/sources-override/issues.exp new file mode 100644 index 000000000..fc71bc72f --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/annotation-reachability/sources-override/issues.exp @@ -0,0 +1,2 @@ +codetoanalyze/cpp/annotation-reachability/reachability.cpp, CheckFrom::danger_via, 2, TEST_ANNOT_REACH, no_bucket, ERROR, [] +codetoanalyze/cpp/annotation-reachability/reachability.cpp, CheckFrom::death_via, 0, TEST_ANNOT_REACH, no_bucket, ERROR, []