From patchwork Tue Jun 25 01:08:16 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sharad Singhai X-Patchwork-Id: 253995 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 D18AB2C0084 for ; Tue, 25 Jun 2013 11:09:06 +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:from:date:message-id:subject:to:cc:content-type; q=dns; s=default; b=i0sUiZZ8HuCe+FprQ5KKFBBZD2177hWEbcGbu/5t6IZ mdgKVI+99fY+xqIkuT95Ar73gtT7HFID54fLK4cc7cwdMDRSInLkKvEoTFPc3XdE PEF60DaVsH0K4NmanTWa295yfZfx20oogZsd0HC+f/H2zxyJjeJciKfCiNGukTNw = 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:from:date:message-id:subject:to:cc:content-type; s=default; bh=l2/yJfaJ9M82hwSO19sLiTKRXt4=; b=Nc2xoga1kH2HfeEKZ pDPIzQd3vjiSmxOIeJ58EowrPKwfNhCRs6srCUqdjd4db5mXXAhviVNJQnZmhFWX vy0v4Gl3Tz/FDWDYpP9CeD/FlypMqcNJOBj9Hm5+zPVZgieLyCLOy0XmT3HrauB/ /MjD7ss9LYKcvYznaf5taoXE7c= Received: (qmail 11384 invoked by alias); 25 Jun 2013 01:09:00 -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 11372 invoked by uid 89); 25 Jun 2013 01:08:59 -0000 X-Spam-SWARE-Status: No, score=-3.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.1 Received: from mail-ob0-f180.google.com (HELO mail-ob0-f180.google.com) (209.85.214.180) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Tue, 25 Jun 2013 01:08:58 +0000 Received: by mail-ob0-f180.google.com with SMTP id eh20so11209607obb.39 for ; Mon, 24 Jun 2013 18:08:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:from:date:message-id:subject:to:cc:content-type :x-gm-message-state; bh=VlHhVIcp1v+RgECyQ6LJzK6nkfBKHj8Jf4KlX8un2WY=; b=A473wYOFBYGvLaIy9ceKs16OCd1luzSX8WDmhKcC2jgS4rXjJZJS++6Tq1wzSPnxA6 JCpNfyfba/R1InK/aWLx2ryZPm9l5jlyzi86t/6fA8XTt/OsCAzj1rMuiVJya4evJfN4 ulYGcjVLtnRTwjzWxn+TqmZizHNzgoCX5WbSETzn8lKqgBaBN99oUdN12P5VYjV5YCFZ yd3EZxSww0FGg3Cp0pYSzxOxnlMuzBQKSil7M1Fh+ZpBJbFLRkBQmcCKD7pAGaH4PCRz zD1JuMoUhDW709Hplrc3KHXIP7+L9feOIaLVEZstzAp2iCl397AmtpiH+hx7Icp4lBdZ K9LQ== X-Received: by 10.60.144.69 with SMTP id sk5mr1115289oeb.27.1372122536712; Mon, 24 Jun 2013 18:08:56 -0700 (PDT) MIME-Version: 1.0 Received: by 10.182.160.67 with HTTP; Mon, 24 Jun 2013 18:08:16 -0700 (PDT) From: Sharad Singhai Date: Mon, 24 Jun 2013 18:08:16 -0700 Message-ID: Subject: [google/gcc-4_8] Port -Wreal-conversion warning To: "gcc-patches@gcc.gnu.org" Cc: David Li X-Gm-Message-State: ALoCoQk/Dyw0wQ6Az71IWxSePCntUlO30B1bTTFOl2f6fHzjlaLIZNILjt4h5bcSnQocv5AjGjFzkr95/7GeY0ZAFmEKVMxDcoCCtwJov/ZmlghoYqsrbwUfI6ZwQNdnFykxtgavN5q4hXp21GlNQJ/xdfEL2BYLkTF9iFwbPLSQqwxVlVBbJlPSkdQROQsI6s/JlxQESdu2mvUflG63a4C/ir6ecw4J1w== Hi, This patch forward ports the -Wreal-conversion warning to google/gcc-4_8 branch (google ref 39133-p2). I tweaked the patch a little bit to avoid printing duplicate warnings when both -Wconversion and -Wreal-conversion are specified. Also, I trimmed the test case to avoid testing for integer-to-integer narrowing conversions as this warning is about real conversions. Bootstrapped and tested on x86_64. Okay for google/gcc-4_8? Thanks, Sharad 2013-06-24 * 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: 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,23 @@ +// { 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.2; + float f; + 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" } + int q; + q = func3(); // { dg-warning "conversion to" } +} 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,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.2; + float f; + 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" } + int q; + q = func3(); // { dg-warning "conversion to" } +} Index: doc/invoke.texi =================================================================== --- doc/invoke.texi (revision 200359) +++ doc/invoke.texi (working copy) @@ -193,7 +193,7 @@ in the following sections. -fno-default-inline -fvisibility-inlines-hidden @gol -fvisibility-ms-compat @gol -fext-numeric-literals @gol --Wabi -Wconversion-null -Wctor-dtor-privacy @gol +-Wabi -Wconversion-null -Wctor-dtor-privacy @gol -Wdelete-non-virtual-dtor -Wliteral-suffix -Wnarrowing @gol -Wnoexcept -Wnon-virtual-dtor -Wreorder @gol -Weffc++ -Wstrict-null-sentinel @gol @@ -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. + @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 200359) +++ 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 +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 200359) +++ 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 200359) +++ c-family/c-common.c (working copy) @@ -2668,7 +2668,7 @@ conversion_warning (tree type, tree expr) 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; switch (TREE_CODE (expr)) @@ -2715,9 +2715,17 @@ conversion_warning (tree type, tree expr) default: /* 'expr' is not a constant. */ if (unsafe_conversion_p (type, expr, true)) - warning_at (loc, OPT_Wconversion, - "conversion to %qT from %qT may alter its value", - type, expr_type); + { + /* Do not print the same warning twice. */ + bool printed = + warning_at (loc, OPT_Wreal_conversion, + "conversion to %qT from %qT may alter its value", + type, expr_type); + if (!printed) + warning_at (loc, OPT_Wconversion, + "conversion to %qT from %qT may alter its value", + type, expr_type); + } } }