From patchwork Thu Aug 17 14:05:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Jose E. Marchesi" X-Patchwork-Id: 135892 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp724824vqi; Thu, 17 Aug 2023 07:06:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDHTI08QmPHI3f8eoFUUT4H9gGtzahN+lU/BdANzFqs4rJUZhEl120ufuxMN4iDVTNIxWL X-Received: by 2002:a05:6512:29d:b0:4f7:6017:8fb with SMTP id j29-20020a056512029d00b004f7601708fbmr4064561lfp.26.1692281184077; Thu, 17 Aug 2023 07:06:24 -0700 (PDT) Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id b7-20020aa7c907000000b005232d645f14si12872163edt.563.2023.08.17.07.06.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 07:06:24 -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=@gcc.gnu.org header.s=default header.b=L4A4agHq; arc=fail (signature failed); 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 CCC55385C6DE for ; Thu, 17 Aug 2023 14:06:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CCC55385C6DE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692281182; bh=C7tvg6HaVwtffVsDuZ9iiwUo8W6/mk3R3MpK8VQqhCQ=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=L4A4agHqiYen52JW6i2KhEZhdZjpm6LHzW57AukzayClKeQoJolBu1FaLFfnY4b5N Q7VuNiShvoaLrlrAvo03S6Ah6A7ro1guy2oi+xOm3GdhrfucMLhtHo4iFZsz7jGqq+ LOpLbhnWkfPUwHfKqQ/4k4PESL7pYMkkDNuUPoiw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 49CF93858D35 for ; Thu, 17 Aug 2023 14:05:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 49CF93858D35 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 37HCOMQJ016539; Thu, 17 Aug 2023 14:05:32 GMT Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3se2w61m2b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 17 Aug 2023 14:05:31 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 37HDZhdO006606; Thu, 17 Aug 2023 14:05:29 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2168.outbound.protection.outlook.com [104.47.58.168]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3sey2fwa6r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 17 Aug 2023 14:05:25 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mZiY0Jl19nwEAsWcJPGor9tFlwHpFkM2xXifrzlxmo9Hmlu76H5wAUr6hr71MeDdHOLsMLADn92JHTRy/qCiYURMDFnqLsLjJJ/aTk0LzkZYhQT+QlppFDk/x01WUEe9NnTE/ea4En/0ldgs73LOH9n4hPqe85a/NwuQIr3mdnXli0EeOpJZZYez6Yx9xifP5Rok1Gfm1FcLpncuv0qfvN3hOupdWeuPubh5LE7CxbKsmsksQpL/wWSvOMiykKtk1LFkeqDnO+KI1dL2AAoKYotTpUQ7Nh1b9AZkr7HjM9UjB18mQ9qvUKBg6z3+S9q+iP860ctVyj1NNTfJ67/l5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=C7tvg6HaVwtffVsDuZ9iiwUo8W6/mk3R3MpK8VQqhCQ=; b=JshyrOHmJ01NDJ1MQwHZPK+tTCi0JZs8H/TpjBfHUPV9BbEOiHJwkhdtHbfgZInKj4Ji5c9XIoKgC3inKFGaKjc4XDjg2xRVxq1K+ZDfZj5kQEQexF5WLpPod6jcWBfq+IjFCd7sE8YJ6yzozlR4Om9jtWwJVqtHQU6ffSeK7r1EW58Ubi05qgxZpGO7Eji/GaXegxpmGPjplzOSkoQh2k0qvMV9JQXu4ZNT9oo1gJvfLwgXosNRnt9FI6zFz6tVaa4EIlE72NkYhr/HR2F2+Xl1qGqRMLyJZarUIvM7I0ODgnc8TbnZrG5ksWVQpECu349VaFglsZ0SzQhNcRPEQw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from BYAPR10MB2888.namprd10.prod.outlook.com (2603:10b6:a03:88::32) by SA2PR10MB4459.namprd10.prod.outlook.com (2603:10b6:806:11f::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6678.31; Thu, 17 Aug 2023 14:05:19 +0000 Received: from BYAPR10MB2888.namprd10.prod.outlook.com ([fe80::7d31:72cf:ebed:894f]) by BYAPR10MB2888.namprd10.prod.outlook.com ([fe80::7d31:72cf:ebed:894f%5]) with mapi id 15.20.6678.029; Thu, 17 Aug 2023 14:05:19 +0000 To: gcc-patches@gcc.gnu.org Cc: joseph@codesourcery.com Subject: [PATCH V4] Add warning options -W[no-]compare-distinct-pointer-types Date: Thu, 17 Aug 2023 16:05:13 +0200 Message-Id: <20230817140513.26888-1-jose.marchesi@oracle.com> X-Mailer: git-send-email 2.30.2 X-ClientProxiedBy: LO4P265CA0108.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2c3::10) To BYAPR10MB2888.namprd10.prod.outlook.com (2603:10b6:a03:88::32) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB2888:EE_|SA2PR10MB4459:EE_ X-MS-Office365-Filtering-Correlation-Id: 99955281-2ba1-474a-4887-08db9f2afd2b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hZCah7mxrLFRN/xTXvOiqG38oKEIwYb8Gk5OiGHqFQVrUWW25XaZ1jK+Fy+Dl4NIrT/nwnikR3bEumIiZVBS47Tt52DEAZSWp3oIPmRQ+z525j/ByNsIGoY54w9pwuBuFEgS+dANM3B7xI/4vqPSrpMQcvByYHolIB2y116HKo5+G4zP84pQCSWv0v51VNBUu369BjKrCq1i1iMJQE9hxVjf9/qBBvwz+z/H+w0hf3Zcfoetyge0MvgVJkD7Q+lMR5e2r8qquwTxIZxsOo9n1IzvL2Lks69nML1xLfqU+uMoI98a4/BOJG4bTm7mesxp09OQz6gSG57TGnaNIbrlvYNFrAkiBcmjiPRYsrNdcLhzutcCfNUIHRUvOFMhWLAJFkuQwMdqFe5VvpSe8x73lICFWHKmv2W/aeMy9d5Tgk5gGAivzkGdRxxmZNMlLwDXlWzsqbD8cOYJ0E7fnvU8rhHrSSvg+mw7Z0CDUajIn+uJvCrY4qkM37gQd2OH5KS2pc9W3X9n9HUfk3nzXKW+zGIc0VwPzLFyk64zKnA2t1U= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BYAPR10MB2888.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(376002)(366004)(136003)(346002)(39860400002)(396003)(186009)(451199024)(1800799009)(36756003)(86362001)(83380400001)(8936002)(5660300002)(8676002)(4326008)(2906002)(41300700001)(1076003)(26005)(6666004)(6506007)(2616005)(6486002)(6512007)(966005)(478600001)(316002)(6916009)(66476007)(38100700002)(66556008)(66946007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?ulcI2THKXfy9fVrKi4OwfOnWYZ6I?= =?utf-8?q?I+x45izWRtsqKsMT3jM8RC1Ppi01dYSi10wDfNhGFdLR0HeBVPto1FYw3do6Z7dmw?= =?utf-8?q?3MR7CodSNIkqHfJrKEYU1ck1EauZ3MEaiHS1QaBiXMLcP93igLT3R7Bltym7n1xjA?= =?utf-8?q?i03pe3NPi6fmT67l8Ga/WI3W4NkVW8EMtshKNerDF+7lrDdDcjcFgJlVdGeFbgjKY?= =?utf-8?q?SVPGNLCpB4v3xOMy/LFu5/NmMqNUp/6uGpSYcDYXLGppWz/qZtnd6Q2X52BXwxnuT?= =?utf-8?q?wzp8ZsS5lvuiTtKg/ubGutjZI77+gGsagcDFnRfHneSJQn3vtzNlc/4BffnwE1O9H?= =?utf-8?q?UhQJXlETcNZls5xwsBSJGoDneNLLp1dl00Vf+IMF1ILLzXN5fBWK/YqWTjNMVdTl+?= =?utf-8?q?DaorT3f6t2niyj6koVZXTuV93F0JR0dKW+dDogXTd2KTN149gBiCmsQDMqOi28AS1?= =?utf-8?q?JKe/fMPFUFrWFTVv3Tz+rtBl/Qd5jEBSNPD2439Ez+ABlTm5cj/M58Hgb10R3To1i?= =?utf-8?q?WRGK5eA6YiUn/vrUQOTRwDJBHFW+aX5Xm3wMOucwZ8YTxyBuZaAJR0xTaQMyZxYwm?= =?utf-8?q?n4UDApyrV1XOAwiSduxLtnIzwalhYCZppELu4WP3yyNOFnQbBPmZknFqsPF6g3FYN?= =?utf-8?q?NNcRil3CgGUat2dm6ihka50+Wo1Sd235heSKuj7HmgKhuVMnvzzHyfCEzmS6q/MgW?= =?utf-8?q?1uyze4xvfVF8NQXNDlMQGgPAMP9XRyzXaRwbQ/mYgckHg950wYvVsfZtlnWbuDRJh?= =?utf-8?q?lNJRtmoelc81VRYjlVLivzFnpVaxMwm2HM8H4Ae/uz3L4Zjw/+dPf2KbWgDX1IgEv?= =?utf-8?q?0TIf05VIDi7UH8Devit8JCtdlH37fhsNSpuxijF6chLdL5+2Z0Lmht8UXqQ2qR14B?= =?utf-8?q?z3dUUhrsekfB6zIvOQZdeRGMnXTeY//eH6I+sC7sml1US4EAdIP3aNXiJHekwEXcd?= =?utf-8?q?zqc9ttdrH0kQ+C6tmy6Tv9ySW5lj+G3PK6sLMVCqN71cDGs9a0omc9Mp8EBsRbN3O?= =?utf-8?q?nwQvsfDgn20JyP4rxuows7AumQuXCEfp/LQJvvGG0BhRlpKeyoSUVGWUQcPasfRkh?= =?utf-8?q?3XNqvlWJl+S8kPfwy6UQsNm7WaDCoYlGy/MyTn7gnKmZYoJLhdlF22L5knmf+LQiO?= =?utf-8?q?wOb+PMuHfWpvzaiL0lCYHHU1I7v4QvXXa2bTtCTNj/rCWO4HuMXREk9mEZgghzRsZ?= =?utf-8?q?L0BJIWf3KHXigOserJNtQQ5gEgt8VsX9ZUtrmq/2P9iEuB4P83cRyzcUPkIBUxg9l?= =?utf-8?q?1iIxeQ4k2iIFSUj6vT9HT2zOCUA68c+c84XpXiygDCytgvW6HoHPJI4ymt12BKKJj?= =?utf-8?q?iVrXHuD+/sp0ZeicuWQduMS5le0cDcO/4Odtv5+JB1CSfZaJ0HBprLrVqtEw8R/CA?= =?utf-8?q?m1vKqT7qvIUELI3fpUwWDwiJ2HS6ejm6fooU11Fpa23l1upgxZrhg3N4lcWyXy+gY?= =?utf-8?q?K04rjVa6SvulhEuIDLuldZRP9HY083MOB+2wpmOEFV3kss355JQwNPtWGJySUaaZ+?= =?utf-8?q?2XE7eTyOgha/9YxycRIn0gfQWM/DD3rtxg=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: DvLHD6CwlJUqrBbzLOGmYj8SDPIriSDOyTVQTZRVwd0gJaRTjDicqDabER6qI89LktGRcttTsp9BgpMaUpU9BkfP/ZbqLtqdptH/xLqtjgANFUvDR09qfJa/R93myHWBdylIPmmju/K7w34QxudqCQWDOZuzTkdrwJTpvwtuDDBe+atNdLFP3fZniV6MSWkaLPFLtx8ao4VxO4jUh8sW5C3boh5taIy7itZqpu3ui7YiFI8HZ6S4DrL2nmZxn50nci0f9Ku5EsTUKG8aBsO77fkhtip/QaHYOJv+ymII9f9fSJYLRLE2rMi8r9+bE/lP06zfsTy86FwLPnwRwq67nUDFNdMw3c0r1/zWkGNj8H7pRUYkxwNK0S/EhBltWy67JZoABRFRUHXJZwvw1iplB13Ut2gz+Kh1s23BBNc1zxJc0L6EjmXZG61RUeXg2DLBb96WaBtwog+EqdIBPWUNR5sMSyZjcu1Lanh553Ug2TAIm1gSpHZjQrueWMWPFNFtd4YiOzETjshtu+5ADsRMFR4I2pOHX6069KQEXzd2cjy7MKsDANe2l641oNe0EQOacWxMT/Pa7CNvQ3tUvDLyOu/S9iQv3MeLZoQRckcT51M0k8zCE8Jb0H2gjNmevpJXOne13AsXyCEGJPmaExqkvmsO7t+mbtxapGQT8RXo7w3nxdjn0GJMCF5gBQGjgelylZ1vxT8EihAHRiV4fqeo1Ea9isPErYmpLaEnm5eCVJSJKAnEZrEwyJvdwT4dXXNEQK3uM1gMS/+3eCRjmQChOhDh5wGRj5k0P9tGkohqBy8= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 99955281-2ba1-474a-4887-08db9f2afd2b X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB2888.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Aug 2023 14:05:19.0322 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: i/e2YedV5Z+3rgJCnJkeUxS65Sa+qy4WBtBC9qYc75rWQUzAuxD+pD8cxfNwuesChnJCtReK4SsMNpJMup94JqirESOW0jAMjcKjg21tgCU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4459 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-08-17_08,2023-08-17_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 mlxlogscore=999 bulkscore=0 adultscore=0 spamscore=0 malwarescore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2306200000 definitions=main-2308170127 X-Proofpoint-ORIG-GUID: VPbuZFqJxPokpy7HsH2DWYrQFINbx6nk X-Proofpoint-GUID: VPbuZFqJxPokpy7HsH2DWYrQFINbx6nk X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "Jose E. Marchesi via Gcc-patches" From: "Jose E. Marchesi" Reply-To: "Jose E. Marchesi" Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774485434992379681 X-GMAIL-MSGID: 1774485434992379681 [Changes from V3: - Previous thread: https://gcc.gnu.org/pipermail/gcc-patches/2022-August/600625.html - The tests have been augmented to check all six relational operators. In particular it covers both code paths impacted by the patch: the equality/inequality and the relational ops.] GCC emits pedwarns unconditionally when comparing pointers of different types, for example: int xdp_context (struct xdp_md *xdp) { void *data = (void *)(long)xdp->data; __u32 *metadata = (void *)(long)xdp->data_meta; __u32 ret; if (metadata + 1 > data) return 0; return 1; } /home/jemarch/foo.c: In function ‘xdp_context’: /home/jemarch/foo.c:15:20: warning: comparison of distinct pointer types lacks a cast 15 | if (metadata + 1 > data) | ^ LLVM supports an option -W[no-]compare-distinct-pointer-types that can be used in order to enable or disable the emission of such warnings. It is enabled by default. This patch adds the same options to GCC. Documentation and testsuite updated included. Regtested in x86_64-linu-gnu. No regressions observed. gcc/ChangeLog: PR c/106537 * doc/invoke.texi (Option Summary): Mention -Wcompare-distinct-pointer-types under `Warning Options'. (Warning Options): Document -Wcompare-distinct-pointer-types. gcc/c-family/ChangeLog: PR c/106537 * c.opt (Wcompare-distinct-pointer-types): New option. gcc/c/ChangeLog: PR c/106537 * c-typeck.cc (build_binary_op): Warning on comparing distinct pointer types only when -Wcompare-distinct-pointer-types. gcc/testsuite/ChangeLog: PR c/106537 * gcc.c-torture/compile/pr106537-1.c: New test. * gcc.c-torture/compile/pr106537-2.c: Likewise. * gcc.c-torture/compile/pr106537-3.c: Likewise. --- gcc/c-family/c.opt | 4 +++ gcc/c/c-typeck.cc | 6 ++-- gcc/doc/invoke.texi | 6 ++++ .../gcc.c-torture/compile/pr106537-1.c | 34 +++++++++++++++++++ .../gcc.c-torture/compile/pr106537-2.c | 32 +++++++++++++++++ .../gcc.c-torture/compile/pr106537-3.c | 32 +++++++++++++++++ 6 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr106537-1.c create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr106537-2.c create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr106537-3.c diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index c7b567ba7ab..2242524cd3e 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1935,6 +1935,10 @@ Winvalid-imported-macros C++ ObjC++ Var(warn_imported_macros) Warning Warn about macros that have conflicting header units definitions. +Wcompare-distinct-pointer-types +C ObjC Var(warn_compare_distinct_pointer_types) Warning Init(1) +Warn if pointers of distinct types are compared without a cast. + flang-info-include-translate C++ Var(note_include_translate_yes) Note #include directives translated to import declarations. diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 6f2fff51683..e6ddf37d412 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -12772,7 +12772,7 @@ build_binary_op (location_t location, enum tree_code code, else /* Avoid warning about the volatile ObjC EH puts on decls. */ if (!objc_ok) - pedwarn (location, 0, + pedwarn (location, OPT_Wcompare_distinct_pointer_types, "comparison of distinct pointer types lacks a cast"); if (result_type == NULL_TREE) @@ -12912,8 +12912,8 @@ build_binary_op (location_t location, enum tree_code code, int qual = ENCODE_QUAL_ADDR_SPACE (as_common); result_type = build_pointer_type (build_qualified_type (void_type_node, qual)); - pedwarn (location, 0, - "comparison of distinct pointer types lacks a cast"); + pedwarn (location, OPT_Wcompare_distinct_pointer_types, + "comparison of distinct pointer types lacks a cast"); } } else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 3380ed8bd6f..28ee6fb62bb 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -345,6 +345,7 @@ Objective-C and Objective-C++ Dialects}. -Wcast-align -Wcast-align=strict -Wcast-function-type -Wcast-qual -Wchar-subscripts -Wclobbered -Wcomment +-Wcompare-distinct-pointer-types -Wno-complain-wrong-lang -Wconversion -Wno-coverage-mismatch -Wno-cpp -Wdangling-else -Wdangling-pointer -Wdangling-pointer=@var{n} @@ -9106,6 +9107,11 @@ The latter front end diagnoses @samp{f951: Warning: command-line option '-fno-rtti' is valid for C++/D/ObjC++ but not for Fortran}, which may be disabled with @option{-Wno-complain-wrong-lang}. +@opindex Wcompare-distinct-pointer-types +@item -Wcompare-distinct-pointer-types +Warn if pointers of distinct types are compared without a cast. This +warning is enabled by default. + @opindex Wconversion @opindex Wno-conversion @item -Wconversion diff --git a/gcc/testsuite/gcc.c-torture/compile/pr106537-1.c b/gcc/testsuite/gcc.c-torture/compile/pr106537-1.c new file mode 100644 index 00000000000..3f3b06577d5 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr106537-1.c @@ -0,0 +1,34 @@ +/* { dg-do compile } + { dg-options "-O0" } + This testcase checks that warn_compare_distinct_pointer_types is enabled by + default. */ + +typedef int __u32; + +struct xdp_md +{ + char *data; + char *data_meta; +}; + +int xdp_context (struct xdp_md *xdp) +{ + void *data = (void *)(long)xdp->data; + __u32 *metadata = (void *)(long)xdp->data_meta; + __u32 ret; + + if (metadata + 1 > data) /* { dg-warning "comparison of distinct pointer types" } */ + return 1; + if (metadata + 1 >= data) /* { dg-warning "comparison of distinct pointer types" } */ + return 2; + if (metadata + 1 < data) /* { dg-warning "comparison of distinct pointer types" } */ + return 3; + if (metadata + 1 <= data) /* { dg-warning "comparison of distinct pointer types" } */ + return 4; + if (metadata + 1 == data) /* { dg-warning "comparison of distinct pointer types" } */ + return 5; + if (metadata + 1 != data) /* { dg-warning "comparison of distinct pointer types" } */ + return 5; + + return 1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr106537-2.c b/gcc/testsuite/gcc.c-torture/compile/pr106537-2.c new file mode 100644 index 00000000000..6876adf3aab --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr106537-2.c @@ -0,0 +1,32 @@ +/* { dg-do compile } */ +/* { dg-options "-Wcompare-distinct-pointer-types" } */ + +typedef int __u32; + +struct xdp_md +{ + char *data; + char *data_meta; +}; + +int xdp_context (struct xdp_md *xdp) +{ + void *data = (void *)(long)xdp->data; + __u32 *metadata = (void *)(long)xdp->data_meta; + __u32 ret; + + if (metadata + 1 > data) /* { dg-warning "comparison of distinct pointer types" } */ + return 1; + if (metadata + 1 >= data) /* { dg-warning "comparison of distinct pointer types" } */ + return 2; + if (metadata + 1 < data) /* { dg-warning "comparison of distinct pointer types" } */ + return 3; + if (metadata + 1 <= data) /* { dg-warning "comparison of distinct pointer types" } */ + return 4; + if (metadata + 1 == data) /* { dg-warning "comparison of distinct pointer types" } */ + return 5; + if (metadata + 1 != data) /* { dg-warning "comparison of distinct pointer types" } */ + return 5; + + return 1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr106537-3.c b/gcc/testsuite/gcc.c-torture/compile/pr106537-3.c new file mode 100644 index 00000000000..73c9b251824 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr106537-3.c @@ -0,0 +1,32 @@ +/* { dg-do compile } */ +/* { dg-options "-O0 -Wno-compare-distinct-pointer-types" } */ + +typedef int __u32; + +struct xdp_md +{ + char *data; + char *data_meta; +}; + +int xdp_context (struct xdp_md *xdp) +{ + void *data = (void *)(long)xdp->data; + __u32 *metadata = (void *)(long)xdp->data_meta; + __u32 ret; + + if (metadata + 1 > data) /* There shouldn't be a warning here. */ + return 1; + if (metadata + 1 >= data) /* There shouldn't be a warning here. */ + return 2; + if (metadata + 1 < data) /* There shouldn't be a warning here. */ + return 3; + if (metadata + 1 <= data) /* There shouldn't be a warning here. */ + return 4; + if (metadata + 1 == data) /* There shouldn't be a warning here. */ + return 5; + if (metadata + 1 != data) /* There shouldn't be a warning here. */ + return 5; + + return 1; +}