public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: No warning for module global variable which is set but never used
       [not found] <2f9b5eb5-ed1a-61d7-241d-bcf520db7570@codesourcery.com>
@ 2020-12-09 15:41 ` webmaster
  2020-12-09 15:45   ` webmaster
  0 siblings, 1 reply; 11+ messages in thread
From: webmaster @ 2020-12-09 15:41 UTC (permalink / raw)
  To: gcc

I'm talking about C/C++ :-)
-------- Ursprüngliche Nachricht --------Von: Tobias Burnus <tobias@codesourcery.com> Datum: 09.12.20  13:15  (GMT+01:00) An: webmaster <webmaster@defcon-cc.org> Betreff: Re: No warning for module global variable which is set but never used The example below is for Fortran – but the same applies to C++ modulesand to static variable in general, especially if they are global variables.(It is not clear from the question about which language you were talking.)TobiasOn 09.12.20 13:13, Tobias Burnus wrote:> On 09.12.20 13:02, webmaster wrote:>>> Ahh, ok. With these explanation I understand that the compiler does>> remove it because of optimatizion. This is somehow good and bad.>> Good that unused variable does not consume any memory. Bad that>> developer is not informed that variable can be removed.>> That's the general problem with (module) global variables:>> module m>   integer :: A> end>> program main>   use m>   external foo>   A = 5>   call foo()> end>> ----------------------------> Now assume in a different file:>> subroutine foo>   use m>   print *, A> end>> In this case, the compiler cannot see while processing the first file> that 'foo' in a different file actually uses the variable - and cannot> warn.>> Doing tracking for static (implicit or explicit "SAVE") is difficult;> for (module) global variable it is impossible to do in general.>> Tobias>-----------------Mentor Graphics (Deutschland) GmbH, Arnulfstraße 201, 80634 München / GermanyRegistergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Alexander Walter

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: No warning for module global variable which is set but never used
  2020-12-09 15:41 ` No warning for module global variable which is set but never used webmaster
@ 2020-12-09 15:45   ` webmaster
  2020-12-10 15:10     ` webmaster
  0 siblings, 1 reply; 11+ messages in thread
From: webmaster @ 2020-12-09 15:45 UTC (permalink / raw)
  To: gcc

I have the following Code C\C++:

static int foo = 0;

static void bar(void)
{
    foo = 1;
}

Here it is clear for the compiler that the variable foo can only be
accessed from the same modul and not from ther modules. From the
explanations before I understand that the variable is removed due to
optimization. But I do not understand why GCC does not throws a warning.

From my point of view it is responsibility of the developer to remove
the unused variable.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: No warning for module global variable which is set but never used
  2020-12-09 15:45   ` webmaster
@ 2020-12-10 15:10     ` webmaster
  2020-12-10 15:26       ` David Brown
  0 siblings, 1 reply; 11+ messages in thread
From: webmaster @ 2020-12-10 15:10 UTC (permalink / raw)
  To: gcc

Is it possible to request such feature?

