From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 9EDDB3951438; Wed, 16 Sep 2020 19:22:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9EDDB3951438 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1600284120; bh=anPBt3KDPjURsL+AGRqJ7MNeC59Vnj7K4aIVfulmcao=; h=From:To:Subject:Date:From; b=HDGELXVZCEOQphwMY+5c6Y8o1BB7TjfENMcU2ntBPv4rYWaITX3SGm0aVSViWFimz oQj8wLO1ObOWzEMn1vrPuU6kJsu69grVNxkoTjDPIkCarvQqI2awh7IImY7LKeswoV aQF2UZ+WOFJYgXWZNfKgXp3syV9enFGI9cHLW+qc= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r9-8897] c++: Avoid strict_aliasing_warning on dependent types or expressions [PR94951] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-9 X-Git-Oldrev: 79002374e81588fa1052c21f3d397cf9ad61f97d X-Git-Newrev: eca1dbea010ba4c18ccec90bacbad5d8ec72c09f Message-Id: <20200916192200.9EDDB3951438@sourceware.org> Date: Wed, 16 Sep 2020 19:22:00 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Sep 2020 19:22:00 -0000 https://gcc.gnu.org/g:eca1dbea010ba4c18ccec90bacbad5d8ec72c09f commit r9-8897-geca1dbea010ba4c18ccec90bacbad5d8ec72c09f Author: Jakub Jelinek Date: Wed May 6 23:38:13 2020 +0200 c++: Avoid strict_aliasing_warning on dependent types or expressions [PR94951] The following testcase gets a bogus warning during build_base_path, when cp_build_indirect_ref* calls strict_aliasing_warning with a dependent expression. IMHO calling get_alias_set etc. on dependent types feels wrong to me, we should just defer the warnings in those cases until instantiation and only handle the cases where neither type nor expr are dependent. 2020-05-06 Jakub Jelinek PR c++/94951 * typeck.c (cp_strict_aliasing_warning): New function. (cp_build_indirect_ref_1, build_reinterpret_cast_1): Use it instead of strict_aliasing_warning. * g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C: New test. (cherry picked from commit d82414ebcf7716ea24688510594a2c464a105908) Diff: --- gcc/cp/typeck.c | 28 +++++++++++++++++----- .../g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C | 12 ++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 2169f8c4efd..ab703a3ff07 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -3238,6 +3238,22 @@ build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring, return rval; } +/* Like c-family strict_aliasing_warning, but don't warn for dependent + types or expressions. */ + +static bool +cp_strict_aliasing_warning (location_t loc, tree type, tree expr) +{ + if (processing_template_decl) + { + tree e = expr; + STRIP_NOPS (e); + if (dependent_type_p (type) || type_dependent_expression_p (e)) + return false; + } + return strict_aliasing_warning (loc, type, expr); +} + /* The implementation of the above, and of indirection implied by other constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */ @@ -3280,10 +3296,10 @@ cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring, /* If a warning is issued, mark it to avoid duplicates from the backend. This only needs to be done at warn_strict_aliasing > 2. */ - if (warn_strict_aliasing > 2) - if (strict_aliasing_warning (EXPR_LOCATION (ptr), - type, TREE_OPERAND (ptr, 0))) - TREE_NO_WARNING (ptr) = 1; + if (warn_strict_aliasing > 2 + && cp_strict_aliasing_warning (EXPR_LOCATION (ptr), + type, TREE_OPERAND (ptr, 0))) + TREE_NO_WARNING (ptr) = 1; } if (VOID_TYPE_P (t)) @@ -7535,7 +7551,7 @@ build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p, expr = cp_build_addr_expr (expr, complain); if (warn_strict_aliasing > 2) - strict_aliasing_warning (EXPR_LOCATION (expr), type, expr); + cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr); if (expr != error_mark_node) expr = build_reinterpret_cast_1 @@ -7648,7 +7664,7 @@ build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p, if (warn_strict_aliasing <= 2) /* strict_aliasing_warning STRIP_NOPs its expr. */ - strict_aliasing_warning (EXPR_LOCATION (expr), type, expr); + cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr); return build_nop_reinterpret (type, expr); } diff --git a/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C b/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C new file mode 100644 index 00000000000..d0375042e42 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C @@ -0,0 +1,12 @@ +// PR c++/94951 +// { dg-do compile } +// { dg-options "-O2 -Wall" } + +struct A { int a; }; +template +struct B : public A +{ + static B foo () { B t; t.a = 4; return t; } // { dg-bogus "dereferencing type-punned pointer will break strict-aliasing rules" } +}; + +B<0> b = B<0>::foo ();