From 996f7c4f02828440ceaef4f72cb4e544f74496e9 Mon Sep 17 00:00:00 2001 From: David Lively Date: Tue, 9 Apr 2019 07:37:19 -0700 Subject: [PATCH] Allow Cxx annotation-reachability src/sink/override w/paths AND symbols Reviewed By: jvillard Differential Revision: D14841296 fbshipit-source-id: dfbd1b3ab --- infer/src/checkers/annotationReachability.ml | 18 +++++++++++------- .../cpp/annotation-reachability/Makefile | 2 +- .../reachability-approved.cpp | 16 ++++++++++++++++ .../reachability-approved.h | 15 +++++++++++++++ .../annotation-reachability/reachability.cpp | 9 +-------- 5 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 infer/tests/codetoanalyze/cpp/annotation-reachability/reachability-approved.cpp create mode 100644 infer/tests/codetoanalyze/cpp/annotation-reachability/reachability-approved.h diff --git a/infer/src/checkers/annotationReachability.ml b/infer/src/checkers/annotationReachability.ml index 220868977..75416fcc2 100644 --- a/infer/src/checkers/annotationReachability.ml +++ b/infer/src/checkers/annotationReachability.ml @@ -304,13 +304,17 @@ module CxxAnnotationSpecs = struct 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 let paths = U.yojson_lookup entry "paths" ~src ~f:U.string_list_of_yojson ~default:[] in - if not (List.is_empty symbols) then ( - if not (List.is_empty paths) then - Logging.(die UserError) "Cannot specify both `paths` and `symbols` in %s" src ; - fun pname -> List.exists ~f:(symbol_match (Typ.Procname.to_string pname)) symbols ) - else if not (List.is_empty paths) then fun pname -> - List.exists ~f:(path_match (src_path_of pname)) paths - else Logging.(die UserError) "Must specify either `paths` or `symbols` in %s" src + let sym_pred pname = List.exists ~f:(symbol_match (Typ.Procname.to_string pname)) symbols in + let path_pred pname = List.exists ~f:(path_match (src_path_of pname)) paths in + match (symbols, paths) with + | [], [] -> + Logging.(die UserError) "Must specify either `paths` or `symbols` in %s" src + | _, [] -> + sym_pred + | [], _ -> + path_pred + | _, _ -> + 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 diff --git a/infer/tests/codetoanalyze/cpp/annotation-reachability/Makefile b/infer/tests/codetoanalyze/cpp/annotation-reachability/Makefile index f5e7a3cfd..6b2e6c518 100644 --- a/infer/tests/codetoanalyze/cpp/annotation-reachability/Makefile +++ b/infer/tests/codetoanalyze/cpp/annotation-reachability/Makefile @@ -7,7 +7,7 @@ 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": [ "Approved::"] },"symbols": [ "Danger::", "death" ] } } }' +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" ] } } }' INFERPRINT_OPTIONS = --issues-tests diff --git a/infer/tests/codetoanalyze/cpp/annotation-reachability/reachability-approved.cpp b/infer/tests/codetoanalyze/cpp/annotation-reachability/reachability-approved.cpp new file mode 100644 index 000000000..98f49758f --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/annotation-reachability/reachability-approved.cpp @@ -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. + */ + +#include + +void death() { exit(-1); } + +void good() {} + +namespace Approved { +void baz() { death(); } +} // namespace Approved diff --git a/infer/tests/codetoanalyze/cpp/annotation-reachability/reachability-approved.h b/infer/tests/codetoanalyze/cpp/annotation-reachability/reachability-approved.h new file mode 100644 index 000000000..7470fcf12 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/annotation-reachability/reachability-approved.h @@ -0,0 +1,15 @@ +/* + * 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. + */ +#pragma once + +void death(); + +namespace Approved { +void baz(); +} + +void good(); diff --git a/infer/tests/codetoanalyze/cpp/annotation-reachability/reachability.cpp b/infer/tests/codetoanalyze/cpp/annotation-reachability/reachability.cpp index c0aa3e5ff..70c62c6a8 100644 --- a/infer/tests/codetoanalyze/cpp/annotation-reachability/reachability.cpp +++ b/infer/tests/codetoanalyze/cpp/annotation-reachability/reachability.cpp @@ -4,25 +4,18 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ +#include "reachability-approved.h" namespace Danger { void foo(); void bar(); } // namespace Danger -void death(); - -void good(); - namespace Ok { void foo(); void bar(); } // namespace Ok -namespace Approved { -void baz() { death(); } -} // namespace Approved - namespace CheckFrom { void death_via() { death(); }