From patchwork Mon Apr 9 20:21:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 896405 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-476084-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="k3jeEvgx"; 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 40KhYG6kg3z9s08 for ; Tue, 10 Apr 2018 06:22:10 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=C5GNzEC085dtefGR15UGDeLTcvP06qSfZEg/j5LF+2g+81 TGc+xU482li/l7RqegcAOPg0dJxoVoCvnNzJjHblBkj44+4jj+hLuaBWWb24iBaS 5yKmfeYpMbEh0wiYFQqLl/KEpd3lSql+ylOovaK4uOFSG0NKBZ//3AipDnx7Q= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=aGPKpI9yikrqp4q+xFQ0rcy5e5U=; b=k3jeEvgxOhbPEAf8MU4t j9sS3TJfNoGBpQtESGx5eMkMPqvOdsN+1cSKWl8Uq2bbeck09PXo1TeU67ogTeip L3dnFMlBjSiP0ULgm9WS/Hgt/mAkoJ3GnpVF/FlPGz1c2r4pVGm6W4swaku6W+WZ fDH5Y8gLWwSCqvxEhJISW80= Received: (qmail 92493 invoked by alias); 9 Apr 2018 20:22:03 -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 92480 invoked by uid 89); 9 Apr 2018 20:22:02 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 spammy= X-HELO: mail-ot0-f172.google.com Received: from mail-ot0-f172.google.com (HELO mail-ot0-f172.google.com) (74.125.82.172) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 09 Apr 2018 20:22:01 +0000 Received: by mail-ot0-f172.google.com with SMTP id v6-v6so1563423otj.12 for ; Mon, 09 Apr 2018 13:22:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=RSLZo863sLppNIem4+Aw/0/4XCwrvs4yi0eaY27SPMc=; b=WEMOaEUtU2tqOVC1jbwYavtH0dw3Duy36sjKXxUlDxJr3JfAzdKR+UzmG1C/PXZsVw DWql1Kg+nvlAv1ZDG2CPSlKeV5J8aTE1WS/0z/qJ4099H+wUmgNtgoP/TYtw0Sup9YBi meRg7aUHh9XeGGp3W0eWAiJKZbU1ooCd06hfau/5HcgGEWM9NxryC2wrX0MkBGGpWYOn YrQZTOtBdd8Fu71x4s9F/0cyqgSdB4RBcJd8lAak6VnTyk5ooVzoAn3JE7zNOI/0NwnI QvaqEABHjW4UldCZyX70xD/8IcP2sUm3DkM/m1DI5gJB0x3W7AT5ryvqk3loSxJBJ0fd MhHQ== X-Gm-Message-State: ALQs6tCmrgPWtxkdxpzRtJC1xdA2McxfU9sOJYT5tJq54uzrl+uCOXOt TCFYlTQqib1yvxwam6NNQbqSRnODxueTVnYbkZYXbEcp X-Google-Smtp-Source: AIpwx49QSicGjYTG6SLyGE5jjIhdSri4zWMT+KxDPOmlBB8vBy9nsrUOoooTWnZlBvn+ZERAEPnrROkx6OZCRpTfm/U= X-Received: by 2002:a9d:29ae:: with SMTP id n43-v6mr8135915otb.313.1523305319572; Mon, 09 Apr 2018 13:21:59 -0700 (PDT) MIME-Version: 1.0 Received: by 10.201.7.234 with HTTP; Mon, 9 Apr 2018 13:21:38 -0700 (PDT) From: Jason Merrill Date: Mon, 9 Apr 2018 16:21:38 -0400 Message-ID: Subject: C++ PATCH for c++/85277, ICE with invalid offsetof To: gcc-patches List X-IsSubscribed: yes Here, the code was improperly assuming that the member argument must be a decl at the point of the error, but since we stopped folding foo[0] to foo, in this case it's an INDIRECT_REF. And the existing error for INDIRECT_REF is inaccurate for this case, so let's stick to complaining about asking for offsetof a function. While I was here, I updated the non-standard-layout diagnostic to say "conditionally-supported" rather than undefined. Tested x86_64-cp-linux-gnu, applying to trunk. commit cb3221a892dc8ea6c06d148b3756d82067644869 Author: Jason Merrill Date: Mon Apr 9 12:51:38 2018 -0400 PR c++/85277 - ICE with invalid offsetof. * semantics.c (finish_offsetof): Avoid passing non-DECL to %qD. Adjust -Winvalid-offsetof diagnostic to say conditionally supported. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 59cac77f6b7..8c893ed64b0 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -4043,17 +4043,17 @@ finish_offsetof (tree object_ptr, tree expr, location_t loc) || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE || TREE_TYPE (expr) == unknown_type_node) { - if (INDIRECT_REF_P (expr)) - error ("second operand of % is neither a single " - "identifier nor a sequence of member accesses and " - "array references"); - else + while (TREE_CODE (expr) == COMPONENT_REF + || TREE_CODE (expr) == COMPOUND_EXPR) + expr = TREE_OPERAND (expr, 1); + + if (DECL_P (expr)) { - if (TREE_CODE (expr) == COMPONENT_REF - || TREE_CODE (expr) == COMPOUND_EXPR) - expr = TREE_OPERAND (expr, 1); error ("cannot apply % to member function %qD", expr); + inform (DECL_SOURCE_LOCATION (expr), "declared here"); } + else + error ("cannot apply % to member function"); return error_mark_node; } if (TREE_CODE (expr) == CONST_DECL) @@ -4069,9 +4069,9 @@ finish_offsetof (tree object_ptr, tree expr, location_t loc) && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr))) && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr))) && cp_unevaluated_operand == 0) - pedwarn (loc, OPT_Winvalid_offsetof, - "offsetof within non-standard-layout type %qT is undefined", - TREE_TYPE (TREE_TYPE (object_ptr))); + warning_at (loc, OPT_Winvalid_offsetof, "offsetof within " + "non-standard-layout type %qT is conditionally-supported", + TREE_TYPE (TREE_TYPE (object_ptr))); return fold_offsetof (expr); } diff --git a/gcc/testsuite/g++.dg/ext/builtin-offsetof1.C b/gcc/testsuite/g++.dg/ext/builtin-offsetof1.C index 5c5e9cf246b..cbc2daafbdd 100644 --- a/gcc/testsuite/g++.dg/ext/builtin-offsetof1.C +++ b/gcc/testsuite/g++.dg/ext/builtin-offsetof1.C @@ -1,9 +1,11 @@ // PR c++/51413 -// { dg-options "-w" } +// PR c++/85277 +// { dg-options "-Wno-pointer-arith" } struct A { static void foo(); }; -int i = __builtin_offsetof(A, foo[1]); // { dg-error "neither a single identifier nor a sequence of member accesses and array references" } +int i = __builtin_offsetof(A, foo[1]); // { dg-error "offsetof" } +int j = __builtin_offsetof(volatile A, foo[0]); // { dg-error "offsetof" } diff --git a/gcc/testsuite/g++.dg/other/offsetof3.C b/gcc/testsuite/g++.dg/other/offsetof3.C index 8d982426560..779fc72670a 100644 --- a/gcc/testsuite/g++.dg/other/offsetof3.C +++ b/gcc/testsuite/g++.dg/other/offsetof3.C @@ -12,4 +12,4 @@ protected: typedef X* pX; typedef __SIZE_TYPE__ size_t; -size_t yoff = __builtin_offsetof (X, y); /* { dg-error "35:non-standard-layout" } */ +size_t yoff = __builtin_offsetof (X, y); /* { dg-message "35:non-standard-layout" } */ diff --git a/gcc/testsuite/g++.dg/other/offsetof5.C b/gcc/testsuite/g++.dg/other/offsetof5.C index 86b14488246..8514af087ad 100644 --- a/gcc/testsuite/g++.dg/other/offsetof5.C +++ b/gcc/testsuite/g++.dg/other/offsetof5.C @@ -9,14 +9,14 @@ struct A int &i; }; -int j = offsetof (A, i); // { dg-error "offsetof" } +int j = offsetof (A, i); // { dg-message "offsetof" } template struct S { T h; T &i; - static const int j = offsetof (S, i); // { dg-error "offsetof" } + static const int j = offsetof (S, i); // { dg-message "offsetof" } }; int k = S::j; // { dg-message "required from here" } diff --git a/gcc/testsuite/g++.dg/other/offsetof8.C b/gcc/testsuite/g++.dg/other/offsetof8.C index 0668199b366..211c5127026 100644 --- a/gcc/testsuite/g++.dg/other/offsetof8.C +++ b/gcc/testsuite/g++.dg/other/offsetof8.C @@ -9,4 +9,4 @@ struct B: virtual A { }; int a[] = { !&((B*)0)->i, // { dg-error "invalid access to non-static data member" } __builtin_offsetof (B, i) // { dg-error "invalid access to non-static" } -}; // { dg-error "offsetof within non-standard-layout type" "" { target *-*-* } .-1 } +}; // { dg-message "offsetof within non-standard-layout type" "" { target *-*-* } .-1 }