Skip to content

Commit f4a5e8f

Browse files
committed
Disable -Wweak-vtables when there are no key functions
Our -Wweak-vtables diagnostic is powered by our key function calculation, which checks if key functions are enabled. We won't find any key functions in C++ ABIs that lack key functions, so -Wweak-vtables was warning on every dynamic class before this change. So, turn off this warning in ABIs without key functions. Addresses PR31220 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@288850 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5dee4f4 commit f4a5e8f

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

lib/Sema/SemaDeclCXX.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14281,6 +14281,8 @@ bool Sema::DefineUsedVTables() {
1428114281
CXXRecordDecl *Class = VTableUses[I].first->getDefinition();
1428214282
if (!Class)
1428314283
continue;
14284+
TemplateSpecializationKind ClassTSK =
14285+
Class->getTemplateSpecializationKind();
1428414286

1428514287
SourceLocation Loc = VTableUses[I].second;
1428614288

@@ -14304,9 +14306,8 @@ bool Sema::DefineUsedVTables() {
1430414306
// of an explicit instantiation declaration, suppress the
1430514307
// vtable; it will live with the explicit instantiation
1430614308
// definition.
14307-
bool IsExplicitInstantiationDeclaration
14308-
= Class->getTemplateSpecializationKind()
14309-
== TSK_ExplicitInstantiationDeclaration;
14309+
bool IsExplicitInstantiationDeclaration =
14310+
ClassTSK == TSK_ExplicitInstantiationDeclaration;
1431014311
for (auto R : Class->redecls()) {
1431114312
TemplateSpecializationKind TSK
1431214313
= cast<CXXRecordDecl>(R)->getTemplateSpecializationKind();
@@ -14339,17 +14340,20 @@ bool Sema::DefineUsedVTables() {
1433914340
if (VTablesUsed[Canonical])
1434014341
Consumer.HandleVTable(Class);
1434114342

14342-
// Optionally warn if we're emitting a weak vtable.
14343-
if (Class->isExternallyVisible() &&
14344-
Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
14343+
// Warn if we're emitting a weak vtable. The vtable will be weak if there is
14344+
// no key function or the key function is inlined. Don't warn in C++ ABIs
14345+
// that lack key functions, since the user won't be able to make one.
14346+
if (Context.getTargetInfo().getCXXABI().hasKeyFunctions() &&
14347+
Class->isExternallyVisible() && ClassTSK != TSK_ImplicitInstantiation) {
1434514348
const FunctionDecl *KeyFunctionDef = nullptr;
14346-
if (!KeyFunction ||
14347-
(KeyFunction->hasBody(KeyFunctionDef) &&
14348-
KeyFunctionDef->isInlined()))
14349-
Diag(Class->getLocation(), Class->getTemplateSpecializationKind() ==
14350-
TSK_ExplicitInstantiationDefinition
14351-
? diag::warn_weak_template_vtable : diag::warn_weak_vtable)
14352-
<< Class;
14349+
if (!KeyFunction || (KeyFunction->hasBody(KeyFunctionDef) &&
14350+
KeyFunctionDef->isInlined())) {
14351+
Diag(Class->getLocation(),
14352+
ClassTSK == TSK_ExplicitInstantiationDefinition
14353+
? diag::warn_weak_template_vtable
14354+
: diag::warn_weak_vtable)
14355+
<< Class;
14356+
}
1435314357
}
1435414358
}
1435514359
VTableUses.clear();

test/SemaCXX/warn-weak-vtables.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// RUN: %clang_cc1 %s -fsyntax-only -verify -triple %itanium_abi_triple -Wweak-vtables -Wweak-template-vtables
2-
// RUN: %clang_cc1 %s -fsyntax-only -triple %ms_abi_triple -Werror -Wno-weak-vtables -Wno-weak-template-vtables
2+
//
3+
// Check that this warning is disabled on MS ABI targets which don't have key
4+
// functions.
5+
// RUN: %clang_cc1 %s -fsyntax-only -triple %ms_abi_triple -Werror -Wweak-vtables -Wweak-template-vtables
36

47
struct A { // expected-warning {{'A' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit}}
58
virtual void f() { }

0 commit comments

Comments
 (0)