[cfe-commits] r159212 - in /cfe/trunk: include/clang/AST/DeclCXX.h include/clang/AST/Expr.h lib/AST/DeclCXX.cpp lib/AST/Expr.cpp lib/CodeGen/CGExprCXX.cpp lib/Sema/SemaExpr.cpp test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp

John McCall rjmccall at apple.com
Wed Jun 27 12:11:49 PDT 2012


On Jun 27, 2012, at 11:41 AM, Rafael EspĂ­ndola wrote:
>> There is another latent bug in this optimization:  'this' will have been
>> converted to the base type, but we may be calling a method on a
>> derived type, which will then be passed an invalid 'this' pointer.
>> 
>> Why don't we just constrain the optimization for now to only apply
>> when the static method has not been overridden in the most-derived
>> type?  That will catch the common case of derived->foo().
> 
> I fixed the other comments (thanks!), but I am not able to build a
> testcase where this fails. What I have tried was
> 
> struct foo {
>  int x;
> };
> struct bar {
>  int y;
>  virtual int f() { return y; }
> };
> struct zed final : public foo, public bar {
>  int z;
>  virtual int f() {return z;}
> };
> int f(zed *z) {
>  return static_cast<bar*>(z)->f();
> }
> 
> In the hope that f would call zed::f with a bar* this, but it is
> correctly passing a zed*. Is this what you had in mind?


Yes.  Your test case has a subtle flaw:  'bar' is actually the primary base
of 'zed' (and is therefore laid out at offset zero) because 'foo' doesn't
provide a vtable.

John.



More information about the cfe-commits mailing list