public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/32525]  New: Request for new warning: useless dynamic_casts
@ 2007-06-27 18:08 lloyd at randombit dot net
  2007-06-27 18:14 ` [Bug c++/32525] " pinskia at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: lloyd at randombit dot net @ 2007-06-27 18:08 UTC (permalink / raw)
  To: gcc-bugs

It might be useful for GCC to warn about dynamic_casts which are not necessary.
For instance a dynamic_cast<T*>(T*), or a dynamic_cast from a derived class to
a base class (there might be some corner cases here with multiple
inheritance?). I do see that such dynamic_casts are no-op'ed away (even without
any optimization flags! (at least in my toy test program)), which is certainly
positive. However it would be nice if the programmer was notified about them,
since even if there is no run-time cost, there is a source-level increase in
complexity which can easily be avoided (and there may well be run-time costs
involved with other compilers).

A quick example of the sort of thing I have in mind:

class base
{
  public:
    virtual int f() = 0;
    virtual ~base() {}
};

class derived : public base
{
  public:
    int f() { return 1; }
};

#include <stdio.h>

int main()
{
    derived* obj = new derived();
    base* baseptr = dynamic_cast<base*>(obj); // warn: to a base class
    derived* sametype = dynamic_cast<derived*>(obj); // warn: same type
    derived* from_base = dynamic_cast<derived*>(baseptr); // ok

    printf("%d %d %d %d\n",
           obj->f(), baseptr->f(), sametype->f(), from_base->f());
}

(Compiling this on x86-64 shows GCC 4.1.0 is no-op'ing the first two
dynamic_casts, with or without optimization).


-- 
           Summary: Request for new warning: useless dynamic_casts
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: lloyd at randombit dot net


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32525


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

* [Bug c++/32525] Request for new warning: useless dynamic_casts
  2007-06-27 18:08 [Bug c++/32525] New: Request for new warning: useless dynamic_casts lloyd at randombit dot net
@ 2007-06-27 18:14 ` pinskia at gcc dot gnu dot org
  2007-06-27 18:54 ` bangerth at dealii dot org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-06-27 18:14 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-06-27 18:14 -------
> However it would be nice if the programmer was notified about them,
> since even if there is no run-time cost, there is a source-level increase in
> complexity which can easily be avoided (and there may well be run-time costs
> involved with other compilers).

If this warning comes into GCC, we should disable it for templates.  The main
reason why I say that is because if you do:
template<typename A, typename B>
A* f(B *b)
{
  return dynamic_cast<A*>(b);
}

And then instanitite it where typename A == typename B, it is hard to avoid the
warning in this case.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32525


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

* [Bug c++/32525] Request for new warning: useless dynamic_casts
  2007-06-27 18:08 [Bug c++/32525] New: Request for new warning: useless dynamic_casts lloyd at randombit dot net
  2007-06-27 18:14 ` [Bug c++/32525] " pinskia at gcc dot gnu dot org
@ 2007-06-27 18:54 ` bangerth at dealii dot org
  2007-06-27 19:06 ` lloyd at randombit dot net
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: bangerth at dealii dot org @ 2007-06-27 18:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from bangerth at dealii dot org  2007-06-27 18:53 -------
This strikes me as one of the things that hardly anybody would ever find
useful. I mean, yes it happens, but no, it doesn't hurt, and I haven't
seen such code written in the first place ever. Warnings are for cases
where either code may not do what you expect, or where a certain way
of coding has a significant cost that can be avoided.

I doubt anyone will ever implement this.

W.


-- 

bangerth at dealii dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bangerth at dealii dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32525


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

* [Bug c++/32525] Request for new warning: useless dynamic_casts
  2007-06-27 18:08 [Bug c++/32525] New: Request for new warning: useless dynamic_casts lloyd at randombit dot net
  2007-06-27 18:14 ` [Bug c++/32525] " pinskia at gcc dot gnu dot org
  2007-06-27 18:54 ` bangerth at dealii dot org
