From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16765 invoked by alias); 21 Sep 2013 18:27:24 -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 16753 invoked by uid 89); 21 Sep 2013 18:27:24 -0000 Received: from mail-lb0-f171.google.com (HELO mail-lb0-f171.google.com) (209.85.217.171) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Sat, 21 Sep 2013 18:27:24 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,NO_RELAYS autolearn=ham version=3.3.2 X-HELO: mail-lb0-f171.google.com Received: by mail-lb0-f171.google.com with SMTP id u14so1527729lbd.2 for ; Sat, 21 Sep 2013 11:27:20 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.112.159.166 with SMTP id xd6mr11889004lbb.22.1379788040202; Sat, 21 Sep 2013 11:27:20 -0700 (PDT) Received: by 10.112.101.7 with HTTP; Sat, 21 Sep 2013 11:27:20 -0700 (PDT) In-Reply-To: <20130921180952.GF3086@a.lan> References: <20130921164609.GC3086@a.lan> <20130921180952.GF3086@a.lan> Date: Sat, 21 Sep 2013 18:27:00 -0000 Message-ID: Subject: Re: how to make gcc warn about arithmetic signed overflow From: Jonathan Wakely To: wempwer@gmail.com Cc: gcc-help Content-Type: text/plain; charset=ISO-8859-1 X-IsSubscribed: yes X-SW-Source: 2013-09/txt/msg00140.txt.bz2 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.