From: Hans Wennborg Date: Fri, 4 Sep 2015 19:59:39 +0000 (+0000) Subject: Don't allow dllexport/import on static local variables X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c2705243084f9fa2defec1970aebf9b066ac44d7;p=clang Don't allow dllexport/import on static local variables They might technically have external linkage, but it still doesn't make sense for the user to try and export such variables. This matches MSVC's and MinGW's behaviour. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@246864 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 141f35a6a0..acff354f70 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -5355,9 +5355,11 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) { } } - // dll attributes require external linkage. if (const InheritableAttr *Attr = getDLLAttr(&ND)) { - if (!ND.isExternallyVisible()) { + // 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())) { S.Diag(ND.getLocation(), diag::err_attribute_dll_not_extern) << &ND << Attr; ND.setInvalidDecl(); diff --git a/test/Sema/dllexport.c b/test/Sema/dllexport.c index 69aad2eb54..56c9e74225 100644 --- a/test/Sema/dllexport.c +++ b/test/Sema/dllexport.c @@ -109,6 +109,11 @@ __declspec(dllexport) void redecl6(); // expected-warning{{redeclaration of 'red // External linkage is required. __declspec(dllexport) static int staticFunc(); // expected-error{{'staticFunc' must have external linkage when declared 'dllexport'}} +// Static locals don't count as having external linkage. +void staticLocalFunc() { + __declspec(dllexport) static int staticLocal; // expected-error{{'staticLocal' must have external linkage when declared 'dllexport'}} +} + //===----------------------------------------------------------------------===// diff --git a/test/Sema/dllimport.c b/test/Sema/dllimport.c index d81aecd604..f863499cf8 100644 --- a/test/Sema/dllimport.c +++ b/test/Sema/dllimport.c @@ -168,3 +168,8 @@ __declspec(dllimport) inline void redecl7() {} // External linkage is required. __declspec(dllimport) static int staticFunc(); // expected-error{{'staticFunc' must have external linkage when declared 'dllimport'}} + +// Static locals don't count as having external linkage. +void staticLocalFunc() { + __declspec(dllimport) static int staticLocal; // expected-error{{'staticLocal' must have external linkage when declared 'dllimport'}} +} diff --git a/test/SemaCXX/dllexport.cpp b/test/SemaCXX/dllexport.cpp index c3f26aa315..f7076fb8e8 100644 --- a/test/SemaCXX/dllexport.cpp +++ b/test/SemaCXX/dllexport.cpp @@ -71,14 +71,9 @@ __declspec(dllexport) auto ExternalAutoTypeGlobal = External(); // Thread local variables are invalid. __declspec(dllexport) __thread int ThreadLocalGlobal; // expected-error{{'ThreadLocalGlobal' cannot be thread local when declared 'dllexport'}} -inline void InlineWithThreadLocal() { - static __declspec(dllexport) __thread int ThreadLocalGlobal; // expected-error{{'ThreadLocalGlobal' cannot be thread local when declared 'dllexport'}} -} - -// But if they're in a dllexport function, it's ok, because they will never get imported. +// But a static local TLS var in an export function is OK. inline void __declspec(dllexport) ExportedInlineWithThreadLocal() { - static __declspec(dllexport) __thread int OK1; // no-error - static __thread int OK2; // no-error + static __thread int OK; // no-error } // Export in local scope. diff --git a/test/SemaCXX/dllimport.cpp b/test/SemaCXX/dllimport.cpp index f17c2e2241..91ed6b4260 100644 --- a/test/SemaCXX/dllimport.cpp +++ b/test/SemaCXX/dllimport.cpp @@ -93,16 +93,10 @@ __declspec(dllimport) auto InternalAutoTypeGlobal = Internal(); // expected-erro // Thread local variables are invalid. __declspec(dllimport) __thread int ThreadLocalGlobal; // expected-error{{'ThreadLocalGlobal' cannot be thread local when declared 'dllimport'}} -inline void InlineWithThreadLocal() { - static __declspec(dllimport) __thread int ThreadLocalGlobal; // expected-error{{'ThreadLocalGlobal' cannot be thread local when declared 'dllimport'}} -} - -// But if they're in a dllimported function, it's OK because we will not inline the function. // This doesn't work on MinGW, because there, dllimport on the inline function is ignored. #ifndef GNU inline void __declspec(dllimport) ImportedInlineWithThreadLocal() { - static __declspec(dllimport) __thread int OK1; // no-error - static __thread int OK2; // no-error + static __thread int OK; // no-error } #endif