什么是“降低vtable引用”?

时间:2011-08-03 13:42:10

标签: gcc compiler-errors compiler-warnings clang vtable

Clang自己的diagnostics propaganda包含此内容:

  

由于Clang具有范围突出显示功能,因此您无需再将代码打印出来。这在G ++中尤其糟糕(通常会发出包含较低vtable引用的错误),但在某些情况下,即使GCC在尝试执行此操作时也会产生难以理解的错误消息。

谷歌搜索这句话并没有给出任何有用的信息,后续的例子完全不相关。

有人可以发一个关于它在谈论什么的例子吗?

感谢。

1 个答案:

答案 0 :(得分:5)

以下是一个例子:

struct a {
  virtual int bar();
};

struct foo : public virtual a {
};

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

Clang产生:

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

GCC 4.2产生:

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这样做是因为在许多情况下,它的C ++前端用螺栓固定在C前端之上。而不是为各种C ++操作构建特定于C ++的抽象语法树(AST),解析器只是将它们立即降低到它们的C等价物。在这种情况下,GCC合成一个包含vtable的结构,然后将指向dereference的bar降低为一系列C指针解引用,强制转换,指针运算等。

Clang没有这个问题,因为它有一个非常干净的AST直接代表源代码。如果您将示例更改为:

struct a {
  virtual int bar();
};

struct foo : public virtual a {
};

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

..以便代码有效,然后让clang用“clang -cc1 -ast-dump t.cc”转储它的ast,你得到:

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

相关问题