From patchwork Thu Jun 27 05:16:55 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sharad Singhai X-Patchwork-Id: 254963 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 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id AB6522C0097 for ; Thu, 27 Jun 2013 15:17:45 +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 :mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; q=dns; s=default; b=Le3EoP71i2QASpQ rasQiKZJ6fAoxG2FBrXZB/GrfxV+93p3avTRUbHne5/kO2Ua/P96atvf711HY+2C x7+p8MOZBHSu896wVQ2eMuqBZmSNC3vE8mbdybkOn5tCOFtxHpSUD/3BIgeX9MlB 9G8cwcEY6JUsVSur4j4URfEgFTDU= 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 :mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; s=default; bh=ZLOHarjYUPfwmB7b2/xgD FlWHac=; b=Gtt8nSXen5xIuAWl05gowKZMWN35WIaw+MV7J+6vLwRR6Y4aPbzKl OyU5EcDoULz7PROOSaLXJX7I5yut1lppitKrv+W8BE1G+5aLIvEa19kaffvZoEqP GPBnwKaMiYbyhlh1GHvB9kPsY7BShIHylG0SxSsFZnVeuuBl8G4nY8= Received: (qmail 18542 invoked by alias); 27 Jun 2013 05:17:38 -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 18532 invoked by uid 89); 27 Jun 2013 05:17:38 -0000 X-Spam-SWARE-Status: No, score=-4.7 required=5.0 tests=AWL, BAYES_00, KHOP_THREADED, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.1 Received: from mail-oa0-f54.google.com (HELO mail-oa0-f54.google.com) (209.85.219.54) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Thu, 27 Jun 2013 05:17:37 +0000 Received: by mail-oa0-f54.google.com with SMTP id o6so329370oag.13 for ; Wed, 26 Jun 2013 22:17:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:x-gm-message-state; bh=toxzO/Wyj/jfBR3MLDReRHNwMtcg73A4t3vtYdOSHDM=; b=KBYlMKWZnilX7JphYWYBKj8vpjtEcX8C8LFgMzYohmDHiUHmaKj2qUY2h8D8pXeq2O m9DCWTdsLmeFwNvsu/AX9ho6+bHuIwEtJAItrKFz/JBsx/V8HEgBwcT/5e9bruevhif8 7oiT+2Tv5GqPTDkjNMaPGfeT/+bn1x/+ER2XPGKq2eTw6sFHeiolPl61HRMmfP8ixcku E1b891Pv01qY03d4ZNaJ62IVzLFCpTscIUbdn4TtrzDPL30xDsm6ikv/mzf+0TFZn/0o uyxNUTekdt05N5RbwpK4PcHbEN/fr9wSJlYXgb6Pi3I61hPbWlS5Gz03E+sne0cTR7wq r8hA== X-Received: by 10.182.81.34 with SMTP id w2mr3348792obx.8.1372310255540; Wed, 26 Jun 2013 22:17:35 -0700 (PDT) MIME-Version: 1.0 Received: by 10.182.51.234 with HTTP; Wed, 26 Jun 2013 22:16:55 -0700 (PDT) In-Reply-To: References: From: Sharad Singhai Date: Wed, 26 Jun 2013 22:16:55 -0700 Message-ID: Subject: Re: [google/gcc-4_8] Port -Wreal-conversion warning To: Xinliang David Li Cc: "gcc-patches@gcc.gnu.org" X-Gm-Message-State: ALoCoQlXZ2eZWhfPLG7Dq7yY3IbyUg28HCsyMfq7Zvw5/EADTyBEdiz89yOIz0GB9RdeDbCs4/Ir2wG7lrdEyzBTUSMfXR8ZErl13VlJB57hI1lEKfEX+PypN6tSFUws1YzDp3f6brhuw8b7/Z4iba/c9AmFPPNfWgzqv3jsrmNiSGLsoKPxGCh24YDxuwpvpvlZsYn2fl9B2BdMawzB3WGYljo6pWeJIw== I reverted the earlier broken patch. I am including an updated patch which warns only for real conversion, not for integral conversions. I also updated the test case to include an integral conversion (int to char) which doesn't emit the warning with the -Wreal-conversion flag. Bootstrapped and tested on x86_64. Okay for google/gcc-4_8? Thanks, Sharad 2013-06-26 * doc/invoke.texi: Document new option -Wreal-conversion. * c-family/c.opt: Handle new option. * c-family/c-opts.c (c_common_post_options): Ditto. * c-family/c-common.c (conversion_warning): Ditto. testsuite/ChangeLog: * testsuite/gcc.dg/Wreal-conversion-1.c: New test. * testsuite/g++.dg/warn/Wreal-conversion-1.C: Ditto. Index: doc/invoke.texi =================================================================== --- doc/invoke.texi (revision 200448) +++ doc/invoke.texi (working copy) @@ -237,7 +237,7 @@ Objective-C and Objective-C++ Dialects}. -Wno-attributes -Wno-builtin-macro-redefined @gol -Wc++-compat -Wc++11-compat -Wcast-align -Wcast-qual @gol -Wchar-subscripts -Wclobbered -Wcomment @gol --Wconversion -Wcoverage-mismatch -Wno-cpp -Wno-deprecated @gol +-Wconversion -Wreal-conversion -Wcoverage-mismatch -Wno-cpp -Wno-deprecated @gol -Wno-deprecated-declarations -Wdisabled-optimization @gol -Wno-div-by-zero -Wdouble-promotion -Wempty-body -Wenum-compare @gol -Wno-endif-labels -Werror -Werror=* @gol @@ -4452,6 +4452,12 @@ reference to them. Warnings about conversions betw unsigned integers are disabled by default in C++ unless @option{-Wsign-conversion} is explicitly enabled. +@item -Wreal-conversion +@opindex Wreal-conversion +@opindex Wno-real-conversion +Warn for implicit type conversions from real (@code{double} or @code{float}) +to integral values. This warning is also enabled by @option{-Wconversion}. + @item -Wno-conversion-null @r{(C++ and Objective-C++ only)} @opindex Wconversion-null @opindex Wno-conversion-null Index: c-family/c.opt =================================================================== --- c-family/c.opt (revision 200448) +++ c-family/c.opt (working copy) @@ -677,6 +677,10 @@ Wsign-compare C ObjC C++ ObjC++ EnabledBy(Wextra) ; +Wreal-conversion +C ObjC C++ ObjC++ Var(warn_real_conversion) Init(-1) Warning EnabledBy(Wconversion) +Warn for implicit type conversions from real to integral values + Wsign-conversion C ObjC C++ ObjC++ Var(warn_sign_conversion) LangEnabledBy(C ObjC,Wconversion) Warn for implicit type conversions between signed and unsigned integers Index: c-family/c-opts.c =================================================================== --- c-family/c-opts.c (revision 200448) +++ c-family/c-opts.c (working copy) @@ -876,6 +876,12 @@ c_common_post_options (const char **pfilename) if (warn_packed_bitfield_compat == -1) warn_packed_bitfield_compat = 1; + /* Enable warning for converting real values to integral values + when -Wconversion is specified (unless disabled through + -Wno-real-conversion). */ + if (warn_real_conversion == -1) + warn_real_conversion = warn_conversion; + /* Special format checking options don't work without -Wformat; warn if they are used. */ if (!warn_format) Index: c-family/c-common.c =================================================================== --- c-family/c-common.c (revision 200448) +++ c-family/c-common.c (working copy) @@ -2665,12 +2665,22 @@ unsafe_conversion_p (tree type, tree expr, bool pr static void conversion_warning (tree type, tree expr) { + int warn_option; tree expr_type = TREE_TYPE (expr); location_t loc = EXPR_LOC_OR_HERE (expr); - if (!warn_conversion && !warn_sign_conversion) + if (!warn_conversion && !warn_sign_conversion && !warn_real_conversion) return; + /* When either type is a floating point type, warn with + -Wreal-conversion instead of -Wconversion (-Wreal-conversion is a + subset of -Wconversion that only warns for conversions of real + types to integral types). */ + warn_option = (warn_real_conversion + && (FLOAT_TYPE_P (type) || FLOAT_TYPE_P (expr_type))) + ? OPT_Wreal_conversion + : OPT_Wconversion; + switch (TREE_CODE (expr)) { case EQ_EXPR: @@ -2689,14 +2699,14 @@ conversion_warning (tree type, tree expr) can hold the values 0 and -1) doesn't lose information - but it does change the value. */ if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) - warning_at (loc, OPT_Wconversion, + warning_at (loc, warn_option, "conversion to %qT from boolean expression", type); return; case REAL_CST: case INTEGER_CST: if (unsafe_conversion_p (type, expr, true)) - warning_at (loc, OPT_Wconversion, + warning_at (loc, warn_option, "conversion to %qT alters %qT constant value", type, expr_type); return; @@ -2715,7 +2725,7 @@ conversion_warning (tree type, tree expr) default: /* 'expr' is not a constant. */ if (unsafe_conversion_p (type, expr, true)) - warning_at (loc, OPT_Wconversion, + warning_at (loc, warn_option, "conversion to %qT from %qT may alter its value", type, expr_type); } Index: testsuite/g++.dg/warn/Wreal-conversion-1.C =================================================================== --- testsuite/g++.dg/warn/Wreal-conversion-1.C (revision 0) +++ testsuite/g++.dg/warn/Wreal-conversion-1.C (revision 0) @@ -0,0 +1,25 @@ +// { dg-do compile } +// { dg-options "-Wreal-conversion" } + +#include + +int func1(int a) { + double f = a; + return f; // { dg-warning "conversion to" } +} + +double func3(); + +void func2() { + double g = 3.14; + float f = 1.8f; + int t = g; // { dg-warning "conversion to" } + bool b = g; + int p; + p = f; // { dg-warning "conversion to" } + func1(g); // { dg-warning "conversion to" } + char c = f; // { dg-warning "conversion to" } + c = p; + int q; + q = func3(); // { dg-warning "conversion to" } +} Index: testsuite/gcc.dg/Wreal-conversion-1.c =================================================================== --- testsuite/gcc.dg/Wreal-conversion-1.c (revision 0) +++ testsuite/gcc.dg/Wreal-conversion-1.c (revision 0) @@ -0,0 +1,24 @@ +// { dg-do compile } +// { dg-options "-Wreal-conversion" } + +#include + +int func1(int a) { + double f = a; + return f; // { dg-warning "conversion to" } +} + +double func3(); + +void func2() { + double g = 3.14; + float f = 1.8f; + int t = g; // { dg-warning "conversion to" } + int p; + p = f; // { dg-warning "conversion to" } + func1(g); // { dg-warning "conversion to" } + char c = f; // { dg-warning "conversion to" } + c = p; + int q; + q = func3(); // { dg-warning "conversion to" } +}