[dead store objc] Whitelisting macro variables in dead stored checker for Objective-C

Reviewed By: sblackshear

Differential Revision: D6135723

fbshipit-source-id: e703345
master
Dulma Churchill 7 years ago committed by Facebook Github Bot
parent a2f69050ac
commit b95c4e34d6

@ -67,7 +67,7 @@ BUILD_SYSTEMS_TESTS += xcodebuild_no_xcpretty objc_getters_setters
DIRECT_TESTS += \ DIRECT_TESTS += \
objc_frontend objc_errors objc_linters objc_ioslints \ objc_frontend objc_errors objc_linters objc_ioslints \
objcpp_frontend objcpp_linters objc_linters-for-test-only objcpp_linters-for-test-only \ objcpp_frontend objcpp_linters objc_linters-for-test-only objcpp_linters-for-test-only \
objc_linters-def-folder objc_checkers objc_linters-def-folder objc_checkers objc_liveness
ifneq ($(XCPRETTY),no) ifneq ($(XCPRETTY),no)
BUILD_SYSTEMS_TESTS += xcodebuild BUILD_SYSTEMS_TESTS += xcodebuild
endif endif

@ -61,6 +61,13 @@ end
module CFG = ProcCfg.OneInstrPerNode (ProcCfg.Backward (ProcCfg.Exceptional)) module CFG = ProcCfg.OneInstrPerNode (ProcCfg.Backward (ProcCfg.Exceptional))
module Analyzer = AbstractInterpreter.Make (CFG) (TransferFunctions) module Analyzer = AbstractInterpreter.Make (CFG) (TransferFunctions)
let is_whitelisted_var var =
let whitelisted_vars = ["__assert_file__"; "__assert_fn__"] in
List.exists
~f:(fun whitelisted_var -> String.equal whitelisted_var (Pvar.get_simplified_name var))
whitelisted_vars
let checker {Callbacks.tenv; summary; proc_desc} : Specs.summary = let checker {Callbacks.tenv; summary; proc_desc} : Specs.summary =
let cfg = CFG.from_pdesc proc_desc in let cfg = CFG.from_pdesc proc_desc in
let invariant_map = let invariant_map =
@ -83,7 +90,8 @@ let checker {Callbacks.tenv; summary; proc_desc} : Specs.summary =
| Sil.Store (Lvar pvar, _, _, loc) | Sil.Store (Lvar pvar, _, _, loc)
when not when not
( Pvar.is_frontend_tmp pvar || Pvar.is_return pvar || Pvar.is_global pvar ( Pvar.is_frontend_tmp pvar || Pvar.is_return pvar || Pvar.is_global pvar
|| Domain.mem (Var.of_pvar pvar) live_vars || is_captured_var pvar ) -> || Domain.mem (Var.of_pvar pvar) live_vars || is_captured_var pvar
|| is_whitelisted_var pvar ) ->
let issue_id = IssueType.dead_store.unique_id in let issue_id = IssueType.dead_store.unique_id in
let message = F.asprintf "The value written to %a is never used" (Pvar.pp Pp.text) pvar in let message = F.asprintf "The value written to %a is never used" (Pvar.pp Pp.text) pvar in
let ltr = [Errlog.make_trace_element 0 loc "Write of unused value" []] in let ltr = [Errlog.make_trace_element 0 loc "Write of unused value" []] in
@ -106,4 +114,3 @@ let checker {Callbacks.tenv; summary; proc_desc} : Specs.summary =
in in
List.iter (CFG.nodes cfg) ~f:report_on_node ; List.iter (CFG.nodes cfg) ~f:report_on_node ;
summary summary

@ -0,0 +1,19 @@
# Copyright (c) 2016 - 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.
TESTS_DIR = ../../..
ANALYZER = checkers
CLANG_OPTIONS = -x objective-c -c
INFER_OPTIONS = --liveness-only --debug-exceptions --project-root $(TESTS_DIR)
INFERPRINT_OPTIONS = --issues-tests
SOURCES = $(wildcard *.m)
include $(TESTS_DIR)/clang.make
infer-out/report.json: $(MAKEFILE_LIST)

@ -0,0 +1,47 @@
/*
* 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 <Foundation/Foundation.h>
@interface A : NSObject
@end
@implementation A
+ (void)foo {
if (!(1)) {
NSString* __assert_fn__ =
[NSString stringWithUTF8String:__PRETTY_FUNCTION__];
__assert_fn__ = __assert_fn__ ? __assert_fn__ : @"<Unknown Function>";
NSString* __assert_file__ = [NSString stringWithUTF8String:"A.m"];
__assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>";
[[NSAssertionHandler currentHandler]
handleFailureInFunction:__assert_fn__
file:__assert_file__
lineNumber:23
description:(@"Hello")];
}
}
+ (void)bar {
int phoneNumberError = 5;
int PhoneNumberNoError = 10;
if (!((phoneNumberError != PhoneNumberNoError))) {
NSString* __assert_file__ = [NSString stringWithUTF8String:"A.m"];
__assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>";
[[NSAssertionHandler currentHandler]
handleFailureInMethod:_cmd
object:self
file:__assert_file__
lineNumber:12
description:(@"Invalid parameter not satisfying: %@"),
@"phoneNumberError != PhoneNumberNoError"];
}
}
@end
Loading…
Cancel
Save