diff options
author | manu <manu@138bc75d-0d04-0410-961f-82ee72b054a4> | 2015-08-05 17:36:29 +0000 |
---|---|---|
committer | manu <manu@138bc75d-0d04-0410-961f-82ee72b054a4> | 2015-08-05 17:36:29 +0000 |
commit | 254d68a930fa4cd7a429910706454830d6b7ef07 (patch) | |
tree | 95eb22912610de69685ef6ec213edea83c4c6c6d /gcc/gimple.c | |
parent | 373f5172dd219254b2f0fb6ce3553c25eb98c61e (diff) | |
download | gcc-254d68a930fa4cd7a429910706454830d6b7ef07.tar.gz |
gcc/ChangeLog:
2015-08-05 Manuel López-Ibáñez <manu@gcc.gnu.org>
Jeff Law <law@redhat.com>
PR c/16351
* doc/invoke.texi (Wnull-dereference): New.
* tree-vrp.c (infer_value_range): Update call to infer_nonnull_range.
* gimple-ssa-isolate-paths.c (find_implicit_erroneous_behaviour):
Warn for potential NULL dereferences.
(find_explicit_erroneous_behaviour): Warn for NULL dereferences.
* ubsan.c (instrument_nonnull_arg): Call
infer_nonnull_range_by_attribute.
(instrument_nonnull_return): Likewise.
* common.opt (Wnull-dereference); New.
* gimple.c (infer_nonnull_range): Remove bool arguments.
(infer_nonnull_range_by_dereference): New.
(infer_nonnull_range_by_attribute): New.
* gimple.h: Update declarations.
gcc/testsuite/ChangeLog:
2015-08-05 Manuel López-Ibáñez <manu@gcc.gnu.org>
Jeff Law <law@redhat.com>
PR c/16351
* gcc.dg/tree-ssa/isolate-2.c: Close comment.
* gcc.dg/tree-ssa/isolate-4.c: Likewise.
* gcc.dg/tree-ssa/wnull-dereference.c: New test.
* gcc.dg/tree-ssa/isolate-1.c: Test warnings with -Wnull-dereference.
* gcc.dg/tree-ssa/isolate-3.c: Likewise.
* gcc.dg/tree-ssa/isolate-5.c: Likewise.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@226640 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/gimple.c')
-rw-r--r-- | gcc/gimple.c | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/gcc/gimple.c b/gcc/gimple.c index 89291b07cf4..e31a2731727 100644 --- a/gcc/gimple.c +++ b/gcc/gimple.c @@ -2618,16 +2618,20 @@ check_loadstore (gimple, tree op, tree, void *data) return false; } -/* If OP can be inferred to be non-NULL after STMT executes, return true. - DEREFERENCE is TRUE if we can use a pointer dereference to infer a - non-NULL range, FALSE otherwise. - - ATTRIBUTE is TRUE if we can use attributes to infer a non-NULL range - for function arguments and return values. FALSE otherwise. */ +/* Return true if OP can be inferred to be non-NULL after STMT executes, + either by using a pointer dereference or attributes. */ +bool +infer_nonnull_range (gimple stmt, tree op) +{ + return infer_nonnull_range_by_dereference (stmt, op) + || infer_nonnull_range_by_attribute (stmt, op); +} +/* Return true if OP can be inferred to be non-NULL after STMT + executes by using a pointer dereference. */ bool -infer_nonnull_range (gimple stmt, tree op, bool dereference, bool attribute) +infer_nonnull_range_by_dereference (gimple stmt, tree op) { /* We can only assume that a pointer dereference will yield non-NULL if -fdelete-null-pointer-checks is enabled. */ @@ -2636,13 +2640,26 @@ infer_nonnull_range (gimple stmt, tree op, bool dereference, bool attribute) || gimple_code (stmt) == GIMPLE_ASM) return false; - if (dereference - && walk_stmt_load_store_ops (stmt, (void *)op, - check_loadstore, check_loadstore)) + if (walk_stmt_load_store_ops (stmt, (void *)op, + check_loadstore, check_loadstore)) return true; - if (attribute - && is_gimple_call (stmt) && !gimple_call_internal_p (stmt)) + return false; +} + +/* Return true if OP can be inferred to be a non-NULL after STMT + executes by using attributes. */ +bool +infer_nonnull_range_by_attribute (gimple stmt, tree op) +{ + /* We can only assume that a pointer dereference will yield + non-NULL if -fdelete-null-pointer-checks is enabled. */ + if (!flag_delete_null_pointer_checks + || !POINTER_TYPE_P (TREE_TYPE (op)) + || gimple_code (stmt) == GIMPLE_ASM) + return false; + + if (is_gimple_call (stmt) && !gimple_call_internal_p (stmt)) { tree fntype = gimple_call_fntype (stmt); tree attrs = TYPE_ATTRIBUTES (fntype); @@ -2681,13 +2698,12 @@ infer_nonnull_range (gimple stmt, tree op, bool dereference, bool attribute) /* If this function is marked as returning non-null, then we can infer OP is non-null if it is used in the return statement. */ - if (attribute) - if (greturn *return_stmt = dyn_cast <greturn *> (stmt)) - if (gimple_return_retval (return_stmt) - && operand_equal_p (gimple_return_retval (return_stmt), op, 0) - && lookup_attribute ("returns_nonnull", - TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl)))) - return true; + if (greturn *return_stmt = dyn_cast <greturn *> (stmt)) + if (gimple_return_retval (return_stmt) + && operand_equal_p (gimple_return_retval (return_stmt), op, 0) + && lookup_attribute ("returns_nonnull", + TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl)))) + return true; return false; } |