Skip to content

Commit 0f43c99

Browse files
committed
[clang] Check inline defs when emitting speculative vtable
Clang should only emit an available_externally vtable when there are no unused virtual inline functions. Currently, if such such a function is declared without inline inside the class, but is defined inline outside the class, Clang might emit the vtable as available_externally. This happens because Clang only considers the declarations of vtable entries, but not the definitions. This patch addresses this by inspecting the definitions in addition to the declarations.
1 parent 97743b8 commit 0f43c99

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,10 @@ class ItaniumCXXABI : public CodeGen::CGCXXABI {
442442
continue;
443443

444444
const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
445-
if (!Method->getCanonicalDecl()->isInlined())
445+
const FunctionDecl *FD = Method->getDefinition();
446+
const bool IsInlined =
447+
Method->getCanonicalDecl()->isInlined() || (FD && FD->isInlined());
448+
if (!IsInlined)
446449
continue;
447450

448451
StringRef Name = CGM.getMangledName(VtableComponent.getGlobalDecl());

clang/test/CodeGenCXX/vtable-available-externally.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,28 +250,39 @@ struct C : A {
250250
virtual void car();
251251
};
252252

253+
// Inline definition outside body, so we can't emit vtable available_externally
254+
// (see previous).
255+
// CHECK-TEST10-DAG: @_ZTVN6Test101FE = external unnamed_addr constant
256+
struct F : A {
257+
void foo();
258+
virtual void cat(); // inline outside body
259+
};
260+
inline void F::cat() {}
261+
253262
// no key function, vtable will be generated everywhere it will be used
254263
// CHECK-TEST10-DAG: @_ZTVN6Test101EE = linkonce_odr unnamed_addr constant
255264
// CHECK-FORCE-EMIT-DAG: @_ZTVN6Test101EE = linkonce_odr unnamed_addr constant
256265

257266
struct E : A {};
258267

259-
void g(A& a) {
268+
void h(A& a) {
260269
a.foo();
261270
a.bar();
262271
}
263272

264-
void f() {
273+
void g() {
265274
A a;
266-
g(a);
275+
h(a);
267276
B b;
268-
g(b);
277+
h(b);
269278
C c;
270-
g(c);
279+
h(c);
271280
D d;
272-
g(d);
281+
h(d);
273282
E e;
274-
g(e);
283+
h(e);
284+
F f;
285+
h(f);
275286
}
276287

277288
} // Test10

0 commit comments

Comments
 (0)