From patchwork Wed Sep 30 19:20:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1374633 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=bVz9a+0o; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4C1mM23lZqz9sSC for ; Thu, 1 Oct 2020 05:21:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C0652386F807; Wed, 30 Sep 2020 19:20:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0652386F807 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1601493659; bh=BmICbtPDuingKX2UBFO2KBEieZXiGTpwdU57autzBOg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=bVz9a+0oxT+eZMmz6dBmaYSDPx/MnOnZSYZJBLa/rX3ch0V8c6PSJ7oalFwdapfbq 8rmYMXKBrWm6umxzqOTGb3HJ1uvzhz2L4QUe0SJtCVrtJtcYAOuuOkt0KC3mZ4GCSu pRDPmskUM9MYdbO9V5sb+XRGdBv7gqnIczf/JtsY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 30B3B385E021 for ; Wed, 30 Sep 2020 19:20:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 30B3B385E021 Received: by mail-ot1-x32e.google.com with SMTP id c2so2950008otp.7 for ; Wed, 30 Sep 2020 12:20:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=BmICbtPDuingKX2UBFO2KBEieZXiGTpwdU57autzBOg=; b=LNw+eAku7L1j69FdMrAsLGFUQzeiBhwoZcNw3c0XVuxhRQRmEiPe40PXb5pf67851P awxa4Vde57r28fzEcottoMZhAwNTvIoZxWheBvDSnMJuhekmBZEJjf8/5iI9rrbFnKtL V/vPb1U2z8Z1BNe3w2lozWkv8T+Lc916Vpfp7H0gcyFbU3LINAKatLmjAofS6aUEs9aK +KOAy6z4DQlBn1nc+VC8QMGbOiwVFdJgYdkSlLV456b9YpHY1b/9hh7N9bEeTZVe6Lcm l5ZUdsWStauBrcQK3SQ/+4vvnODl3y8zfPA65HARzb8aTnaPjyBvLJs+Bzr6PdR4sfG6 Z+yw== X-Gm-Message-State: AOAM530THw4v7588FT79OX9Vi78mtteciCX+hKzSjSkDH8PSGXNwOGax LC2VDDsxnkIbWuQTePdMcXEkVBo8DTM= X-Google-Smtp-Source: ABdhPJzLjnK7VBdhDEO19ZBSKLuGNA1hRS/VAK+LPYJ/unD6jCpCdYdn0MSqRqfSc3APZTLob6mC6w== X-Received: by 2002:a9d:4818:: with SMTP id c24mr2612835otf.128.1601493654452; Wed, 30 Sep 2020 12:20:54 -0700 (PDT) Received: from [192.168.0.41] (97-118-127-189.hlrn.qwest.net. [97.118.127.189]) by smtp.gmail.com with ESMTPSA id r7sm527729oij.34.2020.09.30.12.20.53 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 30 Sep 2020 12:20:53 -0700 (PDT) To: gcc-patches Subject: [committed] fix ICE in attribute access formatting (PR middle-end/97189) Message-ID: Date: Wed, 30 Sep 2020 13:20:53 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Sebor via Gcc-patches From: Martin Sebor Reply-To: Martin Sebor Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Redeclaring a function that takes a VLA parameter with attribute access that references the same parameter can cause conflicts when the two aren't in sync. The conflicts are detected and diagnosed but also have to be resolved. The code wasn't robust enough to handle all cases gracefully, leading to the ICE reported in the PR. After testing on x86_64-linux I have committed in r11-3571 the attached fix to improve it and avoid the ICE. Martin Avoid assuming a VLA access specification string contains a closing bracket (PR middle-end/97189). Resolves: PR middle-end/97189 - ICE on redeclaration of a function with VLA argument and attribute access gcc/ChangeLog: PR middle-end/97189 * attribs.c (attr_access::array_as_string): Avoid assuming a VLA access specification string contains a closing bracket. gcc/c-family/ChangeLog: PR middle-end/97189 * c-attribs.c (append_access_attr): Use the function declaration location for a warning about an attribute access argument. gcc/testsuite/ChangeLog: PR middle-end/97189 * gcc.dg/attr-access-2.c: Adjust caret location. * gcc.dg/Wvla-parameter-6.c: New test. * gcc.dg/Wvla-parameter-7.c: New test. diff --git a/gcc/attribs.c b/gcc/attribs.c index 3f6ec3d3aa3..94b9e02699f 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -2270,11 +2270,11 @@ attr_access::array_as_string (tree type) const bound is nonconstant and whose access string has "$]" in it) extract the bound expression from SIZE. */ const char *p = end; - for ( ; *p-- != ']'; ); + for ( ; p != str && *p-- != ']'; ); if (*p == '$') index_type = build_index_type (TREE_VALUE (size)); } - else if (minsize) + else if (minsize) index_type = build_index_type (size_int (minsize - 1)); tree arat = NULL_TREE; diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c index 70b00037d98..c779d13f023 100644 --- a/gcc/c-family/c-attribs.c +++ b/gcc/c-family/c-attribs.c @@ -4151,18 +4151,12 @@ append_access_attr (tree node[3], tree attrs, const char *attrstr, "missing in previous designation", attrstr); else if (newa->internal_p || cura->internal_p) - { - /* Mismatch in the value of the size argument and a VLA - bound. */ - location_t argloc = curloc; - if (tree arg = get_argument (node[2], newa->sizarg)) - argloc = DECL_SOURCE_LOCATION (arg); - warned = warning_at (argloc, OPT_Wattributes, - "attribute %qs positional argument 2 " - "conflicts with previous designation " - "by argument %u", - attrstr, cura->sizarg + 1); - } + /* Mismatch in the value of the size argument and a VLA bound. */ + warned = warning_at (curloc, OPT_Wattributes, + "attribute %qs positional argument 2 " + "conflicts with previous designation " + "by argument %u", + attrstr, cura->sizarg + 1); else /* Mismatch in the value of the size argument between two explicit access attributes. */ diff --git a/gcc/testsuite/gcc.dg/Wvla-parameter-6.c b/gcc/testsuite/gcc.dg/Wvla-parameter-6.c new file mode 100644 index 00000000000..268aeec9251 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wvla-parameter-6.c @@ -0,0 +1,34 @@ +/* PR middle-end/97189 - ICE on redeclaration of a function with VLA argument + and attribute access + Also verify the right arguments are underlined in the notes. + { dg-do compile } + { dg-options "-Wall -fdiagnostics-show-caret" } */ + +#define RW(...) __attribute__ ((access (read_write, __VA_ARGS__))) + +RW (2, 3) void f1 (int n, int[n], int); +/* { dg-warning "attribute 'access \\(read_write, 2, 3\\)' positional argument 2 conflicts with previous designation by argument 3" "warning" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + RW (2, 3) void f1 (int n, int[n], int); + ^~ + { dg-end-multiline-output "" } + { dg-message "designating the bound of variable length array argument 2" "note" { target *-*-* } .-6 } + { dg-begin-multiline-output "" } + RW (2, 3) void f1 (int n, int[n], int); + ~~~~^ ~~~~~~ + { dg-end-multiline-output "" } */ + + +RW (2) void f2 (int, int[*], int); +/* { dg-message "previously declared as a variable length array 'int\\\[\\\*]'" "note" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + RW (2, 3) void f2 (int, int[], int); + ^~~~~ + { dg-end-multiline-output "" } */ + +RW (2, 3) void f2 (int, int[], int); +/* { dg-warning "argument 2 of type 'int\\\[]' declared as an ordinary array" "warning" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + RW (2) void f2 (int, int[*], int); + ^~~~~~ + { dg-end-multiline-output "" } */ diff --git a/gcc/testsuite/gcc.dg/Wvla-parameter-7.c b/gcc/testsuite/gcc.dg/Wvla-parameter-7.c new file mode 100644 index 00000000000..14ce75f3e2e --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wvla-parameter-7.c @@ -0,0 +1,36 @@ +/* PR middle-end/97189 - ICE on redeclaration of a function with VLA argument + and attribute access + { dg-do compile } + { dg-options "-Wall" } */ + +#define RW(...) __attribute__ ((access (read_write, __VA_ARGS__))) + +RW (2, 3) void f1 (int n, int[n], int); +/* { dg-warning "attribute 'access \\(read_write, 2, 3\\)' positional argument 2 conflicts with previous designation by argument 3" "warning" { target *-*-* } .-1 } + { dg-message "designating the bound of variable length array argument 2" "note" { target *-*-* } .-2 } */ + +void call_f1 (int *p) +{ + /* Verify that a warning is issued. Ideally, it seems the VLA bound + should take precedence over the attribute and the warning would + reference argument 1 but since the conflict in the redeclarations + of the function is already diagnosed don't test that (and let it + be acceptable for this warning to reference argument 3). */ + f1 (-1, p, -1); + // { dg-warning "argument \\d value -1 is negative" "warning" { target *-*-* } .-1 } +} + +RW (2) void f2 (int, int[*], int); +// { dg-message "previously declared as a variable length array 'int\\\[\\\*]'" "note" { target *-*-* } .-1 } +RW (2, 3) void f2 (int, int[], int); +// { dg-warning "argument 2 of type 'int\\\[]' declared as an ordinary array" "warning" { target *-*-* } .-1 } + +void call_f2 (int *p) +{ + f2 (-1, p, 0); + + /* Verify that the attribute access on the redeclaration of f2() takes + precedence over the one on the first declaration. */ + f2 (0, p, -1); + // { dg-warning "argument 3 value -1 is negative" "warning" { target *-*-* } .-1 } +} diff --git a/gcc/testsuite/gcc.dg/attr-access-2.c b/gcc/testsuite/gcc.dg/attr-access-2.c index 74762610f98..76baddffc9f 100644 --- a/gcc/testsuite/gcc.dg/attr-access-2.c +++ b/gcc/testsuite/gcc.dg/attr-access-2.c @@ -112,5 +112,11 @@ typedef void G1 (int n, int[n], int); G1 g1; -RW (2, 3) void g1 (int n, int[n], int); // { dg-warning "24: attribute 'access *\\\(read_write, 2, 3\\\)' positional argument 2 conflicts with previous designation by argument 3" } -// { dg-message "designating the bound of variable length array argument 2" "note" { target *-*-* } .-1 } +/* The warning is about the attribute positional argument 2 which refers + to the last function argument. Ideally, the caret would be under + the corresponding function argument, i.e., the last one here) but + that location isn't available yet. Verify that the caret doesn't + point to function argument 1 which is the VLA bound (that's what + the caret in the note points to). */ +RW (2, 3) void g1 (int n, int[n], int); // { dg-warning "16: attribute 'access *\\\(read_write, 2, 3\\\)' positional argument 2 conflicts with previous designation by argument 3" } +// { dg-message "24:designating the bound of variable length array argument 2" "note" { target *-*-* } .-1 }