@ 2007-06-27 19:06 ` lloyd at randombit dot net
  2007-07-14 13:02   ` Gabriel Dos Reis
  2007-06-27 19:17 ` bangerth at dealii dot org
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 10+ messages in thread
From: lloyd at randombit dot net @ 2007-06-27 19:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from lloyd at randombit dot net  2007-06-27 19:06 -------
"I haven't seen such code written in the first place ever."

Neither had I, until I found out it is endemic in a large project at work.  

I'd just as soon write a script to find these cases, but figuring out what the
type of the casted-from pointer/reference is can be somewhat nontrivial.

"Warnings are for cases where either code may not do what you expect, or where
a certain way of coding has a significant cost that can be avoided."

I think that's a good definition. My impression is that dynamic_cast is fairly
expensive, and while it is great that GCC noops out this case I suspect not all
compilers will do the same; at this point I'm not even sure that GCC does it
consistently. So I'd figure it a reasonable case for a warning as per your
second condition.

"I doubt anyone will ever implement this."

I've gotten used to that. :)


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32525


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

* [Bug c++/32525] Request for new warning: useless dynamic_casts
  2007-06-27 18:08 [Bug c++/32525] New: Request for new warning: useless dynamic_casts lloyd at randombit dot net
                   ` (2 preceding siblings ...)
  2007-06-27 19:06 ` lloyd at randombit dot net
@ 2007-06-27 19:17 ` bangerth at dealii dot org
  2007-06-27 19:33 ` lloyd at randombit dot net
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: bangerth at dealii dot org @ 2007-06-27 19:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from bangerth at dealii dot org  2007-06-27 19:17 -------
(In reply to comment #3)
> I think that's a good definition. My impression is that dynamic_cast is fairly
> expensive,

But only if the compiler can't know the actual type of an object (which
is exactly the case that you want to treat). If the actual type of an
object is known or if you are casting to a base class, dynamic_cast is as 
cheap as static_cast.

> "I doubt anyone will ever implement this."
> 
> I've gotten used to that. :)

Well, people implement what they consider important to them. PRs about
uninteresting
things will lie dormant until there are no interesting things left to 
implement.

I think everyone's time would be better used if you tried to find cases
where gcc doesn't produce a no-op for the constructs you want to warn
about. That would be a missed-optimization, rather than a more or less
uninteresting warning, and would receive more interest.

W.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32525


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

* [Bug c++/32525] Request for new warning: useless dynamic_casts
  2007-06-27 18:08 [Bug c++/32525] New: Request for new warning: useless dynamic_casts lloyd at randombit dot net
                   ` (3 preceding siblings ...)
  2007-06-27 19:17 ` bangerth at dealii dot org
@ 2007-06-27 19:33 ` lloyd at randombit dot net
  2007-07-14 13:02 ` gdr at cs dot tamu dot edu
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: lloyd at randombit dot net @ 2007-06-27 19:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from lloyd at randombit dot net  2007-06-27 19:33 -------

I filed the bug because it seems like this would be at least marginally useful,
and this way people can find it / read the discussion / whatever. Even if the
end result is WONTFIX, that at least lets anyone in the future who searches the
bug database know what the situation is.

I'm sorry if I made it sound like I was expecting this to be implemented
immediately or anything like that. That is not the case at all; even serious
problems like code miscompilations can go a good while without being analyzed
or fixed due to time and resource constraints, and something like this
naturally falls much (much) deeper into the queue of things to work on. Thus my
comment about being used to it, I know there is are many more
interesting/important things to work on in GCC.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32525


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

* Re: [Bug c++/32525] Request for new warning: useless dynamic_casts
  2007-06-27 19:06 ` lloyd at randombit dot net
