public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Turning warning "returning reference to temporary" into error?
@ 2003-05-11 17:45 Richard Guenther
  2003-05-11 17:57 ` Gabriel Dos Reis
  2003-05-12  0:01 ` Eyal Lebedinsky
  0 siblings, 2 replies; 12+ messages in thread
From: Richard Guenther @ 2003-05-11 17:45 UTC (permalink / raw)
  To: gcc

Hi!

I'd like to turn the warning "returning reference to temporary", raised in
cp/typecheck.c:maybe_warn_about_returning_address_of_local() (CVS
HEAD) into an error, but
cannot find an option apart from -Werror, which is not applicable here.
Which warning option controls these warnings, I could not find anything in
the manual - can a specific warning turned into an error anyways? I.e.
like -Werror-strict-aliasing or the like?

Wouldnt it be a good idea to at least turn the
maybe_warn_about_returning_address_of_local warnings into an error with
-pedantic? Or always?

Richard.

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

* Re: Turning warning "returning reference to temporary" into error?
  2003-05-11 17:45 Turning warning "returning reference to temporary" into error? Richard Guenther
@ 2003-05-11 17:57 ` Gabriel Dos Reis
  2003-05-11 18:59   ` Richard Guenther
  2003-05-12  0:01 ` Eyal Lebedinsky
  1 sibling, 1 reply; 12+ messages in thread
From: Gabriel Dos Reis @ 2003-05-11 17:57 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc

Richard Guenther <rguenth@tat.physik.uni-tuebingen.de> writes:

[...]

| Wouldnt it be a good idea to at least turn the
| maybe_warn_about_returning_address_of_local warnings into an error with
| -pedantic? Or always?

This is not always practical since the actual meaning (i.e. error or
not) depends on the code path during runtime.

-- Gaby

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

* Re: Turning warning "returning reference to temporary" into error?
  2003-05-11 17:57 ` Gabriel Dos Reis
@ 2003-05-11 18:59   ` Richard Guenther
  2003-05-11 19:05     ` Gabriel Dos Reis
  0 siblings, 1 reply; 12+ messages in thread
From: Richard Guenther @ 2003-05-11 18:59 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: Richard Guenther, gcc

On 11 May 2003, Gabriel Dos Reis wrote:

> Richard Guenther <rguenth@tat.physik.uni-tuebingen.de> writes:
>
> [...]
>
> | Wouldnt it be a good idea to at least turn the
> | maybe_warn_about_returning_address_of_local warnings into an error with
> | -pedantic? Or always?
>
> This is not always practical since the actual meaning (i.e. error or
> not) depends on the code path during runtime.

Hm - I cannot think of any "legal" use, other than ignoring the return
value, of course.

Richard.

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

* Re: Turning warning "returning reference to temporary" into error?
  2003-05-11 18:59   ` Richard Guenther
@ 2003-05-11 19:05     ` Gabriel Dos Reis
  2003-05-11 19:47       ` Richard Guenther
  2003-05-11 20:26       ` Zack Weinberg
  0 siblings, 2 replies; 12+ messages in thread
From: Gabriel Dos Reis @ 2003-05-11 19:05 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc

Richard Guenther <rguenth@tat.physik.uni-tuebingen.de> writes:

| On 11 May 2003, Gabriel Dos Reis wrote:
| 
| > Richard Guenther <rguenth@tat.physik.uni-tuebingen.de> writes:
| >
| > [...]
| >
| > | Wouldnt it be a good idea to at least turn the
| > | maybe_warn_about_returning_address_of_local warnings into an error with
| > | -pedantic? Or always?
| >
| > This is not always practical since the actual meaning (i.e. error or
| > not) depends on the code path during runtime.
| 
| Hm - I cannot think of any "legal" use, other than ignoring the return
| value, of course.

A compiler writer cannot imagine every possible usage.  Which is why
somethings are warnings.  In this case, if the return value is ignored
then the program is valid.  GCC cannot reject it just because
-pedantic  is specified.

-- Gaby

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

* Re: Turning warning "returning reference to temporary" into error?
  2003-05-11 19:05     ` Gabriel Dos Reis
@ 2003-05-11 19:47       ` Richard Guenther
  2003-05-11 20:26       ` Zack Weinberg
  1 sibling, 0 replies; 12+ messages in thread
From: Richard Guenther @ 2003-05-11 19:47 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: gcc

On 11 May 2003, Gabriel Dos Reis wrote:

> Richard Guenther <rguenth@tat.physik.uni-tuebingen.de> writes:
>
> | On 11 May 2003, Gabriel Dos Reis wrote:
> |
> | > Richard Guenther <rguenth@tat.physik.uni-tuebingen.de> writes:
> | >
> | > [...]
> | >
> | > | Wouldnt it be a good idea to at least turn the
> | > | maybe_warn_about_returning_address_of_local warnings into an error with
> | > | -pedantic? Or always?
> | >
> | > This is not always practical since the actual meaning (i.e. error or
> | > not) depends on the code path during runtime.
> |
> | Hm - I cannot think of any "legal" use, other than ignoring the return
> | value, of course.
>
> A compiler writer cannot imagine every possible usage.  Which is why
> somethings are warnings.  In this case, if the return value is ignored
> then the program is valid.  GCC cannot reject it just because
> -pedantic  is specified.

What about giving the warning a name, -Wreturn f.e., so I can use
-w -Wreturn -Werror to turn it into an error?

Richard.

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

* Re: Turning warning "returning reference to temporary" into error?
  2003-05-11 19:05     ` Gabriel Dos Reis
  2003-05-11 19:47       ` Richard Guenther
@ 2003-05-11 20:26       ` Zack Weinberg
  2003-05-11 20:44         ` Richard Guenther
  2003-05-12  2:21         ` Fergus Henderson
  1 sibling, 2 replies; 12+ messages in thread
