From patchwork Fri Apr 29 14:59:31 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Diego Novillo X-Patchwork-Id: 93435 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]) by ozlabs.org (Postfix) with SMTP id AFAA6B6F07 for ; Sat, 30 Apr 2011 00:59:54 +1000 (EST) Received: (qmail 29268 invoked by alias); 29 Apr 2011 14:59:52 -0000 Received: (qmail 29259 invoked by uid 22791); 29 Apr 2011 14:59:52 -0000 X-SWARE-Spam-Status: No, hits=-2.1 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, SPF_HELO_PASS, TW_CX, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (216.239.44.51) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 29 Apr 2011 14:59:37 +0000 Received: from kpbe15.cbf.corp.google.com (kpbe15.cbf.corp.google.com [172.25.105.79]) by smtp-out.google.com with ESMTP id p3TExYCr032683; Fri, 29 Apr 2011 07:59:34 -0700 Received: from tobiano.tor.corp.google.com (tobiano.tor.corp.google.com [172.29.41.6]) by kpbe15.cbf.corp.google.com with ESMTP id p3TExWxe017590; Fri, 29 Apr 2011 07:59:32 -0700 Received: by tobiano.tor.corp.google.com (Postfix, from userid 54752) id E255FAE1DD; Fri, 29 Apr 2011 10:59:31 -0400 (EDT) To: reply@codereview.appspotmail.com, lcwu@google.com, jason@redhat.com, joseph@codesourcery.com, gcc-patches@gcc.gnu.org Subject: [google] Add new warning -Wreal-conversion (issue4436068) Message-Id: <20110429145931.E255FAE1DD@tobiano.tor.corp.google.com> Date: Fri, 29 Apr 2011 10:59:31 -0400 (EDT) From: dnovillo@google.com (Diego Novillo) X-System-Of-Record: true X-IsSubscribed: yes 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 This patch from Le-Chun Wu adds a new warning flag "-Wreal-conversion" that warns about implicit type conversions from real (double or float) values to integral values. OK for trunk? Applied to google/main. 2011-04-27 Le-Chun Wu Google ref 39133 * c.opt (Wreal-conversion): New flag. * c-common.c (conversion_warning): Use it. * c-opts.c (c_common_post_options): Initialize it. * doc/invoke.texi (Wreal-conversion): Document it. testsuite/ChangeLog.google-main: 2011-04-27 Le-Chun Wu * g++.dg/warn/Wreal-conversion-1.C: New. * gcc.dg/Wreal-conversion-1.c: New. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 13f13a8..c6dc649 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -1941,7 +1941,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; /* If any operand is artificial, then this expression was generated @@ -1984,7 +1984,9 @@ conversion_warning (tree type, tree expr) && TREE_CODE (type) == INTEGER_TYPE) { if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) - give_warning = true; + warning (OPT_Wreal_conversion, + "conversion to %qT from %qT may alter its value", + type, expr_type); } /* Warn for an integer constant that does not fit into integer type. */ else if (TREE_CODE (expr_type) == INTEGER_TYPE @@ -2053,7 +2055,9 @@ conversion_warning (tree type, tree expr) /* Warn for real types converted to integer types. */ if (TREE_CODE (expr_type) == REAL_TYPE && TREE_CODE (type) == INTEGER_TYPE) - give_warning = true; + warning (OPT_Wreal_conversion, + "conversion to %qT from %qT may alter its value", + type, expr_type); else if (TREE_CODE (expr_type) == INTEGER_TYPE && TREE_CODE (type) == INTEGER_TYPE) diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c index c3d1bbe..994bf90 100644 --- a/gcc/c-family/c-opts.c +++ b/gcc/c-family/c-opts.c @@ -939,6 +939,12 @@ c_common_post_options (const char **pfilename) if (warn_enum_compare == -1) warn_enum_compare = c_dialect_cxx () ? 1 : 0; + /* 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; + /* -Wpacked-bitfield-compat is on by default for the C languages. The warning is issued in stor-layout.c which is not part of the front-end so we need to selectively turn it on here. */ diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index af38a1f..ce742fa 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -323,6 +323,10 @@ Wself-assign-non-pod C++ ObjC++ Var(warn_self_assign_non_pod) Init(0) Warning Warn when a variable of a non-POD type is assigned to itself +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) Init(-1) Warn for implicit type conversions between signed and unsigned integers diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index fa247b6..5f6d79d 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -257,7 +257,7 @@ Objective-C and Objective-C++ Dialects}. -Woverlength-strings -Wpacked -Wpacked-bitfield-compat -Wpadded @gol -Wparentheses -Wpedantic-ms-format -Wno-pedantic-ms-format @gol -Wpointer-arith -Wno-pointer-to-int-cast @gol --Wredundant-decls -Wreturn-type -Wripa-opt-mismatch @gol +-Wreal-conversion -Wredundant-decls -Wreturn-type -Wripa-opt-mismatch @gol -Wself-assign -Wself-assign-non-pod -Wsequence-point -Wshadow @gol -Wshadow-compatible-local -Wshadow-local @gol -Wsign-compare -Wsign-conversion -Wstack-protector @gol @@ -4232,6 +4232,12 @@ unsigned integers are disabled by default in C++ unless Do not warn for conversions between @code{NULL} and non-pointer types. @option{-Wconversion-null} is enabled by default. +@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 -Wempty-body @opindex Wempty-body @opindex Wno-empty-body diff --git a/gcc/testsuite/g++.dg/warn/Wreal-conversion-1.C b/gcc/testsuite/g++.dg/warn/Wreal-conversion-1.C new file mode 100644 index 0000000..d96fe50 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wreal-conversion-1.C @@ -0,0 +1,26 @@ +// { 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" } + size_t s; + p = s; + int q; + q = func3(); // { dg-warning "conversion to" } +} diff --git a/gcc/testsuite/gcc.dg/Wreal-conversion-1.c b/gcc/testsuite/gcc.dg/Wreal-conversion-1.c new file mode 100644 index 0000000..eec4e3f --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wreal-conversion-1.c @@ -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.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" } + size_t s; + p = s; + int q; + q = func3(); // { dg-warning "conversion to" } +}