From 21145c75c920962b3c2970d1224eca7b6dc8ba8d Mon Sep 17 00:00:00 2001 From: Dino Distefano Date: Mon, 17 Sep 2018 04:01:20 -0700 Subject: [PATCH] Added new predicate is_const_expr() Reviewed By: jvillard Differential Revision: D9496229 fbshipit-source-id: c0bccf8c9 --- infer/src/clang/cPredicates.ml | 12 ++++++++++++ infer/src/clang/cPredicates.mli | 3 +++ infer/src/clang/cTL.ml | 2 ++ .../tests/codetoanalyze/cpp/linters/extracopy.al | 8 +++++++- infer/tests/codetoanalyze/cpp/linters/issues.exp | 16 ++++++++-------- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/infer/src/clang/cPredicates.ml b/infer/src/clang/cPredicates.ml index 24fcb451e..3de6ee5e1 100644 --- a/infer/src/clang/cPredicates.ml +++ b/infer/src/clang/cPredicates.ml @@ -507,6 +507,18 @@ let is_const_expr_var an = match an with Ctl_parser_types.Decl d -> CAst_utils.is_const_expr_var d | _ -> false +let is_const an = + match an with + | Ctl_parser_types.Stmt s -> ( + match Clang_ast_proj.get_expr_tuple s with + | Some (_, _, ei) -> + ei.Clang_ast_t.ei_qual_type.qt_is_const + | _ -> + false ) + | _ -> + false + + let decl_ref_name ?kind name st = match st with | Clang_ast_t.DeclRefExpr (_, _, _, drti) -> ( diff --git a/infer/src/clang/cPredicates.mli b/infer/src/clang/cPredicates.mli index ed56db65e..cbdae78fb 100644 --- a/infer/src/clang/cPredicates.mli +++ b/infer/src/clang/cPredicates.mli @@ -42,6 +42,9 @@ val is_static_local_var : Ctl_parser_types.ast_node -> bool val is_const_expr_var : Ctl_parser_types.ast_node -> bool (** 'is_const_expr_var an' is true iff an is a 'const' variable declaration *) +val is_const : Ctl_parser_types.ast_node -> bool +(** 'is_const an' is true iff an is a 'const' expression *) + val call_function : Ctl_parser_types.ast_node -> ALVar.alexp -> bool (** 'call_function an name' is true iff an is a call to a function whose name contains 'name' *) diff --git a/infer/src/clang/cTL.ml b/infer/src/clang/cTL.ml index 2184e7426..c388c100a 100644 --- a/infer/src/clang/cTL.ml +++ b/infer/src/clang/cTL.ml @@ -998,6 +998,8 @@ let rec eval_Atomic pred_name_ args an lcxt = CPredicates.is_class an cname | "is_const_var", [], an -> CPredicates.is_const_expr_var an + | "is_const", [], an -> + CPredicates.is_const an | "is_decl", [], an -> CPredicates.is_decl an | "is_enum_constant", [cname], an -> diff --git a/infer/tests/codetoanalyze/cpp/linters/extracopy.al b/infer/tests/codetoanalyze/cpp/linters/extracopy.al index 4bc50e55c..9f53663a7 100644 --- a/infer/tests/codetoanalyze/cpp/linters/extracopy.al +++ b/infer/tests/codetoanalyze/cpp/linters/extracopy.al @@ -47,11 +47,17 @@ DEFINE-CHECKER ITERATOR = { }; + DEFINE-CHECKER CXX11_CONSTANT_EXPR = { + LET eventually_const_sub_expr = HOLDS-NEXT WITH-TRANSITION InitExpr + (is_const HOLDS-EVENTUALLY); + + LET not_static_and_not_global = NOT (is_global_var OR is_static_local_var); + SET report_when = WHEN - is_init_expr_cxx11_constant() + not_static_and_not_global AND (is_init_expr_cxx11_constant() OR eventually_const_sub_expr) HOLDS-IN-NODE VarDecl; SET message = "Found cxx11 constant expression"; diff --git a/infer/tests/codetoanalyze/cpp/linters/issues.exp b/infer/tests/codetoanalyze/cpp/linters/issues.exp index 23b8f981b..b4429e64e 100644 --- a/infer/tests/codetoanalyze/cpp/linters/issues.exp +++ b/infer/tests/codetoanalyze/cpp/linters/issues.exp @@ -1,20 +1,20 @@ -codetoanalyze/cpp/linters/extracopy.cpp, Linters_dummy_method, 33, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] -codetoanalyze/cpp/linters/extracopy.cpp, get_a, 21, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] -codetoanalyze/cpp/linters/extracopy.cpp, get_a_ref, 16, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] -codetoanalyze/cpp/linters/extracopy.cpp, test_a, 26, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters/cxxconst.cpp, test, 24, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters/cxxconst.cpp, test, 27, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters/cxxconst.cpp, test, 29, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters/cxxconst.cpp, test, 30, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters/cxxconst.cpp, test, 31, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters/cxxconst.cpp, test, 33, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] codetoanalyze/cpp/linters/extracopy.cpp, test_a, 27, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] codetoanalyze/cpp/linters/extracopy.cpp, test_a, 27, EXTRA_COPY, no_bucket, WARNING, [] codetoanalyze/cpp/linters/extracopy.cpp, test_a, 28, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] -codetoanalyze/cpp/linters/extracopy.cpp, test_a, 29, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] -codetoanalyze/cpp/linters/extracopy.cpp, test_const_exp, 54, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] -codetoanalyze/cpp/linters/extracopy.cpp, test_const_exp, 55, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters/extracopy.cpp, test_const_exp, 53, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] codetoanalyze/cpp/linters/extracopy.cpp, test_map, 36, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] codetoanalyze/cpp/linters/extracopy.cpp, test_map, 37, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] codetoanalyze/cpp/linters/extracopy.cpp, test_map, 37, EXTRA_COPY, no_bucket, WARNING, [] codetoanalyze/cpp/linters/extracopy.cpp, test_map, 40, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] codetoanalyze/cpp/linters/extracopy.cpp, test_map, 43, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] -codetoanalyze/cpp/linters/iter.cpp, test, 10, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] codetoanalyze/cpp/linters/iter.cpp, test, 11, ITERATOR, no_bucket, WARNING, [] +codetoanalyze/cpp/linters/iter.cpp, test, 12, CXX11_CONSTANT_EXPR, no_bucket, WARNING, [] codetoanalyze/cpp/linters/iter.cpp, test, 12, EXTRA_COPY, no_bucket, WARNING, [] codetoanalyze/cpp/linters/iter.cpp, test, 12, ITERATOR, no_bucket, WARNING, [] codetoanalyze/cpp/linters/type_namespace.cpp, test, 11, CXX11_CONSTANT_EXPR, no_bucket, WARNING, []