From: Zack Weinberg @ 2003-05-11 20:26 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: Richard Guenther, gcc

Gabriel Dos Reis <gdr@integrable-solutions.net> writes:

> A compiler writer cannot imagine every possible usage.  Which is why
> somethings are warnings.  In this case, if the return value is ignored
> then the program is valid.  GCC cannot reject it just because
> -pedantic  is specified.

In other places where we can prove that execution of a certain code
path will cause undefined behavior, but we can't prove that that path
will be taken at runtime, we issue a warning and generate a trap
instruction.  An analogous tactic for this scenario would be if we
generated code to return a distinguished pointer value that's
guaranteed to cause a segmentation fault when dereferenced.  (This
should _not_ be 0, as that may mask the bug.)

zw

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

* Re: Turning warning "returning reference to temporary" into error?
  2003-05-11 20:26       ` Zack Weinberg
@ 2003-05-11 20:44         ` Richard Guenther
  2003-05-12 18:57           ` Zack Weinberg
  2003-05-12  2:21         ` Fergus Henderson
  1 sibling, 1 reply; 12+ messages in thread
From: Richard Guenther @ 2003-05-11 20:44 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: Gabriel Dos Reis, gcc

On Sun, 11 May 2003, Zack Weinberg wrote:

> Gabriel Dos Reis <gdr@integrable-solutions.net> writes:
>
> > A compiler writer cannot imagine every possible usage.  Which is why
> > somethings are warnings.  In this case, if the return value is ignored
> > then the program is valid.  GCC cannot reject it just because
> > -pedantic  is specified.
>
> In other places where we can prove that execution of a certain code
> path will cause undefined behavior, but we can't prove that that path
> will be taken at runtime, we issue a warning and generate a trap
> instruction.  An analogous tactic for this scenario would be if we
> generated code to return a distinguished pointer value that's
> guaranteed to cause a segmentation fault when dereferenced.  (This
> should _not_ be 0, as that may mask the bug.)

I really want this to be diagnosed at compile time, and as this is a large
project, I want to have an error diagnosed, not only a warning. In the
current form this is not possible without turning every default warning
into an error with -Werror. Can we do something about this? I can come up
with a patch, but I neither have a copyright assignment, nor experience
with hacking gcc. At least, would a -Wreturn, default on, warning option
be ok, so I can use -w -Wreturn -Werror (will this work?) to make this an
error?

Thanks, Richard.

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

* Re: Turning warning "returning reference to temporary" into error?
  2003-05-11 17:45 Turning warning "returning reference to temporary" into error? Richard Guenther
  2003-05-11 17:57 ` Gabriel Dos Reis
@ 2003-05-12  0:01 ` Eyal Lebedinsky
  2003-05-12  6:59   ` Richard Guenther
  1 sibling, 1 reply; 12+ messages in thread
From: Eyal Lebedinsky @ 2003-05-12  0:01 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc

Richard Guenther wrote:
> Wouldnt it be a good idea to at least turn the
> maybe_warn_about_returning_address_of_local warnings into an error with
> -pedantic? Or always?

Let me show you an example where I use it. A function has a large object
on the stack which it initialises and returns the address of when called
for the first time.

On a later call it checks how much of it was overwritten and returns
this
number of an estimate of how much stack was used suring the time since
the first call.

Now, I could go through some hoops to hide the fact that I am returning
the adress of an auto, but I prefer not too. And I am OK with seeing
the warning too.

May be crude, but works very reliably.

--
Eyal Lebedinsky (eyal@eyal.emu.id.au) <http://samba.org/eyal/>

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

* Re: Turning warning "returning reference to temporary" into error?
  2003-05-11 20:26       ` Zack Weinberg
  2003-05-11 20:44         ` Richard Guenther
@ 2003-05-12  2:21         ` Fergus Henderson
  1 sibling, 0 replies; 12+ messages in thread
From: Fergus Henderson @ 2003-05-12  2:21 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: Gabriel Dos Reis, Richard Guenther, gcc

