From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24504 invoked by alias); 6 Jan 2012 14:51:26 -0000 Received: (qmail 24488 invoked by uid 22791); 6 Jan 2012 14:51:26 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_LOW,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-iy0-f175.google.com (HELO mail-iy0-f175.google.com) (209.85.210.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 06 Jan 2012 14:51:11 +0000 Received: by iakh37 with SMTP id h37so2742288iak.20 for ; Fri, 06 Jan 2012 06:51:11 -0800 (PST) Received: by 10.50.195.227 with SMTP id ih3mr7630353igc.19.1325861471091; Fri, 06 Jan 2012 06:51:11 -0800 (PST) Received: by 10.50.195.227 with SMTP id ih3mr7630342igc.19.1325861470898; Fri, 06 Jan 2012 06:51:10 -0800 (PST) Received: from coign.google.com ([67.218.110.43]) by mx.google.com with ESMTPS id g34sm214546387ibk.10.2012.01.06.06.51.08 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 06 Jan 2012 06:51:10 -0800 (PST) From: Ian Lance Taylor To: Alexandre Duret-Lutz Cc: gcc-help@gcc.gnu.org Subject: Re: missing warning about parameter set bug no longer used References: Date: Sat, 07 Jan 2012 02:56:00 -0000 In-Reply-To: (Alexandre Duret-Lutz's message of "Fri, 6 Jan 2012 11:57:01 +0100") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes 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 X-SW-Source: 2012-01/txt/msg00036.txt.bz2 Alexandre Duret-Lutz writes: > The following file foo.c is a simplified version of a subtler bug I > found in my code. > int b;void bar(int a);void foo(int a){=C2=A0 bar(a);=C2=A0 a =3D 42;} > > The line a =3D 42 is in fact a typo in my code: I meant b =3D 42. I do > *not* expect the compiler to detect that I made a typo, but I would > like the get a warning that I am assigning to a local variable (or a > function parameter) that is not going to be used anymore. If I compile > this file with > % gcc-4.6 -Wall -Wextra -pedantic -O3 -c foo.c > I get absolutely no warning. Inspecting the generated code shows that > the assignment a =3D 42 is not performed, so gcc is perfectly well aware > that this instruction is useless (hence potentially bogus). Commenting > the call to bar(a); does produce a "warning: parameter =E2=80=98a=E2=80= =99 set but not > used [-Wunused-but-set-parameter]", so it seems like gcc will not warn > as long as variable a is used somewhere in the function, even if it is > before the assignment. > My questions: > 1. Is there a way to tell gcc to produce a warning for such case?2. Is > there a reason for the actual behavior? I.e, some cases were it is > actually desirable to assign to local variables that will be thrown > away by the optimizer? As far as I know way there is no way to get a warning for this case at present. GCC does not currently have a framework for tracking sets and uses which is independent of the optimizers. And warnings that are dependent on the optimizers are troublesome for many reasons. GCC does have some warnings like that today, and there are perennial sources of compliant. So there is unlikely to be an easy way to implement this warning. Ian