Add call_cxx_method predicate

Reviewed By: martintrojer

Differential Revision: D14880808

fbshipit-source-id: 1192dc4c2
master
Lee Howes 6 years ago committed by Facebook Github Bot
parent 86d4c5a55f
commit 183e9ed9fa

@ -1487,6 +1487,19 @@ let is_init_expr_cxx11_constant an =
false false
let call_cxx_method an name =
let open Clang_ast_t in
match an with
| Ctl_parser_types.Stmt (CXXMemberCallExpr (_, member :: _, _)) -> (
match member with
| MemberExpr (_, _, _, memberExprInfo) ->
ALVar.compare_str_with_alexp memberExprInfo.mei_name.ni_name name
| _ ->
false )
| _ ->
false
let source_file_matches src_file path_re = let source_file_matches src_file path_re =
Option.value_map Option.value_map
~f:(fun sf -> ~f:(fun sf ->

@ -20,6 +20,9 @@ val objc_block_is_capturing_values : Ctl_parser_types.ast_node -> bool
val call_method : Ctl_parser_types.ast_node -> ALVar.alexp -> bool val call_method : Ctl_parser_types.ast_node -> ALVar.alexp -> bool
(** 'call_method an m an' is true iff node an is a call to an ObjC method with name containing string m *) (** 'call_method an m an' is true iff node an is a call to an ObjC method with name containing string m *)
val call_cxx_method : Ctl_parser_types.ast_node -> ALVar.alexp -> bool
(** 'call_cxx_method an m an' is true iff node an is a call to a C++ method with name containing string m *)
val call_class_method : Ctl_parser_types.ast_node -> ALVar.alexp -> bool val call_class_method : Ctl_parser_types.ast_node -> ALVar.alexp -> bool
(** 'call_class_method an mname' is true iff node an is a call to an ObjC (** 'call_class_method an mname' is true iff node an is a call to an ObjC
class method with name containing mname *) class method with name containing mname *)

@ -968,6 +968,8 @@ let rec eval_Atomic pred_name_ args an lcxt =
CPredicates.call_instance_method an m CPredicates.call_instance_method an m
| "call_method", [m], an -> | "call_method", [m], an ->
CPredicates.call_method an m CPredicates.call_method an m
| "call_cxx_method", [m], an ->
CPredicates.call_cxx_method an m
| "captures_cxx_references", [], _ -> | "captures_cxx_references", [], _ ->
CPredicates.captures_cxx_references an CPredicates.captures_cxx_references an
| "objc_block_is_capturing_values", [], _ -> | "objc_block_is_capturing_values", [], _ ->

@ -6,7 +6,7 @@
TESTS_DIR = ../../.. TESTS_DIR = ../../..
CLANG_OPTIONS = -std=c++11 -c CLANG_OPTIONS = -std=c++11 -c
INFER_OPTIONS = --no-capture --linters-only --linters-def-file extracopy.al --linters-def-file call_function.al --no-filtering --debug-exceptions --project-root $(TESTS_DIR) \ INFER_OPTIONS = --no-capture --linters-only --linters-def-file extracopy.al --linters-def-file call_function.al --linters-def-file call_cxx_method.al --no-filtering --debug-exceptions --project-root $(TESTS_DIR) \
--enable-issue-type GLOBAL_VARIABLE_INITIALIZED_WITH_FUNCTION_OR_METHOD_CALL --enable-issue-type GLOBAL_VARIABLE_INITIALIZED_WITH_FUNCTION_OR_METHOD_CALL
INFERPRINT_OPTIONS = --issues-tests INFERPRINT_OPTIONS = --issues-tests

@ -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.
DEFINE-CHECKER CALL_CXX_METHOD = {
SET report_when =
WHEN call_cxx_method("fooBar") OR call_cxx_method("bar")
HOLDS-IN-NODE CXXMemberCallExpr;
SET message = "Do not call fooBar or bar.";
SET suggestion = "Call something else.";
SET severity = "WARNING";
SET mode = "OFF";
};

@ -0,0 +1,24 @@
/*
* 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.
*/
namespace {
struct Foo {
void bar(){};
void toUnsafeFuture(){};
};
struct Bar {
virtual void fooBar() { f.bar(); };
Foo f;
};
} // anonymous namespace
int main() {
Foo{}.bar();
Foo f;
f.bar();
Bar{}.fooBar();
return 0;
}

@ -1,3 +1,7 @@
codetoanalyze/cpp/linters/call_cxx_method.cpp, anonymous_namespace_call_cxx_method.cpp::Bar::fooBar, 13, CALL_CXX_METHOD, no_bucket, WARNING, []
codetoanalyze/cpp/linters/call_cxx_method.cpp, main, 19, CALL_CXX_METHOD, no_bucket, WARNING, []
codetoanalyze/cpp/linters/call_cxx_method.cpp, main, 21, CALL_CXX_METHOD, no_bucket, WARNING, []
codetoanalyze/cpp/linters/call_cxx_method.cpp, main, 22, CALL_CXX_METHOD, no_bucket, WARNING, []
codetoanalyze/cpp/linters/call_function.cpp, main, 20, CALL_FUNCTION, no_bucket, WARNING, [] codetoanalyze/cpp/linters/call_function.cpp, main, 20, CALL_FUNCTION, no_bucket, WARNING, []
codetoanalyze/cpp/linters/call_function.cpp, main, 22, CALL_FUNCTION, no_bucket, WARNING, [] codetoanalyze/cpp/linters/call_function.cpp, main, 22, CALL_FUNCTION, no_bucket, WARNING, []
codetoanalyze/cpp/linters/call_function.cpp, main, 23, CALL_FUNCTION, no_bucket, WARNING, [] codetoanalyze/cpp/linters/call_function.cpp, main, 23, CALL_FUNCTION, no_bucket, WARNING, []

Loading…
Cancel
Save