Am 09.12.2020 um 16:45 schrieb webmaster:
> I have the following Code C\C++:
> 
> static int foo = 0;
> 
> static void bar(void)
> {
>     foo = 1;
> }
> 
> Here it is clear for the compiler that the variable foo can only be
> accessed from the same modul and not from ther modules. From the
> explanations before I understand that the variable is removed due to
> optimization. But I do not understand why GCC does not throws a warning.
> 
>From my point of view it is responsibility of the developer to remove
> the unused variable.
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: No warning for module global variable which is set but never used
  2020-12-10 15:10     ` webmaster
@ 2020-12-10 15:26       ` David Brown
  2020-12-10 20:17         ` webmaster
  0 siblings, 1 reply; 11+ messages in thread
From: David Brown @ 2020-12-10 15:26 UTC (permalink / raw)
  To: webmaster, gcc

On 10/12/2020 16:10, webmaster wrote:

(As a general rule, you'll get more useful responses if you use your
name in your posts.  It's common courtesy.)


> Is it possible to request such feature?
> 

Of course you can file a request for it.  Go to the gcc bugzilla site:

<https://gcc.gnu.org/bugzilla/>

First, search thoroughly to see if it is already requested - obvious
duplicate requests just waste developers' time.  If you find a
duplicate, add a comment and put yourself on the cc list.  If you don't
find a duplicate, file it as a new bug.

Given the replies on this list from gcc developers, I would not hold my
breath waiting for this feature.  It is unlikely to be implemented
unless the relevant compiler passes are re-organised in some way, or
extra information is tracked.  So I don't think it will be a priority.

However, it's always good to track these things - and if many people
want a particular feature, it can't harm its chances of getting done
eventually.

mvh.,

David


> Am 09.12.2020 um 16:45 schrieb webmaster:
>> I have the following Code C\C++:
>>
>> static int foo = 0;
>>
>> static void bar(void)
>> {
>>     foo = 1;
>> }
>>
>> Here it is clear for the compiler that the variable foo can only be
>> accessed from the same modul and not from ther modules. From the
>> explanations before I understand that the variable is removed due to
>> optimization. But I do not understand why GCC does not throws a warning.
>>
>> >From my point of view it is responsibility of the developer to remove
>> the unused variable.
>>
> 
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: No warning for module global variable which is set but never used
  2020-12-10 15:26       ` David Brown
