diff options
author | Martin Sebor <msebor@redhat.com> | 2020-06-28 14:22:14 -0600 |
---|---|---|
committer | Martin Sebor <msebor@redhat.com> | 2020-06-28 14:24:52 -0600 |
commit | 75ff24e1920ea6b198350a2961e23175e6108e75 (patch) | |
tree | 88dfd1e43a652ff5f769d9543cd22d764cbe29e3 /gcc/tree-ssa-ccp.c | |
parent | ce56fd949f359a62b86a45aaf975ac2ecc48fa64 (diff) | |
download | gcc-75ff24e1920ea6b198350a2961e23175e6108e75.zip gcc-75ff24e1920ea6b198350a2961e23175e6108e75.tar.gz gcc-75ff24e1920ea6b198350a2961e23175e6108e75.tar.bz2 |
Underline argument in -Wnonnull and in C++ extend warning to the this pointer [PR c++/86568].
Resolves:
PR c++/86568 - -Wnonnull warnings should highlight the relevant argument not the closing parenthesis
gcc/c-family/ChangeLog:
PR c++/86568
* c-common.c (struct nonnull_arg_ctx): Add members.
(check_function_nonnull): Use nonnull_arg_ctx as argument. Handle
C++ member functions specially. Consider the this pointer implicitly
nonnull.
(check_nonnull_arg): Use location of argument when available.
(check_function_arguments): Use nonnull_arg_ctx as argument.
gcc/ChangeLog:
PR c++/86568
* calls.c (maybe_warn_rdwr_sizes): Use location of argument if
available.
* tree-ssa-ccp.c (pass_post_ipa_warn::execute): Same. Adjust
indentation.
* tree.c (get_nonnull_args): Consider the this pointer implicitly
nonnull.
* var-tracking.c (deps_vec): New type.
(var_loc_dep_vec): New function.
(VAR_LOC_DEP_VEC): Use it.
gcc/testsuite/ChangeLog:
PR c++/86568
* g++.dg/warn/Wnonnull5.C: New test.
* c-c++-common/pr28656.c: Adjust text of expected warning.
* c-c++-common/pr66208.c: Same.
* g++.dg/cpp0x/nullptr22.C: Same.
* g++.dg/ext/attr-nonnull.C: Same.
* g++.dg/ext/attrib49.C: Same.
* g++.dg/pr71973-2.C: Same.
* g++.dg/warn/Wnonnull3.C: Same.
* g++.dg/warn/Wnonnull4.C: Same.
* obj-c++.dg/attributes/method-nonnull-1.mm: Same.
* objc.dg/attributes/method-nonnull-1.m: Same.
Diffstat (limited to 'gcc/tree-ssa-ccp.c')
-rw-r--r-- | gcc/tree-ssa-ccp.c | 81 |
1 files changed, 48 insertions, 33 deletions
diff --git a/gcc/tree-ssa-ccp.c b/gcc/tree-ssa-ccp.c index f7a2795..e8333ac 100644 --- a/gcc/tree-ssa-ccp.c +++ b/gcc/tree-ssa-ccp.c @@ -3540,43 +3540,58 @@ pass_post_ipa_warn::execute (function *fun) if (!is_gimple_call (stmt) || gimple_no_warning_p (stmt)) continue; - if (warn_nonnull) + tree fntype = gimple_call_fntype (stmt); + bitmap nonnullargs = get_nonnull_args (fntype); + if (!nonnullargs) + continue; + + tree fndecl = gimple_call_fndecl (stmt); + + for (unsigned i = 0; i < gimple_call_num_args (stmt); i++) { - bitmap nonnullargs - = get_nonnull_args (gimple_call_fntype (stmt)); - if (nonnullargs) + tree arg = gimple_call_arg (stmt, i); + if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE) + continue; + if (!integer_zerop (arg)) + continue; + if (!bitmap_empty_p (nonnullargs) + && !bitmap_bit_p (nonnullargs, i)) + continue; + + /* In C++ non-static member functions argument 0 refers + to the implicit this pointer. Use the same one-based + numbering for ordinary arguments. */ + unsigned argno = TREE_CODE (fntype) == METHOD_TYPE ? i : i + 1; + location_t loc = (EXPR_HAS_LOCATION (arg) + ? EXPR_LOCATION (arg) + : gimple_location (stmt)); + auto_diagnostic_group d; + if (argno == 0) { - for (unsigned i = 0; i < gimple_call_num_args (stmt); i++) - { - tree arg = gimple_call_arg (stmt, i); - if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE) - continue; - if (!integer_zerop (arg)) - continue; - if (!bitmap_empty_p (nonnullargs) - && !bitmap_bit_p (nonnullargs, i)) - continue; - - location_t loc = gimple_location (stmt); - auto_diagnostic_group d; - if (warning_at (loc, OPT_Wnonnull, - "%Gargument %u null where non-null " - "expected", stmt, i + 1)) - { - tree fndecl = gimple_call_fndecl (stmt); - if (fndecl && DECL_IS_BUILTIN (fndecl)) - inform (loc, "in a call to built-in function %qD", - fndecl); - else if (fndecl) - inform (DECL_SOURCE_LOCATION (fndecl), - "in a call to function %qD declared here", - fndecl); - - } - } - BITMAP_FREE (nonnullargs); + if (warning_at (loc, OPT_Wnonnull, + "%G%qs pointer null", stmt, "this") + && fndecl) + inform (DECL_SOURCE_LOCATION (fndecl), + "in a call to non-static member function %qD", + fndecl); + continue; } + + if (!warning_at (loc, OPT_Wnonnull, + "%Gargument %u null where non-null " + "expected", stmt, argno)) + continue; + + tree fndecl = gimple_call_fndecl (stmt); + if (fndecl && DECL_IS_BUILTIN (fndecl)) + inform (loc, "in a call to built-in function %qD", + fndecl); + else if (fndecl) + inform (DECL_SOURCE_LOCATION (fndecl), + "in a call to function %qD declared %qs", + fndecl, "nonnull"); } + BITMAP_FREE (nonnullargs); } } return 0; |