From 416478d453feaa754c252a2ea6df27551a06305c Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Fri, 14 Jul 2017 05:13:26 -0700 Subject: [PATCH] [linters] Predicate for checking enum constants Reviewed By: akotulski Differential Revision: D5423574 fbshipit-source-id: c1deb13 --- infer/src/clang/cPredicates.ml | 7 +++++++ infer/src/clang/cPredicates.mli | 2 ++ infer/src/clang/cTL.ml | 2 ++ .../objc/linters-for-test-only/enums.m | 20 +++++++++++++++++++ .../objc/linters-for-test-only/issues.exp | 2 ++ .../linters-for-test-only/linters_example.al | 5 +++++ 6 files changed, 38 insertions(+) create mode 100644 infer/tests/codetoanalyze/objc/linters-for-test-only/enums.m diff --git a/infer/src/clang/cPredicates.ml b/infer/src/clang/cPredicates.ml index 60841f3c5..6c14649e4 100644 --- a/infer/src/clang/cPredicates.ml +++ b/infer/src/clang/cPredicates.ml @@ -216,6 +216,13 @@ let call_function an name = | _ -> false +let is_enum_constant an name = + match an with + | Ctl_parser_types.Stmt st + -> decl_ref_name ~kind:`EnumConstant name st + | _ + -> false + let is_strong_property an = match an with | Ctl_parser_types.Decl Clang_ast_t.ObjCPropertyDecl (_, _, pdi) diff --git a/infer/src/clang/cPredicates.mli b/infer/src/clang/cPredicates.mli index 9693d43d9..e747aa28e 100644 --- a/infer/src/clang/cPredicates.mli +++ b/infer/src/clang/cPredicates.mli @@ -21,6 +21,8 @@ val call_class_method : Ctl_parser_types.ast_node -> ALVar.alexp -> ALVar.alexp val call_instance_method : Ctl_parser_types.ast_node -> ALVar.alexp -> ALVar.alexp -> bool +val is_enum_constant : Ctl_parser_types.ast_node -> ALVar.alexp -> bool + val is_objc_interface_named : Ctl_parser_types.ast_node -> ALVar.alexp -> bool val is_objc_extension : CLintersContext.context -> bool diff --git a/infer/src/clang/cTL.ml b/infer/src/clang/cTL.ml index 1bcf433cb..e82661f2f 100644 --- a/infer/src/clang/cTL.ml +++ b/infer/src/clang/cTL.ml @@ -760,6 +760,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_enum_constant", [cname], an + -> CPredicates.is_enum_constant an cname | "is_global_var", [], an -> CPredicates.is_syntactically_global_var an | "is_ivar_atomic", [], an diff --git a/infer/tests/codetoanalyze/objc/linters-for-test-only/enums.m b/infer/tests/codetoanalyze/objc/linters-for-test-only/enums.m new file mode 100644 index 000000000..7678dfe86 --- /dev/null +++ b/infer/tests/codetoanalyze/objc/linters-for-test-only/enums.m @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2017 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#import + +typedef NS_ENUM(NSUInteger, MyName) { + MyNameUndefined, +}; + +int test() { return MyNameUndefined; } + +enum { RANDOM, IMMEDIATE, SEARCH } strategy; + +int test_c_style_enum() { return IMMEDIATE; } 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 6370f70c2..3b53bad2d 100644 --- a/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp +++ b/infer/tests/codetoanalyze/objc/linters-for-test-only/issues.exp @@ -1,3 +1,5 @@ +codetoanalyze/objc/linters-for-test-only/enums.m, test, 16, ENUM_CONSTANTS, [] +codetoanalyze/objc/linters-for-test-only/enums.m, test_c_style_enum, 20, ENUM_CONSTANTS, [] codetoanalyze/objc/linters-for-test-only/filter_by_path/include_file.m, main, 9, ALL_PATH_NO_FILTER_EXAMPLE, [] codetoanalyze/objc/linters-for-test-only/filter_by_path/include_file.m, main, 9, FILTER_BY_ALL_PATH_EXAMPLE, [] codetoanalyze/objc/linters-for-test-only/filter_by_path/include_file.m, main, 9, FILTER_BY_PATH_EXAMPLE, [] 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 306f4a53e..a985ca077 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 @@ -338,3 +338,8 @@ DEFINE-CHECKER WHITE_BLACKLIST_PATH_EXAMPLE = { SET blacklist_path = { filtered_files }; SET doc_url = "www.example.com"; }; + +DEFINE-CHECKER ENUM_CONSTANTS = { + SET report_when = is_enum_constant(REGEXP("MyName.*")) OR is_enum_constant("IMMEDIATE"); + SET message = "Do not use the enum MyName or strategy"; +};