From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from xmailer.gwdg.de (xmailer.gwdg.de [134.76.10.29]) by sourceware.org (Postfix) with ESMTPS id 3871D3858D32 for ; Sun, 24 Sep 2023 13:13:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3871D3858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gwdg.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gwdg.de Received: from excmbx-23.um.gwdg.de ([134.76.9.233] helo=email.gwdg.de) by mailer.gwdg.de with esmtp (GWDG Mailer) (envelope-from ) id 1qkOvY-00086c-SZ; Sun, 24 Sep 2023 15:13:36 +0200 Received: from EXCMBX-29.um.gwdg.de (134.76.9.204) by EXCMBX-23.um.gwdg.de (134.76.9.233) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.32; Sun, 24 Sep 2023 15:13:36 +0200 Received: from vra-169-227.tugraz.at (10.250.9.199) by EXCMBX-29.um.gwdg.de (134.76.9.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.2507.32; Sun, 24 Sep 2023 15:13:36 +0200 Message-ID: Subject: [PING] [C PATCH] Synthesize nonnull attribute for parameters declared with static From: Martin Uecker To: CC: Jakub Jelinek Date: Sun, 24 Sep 2023 15:13:35 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-Originating-IP: [10.250.9.199] X-ClientProxiedBy: excmbx-25.um.gwdg.de (134.76.9.235) To EXCMBX-29.um.gwdg.de (134.76.9.204) X-Virus-Scanned: (clean) by clamav X-Spam-Status: No, score=-8.8 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Am Mittwoch, dem 26.07.2023 um 18:06 +0200 schrieb Martin Uecker: >=20 > C programmers increasingly use static to indicate that > pointer parameters are non-null. Clang can exploit this > for warnings and optimizations. GCC has some warnings > but not all warnings it has for nonnull. Below is a > patch to add a nonnull attribute automatically for such=C2=A0 > arguments and to remove the special and more limited > nonnull warnings for static. This patch found some=C2=A0 > misplaced annotations=C2=A0in one of my projects via > -Wnonnull-compare which clang does not seem to have,=C2=A0 > so I=C2=A0think this could be useful. c: Synthesize nonnull attribute for parameters declared with static [PR= 110815] =20 Parameters declared with `static` are nonnull. We synthesize an artifical nonnull attribute for such parameters to get the same warnings and optimizations. =20 Bootstrapped and regression tested on x86. =20 PR c/102558 PR 102556 PR c/110815 =20 gcc/c-family: * c-attribs.cc (build_attr_access_from_parms): Synthesize nonnull attribute for parameters declared with `static`. =20 gcc: * gimple-ssa-warn-access.cc (pass_waccess::maybe_check_access_s= izes): remove warning for parameters declared with `static`. =20 gcc/testsuite: * gcc.dg/Wnonnull-8.c: Adapt test. * gcc.dg/Wnonnull-9.c: New test. diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index e2792ca6898..ae7ffeb1f65 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -5280,6 +5280,7 @@ build_attr_access_from_parms (tree parms, bool skip_v= oidptr) arg2pos.put (arg, argpos); } =20 + tree nnlist =3D NULL_TREE; argpos =3D 0; for (tree arg =3D parms; arg; arg =3D TREE_CHAIN (arg), ++argpos) { @@ -5313,6 +5314,11 @@ build_attr_access_from_parms (tree parms, bool skip_= voidptr) tree str =3D TREE_VALUE (argspec); const char *s =3D TREE_STRING_POINTER (str); =20 + /* Collect the list of nonnull arguments which use "[static ..]". *= / + if (s !=3D NULL && s[0] =3D=3D '[' && s[1] =3D=3D 's') + nnlist =3D tree_cons (NULL_TREE, build_int_cst (integer_type_node, + argpos + 1), nnlist); + /* Create the attribute access string from the arg spec string, optionally followed by position of the VLA bound argument if it is one. */ @@ -5380,6 +5386,10 @@ build_attr_access_from_parms (tree parms, bool skip_= voidptr) if (!spec.length ()) return NULL_TREE; =20 + /* If we have nonnull arguments, synthesize an attribute. */ + if (nnlist !=3D NULL_TREE) + nnlist =3D build_tree_list (get_identifier ("nonnull"), nnlist); + /* Attribute access takes a two or three arguments. Wrap VBLIST in another list in case it has more nodes than would otherwise fit. */ vblist =3D build_tree_list (NULL_TREE, vblist); @@ -5390,7 +5400,7 @@ build_attr_access_from_parms (tree parms, bool skip_v= oidptr) tree str =3D build_string (spec.length (), spec.c_str ()); tree attrargs =3D tree_cons (NULL_TREE, str, vblist); tree name =3D get_identifier ("access"); - return build_tree_list (name, attrargs); + return tree_cons (name, attrargs, nnlist); } =20 /* Handle a "nothrow" attribute; arguments as in diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index b70d67dc47b..b8e89d01088 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -3491,16 +3491,6 @@ pass_waccess::maybe_check_access_sizes (rdwr_map *rw= m, tree fndecl, tree fntype, ptridx + 1, sizidx + 1, sizstr)) arg_warned =3D OPT_Wnonnull; } - else if (access_size && access.second.static_p) - { - /* Warn about null pointers for [static N] array arguments - but do not warn for ordinary (i.e., nonstatic) arrays. */ - if (warning_at (loc, OPT_Wnonnull, - "argument %i to %<%T[static %E]%> " - "is null where non-null expected", - ptridx + 1, argtype, access_nelts)) - arg_warned =3D OPT_Wnonnull; - } =20 if (arg_warned !=3D no_warning) { diff --git a/gcc/testsuite/gcc.dg/Wnonnull-8.c b/gcc/testsuite/gcc.dg/Wnonn= ull-8.c index 02871a76689..b24fd67cebc 100644 --- a/gcc/testsuite/gcc.dg/Wnonnull-8.c +++ b/gcc/testsuite/gcc.dg/Wnonnull-8.c @@ -10,5 +10,5 @@ foo (int a[static 7]) int main () { - foo ((int *) 0); /* { dg-warning "argument 1 to 'int\\\[static 7\\\]' is= null where non-null expected" } */ + foo ((int *) 0); /* { dg-warning "argument 1 null where non-null expecte= d" } */ } diff --git a/gcc/testsuite/gcc.dg/Wnonnull-9.c b/gcc/testsuite/gcc.dg/Wnonn= ull-9.c new file mode 100644 index 00000000000..1c57eefd2ae --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wnonnull-9.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall" } */ + + +void +foo (int a[static 1]) +{ + if ((void*)0 =3D=3D a) /* { dg-warning "argument" "compared to NULL" } *= / + return; +} + +int +main () +{ + foo ((void*)0); /* { dg-warning "argument 1 null where non-null expected= " } */ +} +