From patchwork Sat Oct 21 11:09:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 156436 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp232663vqx; Sat, 21 Oct 2023 04:10:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFyak3vm5Rs5Xq3A7a7zAFvMZQrO+j/EaxkcGG7zk8U5e8Iaz7VqvrIGpdiJQyl/SW2Ngsq X-Received: by 2002:a05:6214:224c:b0:658:7441:ff1b with SMTP id c12-20020a056214224c00b006587441ff1bmr5241809qvc.45.1697886625941; Sat, 21 Oct 2023 04:10:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697886625; cv=pass; d=google.com; s=arc-20160816; b=qc6+4YnfNylPCO1dgW2v5xcAKHgKEF6wKVCugZowu+fg1KemTUIPL4pSsOWPkX7Tyx flzZ3ZHkvFz3d9OVLWQqbWaq2HU2eK3rR/sZyLCuyUerZxmtwGsaLZPU71jarjc8RhuP IvMRt+Rpt9/QbUxw5MQzN4SfW8wE4VjyYZ4qMikJpCGJDgE2ldEUZLsA9KJKfxIM9UJG L0HjVGVnkcDoCMiGQ6/LmKITT7e1t1YPspoUDFTIuiTjjUipPrc/BBzejeDTYka24z8h b+h+paNXElyxCVr8fpqIJp2M1YxrcdvTQguITML79NK+Ru36IfKxwmLZBuj2I1GHqOvk Tj4Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:user-agent :content-transfer-encoding:references:in-reply-to:date:cc:to:from :subject:message-id:dkim-signature:arc-filter:dmarc-filter :delivered-to; bh=l2JxMiwijzrMvULU+5kJGbXf4MoQ0X6FIHX+PSfR9CQ=; fh=oUsJQX2IveB08nxA8hRjOFRU5aBS2r2h9/b2Fqx5GLM=; b=pEGmkkbAT0jJm8cqUMWjEoFswpjjoUdakf+BPrVGeRLPGaWN7U8NferqDM52rW+Cfr chyOYGjm7hnTuSS0NSH/ztgOwgsnIOsu86yvq9ji7mqafzJfO9ynhwq0TwzvtziJf/09 gzlnh9e0JTfmEHZgFPzZYVFlGm1HmSONT8NYAUCiuHstyhNOvWr745Hk5UHyrZaOdNeF AEYPNWgxEIejnEccv7wqYlJ5F+qXkZOqoTCQAAtlXx4mc5GM+EVRvTyaYVwE5pyOkoGY 9Rlij0i29hzfvEBM4fC9+uUmw4GR9FNEzDWFKJu4ok/ysHxYdCdiVm0r1n1dD54XeiR/ B+kg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tugraz.at header.s=mailrelay header.b=i1E6hedd; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=tugraz.at Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ng3-20020a0562143bc300b00668564060c7si2835977qvb.330.2023.10.21.04.10.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Oct 2023 04:10:25 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@tugraz.at header.s=mailrelay header.b=i1E6hedd; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=tugraz.at Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AE0DD385828E for ; Sat, 21 Oct 2023 11:10:25 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id E8AC8385840A for ; Sat, 21 Oct 2023 11:09:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E8AC8385840A Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E8AC8385840A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697886602; cv=none; b=uv07upKie/8TeGCjxQVSXF2xhBrXuog+zDxvwaEYOu8djHvJvg1ycPm4DT48e1CCdXDkmz9qCeGg/NTerzxIog00caxMnWKtnKKuZnKr8hj8tcZ365pM5IvqZNeDJMi996BG6WCNBuPbpzFqve02toGtg4/T71Yk4KJdTyEdvQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697886602; c=relaxed/simple; bh=3SGzPPSXUe1CkpW9Q8iHbzdS30YrDr+CxA3N99M0Rcg=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=UREwsYFvHKUylV509CDVdOx/hTlEmU/xiFfLHXf3xD5YYMg3lmY7SQBcVQsTryx8zzTA5T0XREDZ11xJaHNyPIJhXRrKlNKP867YaYRnF/iaVHp3kHAnAct04SxU76k7+elfUSzSj/Y2mkQ9ec8XL3GXXt/SFuxCti3xCaO/bVM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from vra-173-9.tugraz.at (vra-173-9.tugraz.at [129.27.173.9]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4SCJd26k3Dz3wh7; Sat, 21 Oct 2023 13:09:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1697886583; bh=l2JxMiwijzrMvULU+5kJGbXf4MoQ0X6FIHX+PSfR9CQ=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=i1E6heddHwT4g3Jy69WL7Bll5QunzDX700TS5u4Gz51hZDt/n3M2zlWHEP8EIkl3l 4cKX0XEr4ibVWWvARh2gIVYxj6/fBaJQP8c/tq+BBqbGiT0mlxFf51YLXUgwO1A8B3 yAmu0g534Ct/aCgoOO5zQWuwE3pANOcItV0tteP0= Message-ID: <564177ba373bb73cfc2c2b106b7e57b4bcbc512e.camel@tugraz.at> Subject: [PING 2] [C PATCH] Synthesize nonnull attribute for parameters declared with static From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Joseph Myers , jakub Jelinek Date: Sat, 21 Oct 2023 13:09:42 +0200 In-Reply-To: References: User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.116 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772499942399793194 X-GMAIL-MSGID: 1780363166837077900 > > 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  > arguments and to remove the special and more limited > nonnull warnings for static. This patch found some  > misplaced annotations in one of my projects via > -Wnonnull-compare which clang does not seem to have,  > so I think this could be useful. c: Synthesize nonnull attribute for parameters declared with static [PR110815] Parameters declared with `static` are nonnull. We synthesize an artifical nonnull attribute for such parameters to get the same warnings and optimizations. Bootstrapped and regression tested on x86. PR c/102558 PR 102556 PR c/110815 gcc/c-family: * c-attribs.cc (build_attr_access_from_parms): Synthesize nonnull attribute for parameters declared with `static`. gcc: * gimple-ssa-warn-access.cc (pass_waccess::maybe_check_access_sizes): remove warning for parameters declared with `static`. 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_voidptr) arg2pos.put (arg, argpos); } + tree nnlist = NULL_TREE; argpos = 0; for (tree arg = parms; arg; arg = TREE_CHAIN (arg), ++argpos) { @@ -5313,6 +5314,11 @@ build_attr_access_from_parms (tree parms, bool skip_voidptr) tree str = TREE_VALUE (argspec); const char *s = TREE_STRING_POINTER (str); + /* Collect the list of nonnull arguments which use "[static ..]". */ + if (s != NULL && s[0] == '[' && s[1] == 's') + nnlist = 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; + /* If we have nonnull arguments, synthesize an attribute. */ + if (nnlist != NULL_TREE) + nnlist = 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 = build_tree_list (NULL_TREE, vblist); @@ -5390,7 +5400,7 @@ build_attr_access_from_parms (tree parms, bool skip_voidptr) tree str = build_string (spec.length (), spec.c_str ()); tree attrargs = tree_cons (NULL_TREE, str, vblist); tree name = get_identifier ("access"); - return build_tree_list (name, attrargs); + return tree_cons (name, attrargs, nnlist); } /* 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 *rwm, tree fndecl, tree fntype, ptridx + 1, sizidx + 1, sizstr)) arg_warned = 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 = OPT_Wnonnull; - } if (arg_warned != no_warning) { diff --git a/gcc/testsuite/gcc.dg/Wnonnull-8.c b/gcc/testsuite/gcc.dg/Wnonnull-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 expected" } */ } diff --git a/gcc/testsuite/gcc.dg/Wnonnull-9.c b/gcc/testsuite/gcc.dg/Wnonnull-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 == a) /* { dg-warning "argument" "compared to NULL" } */ + return; +} + +int +main () +{ + foo ((void*)0); /* { dg-warning "argument 1 null where non-null expected" } */ +} +