Summary: Pretty basic: warn when we see an assignment instruction `x = ...` and `x` is not live in the post of the instruction. Only enabled for Clang at the moment because linters already warn on this for Java. But we can enable it later if we want to (should be fully generic). Reviewed By: jeremydubreil Differential Revision: D5450439 fbshipit-source-id: 693514cmaster
parent
1fb9fb48f5
commit
73f3eee9cd
@ -0,0 +1,20 @@
|
||||
# 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
|
||||
# see explanations in cpp/errors/Makefile for the custom isystem
|
||||
CLANG_OPTIONS = -x c++ -std=c++11 -nostdinc++ -isystem$(MODELS_DIR)/cpp/include -isystem$(CLANG_INCLUDES)/c++/v1/ -c
|
||||
INFER_OPTIONS = --ml-buckets cpp --debug-exceptions --project-root $(TESTS_DIR) --no-failures-allowed
|
||||
INFERPRINT_OPTIONS = --issues-tests
|
||||
|
||||
SOURCES = $(wildcard *.cpp)
|
||||
|
||||
include $(TESTS_DIR)/clang.make
|
||||
|
||||
infer-out/report.json: $(MAKEFILE_LIST)
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace dead_stores {
|
||||
|
||||
void easy_bad() { int x = 5; }
|
||||
|
||||
void reassign_param_bad(int x) { x = 5; }
|
||||
|
||||
int dead_then_live_bad() {
|
||||
int x = 5;
|
||||
x = 3;
|
||||
return x;
|
||||
}
|
||||
|
||||
int use_then_dead_bad() {
|
||||
int x = 5;
|
||||
int y = x;
|
||||
x = 7;
|
||||
return y;
|
||||
}
|
||||
|
||||
void dead_pointer_bad() {
|
||||
int num = 2;
|
||||
int* x = #
|
||||
}
|
||||
|
||||
int return_ok() {
|
||||
int x = 5;
|
||||
return x;
|
||||
}
|
||||
|
||||
int branch_ok(bool b) {
|
||||
int x = 5;
|
||||
int y = 3;
|
||||
if (b) {
|
||||
y = x;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
int loop_ok(bool b) {
|
||||
int x = 5;
|
||||
int y = 3;
|
||||
while (b) {
|
||||
y = x;
|
||||
b = false;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
int loop_break_ok(bool b) {
|
||||
int x = 5;
|
||||
while (b) {
|
||||
x = 3;
|
||||
break;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int loop_continue_ok(bool b) {
|
||||
int x = 5;
|
||||
int y = 2;
|
||||
while (b) {
|
||||
y = x;
|
||||
x = 3;
|
||||
continue;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
void assign_pointer1_ok(int* ptr) { *ptr = 7; }
|
||||
|
||||
int* assign_pointer2_ok() {
|
||||
int num = 2;
|
||||
int* ptr = #
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void by_ref_ok(int& ref) { ref = 7; }
|
||||
|
||||
char* global;
|
||||
|
||||
void FP_assign_array_tricky_ok() {
|
||||
char arr[1];
|
||||
global = arr;
|
||||
*(int*)arr = 123; // think this is a bug in the frontend... this instruction
|
||||
// looks like &arr:int = 123
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
codetoanalyze/cpp/liveness/dead_stores.cpp, dead_stores::FP_assign_array_tricky_ok, 3, DEAD_STORE, [Write of unused value]
|
||||
codetoanalyze/cpp/liveness/dead_stores.cpp, dead_stores::dead_pointer_bad, 2, DEAD_STORE, [Write of unused value]
|
||||
codetoanalyze/cpp/liveness/dead_stores.cpp, dead_stores::dead_then_live_bad, 1, DEAD_STORE, [Write of unused value]
|
||||
codetoanalyze/cpp/liveness/dead_stores.cpp, dead_stores::easy_bad, 0, DEAD_STORE, [Write of unused value]
|
||||
codetoanalyze/cpp/liveness/dead_stores.cpp, dead_stores::reassign_param_bad, 0, DEAD_STORE, [Write of unused value]
|
||||
codetoanalyze/cpp/liveness/dead_stores.cpp, dead_stores::use_then_dead_bad, 3, DEAD_STORE, [Write of unused value]
|
Loading…
Reference in new issue