On 11-May-2003, Zack Weinberg <zack@codesourcery.com> wrote:
> Gabriel Dos Reis <gdr@integrable-solutions.net> writes:
> 
> > A compiler writer cannot imagine every possible usage.  Which is why
> > somethings are warnings.  In this case, if the return value is ignored
> > then the program is valid.  GCC cannot reject it just because
> > -pedantic  is specified.
> 
> In other places where we can prove that execution of a certain code
> path will cause undefined behavior, but we can't prove that that path
> will be taken at runtime, we issue a warning and generate a trap
> instruction.  An analogous tactic for this scenario would be if we
> generated code to return a distinguished pointer value that's
> guaranteed to cause a segmentation fault when dereferenced.  (This
> should _not_ be 0, as that may mask the bug.)

I don't think that would be a good idea.

Although the behaviour is undefined, there are some important programs
that rely on the assumption that the address of a local is an address
on the stack.  In particular, the Boehm et al. conservative garbage
collector does this.  If I recall correctly, some versions of
LinuxThreads also did this (to implement pthread_self()),
although I don't know off-hand if they still use that technique.

If GCC was changed in a way that stopped this technique from working,
conservative garbage collectors would have to resort to techniques
which are *less* portable, such as inline assembler.  I don't think
that would be an improvement, and I definitely don't think it would
be worth the transition cost.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.

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

* Re: Turning warning "returning reference to temporary" into error?
  2003-05-12  0:01 ` Eyal Lebedinsky
@ 2003-05-12  6:59   ` Richard Guenther
  0 siblings, 0 replies; 12+ messages in thread
From: Richard Guenther @ 2003-05-12  6:59 UTC (permalink / raw)
  To: Eyal Lebedinsky; +Cc: gcc

On Mon, 12 May 2003, Eyal Lebedinsky wrote:

> Richard Guenther wrote:
> > Wouldnt it be a good idea to at least turn the
> > maybe_warn_about_returning_address_of_local warnings into an error with
> > -pedantic? Or always?
>
> Let me show you an example where I use it. A function has a large object
> on the stack which it initialises and returns the address of when called
> for the first time.
>
> On a later call it checks how much of it was overwritten and returns
> this
> number of an estimate of how much stack was used suring the time since
> the first call.

You are relying on undefined behavior here, dont you?

Richard.

--
Richard Guenther <richard dot guenther at uni-tuebingen dot de>
WWW: http://www.tat.physik.uni-tuebingen.de/~rguenth/

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

* Re: Turning warning "returning reference to temporary" into error?
  2003-05-11 20:44         ` Richard Guenther
@ 2003-05-12 18:57           ` Zack Weinberg
  0 siblings, 0 replies; 12+ messages in thread
From: Zack Weinberg @ 2003-05-12 18:57 UTC (permalink / raw)
  To: Richard Guenther; +Cc: Gabriel Dos Reis, gcc

Richard Guenther <rguenth@tat.physik.uni-tuebingen.de> writes:

> I really want this to be diagnosed at compile time, and as this is a
> large project, I want to have an error diagnosed, not only a
> warning. In the current form this is not possible without turning
> every default warning into an error with -Werror.

Probably your best bet is to help out the people working on generic
warning controls.  If we can have a "suppress this specific warning"
toggle, we can have a "turn this specific warning into an error"
toggle too.

zw

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

* Re: Turning warning "returning reference to temporary" into error?
@ 2003-05-12  7:23 Robert Dewar
  0 siblings, 0 replies; 12+ messages in thread
From: Robert Dewar @ 2003-05-12  7:23 UTC (permalink / raw)
  To: eyal, rguenth; +Cc: gcc

> > Richard Guenther wrote:
> > > Wouldnt it be a good idea to at least turn the
> > > maybe_warn_about_returning_address_of_local warnings into an error with
> > > -pedantic? Or always?
> >
> > Let me show you an example where I use it. A function has a large object
> > on the stack which it initialises and returns the address of when called
> > for the first time.
> >
> > On a later call it checks how much of it was overwritten and returns
> > this
> > number of an estimate of how much stack was used suring the time since
> > the first call.
> 
> You are relying on undefined behavior here, dont you?


Sure, that's true from a technical point of view, but in fact this seems a
quite reasonable approach which is likely to work with most compilers. Obviously
there is no legitimate way to do this computation :-)

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

end of thread, other threads:[~2003-05-12 18:57 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-11 17:45 Turning warning "returning reference to temporary" into error? Richard Guenther
2003-05-11 17:57 ` Gabriel Dos Reis
2003-05-11 18:59   ` Richard Guenther
2003-05-11 19:05     ` Gabriel Dos Reis
2003-05-11 19:47       ` Richard Guenther
2003-05-11 20:26       ` Zack Weinberg
2003-05-11 20:44         ` Richard Guenther
2003-05-12 18:57           ` Zack Weinberg
2003-05-12  2:21         ` Fergus Henderson
2003-05-12  0:01 ` Eyal Lebedinsky
2003-05-12  6:59   ` Richard Guenther
2003-05-12  7:23 Robert Dewar

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).