diff --git a/infer/src/clang/ComponentKit.ml b/infer/src/clang/ComponentKit.ml index 352ba10b8..9e7c66762 100644 --- a/infer/src/clang/ComponentKit.ml +++ b/infer/src/clang/ComponentKit.ml @@ -80,7 +80,7 @@ let mutable_local_vars_advice context decl = let condition = context.CLintersContext.is_ck_translation_unit && is_in_main_file decl && (is_objc () || is_objcpp ()) - && (not (is_global_var decl)) + && (not (is_syntactically_global_var decl)) && (not qual_type.qt_is_const) in if condition then Some { diff --git a/infer/src/clang/cFrontend_checkers.ml b/infer/src/clang/cFrontend_checkers.ml index 2c4db592c..a0be27777 100644 --- a/infer/src/clang/cFrontend_checkers.ml +++ b/infer/src/clang/cFrontend_checkers.ml @@ -200,9 +200,9 @@ let global_var_init_with_calls_warning _ decl = match Clang_ast_proj.get_named_decl_tuple decl with | Some (di, ndi) -> di, ndi.ni_name | None -> assert false (* we cannot be here *) in - let condition = (CFrontend_utils.Ast_utils.is_objc () || CFrontend_utils.Ast_utils.is_objcpp ()) - && CFrontend_utils.Ast_utils.is_global_var decl - && (not (CFrontend_utils.Ast_utils.is_const_expr_var decl)) + let condition = (Ast_utils.is_objc () || Ast_utils.is_objcpp ()) + && Ast_utils.is_syntactically_global_var decl + && (not (Ast_utils.is_const_expr_var decl)) && is_initialized_with_expensive_call decl in if condition then Some { diff --git a/infer/src/clang/cFrontend_utils.ml b/infer/src/clang/cFrontend_utils.ml index 458261df4..8cd90982a 100644 --- a/infer/src/clang/cFrontend_utils.ml +++ b/infer/src/clang/cFrontend_utils.ml @@ -409,9 +409,10 @@ struct let _, st_list = Clang_ast_proj.get_stmt_tuple st in IList.exists (exists_eventually_st atomic_pred param) st_list - let is_global_var decl = + let is_syntactically_global_var decl = match decl with - | Clang_ast_t.VarDecl (_, _ ,_, vdi) -> vdi.vdi_is_global + | Clang_ast_t.VarDecl (_, _ ,_, vdi) -> + vdi.vdi_is_global && not vdi.vdi_is_static_local | _ -> false let is_const_expr_var decl = diff --git a/infer/src/clang/cFrontend_utils.mli b/infer/src/clang/cFrontend_utils.mli index 0d3e565a4..a3bd5b099 100644 --- a/infer/src/clang/cFrontend_utils.mli +++ b/infer/src/clang/cFrontend_utils.mli @@ -139,7 +139,7 @@ sig val exists_eventually_st : ('a -> Clang_ast_t.stmt -> bool) -> 'a -> Clang_ast_t.stmt -> bool (** true if a declaration is a global variable *) - val is_global_var : Clang_ast_t.decl -> bool + val is_syntactically_global_var : Clang_ast_t.decl -> bool (** true if a declaration is a constexpr variable *) val is_const_expr_var : Clang_ast_t.decl -> bool diff --git a/infer/tests/codetoanalyze/objcpp/linters/global-var/B.mm b/infer/tests/codetoanalyze/objcpp/linters/global-var/B.mm index 47543f2ec..e41df08d0 100644 --- a/infer/tests/codetoanalyze/objcpp/linters/global-var/B.mm +++ b/infer/tests/codetoanalyze/objcpp/linters/global-var/B.mm @@ -49,11 +49,11 @@ const int dmv = 17; constexpr double max1 = 1.4 * square(dmv); // OK void bla() { - static const int kInsets = foo(); // Error + static const int kInsets = foo(); // OK - static float kPadding = [A bar] ? 10.0 : 11.0; // Error + static float kPadding = [A bar] ? 10.0 : 11.0; // OK - static const float kLineSize = 1 / [A scale]; // Error + static const float kLineSize = 1 / [A scale]; // OK static const float ok = 37; } diff --git a/infer/tests/endtoend/objcpp/linters/GlobalVarTest.java b/infer/tests/endtoend/objcpp/linters/GlobalVarTest.java index 12840b276..063c19821 100644 --- a/infer/tests/endtoend/objcpp/linters/GlobalVarTest.java +++ b/infer/tests/endtoend/objcpp/linters/GlobalVarTest.java @@ -53,7 +53,7 @@ public class GlobalVarTest { assertThat( "Results should contain " + GLOBAL_VARIABLE_INITIALIZED_WITH_FUNCTION_OR_METHOD_CALL, inferResults, - containsOnlyLines(new int[]{30, 32, 34, 52, 54, 56})); + containsOnlyLines(new int[]{30, 32, 34})); } }