[clang] [clang] Implement `__is_virtual_base_of()` intrinsic (PR #100393)
Vlad Serebrennikov via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 24 11:45:06 PDT 2024
https://github.com/Endilll updated https://github.com/llvm/llvm-project/pull/100393
>From 772b5d85dff73de9f6a01a266bd201cbe04923ab Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Wed, 24 Jul 2024 17:38:50 +0300
Subject: [PATCH 1/2] [clang] Impement `__is_virtual_base_of()` intrinsic
---
clang/docs/ReleaseNotes.rst | 3 +
clang/include/clang/Basic/TokenKinds.def | 1 +
clang/lib/Parse/ParseExpr.cpp | 1 +
clang/lib/Sema/SemaExprCXX.cpp | 27 +++++++++
clang/test/SemaCXX/type-traits.cpp | 73 +++++++++++++++++++++++-
5 files changed, 103 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ac1de0db9ce48..242f50bf23924 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -80,6 +80,9 @@ C++23 Feature Support
C++2c Feature Support
^^^^^^^^^^^^^^^^^^^^^
+- Add ``__is_virtual_base_of`` intrinsic, which supports
+ `P2985R0 A type trait for detecting virtual base classes <https://wg21.link/p2985r0>`_
+
Resolutions to C++ Defect Reports
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Basic/TokenKinds.def b/clang/include/clang/Basic/TokenKinds.def
index 7f4912b9bcd96..1915e1f2588cb 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -529,6 +529,7 @@ TYPE_TRAIT_2(__is_trivially_assignable, IsTriviallyAssignable, KEYCXX)
TYPE_TRAIT_N(__is_trivially_constructible, IsTriviallyConstructible, KEYCXX)
TYPE_TRAIT_1(__is_trivially_copyable, IsTriviallyCopyable, KEYCXX)
TYPE_TRAIT_1(__is_union, IsUnion, KEYCXX)
+TYPE_TRAIT_2(__is_virtual_base_of, IsVirtualBaseOf, KEYCXX)
TYPE_TRAIT_1(__has_unique_object_representations,
HasUniqueObjectRepresentations, KEYCXX)
TYPE_TRAIT_2(__is_layout_compatible, IsLayoutCompatible, KEYCXX)
diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp
index a12c375c8d48c..582db2075616c 100644
--- a/clang/lib/Parse/ParseExpr.cpp
+++ b/clang/lib/Parse/ParseExpr.cpp
@@ -822,6 +822,7 @@ bool Parser::isRevertibleTypeTrait(const IdentifierInfo *II,
REVERTIBLE_TYPE_TRAIT(__is_unbounded_array);
REVERTIBLE_TYPE_TRAIT(__is_union);
REVERTIBLE_TYPE_TRAIT(__is_unsigned);
+ REVERTIBLE_TYPE_TRAIT(__is_virtual_base_of);
REVERTIBLE_TYPE_TRAIT(__is_void);
REVERTIBLE_TYPE_TRAIT(__is_volatile);
REVERTIBLE_TYPE_TRAIT(__reference_binds_to_temporary);
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 14d1f395af90e..6cba3a62b694d 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -6027,6 +6027,33 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
return cast<CXXRecordDecl>(rhsRecord->getDecl())
->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl()));
}
+ case BTT_IsVirtualBaseOf: {
+ const RecordType *BaseRecord = LhsT->getAs<RecordType>();
+ const RecordType *DerivedRecord = RhsT->getAs<RecordType>();
+
+ if (!BaseRecord || !DerivedRecord) {
+ DiagnoseVLAInCXXTypeTrait(Self, Lhs, tok::kw___is_virtual_base_of);
+ DiagnoseVLAInCXXTypeTrait(Self, Rhs, tok::kw___is_virtual_base_of);
+ return false;
+ }
+
+ if (BaseRecord->isUnionType() || DerivedRecord->isUnionType())
+ return false;
+
+ if (!BaseRecord->isStructureOrClassType() ||
+ !DerivedRecord->isStructureOrClassType())
+ return false;
+
+ if (Self.RequireCompleteType(Rhs->getTypeLoc().getBeginLoc(), RhsT,
+ diag::err_incomplete_type))
+ return false;
+
+ if (Self.Context.hasSameUnqualifiedType(LhsT, RhsT))
+ return false;
+
+ return cast<CXXRecordDecl>(DerivedRecord->getDecl())
+ ->isVirtuallyDerivedFrom(cast<CXXRecordDecl>(BaseRecord->getDecl()));
+ }
case BTT_IsSame:
return Self.Context.hasSameType(LhsT, RhsT);
case BTT_TypeCompatible: {
diff --git a/clang/test/SemaCXX/type-traits.cpp b/clang/test/SemaCXX/type-traits.cpp
index 23b07cac13eaf..4eedcebfaeffd 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -2402,11 +2402,11 @@ template<typename T> struct DerivedB : BaseA<T> { };
template<typename T> struct CrazyDerived : T { };
-class class_forward; // expected-note 2 {{forward declaration of 'class_forward'}}
+class class_forward; // expected-note 4 {{forward declaration of 'class_forward'}}
template <class T> class DerivedTemp : Base {};
template <class T> class NonderivedTemp {};
-template <class T> class UndefinedTemp; // expected-note {{declared here}}
+template <class T> class UndefinedTemp; // expected-note 2 {{declared here}}
void is_base_of() {
static_assert(__is_base_of(Base, Derived));
@@ -2457,6 +2457,75 @@ void is_base_of() {
static_assert(!__is_base_of(DerivedB<int>, BaseA<int>));
}
+struct DerivedTransitiveViaNonVirtual : Derived3 {};
+struct DerivedTransitiveViaVirtual : virtual Derived3 {};
+
+template <typename T>
+struct CrazyDerivedVirtual : virtual T {};
+
+struct DerivedPrivate : private virtual Base {};
+struct DerivedProtected : protected virtual Base {};
+struct DerivedPrivatePrivate : private DerivedPrivate {};
+struct DerivedPrivateProtected : private DerivedProtected {};
+struct DerivedProtectedPrivate : protected DerivedProtected {};
+struct DerivedProtectedProtected : protected DerivedProtected {};
+
+void is_virtual_base_of(int n) {
+ static_assert(!__is_virtual_base_of(Base, Derived));
+ static_assert(!__is_virtual_base_of(const Base, Derived));
+ static_assert(!__is_virtual_base_of(Derived, Base));
+ static_assert(!__is_virtual_base_of(Derived, int));
+ static_assert(!__is_virtual_base_of(Base, Base));
+ static_assert(!__is_virtual_base_of(Base, Derived3));
+ static_assert(!__is_virtual_base_of(Derived, Derived3));
+ static_assert(__is_virtual_base_of(Derived2b, Derived3));
+ static_assert(__is_virtual_base_of(Derived2a, Derived3));
+ static_assert(!__is_virtual_base_of(BaseA<int>, DerivedB<int>));
+ static_assert(!__is_virtual_base_of(DerivedB<int>, BaseA<int>));
+ static_assert(!__is_virtual_base_of(Union, Union));
+ static_assert(!__is_virtual_base_of(Empty, Empty));
+ static_assert(!__is_virtual_base_of(class_forward, class_forward)); // expected-error {{incomplete type 'class_forward' where a complete type is required}}
+ static_assert(!__is_virtual_base_of(Empty, class_forward)); // expected-error {{incomplete type 'class_forward' where a complete type is required}}
+ static_assert(!__is_virtual_base_of(Base&, Derived&));
+ static_assert(!__is_virtual_base_of(Base[10], Derived[10]));
+ static_assert(!__is_virtual_base_of(Base[n], Derived[n])); // expected-error 2 {{variable length arrays are not supported in '__is_virtual_base_of'}}
+ static_assert(!__is_virtual_base_of(int, int));
+ static_assert(!__is_virtual_base_of(int[], int[]));
+ static_assert(!__is_virtual_base_of(long, int));
+ static_assert(!__is_virtual_base_of(Base, DerivedTemp<int>));
+ static_assert(!__is_virtual_base_of(Base, NonderivedTemp<int>));
+ static_assert(!__is_virtual_base_of(Base, UndefinedTemp<int>)); // expected-error {{implicit instantiation of undefined template 'UndefinedTemp<int>'}}
+ static_assert(__is_virtual_base_of(Base, DerivedPrivate));
+ static_assert(__is_virtual_base_of(Base, DerivedProtected));
+ static_assert(__is_virtual_base_of(Base, DerivedPrivatePrivate));
+ static_assert(__is_virtual_base_of(Base, DerivedPrivateProtected));
+ static_assert(__is_virtual_base_of(Base, DerivedProtectedPrivate));
+ static_assert(__is_virtual_base_of(Base, DerivedProtectedProtected));
+ static_assert(__is_virtual_base_of(Derived2a, DerivedTransitiveViaNonVirtual));
+ static_assert(__is_virtual_base_of(Derived2b, DerivedTransitiveViaNonVirtual));
+ static_assert(__is_virtual_base_of(Derived2a, DerivedTransitiveViaVirtual));
+ static_assert(__is_virtual_base_of(Derived2b, DerivedTransitiveViaVirtual));
+ static_assert(!__is_virtual_base_of(Base, CrazyDerived<Base>));
+ static_assert(!__is_virtual_base_of(CrazyDerived<Base>, Base));
+ static_assert(__is_virtual_base_of(Base, CrazyDerivedVirtual<Base>));
+ static_assert(!__is_virtual_base_of(CrazyDerivedVirtual<Base>, Base));
+
+ static_assert(!__is_virtual_base_of(IncompleteUnion, IncompleteUnion));
+ static_assert(!__is_virtual_base_of(Union, IncompleteUnion));
+ static_assert(!__is_virtual_base_of(IncompleteUnion, Union));
+ static_assert(!__is_virtual_base_of(IncompleteStruct, IncompleteUnion));
+ static_assert(!__is_virtual_base_of(IncompleteUnion, IncompleteStruct));
+ static_assert(!__is_virtual_base_of(Empty, IncompleteUnion));
+ static_assert(!__is_virtual_base_of(IncompleteUnion, Empty));
+ static_assert(!__is_virtual_base_of(int, IncompleteUnion));
+ static_assert(!__is_virtual_base_of(IncompleteUnion, int));
+ static_assert(!__is_virtual_base_of(Empty, Union));
+ static_assert(!__is_virtual_base_of(Union, Empty));
+ static_assert(!__is_virtual_base_of(int, Empty));
+ static_assert(!__is_virtual_base_of(Union, int));
+ static_assert(!__is_virtual_base_of(IncompleteStruct, IncompleteStruct[n])); // expected-error {{variable length arrays are not supported in '__is_virtual_base_of'}}
+}
+
template<class T, class U>
class TemplateClass {};
>From 8b0b0c5e38a7377a590e08c532c6343b5fbf5f2e Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Wed, 24 Jul 2024 21:44:51 +0300
Subject: [PATCH 2/2] Address reviewer feedback
---
clang/docs/LanguageExtensions.rst | 1 +
clang/lib/Parse/ParseExpr.cpp | 1 -
clang/test/SemaCXX/type-traits.cpp | 1 +
3 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index 81784c75081ba..dcb05a00f3280 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -1660,6 +1660,7 @@ The following type trait primitives are supported by Clang. Those traits marked
* ``__is_unsigned`` (C++, Embarcadero):
Returns false for enumeration types. Note, before Clang 13, returned true for
enumeration types if the underlying type was unsigned.
+* ``__is_virtual_base_of`` (C++, GNU, Microsoft)
* ``__is_void`` (C++, Embarcadero)
* ``__is_volatile`` (C++, Embarcadero)
* ``__reference_binds_to_temporary(T, U)`` (Clang): Determines whether a
diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp
index 582db2075616c..a12c375c8d48c 100644
--- a/clang/lib/Parse/ParseExpr.cpp
+++ b/clang/lib/Parse/ParseExpr.cpp
@@ -822,7 +822,6 @@ bool Parser::isRevertibleTypeTrait(const IdentifierInfo *II,
REVERTIBLE_TYPE_TRAIT(__is_unbounded_array);
REVERTIBLE_TYPE_TRAIT(__is_union);
REVERTIBLE_TYPE_TRAIT(__is_unsigned);
- REVERTIBLE_TYPE_TRAIT(__is_virtual_base_of);
REVERTIBLE_TYPE_TRAIT(__is_void);
REVERTIBLE_TYPE_TRAIT(__is_volatile);
REVERTIBLE_TYPE_TRAIT(__reference_binds_to_temporary);
diff --git a/clang/test/SemaCXX/type-traits.cpp b/clang/test/SemaCXX/type-traits.cpp
index 4eedcebfaeffd..60be35be2aae2 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -2486,6 +2486,7 @@ void is_virtual_base_of(int n) {
static_assert(!__is_virtual_base_of(Empty, Empty));
static_assert(!__is_virtual_base_of(class_forward, class_forward)); // expected-error {{incomplete type 'class_forward' where a complete type is required}}
static_assert(!__is_virtual_base_of(Empty, class_forward)); // expected-error {{incomplete type 'class_forward' where a complete type is required}}
+ static_assert(!__is_virtual_base_of(class_forward, Empty));
static_assert(!__is_virtual_base_of(Base&, Derived&));
static_assert(!__is_virtual_base_of(Base[10], Derived[10]));
static_assert(!__is_virtual_base_of(Base[n], Derived[n])); // expected-error 2 {{variable length arrays are not supported in '__is_virtual_base_of'}}
More information about the cfe-commits
mailing list