@ 2020-12-10 20:17         ` webmaster
  0 siblings, 0 replies; 11+ messages in thread
From: webmaster @ 2020-12-10 20:17 UTC (permalink / raw)
  To: gcc

Ahhh, ok. Good too know.

I think also it is not of high priority ;-)

Greets

Patrick

Am 10.12.2020 um 16:26 schrieb David Brown:
> On 10/12/2020 16:10, webmaster wrote:
> 
> (As a general rule, you'll get more useful responses if you use your
> name in your posts.  It's common courtesy.)
> 
> 
>> Is it possible to request such feature?
>>
> 
> Of course you can file a request for it.  Go to the gcc bugzilla site:
> 
> <https://gcc.gnu.org/bugzilla/>
> 
> First, search thoroughly to see if it is already requested - obvious
> duplicate requests just waste developers' time.  If you find a
> duplicate, add a comment and put yourself on the cc list.  If you don't
> find a duplicate, file it as a new bug.
> 
> Given the replies on this list from gcc developers, I would not hold my
> breath waiting for this feature.  It is unlikely to be implemented
> unless the relevant compiler passes are re-organised in some way, or
> extra information is tracked.  So I don't think it will be a priority.
> 
> However, it's always good to track these things - and if many people
> want a particular feature, it can't harm its chances of getting done
> eventually.
> 
> mvh.,
> 
> David
> 
> 
>> Am 09.12.2020 um 16:45 schrieb webmaster:
>>> I have the following Code C\C++:
>>>
>>> static int foo = 0;
>>>
>>> static void bar(void)
>>> {
>>>     foo = 1;
>>> }
>>>
>>> Here it is clear for the compiler that the variable foo can only be
>>> accessed from the same modul and not from ther modules. From the
>>> explanations before I understand that the variable is removed due to
>>> optimization. But I do not understand why GCC does not throws a warning.
>>>
>>> >From my point of view it is responsibility of the developer to remove
>>> the unused variable.
>>>
>>
>>

-- 
___      _ _    _   _ _      __           _       _      _      _
 |_ _|  __| (_)__| | (_) |_   / _|___ _ _  | |_ ___| |_   | |_  _| |___
  | |  / _` | / _` | | |  _| |  _/ _ \ '_| |  _/ -_) ' \  | | || | |_ /
 |___| \__,_|_\__,_| |_|\__| |_| \___/_|    \__\___|_||_| |_|\_,_|_/__|

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2.0.17 (MingW32)

mQENBFHMmT8BCAC0smvU7Bq1ABxAhvBRn7d4ekkk95aCE4TTQo4wy1z/rGLhQfdt
dhiD+Vy61vGrsdK3ei5sW6rBvX2m8+YmBi+8AAgSiZmS0JM3Zz3cmTi5oh0D/yM8
4aDj7wQYfJyzSmYN8InAQ5eA77lwIdqG27kR9wga2szeJwCnWReta0R+7YFkpUW+
zUlf4SWcUx5SmBsaiELQpm+Qcn+fyopo12RX6YVmoNPBvN2nDXDnRhUCKGc+0xhD
UrBpCHrApK6sTnMsD34ClCLTL2L1gckQ0AsQqY3PJlx3R8kIJxlmr6R3WnjPMIG0
lqrukB9PcOrHM1MZXK1gK6AtypHBN98lr8Z9ABEBAAG0KndlYm1hc3RlciA8d2Vi
bWFzdGVyQGRlZmNvbi1jYy5keW5kbnMub3JnPokBPgQTAQIAKAUCUcyZPwIbIwUJ
CWYBgAYLCQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQcN1vxvRQl+0SEwf+KXjf
YtiSUSVS11uqeQ/8g46NwmNa91P3toZvEd7vhLSbjnL9bi/vApzNnUTGT3VP4/NA
dg9SbR4qKlSr8T+YikRMV3tiuiVq8m7g00qM9y8MIomwJTounz8VdO/aJXFSOxAK
Bb6ElREADspCzr2qSZCnozWUzbd+b8owbGeRRq3e33Aa5Nlm/xDRxGDWANbaIA8q
Gkibvy3vWEwrxiwsakvHGaEZnPEtlNm3M1xcmFAuyl73qzUMkLN0u9E/2igo4EB5
EdMb5Ab5hfWdljxBqJr0tsvMfSK4VkzMCbKYkTqHZIRPQnhiSBE6Yo1Q6RCl/Hht
bkvU4RA0J+NkXMZljrkBDQRRzJk/AQgA0HojJnK0uhEkAnbmszYsf477DV+LD02s
ZEAlLGhJlf9qYaDiPMPwaZ3nK8/PYKzPpBWfHgRQP97rLHPIVJYl3BHDa/nWeZ2b
e/HzYhpX0djbK9qe6W/CTfGbXmC/y+4dDGB8dvtTAW3JILm7xEdwiWtywozEVy7V
lnMK4JQvlfOh+3XO6qv71FXyuRkObvkYzqvxUYHewtvvObcVxXHP0C0O6LB44iAW
2boZVVuiHdudnyNAezJajPMUT8SnI0bwL6+0TgnHL4cKNUEPQljIrrvi+9nCkq7V
uBtnsYtyoo2reoxmCbX/Z1zZsxdUcKpeJHlc5AypyN8DUJ+APJ9NnwARAQABiQEl
BBgBAgAPBQJRzJk/AhsMBQkJZgGAAAoJEHDdb8b0UJftOY0H/1TChmQrJC/qzefW
PK7EqFlBg3TIEXdu8JHjF42ZOgzQRfp7E2wWzEx0Y45lNXMs6Yg15hWCEDaUDF6F
5WZKNrP8xIldyR9Aw7fyKqjZ9UuKovqofHsCiaSO7nWzGM6GF3nBDNI9NcFve/wN
wggyjAbohOJrJGal3N0HlG3cakqjEmjBe1gQEMC0ZPlWstb/cqqr49TNPrRmQc4P
SyGffh8Xqhw94m1LDBXFEaYe7AxjNk1sPAVfO1rOdLF6GOun/UwgbhDQX/Rb9C3t
AhjSgyFEiR/gfrUZ7R6SY51qOUf1lN5ZN85C/x27XoZWYlsNaH3Ei6nG+yeswBMk
ZRMbezQ=
=Med3
-----END PGP PUBLIC KEY BLOCK-----

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: No warning for module global variable which is set but never used
  2020-12-09 10:00   ` Jakub Jelinek
  2020-12-09 11:07     ` David Brown
@ 2020-12-09 12:02     ` webmaster
  1 sibling, 0 replies; 11+ messages in thread
From: webmaster @ 2020-12-09 12:02 UTC (permalink / raw)
  To: gcc

