From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18292 invoked by alias); 23 Sep 2013 13:04:00 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 18280 invoked by uid 89); 23 Sep 2013 13:03:59 -0000 Received: from jhsvmret01.hatteland.com (HELO jhsvmret01.hatteland.com) (213.162.247.110) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 23 Sep 2013 13:03:59 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL,BAYES_05 autolearn=ham version=3.3.2 X-HELO: jhsvmret01.hatteland.com Received: from JHSVMHUB02.netsentral.no (172.21.1.26) by jhsvmret01.hatteland.com (213.162.247.110) with Microsoft SMTP Server (TLS) id 14.2.318.4; Mon, 23 Sep 2013 15:03:50 +0200 Received: from [192.168.0.65] (172.21.20.11) by e2010.hatteland.com (172.21.1.26) with Microsoft SMTP Server (TLS) id 14.2.318.4; Mon, 23 Sep 2013 15:03:47 +0200 Message-ID: <52403C33.3010404@westcontrol.com> Date: Mon, 23 Sep 2013 13:04:00 -0000 From: David Brown User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130806 Thunderbird/17.0.8 MIME-Version: 1.0 To: CC: Jonathan Wakely , gcc-help Subject: Re: how to make gcc warn about arithmetic signed overflow References: <20130921164609.GC3086@a.lan> <20130921180952.GF3086@a.lan> <20130921193306.GH3086@a.lan> In-Reply-To: <20130921193306.GH3086@a.lan> Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit X-SW-Source: 2013-09/txt/msg00157.txt.bz2 On 21/09/13 21:33, wempwer@gmail.com wrote: > On Sat, Sep 21, 2013 at 07:27:20PM +0100, Jonathan Wakely wrote: >> On 21 September 2013 19:09, wrote: >>> >>> It tried -O2 and it also worked. However on my system it only said: >>> >>> warning: integer overflow in expression >>> >>> How did you get gcc to produce this part: >>> >>> [-Woverflow] >>> >>> I use gcc 4.5.2. But when I use gcc like this I also do not get any >>> warnings: >>> >>> gcc -Woverflow main.c -o main >> >> 4.5.2 is quite old now. More recent versions show the warning option >> that triggered the warning, that's what the [-Woverflow] part is. >> >>> I wonder: >>> >>> 1) why -Woverflow appears in your gcc output? >> >> Because it's a recent version. >> >>> 2) why `gcc -Woverflow main.c -o main' does not produce a warning? >> >> Because without optimisation the compiler doesn't do the necessary >> analysis to realise that in the multiplication expression it knows the >> values. >> >>> 3) why does `gcc -O main.c -o main' produce warning in the first place >>> and why does it do this only when two operands are const? >> >> Because the optimiser can track the values of constants (because they >> don't change) and see that the values in the multiplication are known. > > But compiler also knows the operand values even if they are not > constant? These values must be known to produce the multiplication > result. Is this warning produced only when two operands are constant > and optimization is turned on because this is just the way gcc does it > and the warnings could also be produced for non-const operands if a > compiler worked in a different way? > The compiler can only produce warnings here if it knows the values you are using for the calculations. If you write "r = ab * bc;" then compiler assumes that you know what you are doing, and using sensible values for "ab" and "bc". But if the compiler can calculate the values involved (it requires -O1 or above for many of these calculations and analysis passes), and it can see there is definitely an error, then it can give a warning. So it needs the values to be "compile-time constants". That does not necessarily mean values declared with "const", such as "const int ab = 50000;". It also includes values set with #define, enumeration constants, literals (i.e., "50000"), as well as statements, expressions and function calls using these if the compiler knows all the details at compile-time. Note that the compiler /can/ give a warning in such cases, but it does not have to do so. gcc is a fantastic compiler, and gives better compile-time warnings and static analysis than any other compiler I have used - but it has limits. If it can easily do the calculations at compile-time, /and/ you have optimisation flags to enable such passes, /and/ you have enabled the appropriate warnings - /then/ gcc will give you the warnings.