From patchwork Fri Mar 3 10:09:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 63855 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp329377wrd; Fri, 3 Mar 2023 02:10:51 -0800 (PST) X-Google-Smtp-Source: AK7set+w7bycjN+QLP9dZG8qLAhSMSohMEGYjoDN35pFz9sB+Mt46Ez7+djokiSWHsF/p/JAIgOD X-Received: by 2002:a17:906:1015:b0:8b0:ad0b:7ab8 with SMTP id 21-20020a170906101500b008b0ad0b7ab8mr1081566ejm.14.1677838251052; Fri, 03 Mar 2023 02:10:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677838251; cv=none; d=google.com; s=arc-20160816; b=xrEvJeJsZi4atyUQg42lgO9HhfKt1SPwFICXEwW8OjU0qtdOTRkKIGJkhwc5jzNzAH cy4sLcY/9IxyTaQl5SqQpcpIKTkwXLJ0uwm7vZqrAyGJbQH9JSd16zec5tCuXOzkeK9C Oca/en8iSBNJjajFkiEkdrtkYfuBtFoEMU1Dg2G5ibgrv0ROUBBhGbm0Jkah9tG+5RwA zkT9c2lWIo4yRXvRPNWtBcI9j/TPWswn0+gUeyasL9BaOT8AkNRRZYxJ9d+xnacbOhjV 55+FCO1R9lbISoqjGXJyQ0K8Y5nxAKrUAPS7hGbH9i5xztJMgWCWI21b9mplepJO2Q92 Y5sA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:content-disposition:mime-version :message-id:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=TaWqZrhq01NEl9cB5SdlZV64tYANUeld5X0X4p3dm+o=; b=X5DWMTTriVrmWalh6E3BWE174d1qLxqfDjt3q4pY7JlNGWeu2SkoX/sK5aQiKQjpGy O5mhqW0fiRSz5eTdClsv8CRmh7y/BVO5xYkpDc5p6cLobf7ScmED351Aknxiqxt7U4Pt 4Eb/7vWAdfKRq3DiSRrA/KPnRLOYlRWsFHcYZ1CbfGnOC37UgSpMgiA5w9lvVYabZY45 /gi4/JTm2/+GvtWYyFBuKmtzE8nfzdVZkdSZceqNNkxkzhGsLnB1luRy3MiTx1nJmIGf zNwQPmoo+5FDbmglLPukz6fkOJk+3UZoY2iZFcN0ssNT3hF8HViFZfe4dLRuJ+8nZ1Jy +LLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=AV3v753C; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id le16-20020a170907171000b008d21d51c307si624414ejc.383.2023.03.03.02.10.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 02:10:51 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=AV3v753C; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CD8AC3858D33 for ; Fri, 3 Mar 2023 10:10:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CD8AC3858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677838249; bh=TaWqZrhq01NEl9cB5SdlZV64tYANUeld5X0X4p3dm+o=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=AV3v753C8Pqw5Qcg9FjhZQj/kT64JFpniHFRntZ/E1PejMd+HsnEYvSiQavG3NWeT LIrVHWmQY0h6SYFmLMui1rd+y2ZOef04fpMpwoUulmyAsQqVLJjCgX+Mq50hJ/+957 msQfxRX6fBfvh22G2Y9Y+RQRBebQh32bqb+pdJeg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id E79BA3858D33 for ; Fri, 3 Mar 2023 10:10:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E79BA3858D33 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-462-UTjW52rRPES0MMyf5-FeaQ-1; Fri, 03 Mar 2023 05:10:02 -0500 X-MC-Unique: UTjW52rRPES0MMyf5-FeaQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DFBD03C0D864; Fri, 3 Mar 2023 10:10:01 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 596831121315; Fri, 3 Mar 2023 10:10:01 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 323A9OgS1820444 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 3 Mar 2023 11:09:32 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 323A9Iwx1820443; Fri, 3 Mar 2023 11:09:18 +0100 Date: Fri, 3 Mar 2023 11:09:18 +0100 To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] waccess: Fix two -Wnonnull warning issues [PR108986] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759340921921606202?= X-GMAIL-MSGID: =?utf-8?q?1759340921921606202?= Hi! The following patch fixes 2 issues with the -Wnonnull warning. One, fixed by the second hunk, is that the warning wording is bogus since r11-3305, instead of printing warning: argument 1 to ‘int[static 7]’ is null where non-null expected [-Wnonnull] it prints warning: argument 1 to ‘int[static 28]’ is null where non-null expected [-Wnonnull] access_size is measured in bytes, so obviously will be correct as array number of elements only if it is 1 byte element array. In the function, access_nelts is either constant (if sizidx == -1) or when the array size is determined by some other parameter, I believe a value passed to that argument. Later on we query the range of it: if (get_size_range (m_ptr_qry.rvals, access_nelts, stmt, sizrng, 1)) which I bet must just return accesS_nelts in sizrng[0] and [1] if it is constant. access_size is later computed as: tree access_size = NULL_TREE; if (tree_int_cst_sgn (sizrng[0]) >= 0) { if (COMPLETE_TYPE_P (argtype)) { ... wide_int minsize = wi::to_wide (sizrng[0], prec); minsize *= wi::to_wide (argsize, prec); access_size = wide_int_to_tree (sizetype, minsize); } } else access_size = access_nelts; } and immediately after this the code does: if (integer_zerop (ptr)) { if (sizidx >= 0 && tree_int_cst_sgn (sizrng[0]) > 0) { some other warning wording } else if (access_size && access.second.static_p) { this spot } } So, because argtype is complete, access_size has been multiplied by argsize, but in case of this exact warning ("this spot" above) I believe access_nelts must be really constant, otherwise "some other warning wording" would handle it. So, I think access_nelts is exactly what we want to print there. The other problem is that since the introduction of -Wdangling-pointer in r12-6606, the pass has early and late instances and while lots of stuff in the pass is guarded on being done in the late pass only, this particular function is not, furthermore it is emitting two different warnings in a loop and already messes up with stuff like clearing warning suppression for one of the warning (ugh!). The end effect is that we warn twice about the same problem, once in the early and once in the late pass. Now, e.g. with -O2 -Wall we warn just once, during the early pass, as it is then optimized away, so I think just making this late warning only wouldn't be best. This patch instead returns early if either of the warnings is suppressed on the call stmt already. I think if one of the passes warned on it already (even if say on some other argument), then warning again (even on some other argument) is unnecessary, if both problems are visible in the same pass we'll still warn about both. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-03-03 Jakub Jelinek PR c/108986 * gimple-ssa-warn-access.cc (pass_waccess::maybe_check_access_sizes): Return immediately if OPT_Wnonnull or OPT_Wstringop_overflow_ is suppressed on stmt. For [static %E] warning, print access_nelts rather than access_size. Fix up comment wording. * gcc.dg/Wnonnull-8.c: New test. Jakub --- gcc/gimple-ssa-warn-access.cc.jj 2023-02-22 20:50:27.418519815 +0100 +++ gcc/gimple-ssa-warn-access.cc 2023-03-02 19:04:24.744280016 +0100 @@ -3318,6 +3318,10 @@ void pass_waccess::maybe_check_access_sizes (rdwr_map *rwm, tree fndecl, tree fntype, gimple *stmt) { + if (warning_suppressed_p (stmt, OPT_Wnonnull) + || warning_suppressed_p (stmt, OPT_Wstringop_overflow_)) + return; + auto_diagnostic_group adg; /* Set if a warning has been issued for any argument (used to decide @@ -3501,7 +3505,7 @@ pass_waccess::maybe_check_access_sizes ( if (warning_at (loc, OPT_Wnonnull, "argument %i to %<%T[static %E]%> " "is null where non-null expected", - ptridx + 1, argtype, access_size)) + ptridx + 1, argtype, access_nelts)) arg_warned = OPT_Wnonnull; } @@ -3593,7 +3597,7 @@ pass_waccess::maybe_check_access_sizes ( "in a call with type %qT", fntype); } - /* Set the bit in case if was cleared and not set above. */ + /* Set the bit in case it was cleared and not set above. */ if (opt_warned != no_warning) suppress_warning (stmt, opt_warned); } --- gcc/testsuite/gcc.dg/Wnonnull-8.c.jj 2023-03-02 17:28:24.323898410 +0100 +++ gcc/testsuite/gcc.dg/Wnonnull-8.c 2023-03-02 17:27:51.804376322 +0100 @@ -0,0 +1,14 @@ +/* PR c/108986 */ +/* { dg-do compile } */ +/* { dg-options "-Wall" } */ + +void +foo (int a[static 7]) +{ +} + +int +main () +{ + foo ((int *) 0); /* { dg-warning "argument 1 to 'int\\\[static 7\\\]' is null where non-null expected" } */ +}