r240889 - [Sema] Unions cannot have virtual functions.

Davide Italiano davide at freebsd.org
Sat Jun 27 12:18:56 PDT 2015


Author: davide
Date: Sat Jun 27 14:18:55 2015
New Revision: 240889

URL: http://llvm.org/viewvc/llvm-project?rev=240889&view=rev
Log:
[Sema] Unions cannot have virtual functions.

PR:		PR23931
Differential Revision:	http://reviews.llvm.org/D10752
Reviewed by:  rsmith

Added:
    cfe/trunk/test/SemaCXX/virtual-function-in-union.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaDecl.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=240889&r1=240888&r2=240889&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sat Jun 27 14:18:55 2015
@@ -1262,6 +1262,8 @@ def ext_mutable_reference : ExtWarn<
 def err_mutable_const : Error<"'mutable' and 'const' cannot be mixed">;
 def err_mutable_nonmember : Error<
   "'mutable' can only be applied to member variables">;
+def err_virtual_in_union : Error<
+  "unions cannot have virtual functions">;
 def err_virtual_non_function : Error<
   "'virtual' can only appear on non-static member functions">;
 def err_virtual_out_of_class : Error<

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=240889&r1=240888&r2=240889&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Jun 27 14:18:55 2015
@@ -7202,6 +7202,11 @@ Sema::ActOnFunctionDeclarator(Scope *S,
           dyn_cast<CXXRecordDecl>(NewFD->getDeclContext())) {
       if (Parent->isInterface() && cast<CXXMethodDecl>(NewFD)->isUserProvided())
         NewFD->setPure(true);
+
+      // C++ [class.union]p2
+      //   A union can have member functions, but not virtual functions.
+      if (isVirtual && Parent->isUnion())
+        Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_virtual_in_union);
     }
 
     SetNestedNameSpecifier(NewFD, D);

Added: cfe/trunk/test/SemaCXX/virtual-function-in-union.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/virtual-function-in-union.cpp?rev=240889&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/virtual-function-in-union.cpp (added)
+++ cfe/trunk/test/SemaCXX/virtual-function-in-union.cpp Sat Jun 27 14:18:55 2015
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+union x {
+  virtual void f(); // expected-error {{unions cannot have virtual functions}}
+};





More information about the cfe-commits mailing list