From patchwork Wed Mar 22 15:27:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Petr_=C5=A0tetiar?= X-Patchwork-Id: 1759884 X-Patchwork-Delegate: ynezz@true.cz Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=DSx0qeAH; dkim=fail reason="key not found in DNS" header.d=true.cz header.i=@true.cz header.a=rsa-sha256 header.s=xnet header.b=Pr67KCy5; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PhXTv6YD3z1yWp for ; Thu, 23 Mar 2023 02:30:11 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=LNN8Lc6G/W+V2+qoY9kRcI6MFlYgGflh8aBKmzN6NCU=; b=DSx0qeAHbXc7tW RAvy4E6pvT2h+hfOByQADqdJ0QoTCp0/mxDnFnr3923+keT7/fNAtLbxSn/QuJAA+A27wBH7vy9Wc rCIgtFNeLR3Uk2b0Xm/W50TaRz7lyOaybWBtqZXUfYX+2qaRaN2kIwls7xXSfEpJYBgddSxhxGWze g5qVeYk0VFGv/RUDTdwrPrVW3BQmbC0w0yj6Vy9ZOGmThQ4JPaMjERD0bbuMwrGc27Tver4fLFlFn k/8EEsc9i9+9sMN1ULk/OS9G/UVV1umL7Lbf5FmH9tc1XlvDvYrYdhCKVtaow8e6+g9Vu1TX8tIKH e7Zf5FVaOcHkTeGRr/wQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pf0N0-00GVNh-2N; Wed, 22 Mar 2023 15:27:22 +0000 Received: from smtp-out.xnet.cz ([178.217.244.18]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pf0Mv-00GVMQ-0I for openwrt-devel@lists.openwrt.org; Wed, 22 Mar 2023 15:27:20 +0000 Received: from meh.true.cz (meh.true.cz [108.61.167.218]) (Authenticated sender: petr@true.cz) by smtp-out.xnet.cz (Postfix) with ESMTPSA id EE4E419774; Wed, 22 Mar 2023 16:27:10 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=true.cz; s=xnet; t=1679498831; bh=cGsZjEPz66OrqypaOh5jNEvrIc5eA8VuHz/LHhofYl8=; h=From:To:Cc:Subject:Date; b=Pr67KCy5+DfYy6dYLbYNqXS/zQD3L9r//DjcXsZA0LK6g5SD43M3YsiR0Tr0IkzoQ /UVgLKYkrEApAHbCRbey13Yl//E1WwBJ1pWb+tPUDbmQ5o41RDiod24Qe/X1bOTsaS aKneuTYjOc/F0iuNjhWC1JGcBij7imsY2ZtTepYA= Received: by meh.true.cz (OpenSMTPD) with ESMTP id 71aa37b5; Wed, 22 Mar 2023 16:26:44 +0100 (CET) From: =?utf-8?q?Petr_=C5=A0tetiar?= To: openwrt-devel@lists.openwrt.org Cc: Andrey Erokhin , =?utf-8?q?Petr_=C5=A0teti?= =?utf-8?q?ar?= Subject: [PATCH 22.03] toolchain: gcc: backport v11.3.0 fix for false positive VLA params warnings Date: Wed, 22 Mar 2023 16:27:01 +0100 Message-Id: <20230322152701.29006-1-ynezz@true.cz> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230322_082717_771921_501B5DC8 X-CRM114-Status: GOOD ( 19.17 ) X-Spam-Score: 0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Andrey Erokhin If the vla parameter has a const specifier, the compiler will warn about mismatched bounds: $ cat mwe.c extern void mwe(const int len, char buf[len]); void mwe(const int len, char buf[len]) {} Content analysis details: (0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid 0.1 DKIM_INVALID DKIM or DK signature exists, but is not valid X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Andrey Erokhin If the vla parameter has a const specifier, the compiler will warn about mismatched bounds: $ cat mwe.c extern void mwe(const int len, char buf[len]); void mwe(const int len, char buf[len]) {} $ make CFLAGS=-Wvla-parameter mwe.o cc -Wvla-parameter -c -o mwe.o mwe.c mwe.c:2:30: warning: argument 2 of type ‘char[len]’ declared with mismatched bound ‘len’ [-Wvla-parameter] 2 | void mwe(const int len, char buf[len]) {} | ~~~~~^~~~~~~~ mwe.c:1:37: note: previously declared as ‘char[len]’ with bound ‘len’ 1 | extern void mwe(const int len, char buf[len]); | ~~~~~^~~~~~~~ On some code bases it might result in a lot of false positive warnings, which can indeed be easily disabled, but on the other this workaround might hide some real issues, so lets rather fix the compiler and make it more reliable. References: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101289 Signed-off-by: Andrey Erokhin Signed-off-by: Petr Štetiar [commit message] --- .../400-v11.3.0-bogus-Wvla-parameter.patch | 192 ++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 toolchain/gcc/patches/11.2.0/400-v11.3.0-bogus-Wvla-parameter.patch diff --git a/toolchain/gcc/patches/11.2.0/400-v11.3.0-bogus-Wvla-parameter.patch b/toolchain/gcc/patches/11.2.0/400-v11.3.0-bogus-Wvla-parameter.patch new file mode 100644 index 000000000000..443839f81b0e --- /dev/null +++ b/toolchain/gcc/patches/11.2.0/400-v11.3.0-bogus-Wvla-parameter.patch @@ -0,0 +1,192 @@ +From 7d3f53c595e1766ca0494e5f56f33b0ce49b3bb4 Mon Sep 17 00:00:00 2001 +From: Martin Sebor +Date: Thu, 15 Jul 2021 10:11:23 -0600 +Subject: [PATCH] Avoid -Wvla-parameter for nontrivial bounds [PR97548]. + +Resolves: +PR c/101289 - bogus -Wvla-paramater warning when using const for vla param +PR c/97548 - bogus -Wvla-parameter on a bound expression involving a parameter + +gcc/c-family/ChangeLog: + + PR c/101289 + PR c/97548 + * c-warn.c (warn_parm_array_mismatch): Use OEP_DECL_NAME. + +gcc/c/ChangeLog: + + PR c/101289 + PR c/97548 + * c-decl.c (get_parm_array_spec): Strip nops. + +gcc/ChangeLog: + + PR c/101289 + PR c/97548 + * fold-const.c (operand_compare::operand_equal_p): Handle OEP_DECL_NAME. + (operand_compare::verify_hash_value): Same. + * tree-core.h (OEP_DECL_NAME): New. + +gcc/testsuite/ChangeLog: + + * gcc.dg/Wvla-parameter-12.c: New test. +--- + gcc/c-family/c-warn.c | 3 +- + gcc/c/c-decl.c | 1 + + gcc/fold-const.c | 33 ++++++++++++++++------ + gcc/testsuite/gcc.dg/Wvla-parameter-12.c | 36 ++++++++++++++++++++++++ + gcc/tree-core.h | 7 ++++- + 5 files changed, 69 insertions(+), 11 deletions(-) + create mode 100644 gcc/testsuite/gcc.dg/Wvla-parameter-12.c + +diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c +index 7414063aa11..250da89a829 100644 +--- a/gcc/c-family/c-warn.c ++++ b/gcc/c-family/c-warn.c +@@ -3646,7 +3646,8 @@ warn_parm_array_mismatch (location_t origloc, tree fndecl, tree newparms) + /* The VLA bounds don't refer to other function parameters. + Compare them lexicographically to detect gross mismatches + such as between T[foo()] and T[bar()]. */ +- if (operand_equal_p (newbnd, curbnd, OEP_LEXICOGRAPHIC)) ++ if (operand_equal_p (newbnd, curbnd, ++ OEP_DECL_NAME | OEP_LEXICOGRAPHIC)) + continue; + + if (warning_at (newloc, OPT_Wvla_parameter, +diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c +index 53b2b5b637d..ddef9c68fb7 100644 +--- a/gcc/c/c-decl.c ++++ b/gcc/c/c-decl.c +@@ -5862,6 +5862,7 @@ get_parm_array_spec (const struct c_parm *parm, tree attrs) + + /* Each variable VLA bound is represented by a dollar sign. */ + spec += "$"; ++ STRIP_NOPS (nelts); + vbchain = tree_cons (NULL_TREE, nelts, vbchain); + } + +diff --git a/gcc/fold-const.c b/gcc/fold-const.c +index a1d08c74025..f5c19a0cfd4 100644 +--- a/gcc/fold-const.c ++++ b/gcc/fold-const.c +@@ -3506,11 +3506,26 @@ operand_compare::operand_equal_p (const_tree arg0, const_tree arg1, + + case tcc_declaration: + /* Consider __builtin_sqrt equal to sqrt. */ +- return (TREE_CODE (arg0) == FUNCTION_DECL +- && fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1) +- && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1) +- && (DECL_UNCHECKED_FUNCTION_CODE (arg0) +- == DECL_UNCHECKED_FUNCTION_CODE (arg1))); ++ if (TREE_CODE (arg0) == FUNCTION_DECL) ++ return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1) ++ && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1) ++ && (DECL_UNCHECKED_FUNCTION_CODE (arg0) ++ == DECL_UNCHECKED_FUNCTION_CODE (arg1))); ++ ++ if (DECL_P (arg0) ++ && (flags & OEP_DECL_NAME) ++ && (flags & OEP_LEXICOGRAPHIC)) ++ { ++ /* Consider decls with the same name equal. The caller needs ++ to make sure they refer to the same entity (such as a function ++ formal parameter). */ ++ tree a0name = DECL_NAME (arg0); ++ tree a1name = DECL_NAME (arg1); ++ const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL; ++ const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL; ++ return a0ns && a1ns && strcmp (a0ns, a1ns) == 0; ++ } ++ return false; + + case tcc_exceptional: + if (TREE_CODE (arg0) == CONSTRUCTOR) +@@ -3921,14 +3936,14 @@ bool + operand_compare::verify_hash_value (const_tree arg0, const_tree arg1, + unsigned int flags, bool *ret) + { +- /* When checking, verify at the outermost operand_equal_p call that +- if operand_equal_p returns non-zero then ARG0 and ARG1 has the same +- hash value. */ ++ /* When checking and unless comparing DECL names, verify that if ++ the outermost operand_equal_p call returns non-zero then ARG0 ++ and ARG1 have the same hash value. */ + if (flag_checking && !(flags & OEP_NO_HASH_CHECK)) + { + if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK)) + { +- if (arg0 != arg1) ++ if (arg0 != arg1 && !(flags & OEP_DECL_NAME)) + { + inchash::hash hstate0 (0), hstate1 (0); + hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK); +diff --git a/gcc/testsuite/gcc.dg/Wvla-parameter-12.c b/gcc/testsuite/gcc.dg/Wvla-parameter-12.c +new file mode 100644 +index 00000000000..1be5e48203b +--- /dev/null ++++ b/gcc/testsuite/gcc.dg/Wvla-parameter-12.c +@@ -0,0 +1,36 @@ ++/* PR c/101289 - bogus -Wvla-parameter warning when using const bound ++ { dg-do compile } ++ { dg-options "-Wall" } */ ++ ++void f1ci_can (const int n, char a[n]); ++void f1ci_can (const int n, char a[n]); // { dg-bogus "-Wvla-parameter" } ++ ++void f2ci_can (const int m, char a[m]); ++void f2ci_can (int n, char a[n]); // { dg-bogus "-Wvla-parameter" } ++ ++void f3i_can (int n, char a[n]); ++void f3i_can (const int n, char a[n]); // { dg-bogus "-Wvla-parameter" } ++ ++void f4i_can (int n, char a[n]); ++void f4i_can (const int n, char a[(int)n]); // { dg-bogus "-Wvla-parameter" } ++ ++void f5i_can (int n, char a[(char)n]); ++void f5i_can (const int n, char a[(char)n]); // { dg-bogus "-Wvla-parameter" } ++ ++void f6i_can (int m, char a[(char)m]); ++void f6i_can (const int n, char a[(char)n]); // { dg-bogus "-Wvla-parameter" "" { xfail *-*-* } } ++ ++ ++/* PR c/97548 - bogus -Wvla-parameter on a bound expression involving ++ a parameter */ ++ ++int n; ++ ++void f7ianp1 (int, int[n + 1]); ++void f7ianp1 (int, int[n + 1]); ++void f7ianp1 (int, int[n + 2]); // { dg-warning "-Wvla-parameter" } ++ ++void f8iakp1 (int k, int [k + 1]); ++void f8iakp1 (int k, int [k + 1]); // { dg-bogus "-Wvla-parameter" } ++void f8iakp1 (int k, int [1 + k]); // { dg-bogus "-Wvla-parameter" } ++void f8iakp1 (int k, int [k + 2]); // { dg-warning "-Wvla-parameter" } +diff --git a/gcc/tree-core.h b/gcc/tree-core.h +index 07ddf91a230..c31b8ebf249 100644 +--- a/gcc/tree-core.h ++++ b/gcc/tree-core.h +@@ -885,6 +885,7 @@ enum size_type_kind { + stk_type_kind_last + }; + ++/* Flags controlling operand_equal_p() behavior. */ + enum operand_equal_flag { + OEP_ONLY_CONST = 1, + OEP_PURE_SAME = 2, +@@ -899,7 +900,11 @@ enum operand_equal_flag { + OEP_BITWISE = 128, + /* For OEP_ADDRESS_OF of COMPONENT_REFs, only consider same fields as + equivalent rather than also different fields with the same offset. */ +- OEP_ADDRESS_OF_SAME_FIELD = 256 ++ OEP_ADDRESS_OF_SAME_FIELD = 256, ++ /* In conjunction with OEP_LEXICOGRAPHIC considers names of declarations ++ of the same kind. Used to compare VLA bounds involving parameters ++ across redeclarations of the same function. */ ++ OEP_DECL_NAME = 512 + }; + + /* Enum and arrays used for tree allocation stats. +-- +2.31.1 +