]> granicus.if.org Git - clang/commitdiff
Make more friendly with unions. Reviewed as https://reviews.llvm.org/D61858
authorMarshall Clow <mclow.lists@gmail.com>
Mon, 13 May 2019 19:29:23 +0000 (19:29 +0000)
committerMarshall Clow <mclow.lists@gmail.com>
Mon, 13 May 2019 19:29:23 +0000 (19:29 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@360614 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExprCXX.cpp
test/SemaCXX/type-traits.cpp

index 8f00887ebc4035be4fa94c23bffe67fe40d7966c..87dc3a9a54a9a5be99c2a5b02b151224b3171025 100644 (file)
@@ -5118,8 +5118,15 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT,
     assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT)
              == (lhsRecord == rhsRecord));
 
+    // Unions are never base classes, and never have base classes.
+    // It doesn't matter if they are complete or not. See PR#41843
+    if (lhsRecord && lhsRecord->getDecl()->isUnion())
+      return false;
+    if (rhsRecord && rhsRecord->getDecl()->isUnion())
+      return false;
+
     if (lhsRecord == rhsRecord)
-      return !lhsRecord->getDecl()->isUnion();
+      return true;
 
     // C++0x [meta.rel]p2:
     //   If Base and Derived are class types and are different types
index 1ab35dfc1a466716fecb3ea9501026d5af25a876..9bc238e67895497c2b875918f5ed408c84c906fb 100644 (file)
@@ -14,6 +14,7 @@ typedef NonPOD NonPODArMB[10][2];
 enum Enum { EV };
 struct POD { Enum e; int i; float f; NonPOD* p; };
 struct Empty {};
+struct IncompleteStruct;
 typedef Empty EmptyAr[10];
 typedef Empty EmptyArNB[];
 typedef Empty EmptyArMB[1][2];
@@ -1915,6 +1916,20 @@ void is_base_of() {
   { int arr[F(__is_base_of(Base, NonderivedTemp<int>))]; }
   { int arr[F(__is_base_of(Base, UndefinedTemp<int>))]; } // expected-error {{implicit instantiation of undefined template 'UndefinedTemp<int>'}}
 
+  { int arr[F(__is_base_of(IncompleteUnion, IncompleteUnion))]; }
+  { int arr[F(__is_base_of(Union, IncompleteUnion))]; }
+  { int arr[F(__is_base_of(IncompleteUnion, Union))]; }
+  { int arr[F(__is_base_of(IncompleteStruct, IncompleteUnion))]; }
+  { int arr[F(__is_base_of(IncompleteUnion, IncompleteStruct))]; }
+  { int arr[F(__is_base_of(Empty, IncompleteUnion))]; }
+  { int arr[F(__is_base_of(IncompleteUnion, Empty))]; }
+  { int arr[F(__is_base_of(int, IncompleteUnion))]; }
+  { int arr[F(__is_base_of(IncompleteUnion, int))]; }
+  { int arr[F(__is_base_of(Empty, Union))]; }
+  { int arr[F(__is_base_of(Union, Empty))]; }
+  { int arr[F(__is_base_of(int, Empty))]; }
+  { int arr[F(__is_base_of(Union, int))]; }
+
   isBaseOfT<Base, Derived>();
   isBaseOfF<Derived, Base>();