Ahh, ok. With these explanation I understand that the compiler does remove it because of optimatizion. This is somehow good and bad.  Good that unused variable does not consume any memory. Bad that developer is not informed that variable can be removed.
-------- Ursprüngliche Nachricht --------Von: Jakub Jelinek <jakub@redhat.com> Datum: 09.12.20  11:00  (GMT+01:00) An: David Brown <david@westcontrol.com> Cc: webmaster <webmaster@defcon-cc.org>, gcc@gcc.gnu.org Betreff: Re: No warning for module global variable which is set but never used On Wed, Dec 09, 2020 at 10:50:22AM +0100, David Brown wrote:> I'd say that it makes sense to have such a warning as a natural> enhancement to the existing "-Wunused-but-set-variable" warning.  But IThat is not really possible.The -Wunused-but-set-* warning works by having two bits for the DECL,TREE_USED and DECL_READ_P, where any uses mark the var TREE_USED and(conservatively) what can read the value marks it DECL_READ_Pand -Wunused-but-set-* is then variables that are TREE_USED and!DECL_READ_P.  All this needs to be done early in the FE.For the static vars, the optimization to remove them altogether is donemuch later, and at that point the compiler doesn't know if it isn't usedbecause all the reads in the program have been optimized away vs. there werenone.	Jakub

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: No warning for module global variable which is set but never used
  2020-12-09 10:00   ` Jakub Jelinek
@ 2020-12-09 11:07     ` David Brown
  2020-12-09 12:02     ` webmaster
  1 sibling, 0 replies; 11+ messages in thread
From: David Brown @ 2020-12-09 11:07 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: webmaster, gcc

On 09/12/2020 11:00, Jakub Jelinek wrote:
> On Wed, Dec 09, 2020 at 10:50:22AM +0100, David Brown wrote:
>> I'd say that it makes sense to have such a warning as a natural
>> enhancement to the existing "-Wunused-but-set-variable" warning.  But I
> 
> That is not really possible.
> The -Wunused-but-set-* warning works by having two bits for the DECL,
> TREE_USED and DECL_READ_P, where any uses mark the var TREE_USED and
> (conservatively) what can read the value marks it DECL_READ_P
> and -Wunused-but-set-* is then variables that are TREE_USED and
> !DECL_READ_P.  All this needs to be done early in the FE.
> For the static vars, the optimization to remove them altogether is done
> much later, and at that point the compiler doesn't know if it isn't used
> because all the reads in the program have been optimized away vs. there were
> none.
> 
> 	Jakub
> 

That's what I thought might be the case.  I've seen this before in
situations where it might seem to the layman that it is "obvious" that
there should be a warning here, or that "the compiler can optimise here,
surely it can also issue a warning".  If it were easy to implement a
warning in a situation like this, I guess the gcc developers would have
implemented it already!

I hope this gives the OP the information he is looking for.

David


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: No warning for module global variable which is set but never used
  2020-12-09  9:50 ` David Brown
@ 2020-12-09 10:00   ` Jakub Jelinek
  2020-12-09 11:07     ` David Brown
  2020-12-09 12:02     ` webmaster
  0 siblings, 2 replies; 11+ messages in thread
From: Jakub Jelinek @ 2020-12-09 10:00 UTC (permalink / raw)
  To: David Brown; +Cc: webmaster, gcc

On Wed, Dec 09, 2020 at 10:50:22AM +0100, David Brown wrote:
> I'd say that it makes sense to have such a warning as a natural
> enhancement to the existing "-Wunused-but-set-variable" warning.  But I

That is not really possible.
The -Wunused-but-set-* warning works by having two bits for the DECL,
TREE_USED and DECL_READ_P, where any uses mark the var TREE_USED and
(conservatively) what can read the value marks it DECL_READ_P
and -Wunused-but-set-* is then variables that are TREE_USED and
!DECL_READ_P.  All this needs to be done early in the FE.
For the static vars, the optimization to remove them altogether is done
much later, and at that point the compiler doesn't know if it isn't used
because all the reads in the program have been optimized away vs. there were
none.

	Jakub


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: No warning for module global variable which is set but never used
  2020-12-09  9:25 webmaster
  2020-12-09  9:39 ` Jonathan Wakely
