Fix locations in conversion_null_warnings (PR c++/71302)

PR c++/71302 reports that g++ shows poor locations for
-Wzero-as-null-pointer-constant for pointers in function calls,
using the close parenthesis of the call, rather than showing the
pertinent argument.

This particular case was fixed in GCC 8, but regressed on trunk
in r260973.

This patch fixes the regression, and adds column numbers to the
test cases (where they're correct) to avoid regressing them in the
future.  There are still various places where the locations aren't
correct, but fixing them isn't stage 4 material.

gcc/cp/ChangeLog:
	PR c++/71302
	* call.c (get_location_for_expr_unwinding_for_system_header): New
	function.
	(conversion_null_warnings): Use it when getting locations for
	EXPR, effectively adding a call to
	get_location_for_expr_unwinding_for_system_header for
	-Wconversion-null and making use of EXPR_LOCATION for
	-Wzero-as-null-pointer-constant.

gcc/testsuite/ChangeLog:
	PR c++/71302
	* g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C: Add expected
	column numbers to dg-warning directives where they are correct.
	* g++.dg/warn/Wzero-as-null-pointer-constant-5.C: Likewise.
	* g++.dg/warn/Wzero-as-null-pointer-constant-7.C: Likewise.
	* g++.dg/warn/Wzero-as-null-pointer-constant-8.C: New test.

From-SVN: r268589
This commit is contained in:
David Malcolm 2019-02-06 19:44:52 +00:00 committed by David Malcolm
parent d0307ff2cd
commit aa9dc19314
8 changed files with 111 additions and 30 deletions

View file

@ -1,3 +1,14 @@
2019-02-06 David Malcolm <dmalcolm@redhat.com>
PR c++/71302
* call.c (get_location_for_expr_unwinding_for_system_header): New
function.
(conversion_null_warnings): Use it when getting locations for
EXPR, effectively adding a call to
get_location_for_expr_unwinding_for_system_header for
-Wconversion-null and making use of EXPR_LOCATION for
-Wzero-as-null-pointer-constant.
2019-02-05 Jakub Jelinek <jakub@redhat.com>
PR c++/89187

View file

@ -6718,6 +6718,22 @@ build_temp (tree expr, tree type, int flags,
return expr;
}
/* Get any location for EXPR, falling back to input_location.
If the result is in a system header and is the virtual location for
a token coming from the expansion of a macro, unwind it to the
location of the expansion point of the macro (e.g. to avoid the
diagnostic being suppressed for expansions of NULL where "NULL" is
in a system header). */
static location_t
get_location_for_expr_unwinding_for_system_header (tree expr)
{
location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
loc = expansion_point_location_if_in_system_header (loc);
return loc;
}
/* Perform warnings about peculiar, but valid, conversions from/to NULL.
Also handle a subset of zero as null warnings.
EXPR is implicitly converted to type TOTYPE.
@ -6730,8 +6746,7 @@ conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
if (null_node_p (expr) && TREE_CODE (totype) != BOOLEAN_TYPE
&& ARITHMETIC_TYPE_P (totype))
{
location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
loc = expansion_point_location_if_in_system_header (loc);
location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
if (fn)
{
auto_diagnostic_group d;
@ -6750,7 +6765,7 @@ conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
&& TYPE_PTR_P (totype))
{
location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
if (fn)
{
auto_diagnostic_group d;
@ -6769,8 +6784,7 @@ conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
else if (null_ptr_cst_p (expr) &&
(TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype)))
{
location_t loc =
expansion_point_location_if_in_system_header (input_location);
location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
maybe_warn_zero_as_null_pointer_constant (expr, loc);
}
}

View file

@ -1,3 +1,12 @@
2019-02-06 David Malcolm <dmalcolm@redhat.com>
PR c++/71302
* g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C: Add expected
column numbers to dg-warning directives where they are correct.
* g++.dg/warn/Wzero-as-null-pointer-constant-5.C: Likewise.
* g++.dg/warn/Wzero-as-null-pointer-constant-7.C: Likewise.
* g++.dg/warn/Wzero-as-null-pointer-constant-8.C: New test.
2019-02-06 Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
PR debug/87451

View file

@ -64,16 +64,16 @@ void f()
if (!p)
;
if (pmf == 0) // { dg-warning "zero as null pointer" }
if (pmf == 0) // { dg-warning "14: zero as null pointer" }
;
if (pdm == 0) // { dg-warning "zero as null pointer" }
if (pdm == 0) // { dg-warning "14: zero as null pointer" }
;
if (pf == 0) // { dg-warning "zero as null pointer" }
if (pf == 0) // { dg-warning "13: zero as null pointer" }
;
if (p == 0) // { dg-warning "zero as null pointer" }
if (p == 0) // { dg-warning "12: zero as null pointer" }
;
if (0 == pmf) // { dg-warning "zero as null pointer" }

View file

@ -13,18 +13,18 @@ int* ps;
void f()
{
pointmemfun pmf(0); // { dg-warning "zero as null pointer" }
pointdmem pdm(0); // { dg-warning "zero as null pointer" }
pointfun pf(0); // { dg-warning "zero as null pointer" }
int* p(0); // { dg-warning "zero as null pointer" }
pointmemfun pmf(0); // { dg-warning "19: zero as null pointer" }
pointdmem pdm(0); // { dg-warning "19: zero as null pointer" }
pointfun pf(0); // { dg-warning "18: zero as null pointer" }
int* p(0); // { dg-warning "17: zero as null pointer" }
pmf = 0; // { dg-warning "zero as null pointer" }
pmf = 0; // { dg-warning "9: zero as null pointer" }
pdm = 0; // { dg-warning "zero as null pointer" }
pdm = 0; // { dg-warning "9: zero as null pointer" }
pf = 0; // { dg-warning "zero as null pointer" }
pf = 0; // { dg-warning "8: zero as null pointer" }
p = 0; // { dg-warning "zero as null pointer" }
p = 0; // { dg-warning "7: zero as null pointer" }
if (pmf)
;
@ -50,22 +50,22 @@ void f()
if (!p)
;
if (pmf == 0) // { dg-warning "zero as null pointer" }
if (pmf == 0) // { dg-warning "14: zero as null pointer" }
;
if (pdm == 0) // { dg-warning "zero as null pointer" }
if (pdm == 0) // { dg-warning "14: zero as null pointer" }
;
if (pf == 0) // { dg-warning "zero as null pointer" }
if (pf == 0) // { dg-warning "13: zero as null pointer" }
;
if (p == 0) // { dg-warning "zero as null pointer" }
if (p == 0) // { dg-warning "12: zero as null pointer" }
;
if (0 == pmf) // { dg-warning "zero as null pointer" }
if (0 == pmf) // { dg-warning "12: zero as null pointer" }
;
if (0 == pdm) // { dg-warning "zero as null pointer" }
if (0 == pdm) // { dg-warning "12: zero as null pointer" }
;
if (0 == pf) // { dg-warning "zero as null pointer" }
@ -74,16 +74,16 @@ void f()
if (0 == p) // { dg-warning "zero as null pointer" }
;
if (pmf != 0) // { dg-warning "zero as null pointer" }
if (pmf != 0) // { dg-warning "14: zero as null pointer" }
;
if (pdm != 0) // { dg-warning "zero as null pointer" }
if (pdm != 0) // { dg-warning "14: zero as null pointer" }
;
if (pf != 0) // { dg-warning "zero as null pointer" }
if (pf != 0) // { dg-warning "13: zero as null pointer" }
;
if (p != 0) // { dg-warning "zero as null pointer" }
if (p != 0) // { dg-warning "12: zero as null pointer" }
;
if (0 != pmf) // { dg-warning "zero as null pointer" }

View file

@ -3,7 +3,7 @@
struct foo
{
foo(void* a = 0) {}; // { dg-warning "zero as null pointer" }
foo(void* a = 0) {}; // { dg-warning "17: zero as null pointer" }
};
void* fun(void* a = 0) {}; // { dg-warning "zero as null pointer" }

View file

@ -8,6 +8,6 @@ void test01()
char* x(NULL);
char* x2{NULL};
char* x3 = NULL;
char* x4(0); // { dg-warning "zero as null pointer" }
char* x5 = 0; // { dg-warning "zero as null pointer" }
char* x4(0); // { dg-warning "12: zero as null pointer" }
char* x5 = 0; // { dg-warning "14: zero as null pointer" }
}

View file

@ -0,0 +1,47 @@
// PR c++/71302
// { dg-options "-Wzero-as-null-pointer-constant -fdiagnostics-show-caret" }
#include <cstddef>
static void
callee_1 (int param1, const char* param2, int param3) {}
void
test_1 (int param1, const char* param2, int param3)
{
callee_1 (0, 0, 0); // { dg-warning "16: zero as null pointer constant" }
/* { dg-begin-multiline-output "" }
callee_1 (0, 0, 0);
^
{ dg-end-multiline-output "" } */
callee_1 (0, NULL, 0);
}
template <typename T>
void
callee_2 (int param1, T* param2, int param3) {}
void
test_2 (int param1, const char* param2, int param3)
{
callee_2<const char*> (0, 0, 0); // { dg-warning "29: zero as null pointer constant" }
/* { dg-begin-multiline-output "" }
callee_2<const char*> (0, 0, 0);
^
{ dg-end-multiline-output "" } */
callee_2<const char*> (0, NULL, 0);
}
void
test_3 ()
{
const char *msg_a = 0; // { dg-warning "23: zero as null pointer constant" }
/* { dg-begin-multiline-output "" }
const char *msg_a = 0;
^
{ dg-end-multiline-output "" } */
const char *msg_b = NULL;
}