// RUN: %clang_cc1 -emit-llvm -triple i686-pc-linux-gnu -std=c++1y -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-NO-OPT // RUN: %clang_cc1 -emit-llvm -triple i686-pc-linux-gnu -std=c++1y -O3 -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-OPT // RUN: %clang_cc1 -emit-llvm -triple i686-pc-win32 -std=c++1y -o - %s | FileCheck %s --check-prefix=CHECK-MS // This check logically is attached to 'template int S::i;' below. // CHECK: @_ZN1SIiE1iE = weak_odr global i32 // This check is logically attached to 'template int ExportedStaticLocal::f()' below. // CHECK-OPT: @_ZZN19ExportedStaticLocal1fIiEEvvE1i = linkonce_odr global template struct plus { Result operator()(const T& t, const U& u) const; }; template Result plus::operator()(const T& t, const U& u) const { return t + u; } // CHECK-LABEL: define weak_odr i32 @_ZNK4plusIillEclERKiRKl template struct plus; namespace EarlyInstantiation { // Check that we emit definitions if we instantiate a function definition before // it gets explicitly instantiatied. template struct S { constexpr int constexpr_function() { return 0; } auto deduced_return_type() { return 0; } }; // From an implicit instantiation. constexpr int a = S().constexpr_function(); int b = S().deduced_return_type(); // From an explicit instantiation declaration. extern template struct S; constexpr int c = S().constexpr_function(); int d = S().deduced_return_type(); // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIcE18constexpr_functionEv( // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIcE19deduced_return_typeEv( // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIiE18constexpr_functionEv( // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIiE19deduced_return_typeEv( template struct S; template struct S; template constexpr int constexpr_function() { return 0; } template auto deduced_return_type() { return 0; } // From an implicit instantiation. constexpr int e = constexpr_function(); int f = deduced_return_type(); // From an explicit instantiation declaration. extern template int constexpr_function(); extern template auto deduced_return_type(); constexpr int g = constexpr_function(); int h = deduced_return_type(); // The FIXMEs below are for PR19551. // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation18constexpr_functionIcEEiv( // FIXME: define weak_odr i32 @_ZN18EarlyInstantiation19deduced_return_typeIcEEiv( // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation18constexpr_functionIiEEiv( // FIXME: define weak_odr i32 @_ZN18EarlyInstantiation19deduced_return_typeIiEEiv( template int constexpr_function(); // FIXME template auto deduced_return_type(); template int constexpr_function(); // FIXME template auto deduced_return_type(); } namespace LateInstantiation { // Check that we downgrade the linkage to available_externally if we see an // explicit instantiation declaration after the function template is // instantiated. template struct S { constexpr int f() { return 0; } }; template constexpr int f() { return 0; } // Trigger eager instantiation of the function definitions. int a, b = S().f() + f() + a; int c, d = S().f() + f() + a; // Don't allow some of those definitions to be emitted. extern template struct S; extern template int f(); // Check that we declare, define, or provide an available-externally // definition as appropriate. // CHECK: define linkonce_odr i32 @_ZN17LateInstantiation1SIcE1fEv( // CHECK: define linkonce_odr i32 @_ZN17LateInstantiation1fIcEEiv( // CHECK-NO-OPT: declare i32 @_ZN17LateInstantiation1SIiE1fEv( // CHECK-NO-OPT: declare i32 @_ZN17LateInstantiation1fIiEEiv( // CHECK-OPT: define available_externally i32 @_ZN17LateInstantiation1SIiE1fEv( // CHECK-OPT: define available_externally i32 @_ZN17LateInstantiation1fIiEEiv( } namespace PR21718 { // The linkage of a used constexpr member function can change from linkonce_odr // to weak_odr after explicit instantiation without errors about defining the // same function twice. template struct S { // CHECK-LABEL: define weak_odr i32 @_ZN7PR217181SIiE1fEv __attribute__((used)) constexpr int f() { return 0; } }; int g() { return S().f(); } template struct S; } namespace NestedClasses { // Check how explicit instantiation of an outer class affects the inner class. template struct Outer { struct Inner { void f() {} }; }; // Explicit instantiation definition of Outer causes explicit instantiation // definition of Inner. template struct Outer; // CHECK: define weak_odr void @_ZN13NestedClasses5OuterIiE5Inner1fEv // CHECK-MS: define weak_odr dso_local x86_thiscallcc void @"?f@Inner@?$Outer@H@NestedClasses@@QAEXXZ" // Explicit instantiation declaration of Outer causes explicit instantiation // declaration of Inner, but not in MSVC mode. extern template struct Outer; auto use = &Outer::Inner::f; // CHECK: {{declare|define available_externally}} void @_ZN13NestedClasses5OuterIcE5Inner1fEv // CHECK-MS: define linkonce_odr dso_local x86_thiscallcc void @"?f@Inner@?$Outer@D@NestedClasses@@QAEXXZ" } // Check that we emit definitions from explicit instantiations even when they // occur prior to the definition itself. template struct S { void f(); static void g(); static int i; struct S2 { void h(); }; }; // CHECK-LABEL: define weak_odr void @_ZN1SIiE1fEv template void S::f(); // CHECK-LABEL: define weak_odr void @_ZN1SIiE1gEv template void S::g(); // See the check line at the top of the file. template int S::i; // CHECK-LABEL: define weak_odr void @_ZN1SIiE2S21hEv template void S::S2::h(); template void S::f() {} template void S::g() {} template int S::i; template void S::S2::h() {} namespace ExportedStaticLocal { void sink(int&); template inline void f() { static int i; sink(i); } // See the check line at the top of the file. extern template void f(); void use() { f(); } } namespace DefaultedMembers { struct B { B(); B(const B&); ~B(); }; template struct A : B { A() = default; ~A() = default; }; extern template struct A; // CHECK-LABEL: define {{.*}} @_ZN16DefaultedMembers1AIiEC2Ev // CHECK-LABEL: define {{.*}} @_ZN16DefaultedMembers1AIiED2Ev A ai; // CHECK-LABEL: define {{.*}} @_ZN16DefaultedMembers1AIiEC2ERKS1_ A ai2(ai); // CHECK-NOT: @_ZN16DefaultedMembers1AIcE template struct A; }