@ 2020-12-09  9:50 ` David Brown
  2020-12-09 10:00   ` Jakub Jelinek
  1 sibling, 1 reply; 11+ messages in thread
From: David Brown @ 2020-12-09  9:50 UTC (permalink / raw)
  To: webmaster, gcc

On 09/12/2020 10:25, webmaster wrote:
> Hello,I'm wondering why GCC does not throw any warning when a module global variable is set (write) but never used (read).Is this behavior wanted? Does it makes sense to add such warning?Greets
> 

How do you expect the compiler to know if the variable is never read?

If it has file linkage (it is declared "static") and its address is not
taken and exported out of the translation unit, then the compiler knows
all about it - and could warn about it.  If it has external linkage
(declared at file scope without "static", or with "extern") or its
address is passed out of the translation unit, then the compiler has no
way to tell how it might be used in other translation units.


If you write:

static int xx;
void foo(void) {
	xx = 2;
}

then gcc will eliminate the variable "xx" entirely, as it is never used.
 The function "foo" is compiled to a single "return".  But no
"unused-but-set-variable" warning is emitted - and clearly the compiler
knows that the variable is set but not used.  (You get the warning if
the static variable is local to the function.)

I'd say that it makes sense to have such a warning as a natural
enhancement to the existing "-Wunused-but-set-variable" warning.  But I
can't say if it is a simple matter or not - sometimes these things are
surprisingly difficult to implement depending on the order of passes in
the compiler.  Then it becomes a trade-off of the utility of such a
warning against the effort needed to implement it.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: No warning for module global variable which is set but never used
  2020-12-09  9:25 webmaster
@ 2020-12-09  9:39 ` Jonathan Wakely
  2020-12-09  9:50 ` David Brown
  1 sibling, 0 replies; 11+ messages in thread
From: Jonathan Wakely @ 2020-12-09  9:39 UTC (permalink / raw)
  To: webmaster; +Cc: gcc

On Wed, 9 Dec 2020 at 09:28, webmaster <webmaster@defcon-cc.org> wrote:
>
> Hello,I'm wondering why GCC does not throw any warning when a module global variable is set (write) but never used (read).Is this behavior wanted? Does it makes sense to add such warning?Greets

This question seems to be more appropriate for the gcc-help list.
Please take any replies to that list instead.

What do you mean by "module"? A Fortran module? A C++ module? Or do
you just mean a source file?

In C and C++ global variables have external linkage by default, which
means they can be accessed from another translation unit (i.e. another
source file). Unless the compiler knows that the entire program is a
single source file, it can't know that there aren't writes to the
variable in other files.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* No warning for module global variable which is set but never used
@ 2020-12-09  9:25 webmaster
  2020-12-09  9:39 ` Jonathan Wakely
  2020-12-09  9:50 ` David Brown
  0 siblings, 2 replies; 11+ messages in thread
From: webmaster @ 2020-12-09  9:25 UTC (permalink / raw)
  To: gcc

Hello,I'm wondering why GCC does not throw any warning when a module global variable is set (write) but never used (read).Is this behavior wanted? Does it makes sense to add such warning?Greets

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-12-10 20:17 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <2f9b5eb5-ed1a-61d7-241d-bcf520db7570@codesourcery.com>
2020-12-09 15:41 ` No warning for module global variable which is set but never used webmaster
2020-12-09 15:45   ` webmaster
2020-12-10 15:10     ` webmaster
2020-12-10 15:26       ` David Brown
2020-12-10 20:17         ` webmaster
2020-12-09  9:25 webmaster
2020-12-09  9:39 ` Jonathan Wakely
2020-12-09  9:50 ` David Brown
2020-12-09 10:00   ` Jakub Jelinek
2020-12-09 11:07     ` David Brown
2020-12-09 12:02     ` webmaster

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).