[PATCH] D113145: Mark virtual method declaration in union as invalid

Yuanfang Chen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 5 00:10:42 PDT 2021


ychen updated this revision to Diff 384975.
ychen added a comment.

- Mark virtual function in union invalid.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D113145

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/test/SemaCXX/PR49534.cpp
  clang/test/SemaCXX/virtual-function-in-union.cpp


Index: clang/test/SemaCXX/virtual-function-in-union.cpp
===================================================================
--- clang/test/SemaCXX/virtual-function-in-union.cpp
+++ clang/test/SemaCXX/virtual-function-in-union.cpp
@@ -1,5 +1,8 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
 
-union x {
-  virtual void f(); // expected-error {{unions cannot have virtual functions}}
+union U {
+  int d;
+  virtual int f() { return d; }; // expected-error {{unions cannot have virtual functions}}
 };
+
+int foo() { U u; return u.d; }
Index: clang/test/SemaCXX/PR49534.cpp
===================================================================
--- clang/test/SemaCXX/PR49534.cpp
+++ clang/test/SemaCXX/PR49534.cpp
@@ -1,6 +1,5 @@
 // RUN: %clang_cc1 -x c++ -fsyntax-only %s -verify
 
 static union {     // expected-warning {{declaration does not declare anything}}
-  virtual int a(); // expected-error {{unions cannot have virtual functions}} \
-                   // expected-error {{functions cannot be declared in an anonymous union}}
+  virtual int a(); // expected-error {{unions cannot have virtual functions}}
 };
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -5296,8 +5296,7 @@
     // trivial in almost all cases, except if a union member has an in-class
     // initializer:
     //   union { int n = 0; };
-    if (!Invalid)
-      ActOnUninitializedDecl(Anon);
+    ActOnUninitializedDecl(Anon);
   }
   Anon->setImplicit();
 
@@ -9103,8 +9102,10 @@
 
       // C++ [class.union]p2
       //   A union can have member functions, but not virtual functions.
-      if (isVirtual && Parent->isUnion())
+      if (isVirtual && Parent->isUnion()) {
         Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_virtual_in_union);
+        NewFD->setInvalidDecl();
+      }
     }
 
     SetNestedNameSpecifier(*this, NewFD, D);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113145.384975.patch
Type: text/x-patch
Size: 1958 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211105/b59d9ee3/attachment-0001.bin>


More information about the cfe-commits mailing list