r209954 - Diagnose dll attribute on member of class that already has a dll attribute

Hans Wennborg hans at hanshq.net
Fri May 30 19:08:49 PDT 2014


Author: hans
Date: Fri May 30 21:08:49 2014
New Revision: 209954

URL: http://llvm.org/viewvc/llvm-project?rev=209954&view=rev
Log:
Diagnose dll attribute on member of class that already has a dll attribute

Differential Revision: http://reviews.llvm.org/D3973

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/dllimport.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=209954&r1=209953&r2=209954&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri May 30 21:08:49 2014
@@ -2109,6 +2109,8 @@ def err_attribute_dllimport_data_definit
   "definition of dllimport data">;
 def err_attribute_dllimport_static_field_definition : Error<
   "definition of dllimport static field not allowed">;
+def err_attribute_dll_member_of_dll_class : Error<
+  "attribute %q0 cannot be applied to member of %q1 class">;
 def err_attribute_weakref_not_static : Error<
   "weakref declaration must have internal linkage">;
 def err_attribute_weakref_not_global_context : Error<

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=209954&r1=209953&r2=209954&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Fri May 30 21:08:49 2014
@@ -4377,16 +4377,25 @@ static void checkDLLAttribute(Sema &S, C
   // specialization bases.
 
   for (Decl *Member : Class->decls()) {
-    if (getDLLAttr(Member)) {
-      // FIXME: Error about importing/exporting individual members.
-    }
-
     if (!isa<CXXMethodDecl>(Member) && !isa<VarDecl>(Member))
       continue;
 
-    auto *NewAttr = cast<InheritableAttr>(ClassAttr->clone(S.getASTContext()));
-    NewAttr->setInherited(true);
-    Member->addAttr(NewAttr);
+    if (InheritableAttr *MemberAttr = getDLLAttr(Member)) {
+      if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
+          !MemberAttr->isInherited()) {
+        S.Diag(MemberAttr->getLocation(),
+               diag::err_attribute_dll_member_of_dll_class)
+            << MemberAttr << ClassAttr;
+        S.Diag(ClassAttr->getLocation(), diag::note_previous_attribute);
+        Member->setInvalidDecl();
+        continue;
+      }
+    } else {
+      auto *NewAttr =
+          cast<InheritableAttr>(ClassAttr->clone(S.getASTContext()));
+      NewAttr->setInherited(true);
+      Member->addAttr(NewAttr);
+    }
 
     if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member)) {
       if (ClassExported) {

Modified: cfe/trunk/test/SemaCXX/dllimport.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/dllimport.cpp?rev=209954&r1=209953&r2=209954&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/dllimport.cpp (original)
+++ cfe/trunk/test/SemaCXX/dllimport.cpp Fri May 30 21:08:49 2014
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -triple i686-win32     -fsyntax-only -verify -std=c++11 %s
-// RUN: %clang_cc1 -triple x86_64-win32   -fsyntax-only -verify -std=c++1y %s
+// RUN: %clang_cc1 -triple i686-win32     -fsyntax-only -verify -std=c++11 -DMS %s
+// RUN: %clang_cc1 -triple x86_64-win32   -fsyntax-only -verify -std=c++1y -DMS %s
 // RUN: %clang_cc1 -triple i686-mingw32   -fsyntax-only -verify -std=c++1y %s
 // RUN: %clang_cc1 -triple x86_64-mingw32 -fsyntax-only -verify -std=c++11 %s
 
@@ -954,3 +954,25 @@ template<typename T> template<typename U
 class __declspec(dllimport) ClassDecl;
 
 class __declspec(dllimport) ClassDef { };
+
+#ifdef MS
+// expected-note at +5{{previous attribute is here}}
+// expected-note at +4{{previous attribute is here}}
+// expected-error at +4{{attribute 'dllexport' cannot be applied to member of 'dllimport' class}}
+// expected-error at +4{{attribute 'dllimport' cannot be applied to member of 'dllimport' class}}
+#endif
+class __declspec(dllimport) ImportClassWithDllMember {
+  void __declspec(dllexport) foo();
+  void __declspec(dllimport) bar();
+};
+
+#ifdef MS
+// expected-note at +5{{previous attribute is here}}
+// expected-note at +4{{previous attribute is here}}
+// expected-error at +4{{attribute 'dllimport' cannot be applied to member of 'dllexport' class}}
+// expected-error at +4{{attribute 'dllexport' cannot be applied to member of 'dllexport' class}}
+#endif
+class __declspec(dllexport) ExportClassWithDllMember {
+  void __declspec(dllimport) foo();
+  void __declspec(dllexport) bar();
+};





More information about the cfe-commits mailing list