From patchwork Thu Aug 10 11:39:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 133928 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp355590vqi; Thu, 10 Aug 2023 04:41:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IESTIzFeHfRQsayMfh69zGD+edj/l7PsYNdNrz8eYSLwgPG8KgblXiKUCmMWKpUAcOX85Bm X-Received: by 2002:a05:6402:610:b0:523:2e23:a0bf with SMTP id n16-20020a056402061000b005232e23a0bfmr2382179edv.11.1691667676162; Thu, 10 Aug 2023 04:41:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691667676; cv=none; d=google.com; s=arc-20160816; b=w3P38aXpeOmYxXesAyIPgND75U+opoefpLK4CtTXQlxE+rU7I1U4lIAUopP9ql67a0 0UB+HYKS8pZ/a/oFGJWqvyHGlTor085bGNDE9wBXs38rU5AnC7+QbMSznLPCeLHsUT5H 5JmTfHaDb6D3sOODK+zGj7eV1+ZlthYnvxUObgF+3VRR2+pgPqhcMCW2DWQ4dVlP55Gp +zFb75J2kj66lybORRiEUrsUVdt6W0Kcq79ldwn7AarEya4+e5unkTGUIbaeA2UXkX3s Fh8tf179dy+nCAe5MzomHjx9LxKNImcAPVqy/sWCsDo4hMvVz43uPrD6c00EOfnxGJFd QvPg== 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:mime-version:user-agent:date:cc:to :subject:message-id:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=47oeyOMun/GQ7Ho4fhFFLzbDm34MOfHOMhYtFy8Indk=; fh=R/+sD+UPvJdLVX74ua6+DVhn5JOW3tdPUZPuWbX+7uQ=; b=l78ITumxn0aFQ06VtOuvTnn05weShZyOlH94PqcHa85uKp3DS0LCeLU5OgL6SMA6p6 AcHTmcUDzEXx+7wOD5jCTzjfzEuvOXNUiyEelZtf6zORc/LKa1cFHjEvznQ3WfkXFFy7 ApkMzupr355WJnE37Q1SRHkWtVTgGQgTUgxyLJ4jZeFLvWPWk6sTP4PtGzzPcXF9rcn9 vewggfMUAoeY08qoES2ZOvjnx0msciYY78d2kJp1bBrN90bqpqgTDiNngGzMLZLc6wyq GP6X/QwmSRDb5iRDRR2t4NGOqN2KD59YoJlMy/5W+YSufe0VRNH9wJbmUAnpdr4/ns8G Lglw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=nyMX74dk; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id r16-20020a056402035000b0052334c8d61esi1325273edw.355.2023.08.10.04.41.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 04:41:16 -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=@gcc.gnu.org header.s=default header.b=nyMX74dk; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4AAE43853D0E for ; Thu, 10 Aug 2023 11:40:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4AAE43853D0E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691667634; bh=47oeyOMun/GQ7Ho4fhFFLzbDm34MOfHOMhYtFy8Indk=; h=Subject:To:Cc:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=nyMX74dkfWtzDfdsmoCshwI663zP5ju3IE/57buodOANSSo2xGhK+1RhzbOlsFO1D 7h40wNYM/8tq3jYg434KHNvXNcIwp4+xdqinbdy7PjvVbpwrSWd702NEJv98oW1LOJ NH6kFly3XYfsx6jJvFyOjI3KGby39s9tXMFNGwTo= 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 E4EC63858D20 for ; Thu, 10 Aug 2023 11:39:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E4EC63858D20 Received: from fbmtpc21.tugraz.at (fbmtpc21.tugraz.at [129.27.144.40]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4RM4hx50ntz3wgY; Thu, 10 Aug 2023 13:39:45 +0200 (CEST) Message-ID: <573a12823f25ecf649457f32018f2f53a2a8b3a2.camel@tugraz.at> Subject: c: Support for -Wuseless-cast [RR84510] To: gcc-patches@gcc.gnu.org Cc: Joseph Myers Date: Thu, 10 Aug 2023 13:39:45 +0200 User-Agent: Evolution 3.38.3-1+deb11u2 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=-10.7 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Uecker via Gcc-patches From: Martin Uecker Reply-To: Martin Uecker Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773842125123397994 X-GMAIL-MSGID: 1773842125123397994 This patch adds the missing support for -Wuseless-cast to the C FE as requested by some users. It found about  50 useless casts in one of my projects without false  positives. (I also implemented a detection for various unneeded pointer casts in convert_for_assignment such as unneeded casts from / to void or casts followed by an implicit conversion to the original type, but I did not figure out how to reliably  identify casts there... But this would be a potential future enhancement.) Regression tested on bootstrapped on x86_64-pc-linux-gnu. c: Support for -Wuseless-cast [RR84510] Add support for Wuseless-cast C (and ObjC). PR c/84510 gcc/c/: * c-typeck.cc (build_c_cast): Add warning. gcc/doc/: * invoke.texi: Update. gcc/testsuite/: * Wuseless-cast.c: New test. diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 0ed87fcc7be..c7b567ba7ab 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1490,7 +1490,7 @@ C++ ObjC++ Var(warn_zero_as_null_pointer_constant) Warning Warn when a literal '0' is used as null pointer. Wuseless-cast -C++ ObjC++ Var(warn_useless_cast) Warning +C ObjC C++ ObjC++ Var(warn_useless_cast) Warning Warn about useless casts. Wsubobject-linkage diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 7cf411155c6..6f2fff51683 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -6062,9 +6062,13 @@ build_c_cast (location_t loc, tree type, tree expr) if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) { - if (RECORD_OR_UNION_TYPE_P (type)) - pedwarn (loc, OPT_Wpedantic, - "ISO C forbids casting nonscalar to the same type"); + if (RECORD_OR_UNION_TYPE_P (type) + && pedwarn (loc, OPT_Wpedantic, + "ISO C forbids casting nonscalar to the same type")) + ; + else if (warn_useless_cast) + warning_at (loc, OPT_Wuseless_cast, + "useless cast to type %qT", type); /* Convert to remove any qualifiers from VALUE's type. */ value = convert (type, value); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 674f956f4b8..75ca72f3190 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -4772,7 +4772,7 @@ pointers after reallocation. @opindex Wuseless-cast @opindex Wno-useless-cast -@item -Wuseless-cast @r{(C++ and Objective-C++ only)} +@item -Wuseless-cast @r{(C, Objective-C, C++ and Objective-C++ only)} Warn when an expression is cast to its own type. This warning does not occur when a class object is converted to a non-reference type as that is a way to create a temporary: diff --git a/gcc/testsuite/gcc.dg/Wuseless-cast.c b/gcc/testsuite/gcc.dg/Wuseless-cast.c new file mode 100644 index 00000000000..86e87584b87 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wuseless-cast.c @@ -0,0 +1,26 @@ +/* { dg-do compile } */ +/* { dg-options "-Wuseless-cast" } */ + +void foo(void) +{ + // casts to the same type + int i = 0; + const int ic = 0; + struct foo { int x; } x = { 0 }; + int q[3]; + (int)ic; /* { dg-warning "useless cast" } */ + (int)i; /* { dg-warning "useless cast" } */ + (const int)ic; /* { dg-warning "useless cast" } */ + (const int)i; /* { dg-warning "useless cast" } */ + (struct foo)x; /* { dg-warning "useless cast" } */ + (int(*)[3])&q; /* { dg-warning "useless cast" } */ + (_Atomic(int))i; /* { dg-warning "useless cast" } */ + + // not the same + int n = 3; + (int(*)[n])&q; // no warning + int j = (int)0UL; + enum X { A = 1 } xx = { A }; + enum Y { B = 1 } yy = (enum Y)xx; +} +