35 #ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHERSINTERNAL_H
36 #define LLVM_CLANG_ASTMATCHERS_ASTMATCHERSINTERNAL_H
49 #include "llvm/ADT/Optional.h"
50 #include "llvm/ADT/VariadicFunction.h"
51 #include "llvm/Support/ManagedStatic.h"
57 namespace ast_matchers {
69 void addNode(StringRef
ID,
const ast_type_traits::DynTypedNode& DynNode) {
70 NodeMap[
ID] = DynNode;
78 const T *getNodeAs(StringRef
ID)
const {
79 IDToNodeMap::const_iterator It = NodeMap.find(ID);
80 if (It == NodeMap.end()) {
83 return It->second.get<T>();
86 ast_type_traits::DynTypedNode getNode(StringRef ID)
const {
87 IDToNodeMap::const_iterator It = NodeMap.find(ID);
88 if (It == NodeMap.end()) {
89 return ast_type_traits::DynTypedNode();
95 bool operator<(
const BoundNodesMap &Other)
const {
96 return NodeMap < Other.NodeMap;
104 typedef std::map<std::string, ast_type_traits::DynTypedNode> IDToNodeMap;
106 const IDToNodeMap &getMap()
const {
112 bool isComparable()
const {
113 for (
const auto &IDAndNode : NodeMap) {
114 if (!IDAndNode.second.getMemoizationData())
128 class BoundNodesTreeBuilder {
134 virtual ~Visitor() {}
139 virtual void visitMatch(
const BoundNodes& BoundNodesView) = 0;
143 void setBinding(StringRef Id,
const ast_type_traits::DynTypedNode &DynNode) {
144 if (Bindings.empty())
145 Bindings.emplace_back();
146 for (BoundNodesMap &Binding : Bindings)
147 Binding.addNode(Id, DynNode);
151 void addMatch(
const BoundNodesTreeBuilder &Bindings);
156 void visitMatches(Visitor* ResultVisitor);
158 template <
typename ExcludePredicate>
159 bool removeBindings(
const ExcludePredicate &Predicate) {
160 Bindings.erase(std::remove_if(Bindings.begin(), Bindings.end(), Predicate),
162 return !Bindings.empty();
166 bool operator<(
const BoundNodesTreeBuilder &Other)
const {
167 return Bindings < Other.Bindings;
172 bool isComparable()
const {
173 for (
const BoundNodesMap &NodesMap : Bindings) {
174 if (!NodesMap.isComparable())
181 SmallVector<BoundNodesMap, 16> Bindings;
184 class ASTMatchFinder;
191 class DynMatcherInterface
192 :
public llvm::ThreadSafeRefCountedBase<DynMatcherInterface> {
194 virtual ~DynMatcherInterface() {}
200 virtual bool dynMatches(
const ast_type_traits::DynTypedNode &DynNode,
202 BoundNodesTreeBuilder *
Builder)
const = 0;
212 template <
typename T>
213 class MatcherInterface :
public DynMatcherInterface {
221 BoundNodesTreeBuilder *
Builder)
const = 0;
223 bool dynMatches(
const ast_type_traits::DynTypedNode &DynNode,
225 BoundNodesTreeBuilder *
Builder)
const override {
226 return matches(DynNode.getUnchecked<T>(), Finder, Builder);
232 template <
typename T>
233 class SingleNodeMatcherInterface :
public MatcherInterface<T> {
238 virtual bool matchesNode(
const T &
Node)
const = 0;
244 BoundNodesTreeBuilder * )
const override {
245 return matchesNode(Node);
249 template <
typename>
class Matcher;
258 class DynTypedMatcher {
261 template <
typename T>
262 DynTypedMatcher(MatcherInterface<T> *Implementation)
264 SupportedKind(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()),
265 RestrictKind(SupportedKind), Implementation(Implementation) {}
268 enum VariadicOperator {
283 static DynTypedMatcher
284 constructVariadic(VariadicOperator Op,
285 ast_type_traits::ASTNodeKind SupportedKind,
291 static DynTypedMatcher trueMatcher(ast_type_traits::ASTNodeKind NodeKind);
293 void setAllowBind(
bool AB) { AllowBind = AB; }
298 bool canMatchNodesOfKind(ast_type_traits::ASTNodeKind
Kind)
const;
302 DynTypedMatcher dynCastTo(
const ast_type_traits::ASTNodeKind
Kind)
const;
305 bool matches(
const ast_type_traits::DynTypedNode &DynNode,
306 ASTMatchFinder *
Finder, BoundNodesTreeBuilder *
Builder)
const;
312 bool matchesNoKindCheck(
const ast_type_traits::DynTypedNode &DynNode,
314 BoundNodesTreeBuilder *
Builder)
const;
328 typedef std::pair<ast_type_traits::ASTNodeKind, uint64_t> MatcherIDType;
329 MatcherIDType getID()
const {
332 return std::make_pair(RestrictKind,
333 reinterpret_cast<uint64_t>(Implementation.get()));
340 ast_type_traits::ASTNodeKind getSupportedKind()
const {
341 return SupportedKind;
349 template <
typename T>
bool canConvertTo()
const {
350 return canConvertTo(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
352 bool canConvertTo(ast_type_traits::ASTNodeKind To)
const;
359 template <
typename T> Matcher<T> convertTo()
const {
360 assert(canConvertTo<T>());
361 return unconditionalConvertTo<T>();
368 template <
typename T> Matcher<T> unconditionalConvertTo()
const;
371 DynTypedMatcher(ast_type_traits::ASTNodeKind SupportedKind,
372 ast_type_traits::ASTNodeKind RestrictKind,
373 IntrusiveRefCntPtr<DynMatcherInterface> Implementation)
375 SupportedKind(SupportedKind),
376 RestrictKind(RestrictKind),
377 Implementation(std::move(Implementation)) {}
380 ast_type_traits::ASTNodeKind SupportedKind;
385 ast_type_traits::ASTNodeKind RestrictKind;
386 IntrusiveRefCntPtr<DynMatcherInterface> Implementation;
393 template <
typename T>
394 class WrapperMatcherInterface :
public MatcherInterface<T> {
396 explicit WrapperMatcherInterface(DynTypedMatcher &&
InnerMatcher)
410 template <
typename T>
414 explicit Matcher(MatcherInterface<T> *Implementation)
415 : Implementation(Implementation) {}
420 template <
typename From>
421 Matcher(
const Matcher<From> &Other,
422 typename std::enable_if<std::is_base_of<From, T>::value &&
423 !std::is_same<From, T>::value>::
type * = 0)
424 : Implementation(restrictMatcher(Other.Implementation)) {
425 assert(Implementation.getSupportedKind().isSame(
426 ast_type_traits::ASTNodeKind::getFromNodeKind<T>()));
432 template <
typename TypeT>
433 Matcher(
const Matcher<TypeT> &Other,
434 typename std::enable_if<
435 std::is_same<T, QualType>::value &&
436 std::is_same<TypeT, Type>::value>::
type* = 0)
437 : Implementation(new TypeToQualType<TypeT>(Other)) {}
442 template <
typename To>
443 Matcher<To> dynCastTo()
const {
444 static_assert(std::is_base_of<To, T>::value,
"Invalid dynCast call.");
445 return Matcher<To>(Implementation);
451 BoundNodesTreeBuilder *
Builder)
const {
457 DynTypedMatcher::MatcherIDType getID()
const {
458 return Implementation.getID();
465 operator DynTypedMatcher()
const {
return Implementation; }
473 template <
typename TypeT>
474 class TypeToQualType :
public WrapperMatcherInterface<QualType> {
477 : TypeToQualType::WrapperMatcherInterface(InnerMatcher) {}
479 bool matches(
const QualType &Node, ASTMatchFinder *Finder,
480 BoundNodesTreeBuilder *Builder)
const override {
490 template <
typename U>
friend class Matcher;
492 friend class DynTypedMatcher;
494 static DynTypedMatcher restrictMatcher(
const DynTypedMatcher &Other) {
495 return Other.dynCastTo(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
498 explicit Matcher(
const DynTypedMatcher &Implementation)
499 : Implementation(restrictMatcher(Implementation)) {
500 assert(this->Implementation.getSupportedKind()
501 .isSame(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()));
504 DynTypedMatcher Implementation;
509 template <
typename T>
510 inline Matcher<T> makeMatcher(MatcherInterface<T> *Implementation) {
511 return Matcher<T>(Implementation);
519 inline Matcher<QualType> DynTypedMatcher::convertTo<QualType>()
const {
520 assert(canConvertTo<QualType>());
521 const ast_type_traits::ASTNodeKind SourceKind = getSupportedKind();
522 if (SourceKind.isSame(
523 ast_type_traits::ASTNodeKind::getFromNodeKind<Type>())) {
525 return unconditionalConvertTo<Type>();
527 return unconditionalConvertTo<QualType>();
531 template <
typename MatcherT,
typename IteratorT>
532 bool matchesFirstInRange(
const MatcherT &
Matcher, IteratorT Start,
533 IteratorT
End, ASTMatchFinder *Finder,
534 BoundNodesTreeBuilder *Builder) {
535 for (IteratorT
I = Start;
I !=
End; ++
I) {
536 BoundNodesTreeBuilder
Result(*Builder);
537 if (Matcher.matches(*
I, Finder, &Result)) {
538 *Builder = std::move(Result);
547 template <
typename MatcherT,
typename IteratorT>
548 bool matchesFirstInPointerRange(
const MatcherT &Matcher, IteratorT Start,
549 IteratorT End, ASTMatchFinder *Finder,
550 BoundNodesTreeBuilder *Builder) {
551 for (IteratorT
I = Start;
I !=
End; ++
I) {
552 BoundNodesTreeBuilder
Result(*Builder);
553 if (Matcher.matches(**
I, Finder, &Result)) {
554 *Builder = std::move(Result);
563 #if defined(_MSC_VER) && !defined(__clang__)
567 template <
typename T>
struct has_getDecl {
568 __if_exists(T::getDecl) {
571 __if_not_exists(T::getDecl) {
580 template <
typename>
struct type_sink_to_void {
typedef void type; };
581 template <
typename T,
typename =
void>
struct has_getDecl : std::false_type {};
582 template <
typename T>
584 T, typename type_sink_to_void<decltype(std::declval<T>().getDecl())>
::type>
592 template <
typename T,
typename ArgT>
593 class HasOverloadedOperatorNameMatcher :
public SingleNodeMatcherInterface<T> {
594 static_assert(std::is_same<T, CXXOperatorCallExpr>::value ||
595 std::is_base_of<FunctionDecl, T>::value,
596 "unsupported class for matcher");
597 static_assert(std::is_same<ArgT, StringRef>::value,
598 "argument type must be StringRef");
601 explicit HasOverloadedOperatorNameMatcher(
const StringRef
Name)
602 : SingleNodeMatcherInterface<T>(), Name(Name) {}
604 bool matchesNode(
const T &Node)
const override {
605 return matchesSpecialized(Node);
613 bool matchesSpecialized(
const CXXOperatorCallExpr &Node)
const {
619 bool matchesSpecialized(
const FunctionDecl &Node)
const {
620 return Node.isOverloadedOperator() &&
630 class HasNameMatcher :
public SingleNodeMatcherInterface<NamedDecl> {
632 explicit HasNameMatcher(StringRef
Name);
634 bool matchesNode(
const NamedDecl &Node)
const override;
641 bool matchesNodeUnqualified(
const NamedDecl &Node)
const;
648 bool matchesNodeFull(
const NamedDecl &Node)
const;
650 const bool UseUnqualifiedMatch;
651 const std::string
Name;
658 template <
typename T,
typename DeclMatcherT>
659 class HasDeclarationMatcher :
public WrapperMatcherInterface<T> {
660 static_assert(std::is_same<DeclMatcherT, Matcher<Decl>>::value,
661 "instantiated with wrong types");
664 explicit HasDeclarationMatcher(
const Matcher<Decl> &
InnerMatcher)
665 : HasDeclarationMatcher::WrapperMatcherInterface(InnerMatcher) {}
667 bool matches(
const T &Node, ASTMatchFinder *Finder,
668 BoundNodesTreeBuilder *Builder)
const override {
669 return matchesSpecialized(Node, Finder, Builder);
675 template <
typename U>
676 bool matchesSpecialized(
677 const U &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder,
678 typename std::enable_if<has_getDecl<U>::value,
int>::
type = 0)
const {
684 bool matchesSpecialized(
const QualType &Node, ASTMatchFinder *Finder,
685 BoundNodesTreeBuilder *Builder)
const {
689 if (
auto *TD = Node->getAsTagDecl())
690 return matchesDecl(TD, Finder, Builder);
691 else if (
auto *TT = Node->getAs<TypedefType>())
697 else if (
auto *TTP = dyn_cast<TemplateTypeParmType>(Node.getTypePtr()))
699 else if (
auto *OCIT = Node->getAs<ObjCInterfaceType>())
701 else if (
auto *UUT = Node->getAs<UnresolvedUsingType>())
703 else if (
auto *ICNT = Node->getAs<InjectedClassNameType>())
711 ASTMatchFinder *Finder,
712 BoundNodesTreeBuilder *Builder)
const {
713 return matchesDecl(Node.getTemplateName().getAsTemplateDecl(),
719 bool matchesSpecialized(
const CallExpr &Node, ASTMatchFinder *Finder,
720 BoundNodesTreeBuilder *Builder)
const {
726 bool matchesSpecialized(
const CXXConstructExpr &Node,
727 ASTMatchFinder *Finder,
728 BoundNodesTreeBuilder *Builder)
const {
734 bool matchesSpecialized(
const MemberExpr &Node,
735 ASTMatchFinder *Finder,
736 BoundNodesTreeBuilder *Builder)
const {
742 bool matchesDecl(
const Decl *Node, ASTMatchFinder *Finder,
743 BoundNodesTreeBuilder *Builder)
const {
744 return Node !=
nullptr &&
752 template <
typename T>
754 static const bool value =
755 std::is_same<T, Decl>::value ||
756 std::is_same<T, Stmt>::value ||
757 std::is_same<T, QualType>::value ||
758 std::is_same<T, Type>::value ||
759 std::is_same<T, TypeLoc>::value ||
760 std::is_same<T, NestedNameSpecifier>::value ||
761 std::is_same<T, NestedNameSpecifierLoc>::value ||
762 std::is_same<T, CXXCtorInitializer>::value;
764 template <
typename T>
765 const bool IsBaseType<T>::value;
785 class ASTMatchFinder {
793 TK_IgnoreImplicitCastsAndParentheses
805 enum AncestorMatchMode {
812 virtual ~ASTMatchFinder() {}
818 virtual bool classIsDerivedFrom(
const CXXRecordDecl *Declaration,
819 const Matcher<NamedDecl> &
Base,
820 BoundNodesTreeBuilder *Builder) = 0;
822 template <
typename T>
823 bool matchesChildOf(
const T &Node,
824 const DynTypedMatcher &Matcher,
825 BoundNodesTreeBuilder *Builder,
826 TraversalKind Traverse,
828 static_assert(std::is_base_of<Decl, T>::value ||
829 std::is_base_of<Stmt, T>::value ||
830 std::is_base_of<NestedNameSpecifier, T>::value ||
831 std::is_base_of<NestedNameSpecifierLoc, T>::value ||
832 std::is_base_of<TypeLoc, T>::value ||
833 std::is_base_of<QualType, T>::value,
834 "unsupported type for recursive matching");
836 Matcher, Builder, Traverse, Bind);
839 template <
typename T>
840 bool matchesDescendantOf(
const T &Node,
841 const DynTypedMatcher &Matcher,
842 BoundNodesTreeBuilder *Builder,
844 static_assert(std::is_base_of<Decl, T>::value ||
845 std::is_base_of<Stmt, T>::value ||
846 std::is_base_of<NestedNameSpecifier, T>::value ||
847 std::is_base_of<NestedNameSpecifierLoc, T>::value ||
848 std::is_base_of<TypeLoc, T>::value ||
849 std::is_base_of<QualType, T>::value,
850 "unsupported type for recursive matching");
852 Matcher, Builder, Bind);
856 template <
typename T>
857 bool matchesAncestorOf(
const T &Node,
858 const DynTypedMatcher &Matcher,
859 BoundNodesTreeBuilder *Builder,
860 AncestorMatchMode MatchMode) {
861 static_assert(std::is_base_of<Decl, T>::value ||
862 std::is_base_of<NestedNameSpecifierLoc, T>::value ||
863 std::is_base_of<Stmt, T>::value ||
864 std::is_base_of<TypeLoc, T>::value,
865 "type not allowed for recursive matching");
867 Matcher, Builder, MatchMode);
870 virtual ASTContext &getASTContext()
const = 0;
873 virtual bool matchesChildOf(
const ast_type_traits::DynTypedNode &Node,
874 const DynTypedMatcher &Matcher,
875 BoundNodesTreeBuilder *Builder,
876 TraversalKind Traverse,
879 virtual bool matchesDescendantOf(
const ast_type_traits::DynTypedNode &Node,
880 const DynTypedMatcher &Matcher,
881 BoundNodesTreeBuilder *Builder,
884 virtual bool matchesAncestorOf(
const ast_type_traits::DynTypedNode &Node,
885 const DynTypedMatcher &Matcher,
886 BoundNodesTreeBuilder *Builder,
887 AncestorMatchMode MatchMode) = 0;
894 template <
typename... Ts>
struct TypeList {};
896 template <
typename T1,
typename... Ts>
struct TypeList<T1, Ts...> {
904 typedef TypeList<Ts...> tail;
908 typedef TypeList<> EmptyTypeList;
912 template <
typename AnyTypeList,
typename T>
913 struct TypeListContainsSuperOf {
914 static const bool value =
915 std::is_base_of<typename AnyTypeList::head, T>::value ||
916 TypeListContainsSuperOf<typename AnyTypeList::tail, T>::value;
918 template <
typename T>
919 struct TypeListContainsSuperOf<EmptyTypeList, T> {
920 static const bool value =
false;
926 typedef TypeList<
Decl,
Stmt, NestedNameSpecifier, NestedNameSpecifierLoc,
927 QualType,
Type, TypeLoc, CXXCtorInitializer> AllNodeBaseTypes;
933 template <
class T>
struct ExtractFunctionArgMeta;
934 template <
class T>
struct ExtractFunctionArgMeta<void(T)> {
939 typedef AllNodeBaseTypes AdaptativeDefaultFromTypes;
940 typedef TypeList<
Decl,
Stmt, NestedNameSpecifier, NestedNameSpecifierLoc,
941 TypeLoc, QualType> AdaptativeDefaultToTypes;
944 typedef TypeList<CallExpr, CXXConstructExpr, DeclRefExpr, EnumType,
945 InjectedClassNameType, LabelStmt, MemberExpr, QualType,
947 TemplateTypeParmType, TypedefType,
948 UnresolvedUsingType> HasDeclarationSupportedTypes;
963 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
964 typename FromTypes = AdaptativeDefaultFromTypes,
965 typename ToTypes = AdaptativeDefaultToTypes>
966 struct ArgumentAdaptingMatcherFunc {
967 template <
typename T>
class Adaptor {
970 : InnerMatcher(InnerMatcher) {}
972 typedef ToTypes ReturnTypes;
974 template <
typename To>
operator Matcher<To>()
const {
975 return Matcher<To>(
new ArgumentAdapterT<To, T>(
InnerMatcher));
982 template <
typename T>
987 template <
typename T>
988 Adaptor<T> operator()(
const Matcher<T> &
InnerMatcher)
const {
989 return create(InnerMatcher);
1005 template <
template <
typename T>
class MatcherT,
1006 typename ReturnTypesF = void(AllNodeBaseTypes)>
1007 class PolymorphicMatcherWithParam0 {
1010 template <
typename T>
1011 operator Matcher<T>()
const {
1012 static_assert(TypeListContainsSuperOf<ReturnTypes, T>::value,
1013 "right polymorphic conversion");
1014 return Matcher<T>(
new MatcherT<T>());
1018 template <
template <
typename T,
typename P1>
class MatcherT,
1020 typename ReturnTypesF = void(AllNodeBaseTypes)>
1021 class PolymorphicMatcherWithParam1 {
1023 explicit PolymorphicMatcherWithParam1(
const P1 &Param1)
1028 template <
typename T>
1029 operator Matcher<T>()
const {
1030 static_assert(TypeListContainsSuperOf<ReturnTypes, T>::value,
1031 "right polymorphic conversion");
1032 return Matcher<T>(
new MatcherT<T, P1>(Param1));
1039 template <
template <
typename T,
typename P1,
typename P2>
class MatcherT,
1040 typename P1,
typename P2,
1041 typename ReturnTypesF = void(AllNodeBaseTypes)>
1042 class PolymorphicMatcherWithParam2 {
1044 PolymorphicMatcherWithParam2(
const P1 &Param1,
const P2 &Param2)
1045 : Param1(Param1), Param2(Param2) {}
1049 template <
typename T>
1050 operator Matcher<T>()
const {
1051 static_assert(TypeListContainsSuperOf<ReturnTypes, T>::value,
1052 "right polymorphic conversion");
1053 return Matcher<T>(
new MatcherT<T, P1, P2>(Param1, Param2));
1067 typedef AllNodeBaseTypes ReturnTypes;
1069 template <
typename T>
1070 operator Matcher<T>()
const {
1071 return DynTypedMatcher::trueMatcher(
1072 ast_type_traits::ASTNodeKind::getFromNodeKind<T>())
1073 .template unconditionalConvertTo<T>();
1081 template <
typename T>
1082 class BindableMatcher :
public Matcher<T> {
1084 explicit BindableMatcher(
const Matcher<T> &M) : Matcher<T>(M) {}
1085 explicit BindableMatcher(MatcherInterface<T> *Implementation)
1086 : Matcher<T>(Implementation) {}
1092 Matcher<T> bind(StringRef ID)
const {
1093 return DynTypedMatcher(*
this)
1095 ->template unconditionalConvertTo<T>();
1100 operator DynTypedMatcher()
const {
1101 DynTypedMatcher
Result =
static_cast<const Matcher<T>&
>(*this);
1102 Result.setAllowBind(
true);
1111 template <
typename T,
typename ChildT>
1112 class HasMatcher :
public WrapperMatcherInterface<T> {
1113 static_assert(IsBaseType<ChildT>::value,
1114 "has only accepts base type matcher");
1117 explicit HasMatcher(
const Matcher<ChildT> &ChildMatcher)
1118 : HasMatcher::WrapperMatcherInterface(ChildMatcher) {}
1120 bool matches(
const T &Node, ASTMatchFinder *Finder,
1121 BoundNodesTreeBuilder *Builder)
const override {
1122 return Finder->matchesChildOf(
1124 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1125 ASTMatchFinder::BK_First);
1134 template <
typename T,
typename ChildT>
1135 class ForEachMatcher :
public WrapperMatcherInterface<T> {
1136 static_assert(IsBaseType<ChildT>::value,
1137 "for each only accepts base type matcher");
1140 explicit ForEachMatcher(
const Matcher<ChildT> &ChildMatcher)
1141 : ForEachMatcher::WrapperMatcherInterface(ChildMatcher) {}
1143 bool matches(
const T& Node, ASTMatchFinder* Finder,
1144 BoundNodesTreeBuilder* Builder)
const override {
1145 return Finder->matchesChildOf(
1147 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1148 ASTMatchFinder::BK_All);
1161 template <
typename... Ps>
class VariadicOperatorMatcher {
1163 VariadicOperatorMatcher(DynTypedMatcher::VariadicOperator Op, Ps &&... Params)
1164 : Op(Op), Params(std::forward<Ps>(Params)...) {}
1166 template <
typename T>
operator Matcher<T>()
const {
1167 return DynTypedMatcher::constructVariadic(
1168 Op, ast_type_traits::ASTNodeKind::getFromNodeKind<T>(),
1169 getMatchers<T>(llvm::index_sequence_for<Ps...>()))
1170 .template unconditionalConvertTo<T>();
1176 std::vector<DynTypedMatcher> getMatchers(llvm::index_sequence<Is...>)
const {
1177 return {Matcher<T>(std::get<Is>(Params))...};
1180 const DynTypedMatcher::VariadicOperator Op;
1181 std::tuple<Ps...> Params;
1186 template <
unsigned MinCount,
unsigned MaxCount>
1187 struct VariadicOperatorMatcherFunc {
1188 DynTypedMatcher::VariadicOperator Op;
1190 template <
typename... Ms>
1191 VariadicOperatorMatcher<Ms...> operator()(Ms &&... Ps)
const {
1192 static_assert(MinCount <=
sizeof...(Ms) &&
sizeof...(Ms) <= MaxCount,
1193 "invalid number of parameters for variadic matcher");
1194 return VariadicOperatorMatcher<Ms...>(Op, std::forward<Ms>(Ps)...);
1200 template <
typename T>
1201 inline Matcher<T> DynTypedMatcher::unconditionalConvertTo()
const {
1202 return Matcher<T>(*this);
1206 template<
typename T>
1207 BindableMatcher<T> makeAllOfComposite(
1211 return BindableMatcher<T>(TrueMatcher());
1219 typedef llvm::pointee_iterator<const Matcher<T> *
const *> PI;
1220 std::vector<DynTypedMatcher> DynMatchers(PI(
InnerMatchers.begin()),
1222 return BindableMatcher<T>(
1223 DynTypedMatcher::constructVariadic(
1224 DynTypedMatcher::VO_AllOf,
1225 ast_type_traits::ASTNodeKind::getFromNodeKind<T>(),
1226 std::move(DynMatchers))
1227 .template unconditionalConvertTo<T>());
1236 template<
typename T,
typename InnerT>
1237 BindableMatcher<T> makeDynCastAllOfComposite(
1239 return BindableMatcher<T>(
1247 template <
typename T,
typename DescendantT>
1248 class HasDescendantMatcher :
public WrapperMatcherInterface<T> {
1249 static_assert(IsBaseType<DescendantT>::value,
1250 "has descendant only accepts base type matcher");
1253 explicit HasDescendantMatcher(
const Matcher<DescendantT> &DescendantMatcher)
1254 : HasDescendantMatcher::WrapperMatcherInterface(DescendantMatcher) {}
1256 bool matches(
const T &Node, ASTMatchFinder *Finder,
1257 BoundNodesTreeBuilder *Builder)
const override {
1258 return Finder->matchesDescendantOf(Node, this->
InnerMatcher, Builder,
1259 ASTMatchFinder::BK_First);
1267 template <
typename T,
typename ParentT>
1268 class HasParentMatcher :
public WrapperMatcherInterface<T> {
1269 static_assert(IsBaseType<ParentT>::value,
1270 "has parent only accepts base type matcher");
1273 explicit HasParentMatcher(
const Matcher<ParentT> &ParentMatcher)
1274 : HasParentMatcher::WrapperMatcherInterface(ParentMatcher) {}
1276 bool matches(
const T &Node, ASTMatchFinder *Finder,
1277 BoundNodesTreeBuilder *Builder)
const override {
1278 return Finder->matchesAncestorOf(Node, this->
InnerMatcher, Builder,
1279 ASTMatchFinder::AMM_ParentOnly);
1287 template <
typename T,
typename AncestorT>
1288 class HasAncestorMatcher :
public WrapperMatcherInterface<T> {
1289 static_assert(IsBaseType<AncestorT>::value,
1290 "has ancestor only accepts base type matcher");
1293 explicit HasAncestorMatcher(
const Matcher<AncestorT> &AncestorMatcher)
1294 : HasAncestorMatcher::WrapperMatcherInterface(AncestorMatcher) {}
1296 bool matches(
const T &Node, ASTMatchFinder *Finder,
1297 BoundNodesTreeBuilder *Builder)
const override {
1298 return Finder->matchesAncestorOf(Node, this->
InnerMatcher, Builder,
1299 ASTMatchFinder::AMM_All);
1309 template <
typename T,
typename DescendantT>
1310 class ForEachDescendantMatcher :
public WrapperMatcherInterface<T> {
1311 static_assert(IsBaseType<DescendantT>::value,
1312 "for each descendant only accepts base type matcher");
1315 explicit ForEachDescendantMatcher(
1316 const Matcher<DescendantT> &DescendantMatcher)
1317 : ForEachDescendantMatcher::WrapperMatcherInterface(DescendantMatcher) {}
1319 bool matches(
const T &Node, ASTMatchFinder *Finder,
1320 BoundNodesTreeBuilder *Builder)
const override {
1321 return Finder->matchesDescendantOf(Node, this->
InnerMatcher, Builder,
1322 ASTMatchFinder::BK_All);
1328 template <
typename T,
typename ValueT>
1329 class ValueEqualsMatcher :
public SingleNodeMatcherInterface<T> {
1330 static_assert(std::is_base_of<CharacterLiteral, T>::value ||
1331 std::is_base_of<CXXBoolLiteralExpr, T>::value ||
1332 std::is_base_of<FloatingLiteral, T>::value ||
1333 std::is_base_of<IntegerLiteral, T>::value,
1334 "the node must have a getValue method");
1337 explicit ValueEqualsMatcher(
const ValueT &ExpectedValue)
1338 : ExpectedValue(ExpectedValue) {}
1340 bool matchesNode(
const T &Node)
const override {
1341 return Node.getValue() == ExpectedValue;
1345 const ValueT ExpectedValue;
1351 inline bool ValueEqualsMatcher<FloatingLiteral, double>::matchesNode(
1352 const FloatingLiteral &Node)
const {
1353 if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle)
1354 return Node.getValue().convertToFloat() == ExpectedValue;
1355 if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble)
1356 return Node.getValue().convertToDouble() == ExpectedValue;
1360 inline bool ValueEqualsMatcher<FloatingLiteral, float>::matchesNode(
1361 const FloatingLiteral &Node)
const {
1362 if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle)
1363 return Node.getValue().convertToFloat() == ExpectedValue;
1364 if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble)
1365 return Node.getValue().convertToDouble() == ExpectedValue;
1369 inline bool ValueEqualsMatcher<FloatingLiteral, llvm::APFloat>::matchesNode(
1370 const FloatingLiteral &Node)
const {
1371 return ExpectedValue.compare(Node.getValue()) == llvm::APFloat::cmpEqual;
1386 template <
typename SourceT,
typename TargetT>
1387 class VariadicDynCastAllOfMatcher
1388 :
public llvm::VariadicFunction<
1389 BindableMatcher<SourceT>, Matcher<TargetT>,
1390 makeDynCastAllOfComposite<SourceT, TargetT> > {
1392 VariadicDynCastAllOfMatcher() {}
1405 template <
typename T>
1406 class VariadicAllOfMatcher :
public llvm::VariadicFunction<
1407 BindableMatcher<T>, Matcher<T>,
1408 makeAllOfComposite<T> > {
1410 VariadicAllOfMatcher() {}
1415 template <
typename TLoc,
typename T>
1416 class LocMatcher :
public WrapperMatcherInterface<TLoc> {
1419 : LocMatcher::WrapperMatcherInterface(InnerMatcher) {}
1421 bool matches(
const TLoc &Node, ASTMatchFinder *Finder,
1422 BoundNodesTreeBuilder *Builder)
const override {
1425 return this->
InnerMatcher.matches(extract(Node), Finder, Builder);
1429 static ast_type_traits::DynTypedNode
1430 extract(
const NestedNameSpecifierLoc &Loc) {
1439 class TypeLocTypeMatcher :
public WrapperMatcherInterface<TypeLoc> {
1441 explicit TypeLocTypeMatcher(
const Matcher<QualType> &
InnerMatcher)
1442 : TypeLocTypeMatcher::WrapperMatcherInterface(InnerMatcher) {}
1444 bool matches(
const TypeLoc &Node, ASTMatchFinder *Finder,
1445 BoundNodesTreeBuilder *Builder)
const override {
1456 template <
typename T>
1457 class TypeTraverseMatcher :
public WrapperMatcherInterface<T> {
1459 explicit TypeTraverseMatcher(
const Matcher<QualType> &
InnerMatcher,
1460 QualType (T::*TraverseFunction)()
const)
1461 : TypeTraverseMatcher::WrapperMatcherInterface(InnerMatcher),
1462 TraverseFunction(TraverseFunction) {}
1464 bool matches(
const T &Node, ASTMatchFinder *Finder,
1465 BoundNodesTreeBuilder *Builder)
const override {
1466 QualType NextNode = (Node.*TraverseFunction)();
1467 if (NextNode.isNull())
1469 return this->InnerMatcher.matches(
1474 QualType (T::*TraverseFunction)()
const;
1480 template <
typename T>
1481 class TypeLocTraverseMatcher :
public WrapperMatcherInterface<T> {
1483 explicit TypeLocTraverseMatcher(
const Matcher<TypeLoc> &InnerMatcher,
1484 TypeLoc (T::*TraverseFunction)()
const)
1485 : TypeLocTraverseMatcher::WrapperMatcherInterface(InnerMatcher),
1486 TraverseFunction(TraverseFunction) {}
1488 bool matches(
const T &Node, ASTMatchFinder *Finder,
1489 BoundNodesTreeBuilder *Builder)
const override {
1490 TypeLoc NextNode = (Node.*TraverseFunction)();
1493 return this->InnerMatcher.matches(
1498 TypeLoc (T::*TraverseFunction)()
const;
1507 template <
typename InnerTBase,
1508 template <
typename OuterT>
class Getter,
1509 template <
typename OuterT>
class MatcherImpl,
1510 typename ReturnTypesF>
1511 class TypeTraversePolymorphicMatcher {
1513 typedef TypeTraversePolymorphicMatcher<InnerTBase, Getter, MatcherImpl,
1520 explicit TypeTraversePolymorphicMatcher(
1524 template <
typename OuterT>
operator Matcher<OuterT>()
const {
1525 return Matcher<OuterT>(
1526 new MatcherImpl<OuterT>(
InnerMatcher, Getter<OuterT>::value()));
1529 struct Func :
public llvm::VariadicFunction<Self, Matcher<InnerTBase>,
1542 template <
typename Matcher, Matcher (*Func)()>
class MemoizedMatcher {
1544 Wrapper() : M(Func()) {}
1549 static const Matcher &getInstance() {
1550 static llvm::ManagedStatic<Wrapper> Instance;
1558 template <
typename InnerTBase,
template <
typename OuterT>
class Getter,
1559 template <
typename OuterT>
class MatcherImpl,
typename ReturnTypesF>
1560 TypeTraversePolymorphicMatcher<InnerTBase, Getter, MatcherImpl, ReturnTypesF>
1561 TypeTraversePolymorphicMatcher<
1562 InnerTBase, Getter, MatcherImpl,
1569 inline ArrayRef<TemplateArgument>
1570 getTemplateSpecializationArgs(
const ClassTemplateSpecializationDecl &D) {
1571 return D.getTemplateArgs().asArray();
1574 inline ArrayRef<TemplateArgument>
1576 return llvm::makeArrayRef(T.getArgs(), T.getNumArgs());
1579 struct NotEqualsBoundNodePredicate {
1580 bool operator()(
const internal::BoundNodesMap &
Nodes)
const {
1581 return Nodes.getNode(ID) !=
Node;
1584 ast_type_traits::DynTypedNode
Node;
C Language Family Type Representation.
Defines the C++ template declaration subclasses.
TemplateSpecializationType(TemplateName T, const TemplateArgument *Args, unsigned NumArgs, QualType Canon, QualType Aliased)
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
const DynTypedMatcher *const Matcher
Defines the Objective-C statement AST node classes.
Defines the clang::Expr interface and subclasses for C++ expressions.
BoundNodesTreeBuilder Nodes
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
const ASTMatchFinder::BindKind Bind
std::vector< DynTypedMatcher > InnerMatchers
const IntrusiveRefCntPtr< DynMatcherInterface > InnerMatcher
detail::InMemoryDirectory::const_iterator I
ID
Defines the set of possible language-specific address spaces.
The result type of a method or function.
The l-value was considered opaque, so the alignment was determined from a type.
ASTMatchFinder *const Finder
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
static DynTypedNode create(const T &Node)
Creates a DynTypedNode from Node.
BoundNodesTreeBuilder BoundNodes
ast_type_traits::DynTypedNode Node
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
const char * getOperatorSpelling(OverloadedOperatorKind Operator)
Retrieve the spelling of the given overloaded operator, without the preceding "operator" keyword...
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
BoundNodesTreeBuilder *const Builder
bool matches(const til::SExpr *E1, const til::SExpr *E2)