From d04e098eb184d4164722bd26ac562538e6140064 Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Wed, 4 Sep 2019 02:23:21 -0700 Subject: [PATCH] [AL] Add a is_static predicate Summary: With this predicate we are able to check for static global variables in AL. Reviewed By: ddino Differential Revision: D17164848 fbshipit-source-id: a3d10598c --- infer/src/al/cPredicates.ml | 4 ++++ infer/src/al/cPredicates.mli | 3 +++ infer/src/al/cTL.ml | 2 ++ .../codetoanalyze/cpp/linters-for-test-only/issues.exp | 6 ++++-- .../cpp/linters-for-test-only/linters_example.al | 7 +++++++ .../cpp/linters-for-test-only/test_constructor.cpp | 2 ++ 6 files changed, 22 insertions(+), 2 deletions(-) diff --git a/infer/src/al/cPredicates.ml b/infer/src/al/cPredicates.ml index 6fca81518..8e5fcc3bd 100644 --- a/infer/src/al/cPredicates.ml +++ b/infer/src/al/cPredicates.ml @@ -509,6 +509,10 @@ let is_static_local_var an = match an with Ctl_parser_types.Decl d -> CAst_utils.is_static_local_var d | _ -> false +let is_static_var an = + match an with Ctl_parser_types.Decl (VarDecl (_, _, _, vdi)) -> vdi.vdi_is_static | _ -> false + + let is_extern_var an = match an with Ctl_parser_types.Decl (VarDecl (_, _, _, vdi)) -> vdi.vdi_is_extern | _ -> false diff --git a/infer/src/al/cPredicates.mli b/infer/src/al/cPredicates.mli index f2de93ee7..be812798d 100644 --- a/infer/src/al/cPredicates.mli +++ b/infer/src/al/cPredicates.mli @@ -45,6 +45,9 @@ val is_global_var : Ctl_parser_types.ast_node -> bool val is_static_local_var : Ctl_parser_types.ast_node -> bool (** 'is_static_local_var an' is true iff an is a static local variable *) +val is_static_var : Ctl_parser_types.ast_node -> bool +(** 'is_static_var an' is true iff an is a static local variable *) + val is_extern_var : Ctl_parser_types.ast_node -> bool (** 'is_extern_var an' is true iff an is a extern variable *) diff --git a/infer/src/al/cTL.ml b/infer/src/al/cTL.ml index c6600377b..702d9d107 100644 --- a/infer/src/al/cTL.ml +++ b/infer/src/al/cTL.ml @@ -1052,6 +1052,8 @@ let rec eval_Atomic pred_name_ args an lcxt = CPredicates.is_global_var an | "is_static_local_var", [], an -> CPredicates.is_static_local_var an + | "is_static_var", [], an -> + CPredicates.is_static_var an | "is_extern_var", [], an -> CPredicates.is_extern_var an | "adhere_to_protocol", [], an -> diff --git a/infer/tests/codetoanalyze/cpp/linters-for-test-only/issues.exp b/infer/tests/codetoanalyze/cpp/linters-for-test-only/issues.exp index db40e9484..2d8d86db1 100644 --- a/infer/tests/codetoanalyze/cpp/linters-for-test-only/issues.exp +++ b/infer/tests/codetoanalyze/cpp/linters-for-test-only/issues.exp @@ -1,6 +1,7 @@ codetoanalyze/cpp/linters-for-test-only/test_constructor.cpp, Linters_dummy_method, 11, FIND_EXTERN_VAR, no_bucket, WARNING, [] -codetoanalyze/cpp/linters-for-test-only/test_constructor.cpp, f, 14, FIND_STATIC_LOCAL_VAR, no_bucket, WARNING, [] -codetoanalyze/cpp/linters-for-test-only/test_constructor.cpp, g, 19, FIND_CXX_COPY_CONSTRUCTOR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters-for-test-only/test_constructor.cpp, Linters_dummy_method, 13, FIND_STATIC_GLOBAL_VAR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters-for-test-only/test_constructor.cpp, f, 16, FIND_STATIC_LOCAL_VAR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters-for-test-only/test_constructor.cpp, g, 21, FIND_CXX_COPY_CONSTRUCTOR, no_bucket, WARNING, [] codetoanalyze/cpp/linters-for-test-only/test_fully_qualified_names.cpp, Foo::Bar::Bar, 26, FIND_NODES_WITH_CXX_FULL_NAME, no_bucket, WARNING, [] codetoanalyze/cpp/linters-for-test-only/test_fully_qualified_names.cpp, Foo::Bar::f, 30, FIND_NODES_WITH_CXX_FULL_NAME, no_bucket, WARNING, [] codetoanalyze/cpp/linters-for-test-only/test_fully_qualified_names.cpp, Foo::Bar::f, 32, FIND_NODES_WITH_CXX_FULL_NAME, no_bucket, WARNING, [] @@ -10,6 +11,7 @@ codetoanalyze/cpp/linters-for-test-only/test_fully_qualified_names.cpp, Foo::Bar codetoanalyze/cpp/linters-for-test-only/test_fully_qualified_names.cpp, Linters_dummy_method, 23, FIND_NODES_WITH_CXX_FULL_NAME, no_bucket, WARNING, [] codetoanalyze/cpp/linters-for-test-only/test_fully_qualified_names.cpp, Linters_dummy_method, 25, FIND_NODES_WITH_CXX_FULL_NAME, no_bucket, WARNING, [] codetoanalyze/cpp/linters-for-test-only/test_fully_qualified_names.cpp, Linters_dummy_method, 28, FIND_NODES_WITH_CXX_FULL_NAME, no_bucket, WARNING, [] +codetoanalyze/cpp/linters-for-test-only/test_fully_qualified_names.cpp, Linters_dummy_method, 28, FIND_STATIC_GLOBAL_VAR, no_bucket, WARNING, [] codetoanalyze/cpp/linters-for-test-only/test_fully_qualified_names.cpp, Linters_dummy_method, 39, FIND_NODES_WITH_CXX_FULL_NAME, no_bucket, WARNING, [] codetoanalyze/cpp/linters-for-test-only/test_included.h, Bazoo::fibble, 11, FIND_CXX_METHODS_FROM_HEADER_FILE, no_bucket, WARNING, [] codetoanalyze/cpp/linters-for-test-only/test_includer.cpp, Bazowey::frazzle, 11, FIND_REF_FROM_SRC_FILE, no_bucket, WARNING, [] diff --git a/infer/tests/codetoanalyze/cpp/linters-for-test-only/linters_example.al b/infer/tests/codetoanalyze/cpp/linters-for-test-only/linters_example.al index 37d7909be..2bedcf55e 100644 --- a/infer/tests/codetoanalyze/cpp/linters-for-test-only/linters_example.al +++ b/infer/tests/codetoanalyze/cpp/linters-for-test-only/linters_example.al @@ -27,6 +27,13 @@ SET message = "Found static local var"; }; +DEFINE-CHECKER FIND_STATIC_GLOBAL_VAR = { +SET report_when = + WHEN is_static_var AND is_global_var + HOLDS-IN-NODE VarDecl; +SET message = "Found a static global var"; +}; + DEFINE-CHECKER FIND_EXTERN_VAR = { SET report_when = WHEN is_extern_var diff --git a/infer/tests/codetoanalyze/cpp/linters-for-test-only/test_constructor.cpp b/infer/tests/codetoanalyze/cpp/linters-for-test-only/test_constructor.cpp index cd398e1bf..b1e9914d7 100644 --- a/infer/tests/codetoanalyze/cpp/linters-for-test-only/test_constructor.cpp +++ b/infer/tests/codetoanalyze/cpp/linters-for-test-only/test_constructor.cpp @@ -10,6 +10,8 @@ struct A { extern A a2; +static A a3; + const A& f() { static A a; return a;