From: Benjamin Kramer Date: Sun, 2 Mar 2014 17:08:43 +0000 (+0000) Subject: [C++11] ASTMatchers: Use standard static_assert and type traits. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=75a3e83c4e795ba9751cc36eb1b8063d0ef8fabd;p=clang [C++11] ASTMatchers: Use standard static_assert and type traits. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@202653 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/ASTMatchers/ASTMatchersInternal.h b/include/clang/ASTMatchers/ASTMatchersInternal.h index b3bb3ca750..dff7fd7aa5 100644 --- a/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -44,7 +44,6 @@ #include "clang/AST/Type.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/VariadicFunction.h" -#include "llvm/Support/type_traits.h" #include #include #include @@ -52,11 +51,6 @@ namespace clang { namespace ast_matchers { -/// TODO: Use static_assert instead of this. -template struct CompileAssert {}; -#define TOOLING_COMPILE_ASSERT(Expr, Msg) LLVM_ATTRIBUTE_UNUSED \ - typedef CompileAssert<(bool(Expr))> Msg[bool(Expr) ? 1 : -1] - class BoundNodes; namespace internal { @@ -225,9 +219,8 @@ public: /// Requires \c T to be derived from \c From. template Matcher(const Matcher &Other, - typename llvm::enable_if_c< - llvm::is_base_of::value && - !llvm::is_same::value >::type* = 0) + typename std::enable_if::value && + !std::is_same::value>::type * = 0) : Implementation(new ImplicitCastMatcher(Other)) {} /// \brief Implicitly converts \c Matcher to \c Matcher. @@ -235,9 +228,9 @@ public: /// The resulting matcher is not strict, i.e. ignores qualifiers. template Matcher(const Matcher &Other, - typename llvm::enable_if_c< - llvm::is_same::value && - llvm::is_same::value >::type* = 0) + typename std::enable_if< + std::is_same::value && + std::is_same::value>::type* = 0) : Implementation(new TypeToQualType(Other)) {} /// \brief Forwards the call to the underlying MatcherInterface pointer. @@ -525,11 +518,12 @@ template struct has_getDecl { /// PolymorphicMatcherWithParam1 and should be StringRef. template class HasOverloadedOperatorNameMatcher : public SingleNodeMatcherInterface { - TOOLING_COMPILE_ASSERT((llvm::is_same::value || - llvm::is_same::value), - unsupported_class_for_matcher); - TOOLING_COMPILE_ASSERT((llvm::is_same::value), - argument_type_must_be_StringRef); + static_assert(std::is_same::value || + std::is_same::value, + "unsupported class for matcher"); + static_assert(std::is_same::value, + "argument type must be StringRef"); + public: explicit HasOverloadedOperatorNameMatcher(const StringRef Name) : SingleNodeMatcherInterface(), Name(Name) {} @@ -563,9 +557,9 @@ private: /// not actually used. template class HasDeclarationMatcher : public MatcherInterface { - TOOLING_COMPILE_ASSERT((llvm::is_same< DeclMatcherT, - Matcher >::value), - instantiated_with_wrong_types); + static_assert(std::is_same>::value, + "instantiated with wrong types"); + public: explicit HasDeclarationMatcher(const Matcher &InnerMatcher) : InnerMatcher(InnerMatcher) {} @@ -582,7 +576,7 @@ private: template bool matchesSpecialized( const U &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder, - typename llvm::enable_if, int>::type = 0) const { + typename std::enable_if::value, int>::type = 0) const { return matchesDecl(Node.getDecl(), Finder, Builder); } @@ -646,14 +640,14 @@ private: template struct IsBaseType { static const bool value = - (llvm::is_same::value || - llvm::is_same::value || - llvm::is_same::value || - llvm::is_same::value || - llvm::is_same::value || - llvm::is_same::value || - llvm::is_same::value || - llvm::is_same::value); + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value; }; template const bool IsBaseType::value; @@ -719,14 +713,13 @@ public: BoundNodesTreeBuilder *Builder, TraversalKind Traverse, BindKind Bind) { - TOOLING_COMPILE_ASSERT( - (llvm::is_base_of::value || - llvm::is_base_of::value || - llvm::is_base_of::value || - llvm::is_base_of::value || - llvm::is_base_of::value || - llvm::is_base_of::value), - unsupported_type_for_recursive_matching); + static_assert(std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value, + "unsupported type for recursive matching"); return matchesChildOf(ast_type_traits::DynTypedNode::create(Node), Matcher, Builder, Traverse, Bind); } @@ -736,14 +729,13 @@ public: const DynTypedMatcher &Matcher, BoundNodesTreeBuilder *Builder, BindKind Bind) { - TOOLING_COMPILE_ASSERT( - (llvm::is_base_of::value || - llvm::is_base_of::value || - llvm::is_base_of::value || - llvm::is_base_of::value || - llvm::is_base_of::value || - llvm::is_base_of::value), - unsupported_type_for_recursive_matching); + static_assert(std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value, + "unsupported type for recursive matching"); return matchesDescendantOf(ast_type_traits::DynTypedNode::create(Node), Matcher, Builder, Bind); } @@ -754,9 +746,9 @@ public: const DynTypedMatcher &Matcher, BoundNodesTreeBuilder *Builder, AncestorMatchMode MatchMode) { - TOOLING_COMPILE_ASSERT((llvm::is_base_of::value || - llvm::is_base_of::value), - only_Decl_or_Stmt_allowed_for_recursive_matching); + static_assert(std::is_base_of::value || + std::is_base_of::value, + "only Decl or Stmt allowed for recursive matching"); return matchesAncestorOf(ast_type_traits::DynTypedNode::create(Node), Matcher, Builder, MatchMode); } @@ -831,7 +823,7 @@ typedef TypeList<> EmptyTypeList; template struct TypeListContainsSuperOf { static const bool value = - llvm::is_base_of::value || + std::is_base_of::value || TypeListContainsSuperOf::value; }; template @@ -931,8 +923,8 @@ public: typedef typename ExtractFunctionArgMeta::type ReturnTypes; template operator Matcher() const { - TOOLING_COMPILE_ASSERT((TypeListContainsSuperOf::value), - right_polymorphic_conversion); + static_assert(TypeListContainsSuperOf::value, + "right polymorphic conversion"); return Matcher(new MatcherT()); } }; @@ -949,8 +941,8 @@ public: template operator Matcher() const { - TOOLING_COMPILE_ASSERT((TypeListContainsSuperOf::value), - right_polymorphic_conversion); + static_assert(TypeListContainsSuperOf::value, + "right polymorphic conversion"); return Matcher(new MatcherT(Param1)); } @@ -970,8 +962,8 @@ public: template operator Matcher() const { - TOOLING_COMPILE_ASSERT((TypeListContainsSuperOf::value), - right_polymorphic_conversion); + static_assert(TypeListContainsSuperOf::value, + "right polymorphic conversion"); return Matcher(new MatcherT(Param1, Param2)); } @@ -1053,8 +1045,9 @@ public: /// ChildT must be an AST base type. template class HasMatcher : public MatcherInterface { - TOOLING_COMPILE_ASSERT(IsBaseType::value, - has_only_accepts_base_type_matcher); + static_assert(IsBaseType::value, + "has only accepts base type matcher"); + public: explicit HasMatcher(const Matcher &ChildMatcher) : ChildMatcher(ChildMatcher) {} @@ -1079,8 +1072,9 @@ public: /// for each child that matches. template class ForEachMatcher : public MatcherInterface { - TOOLING_COMPILE_ASSERT(IsBaseType::value, - for_each_only_accepts_base_type_matcher); + static_assert(IsBaseType::value, + "for each only accepts base type matcher"); + public: explicit ForEachMatcher(const Matcher &ChildMatcher) : ChildMatcher(ChildMatcher) {} @@ -1189,7 +1183,7 @@ struct VariadicOperatorMatcherFunc { template struct EnableIfValidArity - : public llvm::enable_if_c {}; + : public std::enable_if {}; template typename EnableIfValidArity<1, VariadicOperatorMatcher >::type @@ -1287,8 +1281,9 @@ BindableMatcher makeDynCastAllOfComposite( /// DescendantT must be an AST base type. template class HasDescendantMatcher : public MatcherInterface { - TOOLING_COMPILE_ASSERT(IsBaseType::value, - has_descendant_only_accepts_base_type_matcher); + static_assert(IsBaseType::value, + "has descendant only accepts base type matcher"); + public: explicit HasDescendantMatcher(const Matcher &DescendantMatcher) : DescendantMatcher(DescendantMatcher) {} @@ -1310,8 +1305,9 @@ public: /// \c ParentT must be an AST base type. template class HasParentMatcher : public MatcherInterface { - TOOLING_COMPILE_ASSERT(IsBaseType::value, - has_parent_only_accepts_base_type_matcher); + static_assert(IsBaseType::value, + "has parent only accepts base type matcher"); + public: explicit HasParentMatcher(const Matcher &ParentMatcher) : ParentMatcher(ParentMatcher) {} @@ -1333,8 +1329,9 @@ public: /// \c AncestorT must be an AST base type. template class HasAncestorMatcher : public MatcherInterface { - TOOLING_COMPILE_ASSERT(IsBaseType::value, - has_ancestor_only_accepts_base_type_matcher); + static_assert(IsBaseType::value, + "has ancestor only accepts base type matcher"); + public: explicit HasAncestorMatcher(const Matcher &AncestorMatcher) : AncestorMatcher(AncestorMatcher) {} @@ -1358,8 +1355,9 @@ public: /// for each descendant node that matches instead of only for the first. template class ForEachDescendantMatcher : public MatcherInterface { - TOOLING_COMPILE_ASSERT(IsBaseType::value, - for_each_descendant_only_accepts_base_type_matcher); + static_assert(IsBaseType::value, + "for each descendant only accepts base type matcher"); + public: explicit ForEachDescendantMatcher( const Matcher& DescendantMatcher) @@ -1380,12 +1378,12 @@ private: /// the value the ValueEqualsMatcher was constructed with. template class ValueEqualsMatcher : public SingleNodeMatcherInterface { - TOOLING_COMPILE_ASSERT((llvm::is_base_of::value || - llvm::is_base_of::value || - llvm::is_base_of::value || - llvm::is_base_of::value), - the_node_must_have_a_getValue_method); + static_assert(std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value, + "the node must have a getValue method"); + public: explicit ValueEqualsMatcher(const ValueT &ExpectedValue) : ExpectedValue(ExpectedValue) {} diff --git a/lib/ASTMatchers/ASTMatchFinder.cpp b/lib/ASTMatchers/ASTMatchFinder.cpp index f6dcb9779a..ac1bfa363d 100644 --- a/lib/ASTMatchers/ASTMatchFinder.cpp +++ b/lib/ASTMatchers/ASTMatchFinder.cpp @@ -272,8 +272,8 @@ private: // traversal should continue after this function returns. template bool traverse(const T &Node) { - TOOLING_COMPILE_ASSERT(IsBaseType::value, - traverse_can_only_be_instantiated_with_base_type); + static_assert(IsBaseType::value, + "traverse can only be instantiated with base type"); if (!match(Node)) return false; return baseTraverse(Node);