Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -11367,6 +11367,16 @@ !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().isLocalConstQualified() && + 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,17 @@ +// RUN: %clang_cc1 -emit-llvm -fms-extensions -o - %s | FileCheck %s + + +// Export const variable. + +// CHECK: @x = dso_local dllexport constant i32 3, align 4 +// CHECK: @z = dso_local constant i32 4, align 4 +// CHECK: @y = common dso_local dllexport global i32 0, align 4 + +__declspec(dllexport) int const x = 3; +__declspec(dllexport) const int y; +extern int const z = 4; // expected-warning{{'extern' variable has an initializer}} + +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,6 @@ +// RUN: %clang_cc1 -fsyntax-only -fms-extensions -verify %s + +// Export const variable initialization. + +// expected-no-diagnostics +__declspec(dllexport) int const x = 3; Index: test/Sema/dllexport-2.cpp =================================================================== --- /dev/null +++ test/Sema/dllexport-2.cpp @@ -0,0 +1,5 @@ +// RUN: %clang_cc1 -fsyntax-only -fms-extensions -verify %s + +// Export const variable. + +__declspec(dllexport) int const j; // expected-error {{default initialization of an object of const type 'const int'}} // expected-error {{'j' must have external linkage when declared 'dllexport'}}