[PATCH] D45978: dllexport const variables must have external linkage.

Zahira Ammarguellat via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 12 07:10:50 PST 2019


zahiraam updated this revision to Diff 186460.
zahiraam marked 3 inline comments as done.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D45978/new/

https://reviews.llvm.org/D45978

Files:
  lib/Sema/SemaDecl.cpp
  test/CodeGen/dllexport.c
  test/Sema/dllexport-1.cpp
  test/Sema/dllexport-2.cpp


Index: test/Sema/dllexport-2.cpp
===================================================================
--- /dev/null
+++ test/Sema/dllexport-2.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -triple i686-win32 -fsyntax-only -fms-extensions -verify %s
+// RUN: %clang_cc1 -triple x86_64-win32 -fsyntax-only -fms-extensions -verify -std=c++11 %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'}}
Index: test/Sema/dllexport-1.cpp
===================================================================
--- /dev/null
+++ test/Sema/dllexport-1.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple i686-win32 -fsyntax-only -fms-extensions -verify %s
+// RUN: %clang_cc1 -triple x86_64-win32 -fsyntax-only -fms-extensions -verify -std=c++11 %s
+
+// CHECK: @"?x@@3HB" = dso_local dllexport constant i32 3, align 4
+
+// Export const variable initialization.
+
+// expected-no-diagnostics
+__declspec(dllexport) int const x = 3;
Index: test/CodeGen/dllexport.c
===================================================================
--- test/CodeGen/dllexport.c
+++ test/CodeGen/dllexport.c
@@ -113,3 +113,29 @@
 // CHECK-DAG: define dso_local dllexport void @precedenceRedecl2()
 void __declspec(dllexport) precedenceRedecl2(void);
 void __declspec(dllimport) precedenceRedecl2(void) {}
+
+// Export const variable.
+
+// CHECK-DAG: @x = dso_local dllexport constant i32 3, align 4
+// CHECK-DAG: @z = dso_local constant i32 4, align 4
+// CHECK-DAG: @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;
+}
+
+// CHECK-DAG: @a = dso_local dllexport constant i32 3, align 4
+// CHECK-DAG: @b = dso_local constant i32 4, align 4
+
+__declspec(dllexport) int const a = 3;
+extern int const b = 4; // expected-warning{{'extern' variable has an initializer}}
+
+int foo() {
+  int c = a + b;
+  return c;
+}
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<DLLExportAttr>() &&
+        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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45978.186460.patch
Type: text/x-patch
Size: 3110 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190212/d2eaea4f/attachment.bin>


More information about the cfe-commits mailing list