Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -5967,7 +5967,21 @@ // dll attributes require external linkage. Static locals may have external // linkage but still cannot be explicitly imported or exported. auto *VD = dyn_cast(&ND); - if (!ND.isExternallyVisible() || (VD && VD->isStaticLocal())) { + const NamespaceDecl *NS = nullptr; + bool isAnonymousNS = false; + bool isMicrosoft = S.Context.getTargetInfo().getCXXABI().isMicrosoft(); + if (VD) { + DeclContext *DC = VD->getDeclContext(); + NS = dyn_cast(DC); + while (DC && NS) { + isAnonymousNS = isAnonymousNS || (NS && NS->getDeclName().isEmpty()); + DC = DC->getParent(); + NS = dyn_cast(DC); + } + } + if ((ND.isExternallyVisible() && isAnonymousNS && isMicrosoft) || + (!(isAnonymousNS && isMicrosoft) && + (!ND.isExternallyVisible() || (VD && VD->isStaticLocal())))) { S.Diag(ND.getLocation(), diag::err_attribute_dll_not_extern) << &ND << Attr; ND.setInvalidDecl(); @@ -11376,6 +11390,14 @@ !isTemplateInstantiation(VDecl->getTemplateSpecializationKind())) Diag(VDecl->getLocation(), diag::warn_extern_init); + // In Microsoft C++ mode, a const variable defined in namespace scope has + // external linkage by default if the variable is declared with + // __declspec(dllexport). + if (Context.getTargetInfo().getCXXABI().isMicrosoft() && + getLangOpts().CPlusPlus && VDecl->getType().isConstQualified() && + VDecl->hasAttr() && VDecl->getDefinition()) + VDecl->setStorageClass(SC_Extern); + // C99 6.7.8p4. All file scoped initializers need to be constant. if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) CheckForConstantInitializer(Init, DclT); Index: test/CodeGen/dllexport-1.c =================================================================== --- /dev/null +++ test/CodeGen/dllexport-1.c @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -fms-extensions -Wno-ignored-attributes -Wno-extern-initializer -o - %s | FileCheck %s -check-prefix CHECK-LNX +// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -emit-llvm -fms-extensions -o - -DMSVC %s | FileCheck %s -check-prefix CHECK-MSVC + +// Export const variable. + +// CHECK-MSVC: @x = dso_local dllexport constant i32 3, align 4 +// CHECK-LNX: @x = constant i32 3, align 4 + +// CHECK-MSVC: @z = dso_local constant i32 4, align 4 +// CHECK-LNX: @z = constant i32 4, align 4 + +// CHECK-MSVC: @y = common dso_local dllexport global i32 0, align 4 +// CHECK-LNX: @y = common global i32 0, align 4 + +__declspec(dllexport) int const x = 3; +__declspec(dllexport) const int y; + +// expected-warning@+1 {{'extern' variable has an initializer}} +extern int const z = 4; + +int main() { + int a = x + y + z; + return a; +} Index: test/Sema/dllexport-1.cpp =================================================================== --- /dev/null +++ test/Sema/dllexport-1.cpp @@ -0,0 +1,35 @@ +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -fms-extensions -verify %s +// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fsyntax-only -fms-extensions -verify %s -DMSVC + +// Export const variable initialization. + +#if MSVC +// expected-no-diagnostics@+4 +#else + // expected-warning@+2 {{__declspec attribute 'dllexport' is not supported}} +#endif +__declspec(dllexport) int const x = 3; + +namespace { +namespace named { +#if MSVC +// expected-no-diagnostics@+4 +#else + // expected-warning@+2 {{__declspec attribute 'dllexport' is not supported}} +#endif +__declspec(dllexport) int const x = 3; +} +} // namespace + +namespace named1 { +namespace { +namespace named { +#if MSVC +// expected-no-diagnostics@+4 +#else + // expected-warning@+2 {{__declspec attribute 'dllexport' is not supported}} +#endif +__declspec(dllexport) int const x = 3; +} +} // namespace +} // namespace named1 Index: test/Sema/dllexport-2.cpp =================================================================== --- /dev/null +++ test/Sema/dllexport-2.cpp @@ -0,0 +1,32 @@ +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -fms-extensions -verify %s +// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fsyntax-only -fms-extensions -verify %s -DMSVC + +// Export const variable. + +#if MSVC +// expected-error@+6 {{default initialization of an object of const type 'const int'}} +// expected-error@+5 {{'j' must have external linkage when declared 'dllexport'}} +#else +// expected-warning@+3 {{__declspec attribute 'dllexport' is not supported}} +// expected-error@+2 {{default initialization of an object of const type}} +#endif +__declspec(dllexport) int const j; + +// With typedef +typedef const int CInt; + +#if MSVC +// expected-error@+6 {{default initialization of an object of const type 'CInt' (aka 'const int')}} +// expected-error@+5 {{'j2' must have external linkage when declared 'dllexport'}} +#else +// expected-warning@+3 {{__declspec attribute 'dllexport' is not supported}} +// expected-error@+2 {{default initialization of an object of const type 'CInt'}} +#endif +__declspec(dllexport) CInt j2; + +#if MSVC +// expected-nodiagnostics +#else +// expected-warning@+2 {{__declspec attribute 'dllexport' is not supported}} +#endif +__declspec(dllexport) CInt j3 = 3; Index: test/SemaCXX/dllexport.cpp =================================================================== --- test/SemaCXX/dllexport.cpp +++ test/SemaCXX/dllexport.cpp @@ -69,7 +69,12 @@ // External linkage is required. __declspec(dllexport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllexport'}} __declspec(dllexport) Internal InternalTypeGlobal; // expected-error{{'InternalTypeGlobal' must have external linkage when declared 'dllexport'}} -namespace { __declspec(dllexport) int InternalGlobal; } // expected-error{{'(anonymous namespace)::InternalGlobal' must have external linkage when declared 'dllexport'}} +#ifdef MS +// expected-nodiagnostics +#else +// expected-error@+1{{anonymous namespace)::InternalGlobal' must have external linkage when declared 'dllexport'}} +namespace { __declspec(dllexport) int InternalGlobal; } +#endif namespace ns { __declspec(dllexport) int ExternalGlobal; } __declspec(dllexport) auto InternalAutoTypeGlobal = Internal(); // expected-error{{'InternalAutoTypeGlobal' must have external linkage when declared 'dllexport'}} @@ -124,7 +129,12 @@ // External linkage is required. template __declspec(dllexport) static int StaticVarTmpl; // expected-error{{'StaticVarTmpl' must have external linkage when declared 'dllexport'}} template __declspec(dllexport) Internal InternalTypeVarTmpl; // expected-error{{'InternalTypeVarTmpl' must have external linkage when declared 'dllexport'}} -namespace { template __declspec(dllexport) int InternalVarTmpl; } // expected-error{{'(anonymous namespace)::InternalVarTmpl' must have external linkage when declared 'dllexport'}} +#ifdef MS +// expected-nodiagnostics +#else +// expected-error@+1{{anonymous namespace)::InternalVarTmpl' must have external linkage when declared 'dllexport'}} +namespace { template __declspec(dllexport) int InternalVarTmpl; } +#endif namespace ns { template __declspec(dllexport) int ExternalVarTmpl = 1; } template __declspec(dllexport) auto InternalAutoTypeVarTmpl = Internal(); // expected-error{{'InternalAutoTypeVarTmpl' must have external linkage when declared 'dllexport'}} Index: test/SemaCXX/dllimport.cpp =================================================================== --- test/SemaCXX/dllimport.cpp +++ test/SemaCXX/dllimport.cpp @@ -121,7 +121,12 @@ // External linkage is required. __declspec(dllimport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllimport'}} __declspec(dllimport) Internal InternalTypeGlobal; // expected-error{{'InternalTypeGlobal' must have external linkage when declared 'dllimport'}} -namespace { __declspec(dllimport) int InternalGlobal; } // expected-error{{'(anonymous namespace)::InternalGlobal' must have external linkage when declared 'dllimport'}} +#ifdef MS +// expected-nodiagnostics +#else +// expected-error@+1{{'(anonymous namespace)::InternalGlobal' must have external linkage when declared 'dllimport'}} +namespace { __declspec(dllimport) int InternalGlobal; } +#endif namespace ns { __declspec(dllimport) int ExternalGlobal; } __declspec(dllimport) auto InternalAutoTypeGlobal = Internal(); // expected-error{{'InternalAutoTypeGlobal' must have external linkage when declared 'dllimport'}} @@ -213,7 +218,12 @@ // External linkage is required. template __declspec(dllimport) static int StaticVarTmpl; // expected-error{{'StaticVarTmpl' must have external linkage when declared 'dllimport'}} template __declspec(dllimport) Internal InternalTypeVarTmpl; // expected-error{{'InternalTypeVarTmpl' must have external linkage when declared 'dllimport'}} -namespace { template __declspec(dllimport) int InternalVarTmpl; } // expected-error{{'(anonymous namespace)::InternalVarTmpl' must have external linkage when declared 'dllimport'}} +#ifdef MS +// expected-nodiagnostics +#else +// expected-error@+2{{'(anonymous namespace)::InternalVarTmpl' must have external linkage when declared 'dllimport'}} +#endif +namespace { template __declspec(dllimport) int InternalVarTmpl; } namespace ns { template __declspec(dllimport) int ExternalVarTmpl; } template __declspec(dllimport) auto InternalAutoTypeVarTmpl = Internal(); // expected-error{{definition of dllimport data}} // expected-error{{'InternalAutoTypeVarTmpl' must have external linkage when declared 'dllimport'}}