[clang] 7562c64 - [Sema] Mark virtual method declaration in union as invalid
Yuanfang Chen via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 9 10:57:48 PST 2021
Author: Yuanfang Chen
Date: 2021-11-09T10:57:38-08:00
New Revision: 7562c64197acbee60c4bb0d211eb699ad24f5bb8
URL: https://github.com/llvm/llvm-project/commit/7562c64197acbee60c4bb0d211eb699ad24f5bb8
DIFF: https://github.com/llvm/llvm-project/commit/7562c64197acbee60c4bb0d211eb699ad24f5bb8.diff
LOG: [Sema] Mark virtual method declaration in union as invalid
Currently, this is only diagnosed but the decl is not marked invalid. This may hit assertions down the path.
This also reverts the fix for PR49534 since it is not needed anymore.
Reviewed By: hubert.reinterpretcast
Differential Revision: https://reviews.llvm.org/D113145
Added:
Modified:
clang/lib/Sema/SemaDecl.cpp
clang/test/SemaCXX/PR49534.cpp
clang/test/SemaCXX/virtual-function-in-union.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d61570ee6a104..638fe86c20abc 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -5302,8 +5302,7 @@ Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS,
// 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();
@@ -9109,8 +9108,10 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
// 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);
diff --git a/clang/test/SemaCXX/PR49534.cpp b/clang/test/SemaCXX/PR49534.cpp
index 8a17402689e66..6b6452257f6a9 100644
--- a/clang/test/SemaCXX/PR49534.cpp
+++ b/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}}
};
diff --git a/clang/test/SemaCXX/virtual-function-in-union.cpp b/clang/test/SemaCXX/virtual-function-in-union.cpp
index 0c4ba5d32caac..b7cf26618c9fb 100644
--- a/clang/test/SemaCXX/virtual-function-in-union.cpp
+++ b/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; }
More information about the cfe-commits
mailing list