diff --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h --- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -541,12 +541,18 @@ /// Convert \c this into a \c Matcher by applying dyn_cast<> to the /// argument. /// \c To must be a base class of \c T. - template - Matcher dynCastTo() const { + template Matcher dynCastTo() const LLVM_LVALUE_FUNCTION { static_assert(std::is_base_of::value, "Invalid dynCast call."); return Matcher(Implementation); } +#if LLVM_HAS_RVALUE_REFERENCE_THIS + template Matcher dynCastTo() && { + static_assert(std::is_base_of::value, "Invalid dynCast call."); + return Matcher(std::move(Implementation)); + } +#endif + /// Forwards the call to the underlying MatcherInterface pointer. bool matches(const T &Node, ASTMatchFinder *Finder, @@ -563,7 +569,13 @@ /// /// The returned matcher keeps the same restrictions as \c this and remembers /// that it is meant to support nodes of type \c T. - operator DynTypedMatcher() const { return Implementation; } + operator DynTypedMatcher() const LLVM_LVALUE_FUNCTION { + return Implementation; + } + +#if LLVM_HAS_RVALUE_REFERENCE_THIS + operator DynTypedMatcher() && { return std::move(Implementation); } +#endif /// Allows the conversion of a \c Matcher to a \c /// Matcher. @@ -870,7 +882,7 @@ Names, getOperatorSpelling(Node.getOverloadedOperator())); } - const std::vector Names; + std::vector Names; }; /// Matches named declarations with a specific name. @@ -904,8 +916,8 @@ /// It is slower but simple and works on all cases. bool matchesNodeFullSlow(const NamedDecl &Node) const; - const bool UseUnqualifiedMatch; - const std::vector Names; + bool UseUnqualifiedMatch; + std::vector Names; }; /// Trampoline function to use VariadicFunction<> to construct a @@ -926,7 +938,7 @@ static_assert(std::is_same>::value, "instantiated with wrong types"); - const DynTypedMatcher InnerMatcher; + DynTypedMatcher InnerMatcher; public: explicit HasDeclarationMatcher(const Matcher &InnerMatcher) @@ -1315,20 +1327,36 @@ VariadicOperatorMatcher(DynTypedMatcher::VariadicOperator Op, Ps &&... Params) : Op(Op), Params(std::forward(Params)...) {} - template operator Matcher() const { + template operator Matcher() const LLVM_LVALUE_FUNCTION { return DynTypedMatcher::constructVariadic( Op, ASTNodeKind::getFromNodeKind(), getMatchers(std::index_sequence_for())) .template unconditionalConvertTo(); } +#if LLVM_HAS_RVALUE_REFERENCE_THIS + template operator Matcher() && { + return DynTypedMatcher::constructVariadic( + Op, ASTNodeKind::getFromNodeKind(), + getMatchers(std::index_sequence_for())) + .template unconditionalConvertTo(); + } +#endif private: // Helper method to unpack the tuple into a vector. template - std::vector getMatchers(std::index_sequence) const { + std::vector + getMatchers(std::index_sequence) const LLVM_LVALUE_FUNCTION { return {Matcher(std::get(Params))...}; } +#if LLVM_HAS_RVALUE_REFERENCE_THIS + template + std::vector getMatchers(std::index_sequence) && { + return {Matcher(std::get(std::move(Params)))...}; + } +#endif + const DynTypedMatcher::VariadicOperator Op; std::tuple Params; }; @@ -1417,12 +1445,18 @@ using ReturnTypes = ToTypes; - template operator Matcher() const { + template operator Matcher() const LLVM_LVALUE_FUNCTION { return Matcher(new ArgumentAdapterT(InnerMatcher)); } +#if LLVM_HAS_RVALUE_REFERENCE_THIS + template operator Matcher() && { + return Matcher(new ArgumentAdapterT(std::move(InnerMatcher))); + } +#endif + private: - const Matcher InnerMatcher; + Matcher InnerMatcher; }; /// Converts a \c Matcher to a matcher of desired type \c To by @@ -1464,7 +1498,7 @@ }; template class TraversalMatcher : public MatcherInterface { - const DynTypedMatcher InnerMatcher; + DynTypedMatcher InnerMatcher; clang::TraversalKind Traversal; public: @@ -1490,13 +1524,22 @@ TraversalWrapper(TraversalKind TK, const MatcherType &InnerMatcher) : TK(TK), InnerMatcher(InnerMatcher) {} - template operator Matcher() const { + template operator Matcher() const LLVM_LVALUE_FUNCTION { return internal::DynTypedMatcher::constructRestrictedWrapper( new internal::TraversalMatcher(TK, InnerMatcher), ASTNodeKind::getFromNodeKind()) .template unconditionalConvertTo(); } +#if LLVM_HAS_RVALUE_REFERENCE_THIS + template operator Matcher() && { + return internal::DynTypedMatcher::constructRestrictedWrapper( + new internal::TraversalMatcher(TK, std::move(InnerMatcher)), + ASTNodeKind::getFromNodeKind()) + .template unconditionalConvertTo(); + } +#endif + private: TraversalKind TK; MatcherType InnerMatcher; @@ -1522,15 +1565,23 @@ using ReturnTypes = typename ExtractFunctionArgMeta::type; - template - operator Matcher() const { + template operator Matcher() const LLVM_LVALUE_FUNCTION { static_assert(TypeListContainsSuperOf::value, "right polymorphic conversion"); return Matcher(new_from_tuple>(Params)); } +#if LLVM_HAS_RVALUE_REFERENCE_THIS + template operator Matcher() && { + static_assert(TypeListContainsSuperOf::value, + "right polymorphic conversion"); + return Matcher( + new_from_tuple>(std::move(Params))); + } +#endif + private: - const std::tuple Params; + std::tuple Params; }; /// Matches nodes of type T that have child nodes of type ChildT for @@ -1539,7 +1590,7 @@ /// ChildT must be an AST base type. template class HasMatcher : public MatcherInterface { - const DynTypedMatcher InnerMatcher; + DynTypedMatcher InnerMatcher; public: explicit HasMatcher(const Matcher &InnerMatcher) @@ -1562,7 +1613,7 @@ static_assert(IsBaseType::value, "for each only accepts base type matcher"); - const DynTypedMatcher InnerMatcher; + DynTypedMatcher InnerMatcher; public: explicit ForEachMatcher(const Matcher &InnerMatcher) @@ -1592,7 +1643,7 @@ static_assert(IsBaseType::value, "has descendant only accepts base type matcher"); - const DynTypedMatcher DescendantMatcher; + DynTypedMatcher DescendantMatcher; public: explicit HasDescendantMatcher(const Matcher &DescendantMatcher) @@ -1614,7 +1665,7 @@ static_assert(IsBaseType::value, "has parent only accepts base type matcher"); - const DynTypedMatcher ParentMatcher; + DynTypedMatcher ParentMatcher; public: explicit HasParentMatcher(const Matcher &ParentMatcher) @@ -1636,7 +1687,7 @@ static_assert(IsBaseType::value, "has ancestor only accepts base type matcher"); - const DynTypedMatcher AncestorMatcher; + DynTypedMatcher AncestorMatcher; public: explicit HasAncestorMatcher(const Matcher &AncestorMatcher) @@ -1660,7 +1711,7 @@ static_assert(IsBaseType::value, "for each descendant only accepts base type matcher"); - const DynTypedMatcher DescendantMatcher; + DynTypedMatcher DescendantMatcher; public: explicit ForEachDescendantMatcher( @@ -1693,7 +1744,7 @@ } private: - const ValueT ExpectedValue; + ValueT ExpectedValue; }; /// Template specializations to easily write matchers for floating point @@ -1726,7 +1777,7 @@ /// \c Matcher matches. template class LocMatcher : public MatcherInterface { - const DynTypedMatcher InnerMatcher; + DynTypedMatcher InnerMatcher; public: explicit LocMatcher(const Matcher &InnerMatcher) @@ -1750,7 +1801,7 @@ /// /// Used to implement the \c loc() matcher. class TypeLocTypeMatcher : public MatcherInterface { - const DynTypedMatcher InnerMatcher; + DynTypedMatcher InnerMatcher; public: explicit TypeLocTypeMatcher(const Matcher &InnerMatcher) @@ -1769,7 +1820,7 @@ /// another node of type \c T that can be reached using a given traverse /// function. template class TypeTraverseMatcher : public MatcherInterface { - const DynTypedMatcher InnerMatcher; + DynTypedMatcher InnerMatcher; public: explicit TypeTraverseMatcher(const Matcher &InnerMatcher, @@ -1794,7 +1845,7 @@ /// given traverse function. template class TypeLocTraverseMatcher : public MatcherInterface { - const DynTypedMatcher InnerMatcher; + DynTypedMatcher InnerMatcher; public: explicit TypeLocTraverseMatcher(const Matcher &InnerMatcher, @@ -1849,7 +1900,7 @@ }; private: - const Matcher InnerMatcher; + Matcher InnerMatcher; }; /// A simple memoizer of T(*)() functions. @@ -2193,7 +2244,7 @@ return BinaryOperator::getOpcodeStr(*optBinaryOpcode); } - const std::vector Names; + std::vector Names; }; using HasOpNameMatcher = diff --git a/clang/include/clang/ASTMatchers/ASTMatchersMacros.h b/clang/include/clang/ASTMatchers/ASTMatchersMacros.h --- a/clang/include/clang/ASTMatchers/ASTMatchersMacros.h +++ b/clang/include/clang/ASTMatchers/ASTMatchersMacros.h @@ -143,7 +143,7 @@ *Builder) const override; \ \ private: \ - ParamType const Param; \ + ParamType Param; \ }; \ } \ inline ::clang::ast_matchers::internal::Matcher DefineMatcher( \ @@ -151,7 +151,7 @@ return ::clang::ast_matchers::internal::makeMatcher( \ new internal::matcher_##DefineMatcher##OverloadId##Matcher(Param)); \ } \ - typedef ::clang::ast_matchers::internal::Matcher( \ + typedef ::clang::ast_matchers::internal::Matcher ( \ &DefineMatcher##_Type##OverloadId)(ParamType const &Param); \ inline bool internal::matcher_##DefineMatcher##OverloadId##Matcher::matches( \ const Type &Node, \ @@ -192,8 +192,8 @@ *Builder) const override; \ \ private: \ - ParamType1 const Param1; \ - ParamType2 const Param2; \ + ParamType1 Param1; \ + ParamType2 Param2; \ }; \ } \ inline ::clang::ast_matchers::internal::Matcher DefineMatcher( \ @@ -202,7 +202,7 @@ new internal::matcher_##DefineMatcher##OverloadId##Matcher(Param1, \ Param2)); \ } \ - typedef ::clang::ast_matchers::internal::Matcher( \ + typedef ::clang::ast_matchers::internal::Matcher ( \ &DefineMatcher##_Type##OverloadId)(ParamType1 const &Param1, \ ParamType2 const &Param2); \ inline bool internal::matcher_##DefineMatcher##OverloadId##Matcher::matches( \ @@ -281,7 +281,7 @@ *Builder) const override; \ \ private: \ - ParamType const Param; \ + ParamType Param; \ }; \ } \ inline ::clang::ast_matchers::internal::PolymorphicMatcher< \ @@ -333,8 +333,8 @@ *Builder) const override; \ \ private: \ - ParamType1 const Param1; \ - ParamType2 const Param2; \ + ParamType1 Param1; \ + ParamType2 Param2; \ }; \ } \ inline ::clang::ast_matchers::internal::PolymorphicMatcher< \ @@ -469,7 +469,7 @@ *Builder) const override; \ \ private: \ - std::shared_ptr const Param; \ + std::shared_ptr Param; \ }; \ } \ inline ::clang::ast_matchers::internal::Matcher DefineMatcher( \ @@ -521,7 +521,7 @@ *Builder) const override; \ \ private: \ - std::shared_ptr const Param; \ + std::shared_ptr Param; \ }; \ } \ inline ::clang::ast_matchers::internal::PolymorphicMatcher< \