From patchwork Mon Jun 30 18:31:49 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 365714 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 1A7321400A7 for ; Tue, 1 Jul 2014 04:32:13 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=vPl6Yxss55t3/eCVYKrDq7/uo2xk7FsWGn+Dng3DoSOmRb//Fn g8TPfz21qNA0rvcO1svl9zonGrSY+1HbXnnZ/+Iv7cpK9SSyy/uJ3yDZ37LJz1Wz ez/uewS9CEly6dE9md36XHfGaEBCAJezU8gj91nh2u4yvd/iVfZVI29VA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=xKNaf+TRAdaEmt6UDtuv4a+Jp/A=; b=Pql2b8b5YNpvR3ThsRVJ 4Cloa4psp0Tbh540yPdu9n3CZRhjWxeISQpQ/KvJ+cgjfw98WAaEFSX4WBI1Xap5 vV0Q31TB6wJj3/9hr+tMw0aaDX5wVgXktMhaISMD+GuOPPRGZnxGorYuFQM/S80d 60A4j2l9y8pXNal4RLv3+rE= Received: (qmail 27166 invoked by alias); 30 Jun 2014 18:32:06 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 27121 invoked by uid 89); 30 Jun 2014 18:32:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.7 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 30 Jun 2014 18:31:55 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s5UIVqZr015509 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Jun 2014 14:31:52 -0400 Received: from redhat.com (ovpn-116-81.ams2.redhat.com [10.36.116.81]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s5UIVnfl002977 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NO); Mon, 30 Jun 2014 14:31:52 -0400 Date: Mon, 30 Jun 2014 20:31:49 +0200 From: Marek Polacek To: GCC Patches Cc: "Joseph S. Myers" Subject: [C PATCH] Add -Wint-conversion option Message-ID: <20140630183149.GE20427@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) Basically everything I wrote in the patch for -Wincompatible-pointer-types applies here as well. A new option, -Wint-conversion (to be compatible with clang), is added to allow more fine-grained control over the warnings. I think we should print the types here as well, and moreover, we could hint the user that & or * may be used to fix the code. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2014-06-30 Marek Polacek * doc/invoke.texi: Document -Wint-conversion. c-family/ * c.opt (Wint-conversion): New option. c/ * c-typeck.c (convert_for_assignment): Pass OPT_Wint_conversion instead of 0 to WARN_FOR_ASSIGNMENT. testsuite/ * gcc.dg/Wint-conversion.c: New test. Marek diff --git gcc/c-family/c.opt gcc/c-family/c.opt index 6448b1b..c89040a 100644 --- gcc/c-family/c.opt +++ gcc/c-family/c.opt @@ -474,6 +474,10 @@ Winherited-variadic-ctor C++ ObjC++ Var(warn_inh_var_ctor) Init(1) Warning Warn about C++11 inheriting constructors when the base has a variadic constructor +Wint-conversion +C ObjC Var(warn_int_conversion) Init(1) Warning +Warn about incompatible integer to pointer and pointer to integer conversions + Wint-to-pointer-cast C ObjC C++ ObjC++ Var(warn_int_to_pointer_cast) Init(1) Warning Warn when there is a cast to a pointer from an integer of a different size diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c index fff26a3..35bfd14 100644 --- gcc/c/c-typeck.c +++ gcc/c/c-typeck.c @@ -6213,7 +6213,8 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type, or one that results from arithmetic, even including a cast to integer type. */ if (!null_pointer_constant) - WARN_FOR_ASSIGNMENT (location, expr_loc, 0, + WARN_FOR_ASSIGNMENT (location, expr_loc, + OPT_Wint_conversion, G_("passing argument %d of %qE makes " "pointer from integer without a cast"), G_("assignment makes pointer from integer " @@ -6227,7 +6228,8 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type, } else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) { - WARN_FOR_ASSIGNMENT (location, expr_loc, 0, + WARN_FOR_ASSIGNMENT (location, expr_loc, + OPT_Wint_conversion, G_("passing argument %d of %qE makes integer " "from pointer without a cast"), G_("assignment makes integer from pointer " diff --git gcc/doc/invoke.texi gcc/doc/invoke.texi index dfae4f0..e6e71c0 100644 --- gcc/doc/invoke.texi +++ gcc/doc/invoke.texi @@ -253,7 +253,7 @@ Objective-C and Objective-C++ Dialects}. -Wframe-larger-than=@var{len} -Wno-free-nonheap-object -Wjump-misses-init @gol -Wignored-qualifiers -Wincompatible-pointer-types @gol -Wimplicit -Wimplicit-function-declaration -Wimplicit-int @gol --Winit-self -Winline @gol +-Winit-self -Winline -Wno-int-conversion @gol -Wno-int-to-pointer-cast -Wno-invalid-offsetof @gol -Winvalid-pch -Wlarger-than=@var{len} -Wunsafe-loop-optimizations @gol -Wlogical-op -Wlogical-not-parentheses -Wlong-long @gol @@ -4213,6 +4213,12 @@ can be used to suppress such a warning. Do not warn when there is a conversion between pointers that have incompatible types. +@item -Wno-int-conversion @r{(C and Objective-C only)} +@opindex Wno-int-conversion +@opindex Wint-conversion +Do not warn about incompatible integer to pointer and pointer to integer +conversions. + @item -Wno-div-by-zero @opindex Wno-div-by-zero @opindex Wdiv-by-zero diff --git gcc/testsuite/gcc.dg/Wint-conversion.c gcc/testsuite/gcc.dg/Wint-conversion.c index e69de29..1b7a03e 100644 --- gcc/testsuite/gcc.dg/Wint-conversion.c +++ gcc/testsuite/gcc.dg/Wint-conversion.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-Wno-int-conversion" } */ + +int fn1 (int *), *fn2 (int); + +int +fn1 (int *p) +{ + int i = p; + i = p; + fn2 (p); + return p; +} + +int * +fn2 (int i) +{ + int *p = i; + p = i; + fn1 (i); + return i; +}