r288133 - [AST] Use static_assert to verify types instead of undefined classes.

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 29 04:41:21 PST 2016


Author: d0k
Date: Tue Nov 29 06:41:21 2016
New Revision: 288133

URL: http://llvm.org/viewvc/llvm-project?rev=288133&view=rev
Log:
[AST] Use static_assert to verify types instead of undefined classes.

No functionliaty change intended.

Modified:
    cfe/trunk/include/clang/AST/CanonicalType.h
    cfe/trunk/include/clang/AST/Type.h

Modified: cfe/trunk/include/clang/AST/CanonicalType.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/CanonicalType.h?rev=288133&r1=288132&r2=288133&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/CanonicalType.h (original)
+++ cfe/trunk/include/clang/AST/CanonicalType.h Tue Nov 29 06:41:21 2016
@@ -630,8 +630,8 @@ CanQual<T> CanQual<T>::CreateUnsafe(Qual
 template<typename T>
 template<typename U>
 CanProxy<U> CanQual<T>::getAs() const {
-  ArrayType_cannot_be_used_with_getAs<U> at;
-  (void)at;
+  static_assert(!TypeIsArrayType<T>::value,
+                "ArrayType cannot be used with getAs!");
 
   if (Stored.isNull())
     return CanProxy<U>();
@@ -645,8 +645,8 @@ CanProxy<U> CanQual<T>::getAs() const {
 template<typename T>
 template<typename U>
 CanProxy<U> CanQual<T>::castAs() const {
-  ArrayType_cannot_be_used_with_getAs<U> at;
-  (void)at;
+  static_assert(!TypeIsArrayType<U>::value,
+                "ArrayType cannot be used with castAs!");
 
   assert(!Stored.isNull() && isa<U>(Stored.getTypePtr()));
   return CanQual<U>::CreateUnsafe(Stored);

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=288133&r1=288132&r2=288133&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Tue Nov 29 06:41:21 2016
@@ -5916,17 +5916,15 @@ inline const PartialDiagnostic &operator
 
 // Helper class template that is used by Type::getAs to ensure that one does
 // not try to look through a qualified type to get to an array type.
-template <typename T, bool isArrayType = (std::is_same<T, ArrayType>::value ||
-                                          std::is_base_of<ArrayType, T>::value)>
-struct ArrayType_cannot_be_used_with_getAs {};
-
-template<typename T>
-struct ArrayType_cannot_be_used_with_getAs<T, true>;
+template <typename T>
+using TypeIsArrayType =
+    std::integral_constant<bool, std::is_same<T, ArrayType>::value ||
+                                     std::is_base_of<ArrayType, T>::value>;
 
 // Member-template getAs<specific type>'.
 template <typename T> const T *Type::getAs() const {
-  ArrayType_cannot_be_used_with_getAs<T> at;
-  (void)at;
+  static_assert(!TypeIsArrayType<T>::value,
+                "ArrayType cannot be used with getAs!");
 
   // If this is directly a T type, return it.
   if (const T *Ty = dyn_cast<T>(this))
@@ -5956,8 +5954,8 @@ inline const ArrayType *Type::getAsArray
 }
 
 template <typename T> const T *Type::castAs() const {
-  ArrayType_cannot_be_used_with_getAs<T> at;
-  (void) at;
+  static_assert(!TypeIsArrayType<T>::value,
+                "ArrayType cannot be used with castAs!");
 
   if (const T *ty = dyn_cast<T>(this)) return ty;
   assert(isa<T>(CanonicalType));




More information about the cfe-commits mailing list