From 55da0055c98573d979b962b1b7a53938bcc17cb3 Mon Sep 17 00:00:00 2001 From: Dino Distefano Date: Wed, 7 Jun 2017 02:35:19 -0700 Subject: [PATCH] Predicate for `using namespace` Reviewed By: dulmarod Differential Revision: D5172430 fbshipit-source-id: 0cd9251 --- infer/src/clang/cPredicates.ml | 11 +++++++++++ infer/src/clang/cPredicates.mli | 2 ++ infer/src/clang/cTL.ml | 2 ++ .../objc/linters-for-test-only/issues.exp | 3 ++- .../objc/linters-for-test-only/linters_example.al | 11 ++++++++++- .../objc/linters-for-test-only/namespace.mm | 2 ++ 6 files changed, 29 insertions(+), 2 deletions(-) diff --git a/infer/src/clang/cPredicates.ml b/infer/src/clang/cPredicates.ml index c1315503d..d167375f1 100644 --- a/infer/src/clang/cPredicates.ml +++ b/infer/src/clang/cPredicates.ml @@ -422,3 +422,14 @@ let objc_method_has_nth_parameter_of_type an _num _typ = type_ptr_equal_type qt.qt_type_ptr typ | _ -> false) | _, _, _ -> false + +let using_namespace an namespace = + let open Clang_ast_t in + match an with + | Ctl_parser_types.Decl (UsingDirectiveDecl (_, _, uddi)) -> + (match uddi.uddi_nominated_namespace with + | Some dr -> (match dr.dr_kind, dr.dr_name with + | `Namespace, Some ni -> ALVar.compare_str_with_alexp ni.ni_name namespace + | _ -> false) + | None -> false) + | _ -> false diff --git a/infer/src/clang/cPredicates.mli b/infer/src/clang/cPredicates.mli index 64c4cc981..56d94aa80 100644 --- a/infer/src/clang/cPredicates.mli +++ b/infer/src/clang/cPredicates.mli @@ -81,3 +81,5 @@ val within_responds_to_selector_block : val objc_method_has_nth_parameter_of_type : Ctl_parser_types.ast_node -> ALVar.alexp -> ALVar.alexp -> bool + +val using_namespace : Ctl_parser_types.ast_node -> ALVar.alexp -> bool diff --git a/infer/src/clang/cTL.ml b/infer/src/clang/cTL.ml index 35f47c179..49b0f52f5 100644 --- a/infer/src/clang/cTL.ml +++ b/infer/src/clang/cTL.ml @@ -647,6 +647,8 @@ let rec eval_Atomic _pred_name args an lcxt = CPredicates.within_responds_to_selector_block lcxt an | "objc_method_has_nth_parameter_of_type", [num; typ], an -> CPredicates.objc_method_has_nth_parameter_of_type an num typ + | "using_namespace", [namespace], an -> + CPredicates.using_namespace an namespace | _ -> failwith ("ERROR: Undefined Predicate or wrong set of arguments: '" ^ pred_name ^ "'") diff --git a/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp b/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp index 98595817d..a43281812 100644 --- a/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp +++ b/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp @@ -6,7 +6,8 @@ codetoanalyze/objc/linters-for-test-only/implicit_cast.c, main, 9, FILTER_BY_ALL codetoanalyze/objc/linters-for-test-only/implicit_cast.c, main, 10, TEST_VAR_TYPE_CHECK, [] codetoanalyze/objc/linters-for-test-only/implicit_cast.c, main, 11, TEST_IMPLICIT_CAST_CHECK, [] codetoanalyze/objc/linters-for-test-only/implicit_cast.c, main, 11, TEST_VAR_TYPE_CHECK, [] -codetoanalyze/objc/linters-for-test-only/namespace.mm, Linters_dummy_method, 9, TEST_NAMESPACE_NAME, [] +codetoanalyze/objc/linters-for-test-only/namespace.mm, Linters_dummy_method, 9, TEST_DEFINE_NAMESPACE, [] +codetoanalyze/objc/linters-for-test-only/namespace.mm, Linters_dummy_method, 17, TEST_USING_NAMESPACE, [] codetoanalyze/objc/linters-for-test-only/subclassing.m, A_foo:, 13, TEST_BUILTIN_TYPE, [] codetoanalyze/objc/linters-for-test-only/subclassing.m, A_foo:, 13, TEST_PARAM_TYPE_CHECK2, [] codetoanalyze/objc/linters-for-test-only/subclassing.m, A_foo:, 13, TEST_RETURN_METHOD, [] diff --git a/infer/tests/codetoanalyze/objc/linters-for-test-only/linters_example.al b/infer/tests/codetoanalyze/objc/linters-for-test-only/linters_example.al index fedf5c87e..90bb5d5a8 100644 --- a/infer/tests/codetoanalyze/objc/linters-for-test-only/linters_example.al +++ b/infer/tests/codetoanalyze/objc/linters-for-test-only/linters_example.al @@ -217,7 +217,7 @@ DEFINE-CHECKER TEST_NTH_PARAM_TYPE_CHECK = { SET severity = "LIKE"; }; -DEFINE-CHECKER TEST_NAMESPACE_NAME = { +DEFINE-CHECKER TEST_DEFINE_NAMESPACE = { SET report_when = WHEN @@ -228,6 +228,15 @@ DEFINE-CHECKER TEST_NAMESPACE_NAME = { }; +DEFINE-CHECKER TEST_USING_NAMESPACE = { + + SET report_when = + using_namespace(REGEXP("FirstNam*")); + + SET message = "Found using namespace with name...."; + +}; + DEFINE-CHECKER FILTER_BY_PATH_EXAMPLE = { SET report_when = WHEN declaration_has_name("main") diff --git a/infer/tests/codetoanalyze/objc/linters-for-test-only/namespace.mm b/infer/tests/codetoanalyze/objc/linters-for-test-only/namespace.mm index 230a18f97..bed5d12f8 100644 --- a/infer/tests/codetoanalyze/objc/linters-for-test-only/namespace.mm +++ b/infer/tests/codetoanalyze/objc/linters-for-test-only/namespace.mm @@ -14,3 +14,5 @@ class C { } void SecodNameSpace::C::m() {} } +using namespace FirstNameSpace; +void foo() { SecodNameSpace::C(); }