From patchwork Wed Feb 6 20:36:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1037741 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-495410-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="gOW3+WWR"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43vsQ71RKMz9s6w for ; Thu, 7 Feb 2019 06:46:19 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; q=dns; s=default; b=ocUosKhT4pCm 6lOohPqc7zWyZqNEvFgajv4rKoHEs2iAaEmbNVf78jfr+W4Ifza/B956kNxYEOad 7xh1OwzuWO/Aa+RpUktIanxX6StqGYfDESaQ2xzhmbVDG2uXqzpb8oIkaFKVPKdt m8NV9yO0fk7eQvmoZCkzc9Nl6nby+ZI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; s=default; bh=gGkBlIy9V0Ko8U+5Ou PINxqmKpE=; b=gOW3+WWRJKDwVFWefd5knuFlpSI1gLPgva6ADlHoPJnBg9hfO7 mjEEpTqapYOnBO5AHAjZmWX/eKP+vcrbErp51IYnRcQ+a9Vt41+6+eUzlFjGgu5E s77MfOfTiSDlS2pYht/iAqUE+TQC0jZZRD504peA+V7WJUd2LTsXTKrrw= Received: (qmail 22130 invoked by alias); 6 Feb 2019 19:46:09 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 21968 invoked by uid 89); 6 Feb 2019 19:46:04 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=1853, peculiar, sk:convers X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 06 Feb 2019 19:46:02 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5635C89AF1 for ; Wed, 6 Feb 2019 19:45:58 +0000 (UTC) Received: from c64.redhat.com (ovpn-112-17.phx2.redhat.com [10.3.112.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id 222D718AAC; Wed, 6 Feb 2019 19:45:56 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [committed] Fix locations in conversion_null_warnings (PR c++/71302) Date: Wed, 6 Feb 2019 15:36:04 -0500 Message-Id: <1549485364-39510-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes 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. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu; adds 21 PASS results to g++.sum. Committed to trunk as r268589. 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. --- gcc/cp/call.c | 24 ++++++++--- .../cpp0x/Wzero-as-null-pointer-constant-1.C | 8 ++-- .../g++.dg/warn/Wzero-as-null-pointer-constant-1.C | 36 ++++++++--------- .../g++.dg/warn/Wzero-as-null-pointer-constant-5.C | 2 +- .../g++.dg/warn/Wzero-as-null-pointer-constant-7.C | 4 +- .../g++.dg/warn/Wzero-as-null-pointer-constant-8.C | 47 ++++++++++++++++++++++ 6 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-8.C diff --git a/gcc/cp/call.c b/gcc/cp/call.c index c74d1b4..0f0a1f5 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -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); } } diff --git a/gcc/testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C b/gcc/testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C index a9dd155..e280a05 100644 --- a/gcc/testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C +++ b/gcc/testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C @@ -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" } diff --git a/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-1.C b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-1.C index d0f62b2..5047a61 100644 --- a/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-1.C +++ b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-1.C @@ -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" } diff --git a/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-5.C b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-5.C index 4269bed..01662f5 100644 --- a/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-5.C +++ b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-5.C @@ -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" } diff --git a/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C index 0d06dbf..571a4cf 100644 --- a/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C +++ b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C @@ -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" } } diff --git a/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-8.C b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-8.C new file mode 100644 index 0000000..18257f1 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-8.C @@ -0,0 +1,47 @@ +// PR c++/71302 +// { dg-options "-Wzero-as-null-pointer-constant -fdiagnostics-show-caret" } + +#include + +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 +void +callee_2 (int param1, T* param2, int param3) {} + +void +test_2 (int param1, const char* param2, int param3) +{ + callee_2 (0, 0, 0); // { dg-warning "29: zero as null pointer constant" } + /* { dg-begin-multiline-output "" } + callee_2 (0, 0, 0); + ^ + { dg-end-multiline-output "" } */ + + callee_2 (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; +}