From patchwork Fri Oct 20 08:54:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 155893 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp916087vqb; Fri, 20 Oct 2023 01:55:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGNNbdyaNVo+1koKORFrYBNCg/ejZ8NXuMY/QIsaU8AaIkegpGOzxfkx10ZXZCBmGUtVhSz X-Received: by 2002:a0c:aa5c:0:b0:66d:2090:d23 with SMTP id e28-20020a0caa5c000000b0066d20900d23mr1000503qvb.38.1697792129281; Fri, 20 Oct 2023 01:55:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697792129; cv=pass; d=google.com; s=arc-20160816; b=ulaTw60EfIZMnf+4KbSu5jlN2cp8ME4bIeclo43jlAr1uWK1fsAKB6CpWtKrdvrmdK LlBDAuWkGTlbAUt3mxYoDQA2VOT2KaQXEOF/2jhcdWKU7oC7Pas04l0U2NIs1IzMmSJH pO7V7Nz7m2mv4p2G5hYDjbuRdNlSCm5Fi4b9/Ps4d5Sfjnwk3QCXz7TcnxuieWBfhWR3 DKtfaUo7S6nFSFB2O2kzDHFzarOUCsFBjfDE6bWkVM1NhpgdGv8Hihtllj0dBJly+znO Cj0f9OjegVFlNhJ4pKKoE+0eLgKHd4MhdH4MWwOaTCT7cQ6cKcpBLNL1vwfQb2He7RND P+sg== 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 :message-id:date:subject:to:from:dkim-signature:arc-filter :dmarc-filter:delivered-to; bh=FJEfFhgXsEDqUaN4ZvSjtX7nbL45xk/GkiN3LJtfqYg=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=F0K5J1Y+2fG+2AMBrzpL6GjEBuFsh6N9FC4xkNAPDdmp+dR0Yb4bEE25fVtRNiWmP/ /ccoEEJx9m+9gI3N3/ERt3HaIx+8Js98uG0RzHTg1++8kw0SyprJRZdxcSquQ7cWbCst uwgx2ZXC47EhgNg5pTnjXg9vnLv1/XLbXIsFBYLafZdkTjazhnc4Y8qWPhZxYrQtxs9j w7wbePcZDGSItIoAn1KLXDK6gjzvR/2e5jadKU2xoy4ct+ckIgxz0/cHMXsdlOgcFs5a 29ORzKicRe8lywWzzPG8oqyMJlQauNTckN8DiMMrIiNteZAeRI42IWNB6Wvmz+7vY9BZ +QxQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZtQKHEBp; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id t15-20020a05621405cf00b0065b1ab39c55si1017520qvz.540.2023.10.20.01.55.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 01:55:29 -0700 (PDT) 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=@redhat.com header.s=mimecast20190719 header.b=ZtQKHEBp; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 130133858410 for ; Fri, 20 Oct 2023 08:55:29 +0000 (GMT) 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 56E433858D1E for ; Fri, 20 Oct 2023 08:55:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 56E433858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 56E433858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697792103; cv=none; b=npaAp+tPzL9Nhy4Z0VhXmyV1ag70FFE08BthBbrQF0f49dnEqk48U2OQiqpT6+Tp/b74Vit71UBLVOqSWDSbj4JeoZNO0PiVuEMILXM76geexki3vhp5BPHfI0Bajnc2mLxajADAukk69VYs/D2TjDQ0OJ5xk1GcLwZgNloVBH8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697792103; c=relaxed/simple; bh=KOOF+DTSkQ96oNjwCWEn+gAoufO7eisq/O5Q//DrE+A=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=oWpRK1I5sFz+s+Ys9vtUCmmDJOpxdjEip6iohVocyLWihyfy0rkxHOsXqI0ckmvdag3WErSqPtNcofBbMZOY3SUyTylwuCqyfkRFghfSau/6BI7BjiGOAycsiYpsnHMAT3Cno3iBhGU8vYBZo6/cBdZmZTXKHRNsPneYA3HASec= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697792102; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=FJEfFhgXsEDqUaN4ZvSjtX7nbL45xk/GkiN3LJtfqYg=; b=ZtQKHEBpoQkHVltArsvFX2aZrx36sYwhdF5ZtvtS9OykxWwEfhlhJF6RweVBugeZSamqC+ d18hcydTtvy5n1HBroyKPjWxv73Rp0LYu3PbE4KYWv/2k+UoQHOG0j+utrxfsIUCqL5GAF 0nfqmJaAphDeHWDD+VBjC5FD87245Qk= 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-362-YMli0mTlNOeXXFjyDlzRXA-1; Fri, 20 Oct 2023 04:54:55 -0400 X-MC-Unique: YMli0mTlNOeXXFjyDlzRXA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D89A610201E6 for ; Fri, 20 Oct 2023 08:54:54 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.23]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8077B2166B26 for ; Fri, 20 Oct 2023 08:54:54 +0000 (UTC) From: Florian Weimer To: gcc-patches@gcc.gnu.org Subject: [PATCH] c: -Wincompatible-pointer-types should cover mismatches in ?: Date: Fri, 20 Oct 2023 10:54:53 +0200 Message-ID: <87pm19znaa.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.6 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_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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.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: 1780264079588870145 X-GMAIL-MSGID: 1780264079588870145 gcc/c/ PR c/109826 PR other/44209 * c-typeck.cc (build_conditional_expr): Use OPT_Wincompatible_pointer_types for pointer mismatches. Emit location information for the operand. gcc/testsuite/ * gcc.dg/Wincompatible-pointer-types-4.c: New. * gcc.dg/Wincompatible-pointer-types-5.c: New. * gcc.dg/Wincompatible-pointer-types-6.c: New. --- gcc/c/c-typeck.cc | 16 ++++++++-------- gcc/testsuite/gcc.dg/Wincompatible-pointer-types-2.c | 10 ++++++++++ gcc/testsuite/gcc.dg/Wincompatible-pointer-types-3.c | 10 ++++++++++ gcc/testsuite/gcc.dg/Wincompatible-pointer-types-4.c | 10 ++++++++++ 4 files changed, 38 insertions(+), 8 deletions(-) base-commit: 8f4bbdc28df6e87a7ad5ec5ca191a7a836a4f016 diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 6e044b4afbc..0bfae129491 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -5564,14 +5564,14 @@ build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, else { int qual = ENCODE_QUAL_ADDR_SPACE (as_common); - if (bltin1 && bltin2) - warning_at (colon_loc, OPT_Wincompatible_pointer_types, - "pointer type mismatch between %qT and %qT " - "of %qD and %qD in conditional expression", - type1, type2, bltin1, bltin2); - else - pedwarn (colon_loc, 0, - "pointer type mismatch in conditional expression"); + if (emit_diagnostic (bltin1 && bltin2 ? DK_WARNING : DK_PEDWARN, + colon_loc, OPT_Wincompatible_pointer_types, + "pointer type mismatch " + "in conditional expression")) + { + inform (op1_loc, "first expression has type %qT", type1); + inform (op2_loc, "second expression has type %qT", type2); + } result_type = build_pointer_type (build_qualified_type (void_type_node, qual)); } diff --git a/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-2.c b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-2.c new file mode 100644 index 00000000000..19276bbcfba --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-2.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "" } */ + +void * +f1 (int flag, int *a, long *b) +{ + return flag ? a : b; /* { dg-warning "pointer type mismatch in conditional expression \\\[-Wincompatible-pointer-types\\\]" } */ + /* { dg-note "first expression has type 'int \\*'" "" { target *-*-* } .-1 } */ + /* { dg-note "second expression has type 'long int \\*'" "" { target *-*-* } .-2 } */ +} diff --git a/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-3.c b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-3.c new file mode 100644 index 00000000000..3a6f35e79d9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-3.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c90 -pedantic-errors" } */ + +void * +f1 (int flag, int *a, long *b) +{ + return flag ? a : b; /* { dg-error "pointer type mismatch in conditional expression \\\[-Wincompatible-pointer-types\\\]" } */ + /* { dg-note "first expression has type 'int \\*'" "" { target *-*-* } .-1 } */ + /* { dg-note "second expression has type 'long int \\*'" "" { target *-*-* } .-2 } */ +} diff --git a/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-4.c b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-4.c new file mode 100644 index 00000000000..c62a7e02bb9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-4.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c90 -pedantic-errors" } */ + +void * +f1 (int flag) +{ + return flag ? __builtin_memcpy : __builtin_memcmp; /* { dg-warning "pointer type mismatch in conditional expression \\\[-Wincompatible-pointer-types\\\]" } */ + /* { dg-note "first expression has type 'void \\* \\(\\*\\)\\(void \\*," "" { target *-*-* } .-1 } */ + /* { dg-note "second expression has type 'int \\(\\*\\)\\(const void \\*," "" { target *-*-* } .-2 } */ +}