什么是"降低vtable引用"?

spr*_*aff 2 gcc compiler-errors clang vtable compiler-warnings

Clang自己的诊断宣传包含以下内容:

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

谷歌搜索这个短语并没有给出任何非常有用的信息,后续的例子完全不相关.

有人可以发一个它正在谈论的例子吗?

谢谢.

Chr*_*ner 5

这是一个例子:

struct a {
  virtual int bar();
};

struct foo : public virtual a {
};

void test(foo *P) {
  return P->bar()+*P;
}
Run Code Online (Sandbox Code Playgroud)

Clang生产:

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

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'
Run Code Online (Sandbox Code Playgroud)

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

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

struct a {
  virtual int bar();
};

struct foo : public virtual a {
};

void test(foo *P) {
  P->bar();
}
Run Code Online (Sandbox Code Playgroud)

..所以代码是有效的,然后要求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 *'))))))
Run Code Online (Sandbox Code Playgroud)

-克里斯