From patchwork Tue Jul 26 21:20:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 56349 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 40593385829D for ; Tue, 26 Jul 2022 21:20:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 40593385829D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658870450; bh=gou28QxsgmgyWjSI+ceB38eW2o6AZNpLcIiW1ZX5pT0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=c6+lvtue6xOggyyq8aBHObWimi5vfJpWsIOjuEIZ2w68nAHebBQ3DaqB/4VTsjlAj TZL+zIM02tFQoD9MngKdXAB0pJbCBPnbFlp5bJeMLwOt2r1XlZpyc3SC4RiHgcyV7I DPBivCQcpuYA9lqIqPDSPxHAv9iu4YmZUFNGEdhA= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id C17CF3858C56 for ; Tue, 26 Jul 2022 21:20:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C17CF3858C56 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-433-I0Nfm552NiGH2H1lShy0rQ-1; Tue, 26 Jul 2022 17:20:19 -0400 X-MC-Unique: I0Nfm552NiGH2H1lShy0rQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 43DCA85A581 for ; Tue, 26 Jul 2022 21:20:19 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.16.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 065AD400F8F0; Tue, 26 Jul 2022 21:20:18 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [committed] analyzer: fix false +ves from -Wanalyzer-va-arg-type-mismatch on int promotion [PR106319] Date: Tue, 26 Jul 2022 17:20:17 -0400 Message-Id: <20220726212017.2099225-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-1848-gdb613e8fa84125. gcc/analyzer/ChangeLog: PR analyzer/106319 * store.cc (store::set_value): Don't strip away casts if the region has NULL type. gcc/testsuite/ChangeLog: PR analyzer/106319 * gcc.dg/analyzer/stdarg-types-3.c: New test. * gcc.dg/analyzer/stdarg-types-4.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/store.cc | 4 +- .../gcc.dg/analyzer/stdarg-types-3.c | 67 +++++++++++++++++++ .../gcc.dg/analyzer/stdarg-types-4.c | 22 ++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/stdarg-types-3.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/stdarg-types-4.c diff --git a/gcc/analyzer/store.cc b/gcc/analyzer/store.cc index 0b3fb370d1e..46475f6a7fa 100644 --- a/gcc/analyzer/store.cc +++ b/gcc/analyzer/store.cc @@ -2445,7 +2445,9 @@ store::set_value (store_manager *mgr, const region *lhs_reg, remove_overlapping_bindings (mgr, lhs_reg, uncertainty); - rhs_sval = simplify_for_binding (rhs_sval); + if (lhs_reg->get_type ()) + rhs_sval = simplify_for_binding (rhs_sval); + /* ...but if we have no type for the region, retain any cast. */ const region *lhs_base_reg = lhs_reg->get_base_region (); binding_cluster *lhs_cluster; diff --git a/gcc/testsuite/gcc.dg/analyzer/stdarg-types-3.c b/gcc/testsuite/gcc.dg/analyzer/stdarg-types-3.c new file mode 100644 index 00000000000..7351261a55c --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/stdarg-types-3.c @@ -0,0 +1,67 @@ +static void __attribute__((noinline)) +__analyzer_consume_n_ints (int num, ...) +{ + __builtin_va_list ap; + __builtin_va_start (ap, num); + + int i, v; + for (i = 0; i < num; i++) + v = __builtin_va_arg (ap, int); + + __builtin_va_end (ap); +} + +void test_int (int x) +{ + __analyzer_consume_n_ints (1, x); +} + +void test_3_ints (int x, int y, int z) +{ + __analyzer_consume_n_ints (3, x, y, z); +} + +/* Verify that we don't complain about types that get promoted to int + at the variadic call. */ + +void test_short (short s) +{ + __analyzer_consume_n_ints (1, s); +} + +void test_ushort (unsigned short s) +{ + __analyzer_consume_n_ints (1, s); +} + +void test_schar (signed char ch) +{ + __analyzer_consume_n_ints (1, ch); +} + +void test_uchar (unsigned char ch) +{ + __analyzer_consume_n_ints (1, ch); +} + +struct ust +{ + int b0123 : 4; + int b4567 : 4; +}; + +void test_signed_bitfield (struct ust s) +{ + __analyzer_consume_n_ints (2, s.b0123, s.b4567); +} + +struct sst +{ + unsigned int b0123 : 4; + unsigned int b4567 : 4; +}; + +void test_unsigned_bitfield (struct sst s) +{ + __analyzer_consume_n_ints (2, s.b0123, s.b4567); +} diff --git a/gcc/testsuite/gcc.dg/analyzer/stdarg-types-4.c b/gcc/testsuite/gcc.dg/analyzer/stdarg-types-4.c new file mode 100644 index 00000000000..920eccebc2b --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/stdarg-types-4.c @@ -0,0 +1,22 @@ +static void __attribute__((noinline)) +__analyzer_consume_n_uints (int num, ...) +{ + __builtin_va_list ap; + __builtin_va_start (ap, num); + + int i, v; + for (i = 0; i < num; i++) + v = __builtin_va_arg (ap, unsigned int); + + __builtin_va_end (ap); +} + +void test_uint (unsigned int x) +{ + __analyzer_consume_n_uints (1, x); +} + +void test_3_uints (unsigned int x, unsigned int y, unsigned int z) +{ + __analyzer_consume_n_uints (3, x, y, z); +}