@ 2007-07-14 13:02   ` Gabriel Dos Reis
  0 siblings, 0 replies; 10+ messages in thread
From: Gabriel Dos Reis @ 2007-07-14 13:02 UTC (permalink / raw)
  To: gcc-bugzilla; +Cc: gcc-bugs

"lloyd at randombit dot net" <gcc-bugzilla@gcc.gnu.org> writes:

| I think that's a good definition. My impression is that dynamic_cast is fairly
| expensive,

well, I don't think GCC should be getting into the business of warning
about expensive operations.  

-- Gaby


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

* [Bug c++/32525] Request for new warning: useless dynamic_casts
  2007-06-27 18:08 [Bug c++/32525] New: Request for new warning: useless dynamic_casts lloyd at randombit dot net
                   ` (4 preceding siblings ...)
  2007-06-27 19:33 ` lloyd at randombit dot net
@ 2007-07-14 13:02 ` gdr at cs dot tamu dot edu
  2007-07-15  0:04 ` sebor at roguewave dot com
  2010-02-21  0:17 ` manu at gcc dot gnu dot org
  7 siblings, 0 replies; 10+ messages in thread
From: gdr at cs dot tamu dot edu @ 2007-07-14 13:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from gdr at cs dot tamu dot edu  2007-07-14 13:02 -------
Subject: Re:  Request for new warning: useless dynamic_casts

"lloyd at randombit dot net" <gcc-bugzilla@gcc.gnu.org> writes:

| I think that's a good definition. My impression is that dynamic_cast is
fairly
| expensive,

well, I don't think GCC should be getting into the business of warning
about expensive operations.  

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32525


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

* [Bug c++/32525] Request for new warning: useless dynamic_casts
  2007-06-27 18:08 [Bug c++/32525] New: Request for new warning: useless dynamic_casts lloyd at randombit dot net
                   ` (5 preceding siblings ...)
  2007-07-14 13:02 ` gdr at cs dot tamu dot edu
@ 2007-07-15  0:04 ` sebor at roguewave dot com
  2010-02-21  0:17 ` manu at gcc dot gnu dot org
  7 siblings, 0 replies; 10+ messages in thread
From: sebor at roguewave dot com @ 2007-07-15  0:04 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from sebor at roguewave dot com  2007-07-15 00:03 -------
In cases when the compiler can figure out that the cast is unnecessary it would
be even better if it would optimize it away than to complain to the user about
not being able to do it.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32525


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

* [Bug c++/32525] Request for new warning: useless dynamic_casts
  2007-06-27 18:08 [Bug c++/32525] New: Request for new warning: useless dynamic_casts lloyd at randombit dot net
                   ` (6 preceding siblings ...)
  2007-07-15  0:04 ` sebor at roguewave dot com
@ 2010-02-21  0:17 ` manu at gcc dot gnu dot org
  7 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-02-21  0:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from manu at gcc dot gnu dot org  2010-02-21 00:17 -------
(In reply to comment #5)
> I filed the bug because it seems like this would be at least marginally useful,
> and this way people can find it / read the discussion / whatever. Even if the
> end result is WONTFIX, that at least lets anyone in the future who searches the
> bug database know what the situation is.

Fair enough. Thanks everybody for the comments. Closing then. 


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu dot org
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |WONTFIX


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32525


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

end of thread, other threads:[~2010-02-21  0:17 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-27 18:08 [Bug c++/32525] New: Request for new warning: useless dynamic_casts lloyd at randombit dot net
2007-06-27 18:14 ` [Bug c++/32525] " pinskia at gcc dot gnu dot org
2007-06-27 18:54 ` bangerth at dealii dot org
2007-06-27 19:06 ` lloyd at randombit dot net
2007-07-14 13:02   ` Gabriel Dos Reis
2007-06-27 19:17 ` bangerth at dealii dot org
2007-06-27 19:33 ` lloyd at randombit dot net
2007-07-14 13:02 ` gdr at cs dot tamu dot edu
2007-07-15  0:04 ` sebor at roguewave dot com
2010-02-21  0:17 ` manu at gcc dot gnu dot org

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