[clang] 99fd066 - [clang][Sema] Don't try to initialize implicit variable of invalid anonymous union/struct

Ta-Wei Tu via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 30 18:05:52 PDT 2021


Author: Ta-Wei Tu
Date: 2021-03-31T09:05:45+08:00
New Revision: 99fd0662278470f5405b8abd79b681b96cac7856

URL: https://github.com/llvm/llvm-project/commit/99fd0662278470f5405b8abd79b681b96cac7856
DIFF: https://github.com/llvm/llvm-project/commit/99fd0662278470f5405b8abd79b681b96cac7856.diff

LOG: [clang][Sema] Don't try to initialize implicit variable of invalid anonymous union/struct

This fixes https://bugs.llvm.org/show_bug.cgi?id=49534, where the call to the constructor
of the anonymous union is checked and triggers assertion failure when trying to retrieve
the alignment of the `this` argument (which is a union with virtual function).

The extra check for alignment was introduced in D97187.

Reviewed By: tmatheson

Differential Revision: https://reviews.llvm.org/D98548

Added: 
    clang/test/SemaCXX/PR49534.cpp

Modified: 
    clang/lib/Sema/SemaDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 76e3ee965777..b117d73a9b60 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -5211,7 +5211,8 @@ 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; };
-    ActOnUninitializedDecl(Anon);
+    if (!Invalid)
+      ActOnUninitializedDecl(Anon);
   }
   Anon->setImplicit();
 

diff  --git a/clang/test/SemaCXX/PR49534.cpp b/clang/test/SemaCXX/PR49534.cpp
new file mode 100644
index 000000000000..8a17402689e6
--- /dev/null
+++ b/clang/test/SemaCXX/PR49534.cpp
@@ -0,0 +1,6 @@
+// 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}}
+};


        


More information about the cfe-commits mailing list