Index: cfe/trunk/include/clang/Basic/Attr.td =================================================================== --- cfe/trunk/include/clang/Basic/Attr.td +++ cfe/trunk/include/clang/Basic/Attr.td @@ -962,6 +962,10 @@ let Spellings = [Keyword<"__forceinline">]; } +def SelectAny : InheritableAttr { + let Spellings = [Declspec<"selectany">]; +} + def Win64 : InheritableAttr { let Spellings = [Keyword<"__w64">]; } Index: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td @@ -1950,6 +1950,8 @@ "weak identifier %0 never declared">; def err_attribute_weak_static : Error< "weak declaration cannot have internal linkage">; +def err_attribute_selectany_non_extern_data : Error< + "'selectany' can only be applied to data items with external linkage">; def warn_attribute_weak_import_invalid_on_definition : Warning< "'weak_import' attribute cannot be specified on a definition">, InGroup; Index: cfe/trunk/lib/CodeGen/CodeGenModule.cpp =================================================================== --- cfe/trunk/lib/CodeGen/CodeGenModule.cpp +++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp @@ -1937,7 +1937,13 @@ return llvm::Function::DLLImportLinkage; else if (D->hasAttr()) return llvm::Function::DLLExportLinkage; - else if (D->hasAttr()) { + else if (D->hasAttr()) { + // selectany symbols are externally visible, so use weak instead of + // linkonce. MSVC optimizes away references to const selectany globals, so + // all definitions should be the same and ODR linkage should be used. + // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx + return llvm::GlobalVariable::WeakODRLinkage; + } else if (D->hasAttr()) { if (GV->isConstant()) return llvm::GlobalVariable::WeakODRLinkage; else Index: cfe/trunk/lib/Sema/SemaDecl.cpp =================================================================== --- cfe/trunk/lib/Sema/SemaDecl.cpp +++ cfe/trunk/lib/Sema/SemaDecl.cpp @@ -4636,6 +4636,15 @@ ND.dropAttr(); } } + + // 'selectany' only applies to externally visible varable declarations. + // It does not apply to functions. + if (SelectAnyAttr *Attr = ND.getAttr()) { + if (isa(ND) || !ND.isExternallyVisible()) { + S.Diag(Attr->getLocation(), diag::err_attribute_selectany_non_extern_data); + ND.dropAttr(); + } + } } /// Given that we are within the definition of the given function, Index: cfe/trunk/lib/Sema/SemaDeclAttr.cpp =================================================================== --- cfe/trunk/lib/Sema/SemaDeclAttr.cpp +++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp @@ -4718,6 +4718,16 @@ Attr.getAttributeSpellingListIndex())); } +static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { + if (!checkMicrosoftExt(S, Attr)) + return; + // Check linkage after possibly merging declaratinos. See + // checkAttributesAfterMerging(). + D->addAttr(::new (S.Context) + SelectAnyAttr(Attr.getRange(), S.Context, + Attr.getAttributeSpellingListIndex())); +} + //===----------------------------------------------------------------------===// // Top Level Sema Entry Points //===----------------------------------------------------------------------===// @@ -4944,6 +4954,9 @@ case AttributeList::AT_ForceInline: handleForceInlineAttr(S, D, Attr); break; + case AttributeList::AT_SelectAny: + handleSelectAnyAttr(S, D, Attr); + break; // Thread safety attributes: case AttributeList::AT_AssertExclusiveLock: Index: cfe/trunk/test/CodeGen/ms-declspecs.c =================================================================== --- cfe/trunk/test/CodeGen/ms-declspecs.c +++ cfe/trunk/test/CodeGen/ms-declspecs.c @@ -1,5 +1,10 @@ // RUN: %clang_cc1 -triple i386-pc-win32 %s -emit-llvm -fms-compatibility -o - | FileCheck %s +__declspec(selectany) int x1 = 1; +const __declspec(selectany) int x2 = 2; +// CHECK: @x1 = weak_odr global i32 1, align 4 +// CHECK: @x2 = weak_odr constant i32 2, align 4 + struct __declspec(align(16)) S { char x; }; Index: cfe/trunk/test/SemaCXX/attr-selectany.cpp =================================================================== --- cfe/trunk/test/SemaCXX/attr-selectany.cpp +++ cfe/trunk/test/SemaCXX/attr-selectany.cpp @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify %s +// MSVC produces similar diagnostics. + +__declspec(selectany) void foo() { } // expected-error{{'selectany' can only be applied to data items with external linkage}} + +__declspec(selectany) int x1 = 1; + +const __declspec(selectany) int x2 = 2; // expected-error{{'selectany' can only be applied to data items with external linkage}} + +extern const __declspec(selectany) int x3 = 3; + +extern const int x4; +const __declspec(selectany) int x4 = 4; + +// MSDN says this is incorrect, but MSVC doesn't diagnose it. +extern __declspec(selectany) int x5; + +static __declspec(selectany) int x6 = 2; // expected-error{{'selectany' can only be applied to data items with external linkage}} + +// FIXME: MSVC accepts this and makes x7 externally visible and comdat, but keep +// it as internal and not weak/linkonce. +static int x7; // expected-note{{previous definition}} +extern __declspec(selectany) int x7; // expected-warning{{attribute declaration must precede definition}} + +int asdf() { return x7; } + +class X { + public: + X(int i) { i++; }; + int i; +}; + +__declspec(selectany) X x(1);