From: Reid Kleckner Date: Wed, 12 Jun 2019 18:53:49 +0000 (+0000) Subject: [MS] Pretend constexpr variable template specializations are inline X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9fa6b782860070b0501975570743168f45eacebe;p=clang [MS] Pretend constexpr variable template specializations are inline Fixes link errors with clang and the latest Visual C++ 14.21.27702 headers, which was reported as PR42027. I chose to intentionally make these things linkonce_odr, i.e. discardable, so that we don't emit definitions of these things in every translation unit that includes STL headers. Note that this is *not* what MSVC does: MSVC has not yet implemented C++ DR2387, so they emit fully specialized constexpr variable templates with static / internal linkage. Reviewers: rsmith Differential Revision: https://reviews.llvm.org/D63175 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@363191 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index f732a7531d..d696d262c7 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -9799,10 +9799,22 @@ static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, return StrongLinkage; case TSK_ExplicitSpecialization: - return Context.getTargetInfo().getCXXABI().isMicrosoft() && - VD->isStaticDataMember() - ? GVA_StrongODR - : StrongLinkage; + if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { + // If this is a fully specialized constexpr variable template, pretend it + // was marked inline. MSVC 14.21.27702 headers define _Is_integral in a + // header this way, and we don't want to emit non-discardable definitions + // of these variables in every TU that includes . This + // behavior can be removed if the headers change to explicitly mark such + // variable template specializations inline. + if (isa(VD) && VD->isConstexpr()) + return GVA_DiscardableODR; + + // Use ODR linkage for static data members of fully specialized templates + // to prevent duplicate definition errors with MSVC. + if (VD->isStaticDataMember()) + return GVA_StrongODR; + } + return StrongLinkage; case TSK_ExplicitInstantiationDefinition: return GVA_StrongODR; diff --git a/test/CodeGenCXX/ms-constexpr-var-template.cpp b/test/CodeGenCXX/ms-constexpr-var-template.cpp new file mode 100644 index 0000000000..a40f7aacac --- /dev/null +++ b/test/CodeGenCXX/ms-constexpr-var-template.cpp @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -emit-llvm -triple=x86_64-windows-msvc -fms-compatibility %s -o - | FileCheck %s + +template constexpr bool _Is_integer = false; +template <> constexpr bool _Is_integer = true; +template <> constexpr bool _Is_integer = false; +extern "C" const bool *escape = &_Is_integer; + +// CHECK: @"??$_Is_integer@H@@3_NB" = linkonce_odr dso_local constant i8 1, comdat, align 1 +// Should not emit _Is_integer, since it's not referenced. +// CHECK-NOT: @"??$_Is_integer@D@@3_NB" +// CHECK: @escape = dso_local global i8* @"??$_Is_integer@H@@3_NB", align 8