开发者

What are "lowered vtable references"?

开发者 https://www.devze.com 2023-03-25 04:00 出处:网络
Clang\'s own diagnostics propaganda contains this exerpt: Since Clang has range highlighting, it never needs to pretty print your code back out to you. This is particularly bad in G++ (which often e

Clang's own diagnostics propaganda contains this exerpt:

Since Clang has range highlighting, it never needs to pretty print your code back out to you. This is particularly bad in G++ (which often emits errors containing lowered vtable references), but even GCC can produce inscrutible error messages in 开发者_运维百科some cases when it tries to do this.

Googling this phrase doesn't give anything very helpful, and the subsequent example is completely unrelated.

Can someone please post an example of what it's talking about?

Thanks.


Here is an example:

struct a {
  virtual int bar();
};

struct foo : public virtual a {
};

void test(foo *P) {
  return P->bar()+*P;
}

Clang produces:

t.cc:9:18: error: invalid operands to binary expression ('int' and 'foo')
  return P->bar()+*P;
         ~~~~~~~~^~~

GCC 4.2 produces:

t.cc: In function ‘void test(foo*)’:
t.cc:9: error: no match for ‘operator+’ in ‘(((a*)P) + (*(long int*)(P->foo::<anonymous>.a::_vptr$a + -0x00000000000000020)))->a::bar() + * P’
t.cc:9: error: return-statement with a value, in function returning 'void'

GCC does this because its C++ frontend is bolted on top of the C frontend in many cases. Instead of building C++-specific Abstract Syntax Trees (ASTs) for various C++ operations, the parser just lowers them immediately to their C equivalent. In this case, GCC synthesizes an struct to contain the vtable, and the pointer dereference to bar is then lowered into a series of C pointer dereferences, casts, pointer arithmetic etc.

Clang does not have this problem, because it has a very clean AST that directly represents the source code. If you change the example to:

struct a {
  virtual int bar();
};

struct foo : public virtual a {
};

void test(foo *P) {
  P->bar();
}

.. so that the code is valid, then ask clang to dump its ast with "clang -cc1 -ast-dump t.cc", you get:

...
void test(foo *P)
(CompoundStmt 0x10683cae8 <t.cc:8:19, line:10:1>
  (CXXMemberCallExpr 0x10683ca78 <line:9:3, col:10> 'int'
    (MemberExpr 0x10683ca40 <col:3, col:6> '<bound member function type>' ->bar 0x10683bef0
      (ImplicitCastExpr 0x10683cac8 <col:3> 'struct a *' <UncheckedDerivedToBase (virtual a)>
        (ImplicitCastExpr 0x10683ca28 <col:3> 'struct foo *' <LValueToRValue>
          (DeclRefExpr 0x10683ca00 <col:3> 'struct foo *' lvalue ParmVar 0x10683c8a0 'P' 'struct foo *'))))))

-Chris

0

精彩评论

暂无评论...
验证码 换一张
取 消