diff --git a/clang/include/clang/AST/OpenMPClause.h b/clang/include/clang/AST/OpenMPClause.h index c6f99fb21a0f0..5b2206af75bee 100644 --- a/clang/include/clang/AST/OpenMPClause.h +++ b/clang/include/clang/AST/OpenMPClause.h @@ -295,7 +295,8 @@ template class OMPVarListClause : public OMPClause { /// Fetches list of variables associated with this clause. MutableArrayRef getVarRefs() { - return static_cast(this)->template getTrailingObjects(NumVars); + return static_cast(this)->template getTrailingObjectsNonStrict( + NumVars); } /// Sets the list of variables for this clause. @@ -334,8 +335,8 @@ template class OMPVarListClause : public OMPClause { /// Fetches list of all variables in the clause. ArrayRef getVarRefs() const { - return static_cast(this)->template getTrailingObjects( - NumVars); + return static_cast(this) + ->template getTrailingObjectsNonStrict(NumVars); } }; @@ -380,7 +381,7 @@ template class OMPDirectiveListClause : public OMPClause { MutableArrayRef getDirectiveKinds() { return static_cast(this) - ->template getTrailingObjects(NumKinds); + ->template getTrailingObjectsNonStrict(NumKinds); } void setDirectiveKinds(ArrayRef DK) { @@ -5921,15 +5922,17 @@ class OMPMappableExprListClause : public OMPVarListClause, /// Get the unique declarations that are in the trailing objects of the /// class. MutableArrayRef getUniqueDeclsRef() { - return static_cast(this)->template getTrailingObjects( - NumUniqueDeclarations); + return static_cast(this) + ->template getTrailingObjectsNonStrict( + NumUniqueDeclarations); } /// Get the unique declarations that are in the trailing objects of the /// class. ArrayRef getUniqueDeclsRef() const { return static_cast(this) - ->template getTrailingObjects(NumUniqueDeclarations); + ->template getTrailingObjectsNonStrict( + NumUniqueDeclarations); } /// Set the unique declarations that are in the trailing objects of the @@ -5943,15 +5946,15 @@ class OMPMappableExprListClause : public OMPVarListClause, /// Get the number of lists per declaration that are in the trailing /// objects of the class. MutableArrayRef getDeclNumListsRef() { - return static_cast(this)->template getTrailingObjects( - NumUniqueDeclarations); + return static_cast(this) + ->template getTrailingObjectsNonStrict(NumUniqueDeclarations); } /// Get the number of lists per declaration that are in the trailing /// objects of the class. ArrayRef getDeclNumListsRef() const { - return static_cast(this)->template getTrailingObjects( - NumUniqueDeclarations); + return static_cast(this) + ->template getTrailingObjectsNonStrict(NumUniqueDeclarations); } /// Set the number of lists per declaration that are in the trailing @@ -5966,7 +5969,8 @@ class OMPMappableExprListClause : public OMPVarListClause, /// objects of the class. They are appended after the number of lists. MutableArrayRef getComponentListSizesRef() { return MutableArrayRef( - static_cast(this)->template getTrailingObjects() + + static_cast(this) + ->template getTrailingObjectsNonStrict() + NumUniqueDeclarations, NumComponentLists); } @@ -5975,7 +5979,8 @@ class OMPMappableExprListClause : public OMPVarListClause, /// objects of the class. They are appended after the number of lists. ArrayRef getComponentListSizesRef() const { return ArrayRef( - static_cast(this)->template getTrailingObjects() + + static_cast(this) + ->template getTrailingObjectsNonStrict() + NumUniqueDeclarations, NumComponentLists); } @@ -5991,13 +5996,15 @@ class OMPMappableExprListClause : public OMPVarListClause, /// Get the components that are in the trailing objects of the class. MutableArrayRef getComponentsRef() { return static_cast(this) - ->template getTrailingObjects(NumComponents); + ->template getTrailingObjectsNonStrict( + NumComponents); } /// Get the components that are in the trailing objects of the class. ArrayRef getComponentsRef() const { return static_cast(this) - ->template getTrailingObjects(NumComponents); + ->template getTrailingObjectsNonStrict( + NumComponents); } /// Set the components that are in the trailing objects of the class. diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 149b274f36b63..642867c0942b5 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -2020,7 +2020,8 @@ CXXBaseSpecifier **CastExpr::path_buffer() { #define ABSTRACT_STMT(x) #define CASTEXPR(Type, Base) \ case Stmt::Type##Class: \ - return static_cast(this)->getTrailingObjects(); + return static_cast(this) \ + ->getTrailingObjectsNonStrict(); #define STMT(Type, Base) #include "clang/AST/StmtNodes.inc" default: diff --git a/llvm/include/llvm/Support/TrailingObjects.h b/llvm/include/llvm/Support/TrailingObjects.h index f25f2311a81a4..d7211a930ae49 100644 --- a/llvm/include/llvm/Support/TrailingObjects.h +++ b/llvm/include/llvm/Support/TrailingObjects.h @@ -228,12 +228,18 @@ class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl< using ParentType::getTrailingObjectsImpl; - // This function contains only a static_assert BaseTy is final. The - // static_assert must be in a function, and not at class-level - // because BaseTy isn't complete at class instantiation time, but - // will be by the time this function is instantiated. - static void verifyTrailingObjectsAssertions() { + template static void verifyTrailingObjectsAssertions() { + // The static_assert for BaseTy must be in a function, and not at + // class-level because BaseTy isn't complete at class instantiation time, + // but will be by the time this function is instantiated. static_assert(std::is_final(), "BaseTy must be final."); + + // Verify that templated getTrailingObjects() is used only with multiple + // trailing types. Use getTrailingObjectsNonStrict() which does not check + // this. + static_assert(!Strict || sizeof...(TrailingTys) > 1, + "Use templated getTrailingObjects() only when there are " + "multiple trailing types"); } // These two methods are the base of the recursion for this method. @@ -283,7 +289,7 @@ class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl< /// (which must be one of those specified in the class template). The /// array may have zero or more elements in it. template const T *getTrailingObjects() const { - verifyTrailingObjectsAssertions(); + verifyTrailingObjectsAssertions(); // Forwards to an impl function with overloads, since member // function templates can't be specialized. return this->getTrailingObjectsImpl( @@ -295,7 +301,7 @@ class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl< /// (which must be one of those specified in the class template). The /// array may have zero or more elements in it. template T *getTrailingObjects() { - verifyTrailingObjectsAssertions(); + verifyTrailingObjectsAssertions(); // Forwards to an impl function with overloads, since member // function templates can't be specialized. return this->getTrailingObjectsImpl( @@ -310,14 +316,20 @@ class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl< static_assert(sizeof...(TrailingTys) == 1, "Can use non-templated getTrailingObjects() only when there " "is a single trailing type"); - return getTrailingObjects(); + verifyTrailingObjectsAssertions(); + return this->getTrailingObjectsImpl( + static_cast(this), + TrailingObjectsBase::OverloadToken()); } FirstTrailingType *getTrailingObjects() { static_assert(sizeof...(TrailingTys) == 1, "Can use non-templated getTrailingObjects() only when there " "is a single trailing type"); - return getTrailingObjects(); + verifyTrailingObjectsAssertions(); + return this->getTrailingObjectsImpl( + static_cast(this), + TrailingObjectsBase::OverloadToken()); } // Functions that return the trailing objects as ArrayRefs. @@ -337,6 +349,31 @@ class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl< return ArrayRef(getTrailingObjects(), N); } + // Non-strict forms of templated `getTrailingObjects` that work with single + // trailing type. + template const T *getTrailingObjectsNonStrict() const { + verifyTrailingObjectsAssertions(); + return this->getTrailingObjectsImpl( + static_cast(this), + TrailingObjectsBase::OverloadToken()); + } + + template T *getTrailingObjectsNonStrict() { + verifyTrailingObjectsAssertions(); + return this->getTrailingObjectsImpl( + static_cast(this), TrailingObjectsBase::OverloadToken()); + } + + template + MutableArrayRef getTrailingObjectsNonStrict(size_t N) { + return MutableArrayRef(getTrailingObjectsNonStrict(), N); + } + + template + ArrayRef getTrailingObjectsNonStrict(size_t N) const { + return ArrayRef(getTrailingObjectsNonStrict(), N); + } + /// Returns the size of the trailing data, if an object were /// allocated with the given counts (The counts are in the same order /// as the template arguments). This does not include the size of the diff --git a/llvm/unittests/Support/TrailingObjectsTest.cpp b/llvm/unittests/Support/TrailingObjectsTest.cpp index 2590f375b6598..9184a4dd0cc23 100644 --- a/llvm/unittests/Support/TrailingObjectsTest.cpp +++ b/llvm/unittests/Support/TrailingObjectsTest.cpp @@ -45,9 +45,10 @@ class Class1 final : private TrailingObjects { template using FixedSizeStorage = TrailingObjects::FixedSizeStorage; - using TrailingObjects::totalSizeToAlloc; using TrailingObjects::additionalSizeToAlloc; using TrailingObjects::getTrailingObjects; + using TrailingObjects::getTrailingObjectsNonStrict; + using TrailingObjects::totalSizeToAlloc; }; // Here, there are two singular optional object types appended. Note @@ -123,11 +124,11 @@ TEST(TrailingObjects, OneArg) { EXPECT_EQ(Class1::totalSizeToAlloc(3), sizeof(Class1) + sizeof(short) * 3); - EXPECT_EQ(C->getTrailingObjects(), reinterpret_cast(C + 1)); + EXPECT_EQ(C->getTrailingObjects(), reinterpret_cast(C + 1)); EXPECT_EQ(C->get(0), 1); EXPECT_EQ(C->get(2), 3); - EXPECT_EQ(C->getTrailingObjects(), C->getTrailingObjects()); + EXPECT_EQ(C->getTrailingObjects(), C->getTrailingObjectsNonStrict()); delete C; }