1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | #ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHERSINTERNAL_H |
35 | #define LLVM_CLANG_ASTMATCHERS_ASTMATCHERSINTERNAL_H |
36 | |
37 | #include "clang/AST/ASTTypeTraits.h" |
38 | #include "clang/AST/Decl.h" |
39 | #include "clang/AST/DeclCXX.h" |
40 | #include "clang/AST/DeclFriend.h" |
41 | #include "clang/AST/DeclTemplate.h" |
42 | #include "clang/AST/Expr.h" |
43 | #include "clang/AST/ExprObjC.h" |
44 | #include "clang/AST/ExprCXX.h" |
45 | #include "clang/AST/ExprObjC.h" |
46 | #include "clang/AST/NestedNameSpecifier.h" |
47 | #include "clang/AST/Stmt.h" |
48 | #include "clang/AST/TemplateName.h" |
49 | #include "clang/AST/Type.h" |
50 | #include "clang/AST/TypeLoc.h" |
51 | #include "clang/Basic/LLVM.h" |
52 | #include "clang/Basic/OperatorKinds.h" |
53 | #include "llvm/ADT/APFloat.h" |
54 | #include "llvm/ADT/ArrayRef.h" |
55 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
56 | #include "llvm/ADT/None.h" |
57 | #include "llvm/ADT/Optional.h" |
58 | #include "llvm/ADT/STLExtras.h" |
59 | #include "llvm/ADT/SmallVector.h" |
60 | #include "llvm/ADT/StringRef.h" |
61 | #include "llvm/ADT/iterator.h" |
62 | #include "llvm/Support/Casting.h" |
63 | #include "llvm/Support/ManagedStatic.h" |
64 | #include <algorithm> |
65 | #include <cassert> |
66 | #include <cstddef> |
67 | #include <cstdint> |
68 | #include <map> |
69 | #include <string> |
70 | #include <tuple> |
71 | #include <type_traits> |
72 | #include <utility> |
73 | #include <vector> |
74 | |
75 | namespace clang { |
76 | |
77 | class ASTContext; |
78 | |
79 | namespace ast_matchers { |
80 | |
81 | class BoundNodes; |
82 | |
83 | namespace internal { |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | template <typename ResultT, typename ArgT, |
91 | ResultT (*Func)(ArrayRef<const ArgT *>)> |
92 | struct VariadicFunction { |
93 | ResultT operator()() const { return Func(None); } |
94 | |
95 | template <typename... ArgsT> |
96 | ResultT operator()(const ArgT &Arg1, const ArgsT &... Args) const { |
97 | return Execute(Arg1, static_cast<const ArgT &>(Args)...); |
98 | } |
99 | |
100 | |
101 | |
102 | ResultT operator()(ArrayRef<ArgT> Args) const { |
103 | SmallVector<const ArgT*, 8> InnerArgs; |
104 | for (const ArgT &Arg : Args) |
105 | InnerArgs.push_back(&Arg); |
106 | return Func(InnerArgs); |
107 | } |
108 | |
109 | private: |
110 | |
111 | |
112 | template <typename... ArgsT> ResultT Execute(const ArgsT &... Args) const { |
113 | const ArgT *const ArgsArray[] = {&Args...}; |
114 | return Func(ArrayRef<const ArgT *>(ArgsArray, sizeof...(ArgsT))); |
115 | } |
116 | }; |
117 | |
118 | |
119 | |
120 | inline QualType getUnderlyingType(const Expr &Node) { return Node.getType(); } |
121 | |
122 | inline QualType getUnderlyingType(const ValueDecl &Node) { |
123 | return Node.getType(); |
124 | } |
125 | inline QualType getUnderlyingType(const TypedefNameDecl &Node) { |
126 | return Node.getUnderlyingType(); |
127 | } |
128 | inline QualType getUnderlyingType(const FriendDecl &Node) { |
129 | if (const TypeSourceInfo *TSI = Node.getFriendType()) |
130 | return TSI->getType(); |
131 | return QualType(); |
132 | } |
133 | |
134 | |
135 | |
136 | inline const FunctionProtoType * |
137 | getFunctionProtoType(const FunctionProtoType &Node) { |
138 | return &Node; |
139 | } |
140 | |
141 | inline const FunctionProtoType *getFunctionProtoType(const FunctionDecl &Node) { |
142 | return Node.getType()->getAs<FunctionProtoType>(); |
143 | } |
144 | |
145 | |
146 | class BoundNodesMap { |
147 | public: |
148 | |
149 | |
150 | |
151 | void addNode(StringRef ID, const ast_type_traits::DynTypedNode& DynNode) { |
152 | NodeMap[ID] = DynNode; |
153 | } |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | template <typename T> |
160 | const T *getNodeAs(StringRef ID) const { |
161 | IDToNodeMap::const_iterator It = NodeMap.find(ID); |
162 | if (It == NodeMap.end()) { |
163 | return nullptr; |
164 | } |
165 | return It->second.get<T>(); |
166 | } |
167 | |
168 | ast_type_traits::DynTypedNode getNode(StringRef ID) const { |
169 | IDToNodeMap::const_iterator It = NodeMap.find(ID); |
170 | if (It == NodeMap.end()) { |
171 | return ast_type_traits::DynTypedNode(); |
172 | } |
173 | return It->second; |
174 | } |
175 | |
176 | |
177 | bool operator<(const BoundNodesMap &Other) const { |
178 | return NodeMap < Other.NodeMap; |
179 | } |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | using IDToNodeMap = std::map<std::string, ast_type_traits::DynTypedNode>; |
187 | |
188 | const IDToNodeMap &getMap() const { |
189 | return NodeMap; |
190 | } |
191 | |
192 | |
193 | |
194 | bool isComparable() const { |
195 | for (const auto &IDAndNode : NodeMap) { |
196 | if (!IDAndNode.second.getMemoizationData()) |
197 | return false; |
198 | } |
199 | return true; |
200 | } |
201 | |
202 | private: |
203 | IDToNodeMap NodeMap; |
204 | }; |
205 | |
206 | |
207 | |
208 | |
209 | |
210 | class BoundNodesTreeBuilder { |
211 | public: |
212 | |
213 | |
214 | class Visitor { |
215 | public: |
216 | virtual ~Visitor() = default; |
217 | |
218 | |
219 | |
220 | |
221 | virtual void visitMatch(const BoundNodes& BoundNodesView) = 0; |
222 | }; |
223 | |
224 | |
225 | void setBinding(StringRef Id, const ast_type_traits::DynTypedNode &DynNode) { |
226 | if (Bindings.empty()) |
227 | Bindings.emplace_back(); |
228 | for (BoundNodesMap &Binding : Bindings) |
229 | Binding.addNode(Id, DynNode); |
230 | } |
231 | |
232 | |
233 | void addMatch(const BoundNodesTreeBuilder &Bindings); |
234 | |
235 | |
236 | |
237 | |
238 | void visitMatches(Visitor* ResultVisitor); |
239 | |
240 | template <typename ExcludePredicate> |
241 | bool removeBindings(const ExcludePredicate &Predicate) { |
242 | Bindings.erase(std::remove_if(Bindings.begin(), Bindings.end(), Predicate), |
243 | Bindings.end()); |
244 | return !Bindings.empty(); |
245 | } |
246 | |
247 | |
248 | bool operator<(const BoundNodesTreeBuilder &Other) const { |
249 | return Bindings < Other.Bindings; |
250 | } |
251 | |
252 | |
253 | |
254 | bool isComparable() const { |
255 | for (const BoundNodesMap &NodesMap : Bindings) { |
256 | if (!NodesMap.isComparable()) |
257 | return false; |
258 | } |
259 | return true; |
260 | } |
261 | |
262 | private: |
263 | SmallVector<BoundNodesMap, 1> Bindings; |
264 | }; |
265 | |
266 | class ASTMatchFinder; |
267 | |
268 | |
269 | |
270 | |
271 | |
272 | |
273 | class DynMatcherInterface |
274 | : public llvm::ThreadSafeRefCountedBase<DynMatcherInterface> { |
275 | public: |
276 | virtual ~DynMatcherInterface() = default; |
277 | |
278 | |
279 | |
280 | |
281 | |
282 | virtual bool dynMatches(const ast_type_traits::DynTypedNode &DynNode, |
283 | ASTMatchFinder *Finder, |
284 | BoundNodesTreeBuilder *Builder) const = 0; |
285 | }; |
286 | |
287 | |
288 | |
289 | |
290 | |
291 | |
292 | |
293 | |
294 | template <typename T> |
295 | class MatcherInterface : public DynMatcherInterface { |
296 | public: |
297 | |
298 | |
299 | |
300 | |
301 | virtual bool matches(const T &Node, |
302 | ASTMatchFinder *Finder, |
303 | BoundNodesTreeBuilder *Builder) const = 0; |
304 | |
305 | bool dynMatches(const ast_type_traits::DynTypedNode &DynNode, |
306 | ASTMatchFinder *Finder, |
307 | BoundNodesTreeBuilder *Builder) const override { |
308 | return matches(DynNode.getUnchecked<T>(), Finder, Builder); |
309 | } |
310 | }; |
311 | |
312 | |
313 | |
314 | template <typename T> |
315 | class SingleNodeMatcherInterface : public MatcherInterface<T> { |
316 | public: |
317 | |
318 | |
319 | |
320 | virtual bool matchesNode(const T &Node) const = 0; |
321 | |
322 | private: |
323 | |
324 | bool matches(const T &Node, |
325 | ASTMatchFinder * , |
326 | BoundNodesTreeBuilder * ) const override { |
327 | return matchesNode(Node); |
328 | } |
329 | }; |
330 | |
331 | template <typename> class Matcher; |
332 | |
333 | |
334 | |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | class DynTypedMatcher { |
341 | public: |
342 | |
343 | template <typename T> |
344 | DynTypedMatcher(MatcherInterface<T> *Implementation) |
345 | : SupportedKind(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()), |
346 | RestrictKind(SupportedKind), Implementation(Implementation) {} |
347 | |
348 | |
349 | enum VariadicOperator { |
350 | |
351 | VO_AllOf, |
352 | |
353 | |
354 | |
355 | VO_AnyOf, |
356 | |
357 | |
358 | |
359 | VO_EachOf, |
360 | |
361 | |
362 | |
363 | |
364 | |
365 | VO_UnaryNot |
366 | }; |
367 | |
368 | static DynTypedMatcher |
369 | constructVariadic(VariadicOperator Op, |
370 | ast_type_traits::ASTNodeKind SupportedKind, |
371 | std::vector<DynTypedMatcher> InnerMatchers); |
372 | |
373 | |
374 | |
375 | |
376 | static DynTypedMatcher trueMatcher(ast_type_traits::ASTNodeKind NodeKind); |
377 | |
378 | void setAllowBind(bool AB) { AllowBind = AB; } |
379 | |
380 | |
381 | |
382 | |
383 | bool canMatchNodesOfKind(ast_type_traits::ASTNodeKind Kind) const; |
384 | |
385 | |
386 | |
387 | DynTypedMatcher dynCastTo(const ast_type_traits::ASTNodeKind Kind) const; |
388 | |
389 | |
390 | bool matches(const ast_type_traits::DynTypedNode &DynNode, |
391 | ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const; |
392 | |
393 | |
394 | |
395 | |
396 | |
397 | bool matchesNoKindCheck(const ast_type_traits::DynTypedNode &DynNode, |
398 | ASTMatchFinder *Finder, |
399 | BoundNodesTreeBuilder *Builder) const; |
400 | |
401 | |
402 | |
403 | |
404 | llvm::Optional<DynTypedMatcher> tryBind(StringRef ID) const; |
405 | |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | |
412 | |
413 | using MatcherIDType = std::pair<ast_type_traits::ASTNodeKind, uint64_t>; |
414 | MatcherIDType getID() const { |
415 | |
416 | |
417 | return std::make_pair(RestrictKind, |
418 | reinterpret_cast<uint64_t>(Implementation.get())); |
419 | } |
420 | |
421 | |
422 | |
423 | |
424 | |
425 | ast_type_traits::ASTNodeKind getSupportedKind() const { |
426 | return SupportedKind; |
427 | } |
428 | |
429 | |
430 | |
431 | |
432 | |
433 | |
434 | template <typename T> bool canConvertTo() const { |
435 | return canConvertTo(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()); |
436 | } |
437 | bool canConvertTo(ast_type_traits::ASTNodeKind To) const; |
438 | |
439 | |
440 | |
441 | |
442 | |
443 | |
444 | template <typename T> Matcher<T> convertTo() const { |
445 | ()", "/home/seafit/code_projects/clang_source/clang/include/clang/ASTMatchers/ASTMatchersInternal.h", 445, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(canConvertTo<T>()); |
446 | return unconditionalConvertTo<T>(); |
447 | } |
448 | |
449 | |
450 | |
451 | |
452 | |
453 | template <typename T> Matcher<T> unconditionalConvertTo() const; |
454 | |
455 | private: |
456 | DynTypedMatcher(ast_type_traits::ASTNodeKind SupportedKind, |
457 | ast_type_traits::ASTNodeKind RestrictKind, |
458 | IntrusiveRefCntPtr<DynMatcherInterface> Implementation) |
459 | : SupportedKind(SupportedKind), RestrictKind(RestrictKind), |
460 | Implementation(std::move(Implementation)) {} |
461 | |
462 | bool AllowBind = false; |
463 | ast_type_traits::ASTNodeKind SupportedKind; |
464 | |
465 | |
466 | |
467 | |
468 | |
469 | ast_type_traits::ASTNodeKind RestrictKind; |
470 | IntrusiveRefCntPtr<DynMatcherInterface> Implementation; |
471 | }; |
472 | |
473 | |
474 | |
475 | |
476 | |
477 | template <typename T> |
478 | class WrapperMatcherInterface : public MatcherInterface<T> { |
479 | protected: |
480 | explicit WrapperMatcherInterface(DynTypedMatcher &&InnerMatcher) |
481 | : InnerMatcher(std::move(InnerMatcher)) {} |
482 | |
483 | const DynTypedMatcher InnerMatcher; |
484 | }; |
485 | |
486 | |
487 | |
488 | |
489 | |
490 | |
491 | |
492 | |
493 | |
494 | template <typename T> |
495 | class Matcher { |
496 | public: |
497 | |
498 | explicit Matcher(MatcherInterface<T> *Implementation) |
499 | : Implementation(Implementation) {} |
500 | |
501 | |
502 | |
503 | |
504 | template <typename From> |
505 | Matcher(const Matcher<From> &Other, |
506 | typename std::enable_if<std::is_base_of<From, T>::value && |
507 | !std::is_same<From, T>::value>::type * = nullptr) |
508 | : Implementation(restrictMatcher(Other.Implementation)) { |
509 | ())", "/home/seafit/code_projects/clang_source/clang/include/clang/ASTMatchers/ASTMatchersInternal.h", 510, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Implementation.getSupportedKind().isSame( |
510 | ())", "/home/seafit/code_projects/clang_source/clang/include/clang/ASTMatchers/ASTMatchersInternal.h", 510, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> ast_type_traits::ASTNodeKind::getFromNodeKind<T>())); |
511 | } |
512 | |
513 | |
514 | |
515 | |
516 | template <typename TypeT> |
517 | Matcher(const Matcher<TypeT> &Other, |
518 | typename std::enable_if< |
519 | std::is_same<T, QualType>::value && |
520 | std::is_same<TypeT, Type>::value>::type* = nullptr) |
521 | : Implementation(new TypeToQualType<TypeT>(Other)) {} |
522 | |
523 | |
524 | |
525 | |
526 | template <typename To> |
527 | Matcher<To> dynCastTo() const { |
528 | static_assert(std::is_base_of<To, T>::value, "Invalid dynCast call."); |
529 | return Matcher<To>(Implementation); |
530 | } |
531 | |
532 | |
533 | bool matches(const T &Node, |
534 | ASTMatchFinder *Finder, |
535 | BoundNodesTreeBuilder *Builder) const { |
536 | return Implementation.matches(ast_type_traits::DynTypedNode::create(Node), |
537 | Finder, Builder); |
538 | } |
539 | |
540 | |
541 | DynTypedMatcher::MatcherIDType getID() const { |
542 | return Implementation.getID(); |
543 | } |
544 | |
545 | |
546 | |
547 | |
548 | |
549 | operator DynTypedMatcher() const { return Implementation; } |
550 | |
551 | |
552 | |
553 | |
554 | |
555 | |
556 | |
557 | template <typename TypeT> |
558 | class TypeToQualType : public WrapperMatcherInterface<QualType> { |
559 | public: |
560 | TypeToQualType(const Matcher<TypeT> &InnerMatcher) |
561 | : TypeToQualType::WrapperMatcherInterface(InnerMatcher) {} |
562 | |
563 | bool matches(const QualType &Node, ASTMatchFinder *Finder, |
564 | BoundNodesTreeBuilder *Builder) const override { |
565 | if (Node.isNull()) |
566 | return false; |
567 | return this->InnerMatcher.matches( |
568 | ast_type_traits::DynTypedNode::create(*Node), Finder, Builder); |
569 | } |
570 | }; |
571 | |
572 | private: |
573 | |
574 | template <typename U> friend class Matcher; |
575 | |
576 | |
577 | friend class DynTypedMatcher; |
578 | |
579 | static DynTypedMatcher restrictMatcher(const DynTypedMatcher &Other) { |
580 | return Other.dynCastTo(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()); |
581 | } |
582 | |
583 | explicit Matcher(const DynTypedMatcher &Implementation) |
584 | : Implementation(restrictMatcher(Implementation)) { |
585 | Implementation.getSupportedKind() .isSame(ast_type_traits..ASTNodeKind..getFromNodeKind())", "/home/seafit/code_projects/clang_source/clang/include/clang/ASTMatchers/ASTMatchersInternal.h", 586, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(this->Implementation.getSupportedKind() |
586 | Implementation.getSupportedKind() .isSame(ast_type_traits..ASTNodeKind..getFromNodeKind())", "/home/seafit/code_projects/clang_source/clang/include/clang/ASTMatchers/ASTMatchersInternal.h", 586, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> .isSame(ast_type_traits::ASTNodeKind::getFromNodeKind<T>())); |
587 | } |
588 | |
589 | DynTypedMatcher Implementation; |
590 | }; |
591 | |
592 | |
593 | |
594 | template <typename T> |
595 | inline Matcher<T> makeMatcher(MatcherInterface<T> *Implementation) { |
596 | return Matcher<T>(Implementation); |
597 | } |
598 | |
599 | |
600 | |
601 | |
602 | |
603 | template <> |
604 | inline Matcher<QualType> DynTypedMatcher::convertTo<QualType>() const { |
605 | ()", "/home/seafit/code_projects/clang_source/clang/include/clang/ASTMatchers/ASTMatchersInternal.h", 605, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(canConvertTo<QualType>()); |
606 | const ast_type_traits::ASTNodeKind SourceKind = getSupportedKind(); |
607 | if (SourceKind.isSame( |
608 | ast_type_traits::ASTNodeKind::getFromNodeKind<Type>())) { |
609 | |
610 | return unconditionalConvertTo<Type>(); |
611 | } |
612 | return unconditionalConvertTo<QualType>(); |
613 | } |
614 | |
615 | |
616 | template <typename MatcherT, typename IteratorT> |
617 | bool matchesFirstInRange(const MatcherT &Matcher, IteratorT Start, |
618 | IteratorT End, ASTMatchFinder *Finder, |
619 | BoundNodesTreeBuilder *Builder) { |
620 | for (IteratorT I = Start; I != End; ++I) { |
621 | BoundNodesTreeBuilder Result(*Builder); |
622 | if (Matcher.matches(*I, Finder, &Result)) { |
623 | *Builder = std::move(Result); |
624 | return true; |
625 | } |
626 | } |
627 | return false; |
628 | } |
629 | |
630 | |
631 | |
632 | template <typename MatcherT, typename IteratorT> |
633 | bool matchesFirstInPointerRange(const MatcherT &Matcher, IteratorT Start, |
634 | IteratorT End, ASTMatchFinder *Finder, |
635 | BoundNodesTreeBuilder *Builder) { |
636 | for (IteratorT I = Start; I != End; ++I) { |
637 | BoundNodesTreeBuilder Result(*Builder); |
638 | if (Matcher.matches(**I, Finder, &Result)) { |
639 | *Builder = std::move(Result); |
640 | return true; |
641 | } |
642 | } |
643 | return false; |
644 | } |
645 | |
646 | |
647 | template <typename Ty> |
648 | class has_getDecl { |
649 | using yes = char[1]; |
650 | using no = char[2]; |
651 | |
652 | template <typename Inner> |
653 | static yes& test(Inner *I, decltype(I->getDecl()) * = nullptr); |
654 | |
655 | template <typename> |
656 | static no& test(...); |
657 | |
658 | public: |
659 | static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes); |
660 | }; |
661 | |
662 | |
663 | |
664 | |
665 | |
666 | template <typename T, typename ArgT> |
667 | class HasOverloadedOperatorNameMatcher : public SingleNodeMatcherInterface<T> { |
668 | static_assert(std::is_same<T, CXXOperatorCallExpr>::value || |
669 | std::is_base_of<FunctionDecl, T>::value, |
670 | "unsupported class for matcher"); |
671 | static_assert(std::is_same<ArgT, StringRef>::value, |
672 | "argument type must be StringRef"); |
673 | |
674 | public: |
675 | explicit HasOverloadedOperatorNameMatcher(const StringRef Name) |
676 | : SingleNodeMatcherInterface<T>(), Name(Name) {} |
677 | |
678 | bool matchesNode(const T &Node) const override { |
679 | return matchesSpecialized(Node); |
680 | } |
681 | |
682 | private: |
683 | |
684 | |
685 | |
686 | |
687 | bool matchesSpecialized(const CXXOperatorCallExpr &Node) const { |
688 | return getOperatorSpelling(Node.getOperator()) == Name; |
689 | } |
690 | |
691 | |
692 | |
693 | bool matchesSpecialized(const FunctionDecl &Node) const { |
694 | return Node.isOverloadedOperator() && |
695 | getOperatorSpelling(Node.getOverloadedOperator()) == Name; |
696 | } |
697 | |
698 | std::string Name; |
699 | }; |
700 | |
701 | |
702 | |
703 | |
704 | class HasNameMatcher : public SingleNodeMatcherInterface<NamedDecl> { |
705 | public: |
706 | explicit HasNameMatcher(std::vector<std::string> Names); |
707 | |
708 | bool matchesNode(const NamedDecl &Node) const override; |
709 | |
710 | private: |
711 | |
712 | |
713 | |
714 | |
715 | bool matchesNodeUnqualified(const NamedDecl &Node) const; |
716 | |
717 | |
718 | |
719 | |
720 | |
721 | |
722 | |
723 | bool matchesNodeFullFast(const NamedDecl &Node) const; |
724 | |
725 | |
726 | |
727 | |
728 | |
729 | |
730 | bool matchesNodeFullSlow(const NamedDecl &Node) const; |
731 | |
732 | const bool UseUnqualifiedMatch; |
733 | const std::vector<std::string> Names; |
734 | }; |
735 | |
736 | |
737 | |
738 | Matcher<NamedDecl> hasAnyNameFunc(ArrayRef<const StringRef *> NameRefs); |
739 | |
740 | |
741 | |
742 | Matcher<ObjCMessageExpr> hasAnySelectorFunc( |
743 | ArrayRef<const StringRef *> NameRefs); |
744 | |
745 | |
746 | |
747 | |
748 | |
749 | template <typename T, typename DeclMatcherT> |
750 | class HasDeclarationMatcher : public WrapperMatcherInterface<T> { |
751 | static_assert(std::is_same<DeclMatcherT, Matcher<Decl>>::value, |
752 | "instantiated with wrong types"); |
753 | |
754 | public: |
755 | explicit HasDeclarationMatcher(const Matcher<Decl> &InnerMatcher) |
756 | : HasDeclarationMatcher::WrapperMatcherInterface(InnerMatcher) {} |
757 | |
758 | bool matches(const T &Node, ASTMatchFinder *Finder, |
759 | BoundNodesTreeBuilder *Builder) const override { |
760 | return matchesSpecialized(Node, Finder, Builder); |
761 | } |
762 | |
763 | private: |
764 | |
765 | bool matchesSpecialized(const QualType &Node, ASTMatchFinder *Finder, |
766 | BoundNodesTreeBuilder *Builder) const { |
767 | if (Node.isNull()) |
768 | return false; |
769 | |
770 | return matchesSpecialized(*Node, Finder, Builder); |
771 | } |
772 | |
773 | |
774 | |
775 | bool matchesSpecialized(const Type &Node, ASTMatchFinder *Finder, |
776 | BoundNodesTreeBuilder *Builder) const { |
777 | |
778 | |
779 | const Type *EffectiveType = &Node; |
780 | if (const auto *S = dyn_cast<DeducedType>(&Node)) { |
781 | EffectiveType = S->getDeducedType().getTypePtrOrNull(); |
782 | if (!EffectiveType) |
783 | return false; |
784 | } |
785 | |
786 | |
787 | |
788 | if (const auto *S = dyn_cast<TagType>(EffectiveType)) { |
789 | return matchesDecl(S->getDecl(), Finder, Builder); |
790 | } |
791 | if (const auto *S = dyn_cast<InjectedClassNameType>(EffectiveType)) { |
792 | return matchesDecl(S->getDecl(), Finder, Builder); |
793 | } |
794 | if (const auto *S = dyn_cast<TemplateTypeParmType>(EffectiveType)) { |
795 | return matchesDecl(S->getDecl(), Finder, Builder); |
796 | } |
797 | if (const auto *S = dyn_cast<TypedefType>(EffectiveType)) { |
798 | return matchesDecl(S->getDecl(), Finder, Builder); |
799 | } |
800 | if (const auto *S = dyn_cast<UnresolvedUsingType>(EffectiveType)) { |
801 | return matchesDecl(S->getDecl(), Finder, Builder); |
802 | } |
803 | if (const auto *S = dyn_cast<ObjCObjectType>(EffectiveType)) { |
804 | return matchesDecl(S->getInterface(), Finder, Builder); |
805 | } |
806 | |
807 | |
808 | |
809 | |
810 | |
811 | |
812 | |
813 | |
814 | |
815 | if (const auto *S = dyn_cast<SubstTemplateTypeParmType>(EffectiveType)) { |
816 | return matchesSpecialized(S->getReplacementType(), Finder, Builder); |
817 | } |
818 | |
819 | |
820 | |
821 | |
822 | if (const auto *S = dyn_cast<TemplateSpecializationType>(EffectiveType)) { |
823 | if (!S->isTypeAlias() && S->isSugared()) { |
824 | |
825 | |
826 | |
827 | |
828 | |
829 | |
830 | return matchesSpecialized(*S->desugar(), Finder, Builder); |
831 | } |
832 | |
833 | |
834 | return matchesDecl(S->getTemplateName().getAsTemplateDecl(), Finder, |
835 | Builder); |
836 | } |
837 | |
838 | |
839 | |
840 | |
841 | if (const auto *S = dyn_cast<ElaboratedType>(EffectiveType)) { |
842 | return matchesSpecialized(S->desugar(), Finder, Builder); |
843 | } |
844 | return false; |
845 | } |
846 | |
847 | |
848 | |
849 | bool matchesSpecialized(const DeclRefExpr &Node, ASTMatchFinder *Finder, |
850 | BoundNodesTreeBuilder *Builder) const { |
851 | return matchesDecl(Node.getDecl(), Finder, Builder); |
852 | } |
853 | |
854 | |
855 | |
856 | bool matchesSpecialized(const CallExpr &Node, ASTMatchFinder *Finder, |
857 | BoundNodesTreeBuilder *Builder) const { |
858 | return matchesDecl(Node.getCalleeDecl(), Finder, Builder); |
859 | } |
860 | |
861 | |
862 | |
863 | bool matchesSpecialized(const CXXConstructExpr &Node, |
864 | ASTMatchFinder *Finder, |
865 | BoundNodesTreeBuilder *Builder) const { |
866 | return matchesDecl(Node.getConstructor(), Finder, Builder); |
867 | } |
868 | |
869 | bool matchesSpecialized(const ObjCIvarRefExpr &Node, |
870 | ASTMatchFinder *Finder, |
871 | BoundNodesTreeBuilder *Builder) const { |
872 | return matchesDecl(Node.getDecl(), Finder, Builder); |
873 | } |
874 | |
875 | |
876 | |
877 | bool matchesSpecialized(const CXXNewExpr &Node, |
878 | ASTMatchFinder *Finder, |
879 | BoundNodesTreeBuilder *Builder) const { |
880 | return matchesDecl(Node.getOperatorNew(), Finder, Builder); |
881 | } |
882 | |
883 | |
884 | |
885 | bool matchesSpecialized(const MemberExpr &Node, |
886 | ASTMatchFinder *Finder, |
887 | BoundNodesTreeBuilder *Builder) const { |
888 | return matchesDecl(Node.getMemberDecl(), Finder, Builder); |
889 | } |
890 | |
891 | |
892 | |
893 | bool matchesSpecialized(const AddrLabelExpr &Node, |
894 | ASTMatchFinder *Finder, |
895 | BoundNodesTreeBuilder *Builder) const { |
896 | return matchesDecl(Node.getLabel(), Finder, Builder); |
897 | } |
898 | |
899 | |
900 | |
901 | bool matchesSpecialized(const LabelStmt &Node, ASTMatchFinder *Finder, |
902 | BoundNodesTreeBuilder *Builder) const { |
903 | return matchesDecl(Node.getDecl(), Finder, Builder); |
904 | } |
905 | |
906 | |
907 | |
908 | bool matchesDecl(const Decl *Node, ASTMatchFinder *Finder, |
909 | BoundNodesTreeBuilder *Builder) const { |
910 | return Node != nullptr && |
911 | this->InnerMatcher.matches( |
912 | ast_type_traits::DynTypedNode::create(*Node), Finder, Builder); |
913 | } |
914 | }; |
915 | |
916 | |
917 | |
918 | template <typename T> |
919 | struct IsBaseType { |
920 | static const bool value = |
921 | std::is_same<T, Decl>::value || |
922 | std::is_same<T, Stmt>::value || |
923 | std::is_same<T, QualType>::value || |
924 | std::is_same<T, Type>::value || |
925 | std::is_same<T, TypeLoc>::value || |
926 | std::is_same<T, NestedNameSpecifier>::value || |
927 | std::is_same<T, NestedNameSpecifierLoc>::value || |
928 | std::is_same<T, CXXCtorInitializer>::value; |
929 | }; |
930 | template <typename T> |
931 | const bool IsBaseType<T>::value; |
932 | |
933 | |
934 | |
935 | |
936 | |
937 | |
938 | |
939 | |
940 | |
941 | |
942 | |
943 | |
944 | |
945 | |
946 | |
947 | |
948 | |
949 | |
950 | |
951 | class ASTMatchFinder { |
952 | public: |
953 | |
954 | |
955 | enum TraversalKind { |
956 | |
957 | TK_AsIs, |
958 | |
959 | |
960 | TK_IgnoreImplicitCastsAndParentheses |
961 | }; |
962 | |
963 | |
964 | enum BindKind { |
965 | |
966 | BK_First, |
967 | |
968 | |
969 | BK_All |
970 | }; |
971 | |
972 | |
973 | enum AncestorMatchMode { |
974 | |
975 | AMM_All, |
976 | |
977 | |
978 | AMM_ParentOnly |
979 | }; |
980 | |
981 | virtual ~ASTMatchFinder() = default; |
982 | |
983 | |
984 | |
985 | |
986 | |
987 | virtual bool classIsDerivedFrom(const CXXRecordDecl *Declaration, |
988 | const Matcher<NamedDecl> &Base, |
989 | BoundNodesTreeBuilder *Builder) = 0; |
990 | |
991 | template <typename T> |
992 | bool matchesChildOf(const T &Node, |
993 | const DynTypedMatcher &Matcher, |
994 | BoundNodesTreeBuilder *Builder, |
995 | TraversalKind Traverse, |
996 | BindKind Bind) { |
997 | static_assert(std::is_base_of<Decl, T>::value || |
998 | std::is_base_of<Stmt, T>::value || |
999 | std::is_base_of<NestedNameSpecifier, T>::value || |
1000 | std::is_base_of<NestedNameSpecifierLoc, T>::value || |
1001 | std::is_base_of<TypeLoc, T>::value || |
1002 | std::is_base_of<QualType, T>::value, |
1003 | "unsupported type for recursive matching"); |
1004 | return matchesChildOf(ast_type_traits::DynTypedNode::create(Node), |
1005 | Matcher, Builder, Traverse, Bind); |
1006 | } |
1007 | |
1008 | template <typename T> |
1009 | bool matchesDescendantOf(const T &Node, |
1010 | const DynTypedMatcher &Matcher, |
1011 | BoundNodesTreeBuilder *Builder, |
1012 | BindKind Bind) { |
1013 | static_assert(std::is_base_of<Decl, T>::value || |
1014 | std::is_base_of<Stmt, T>::value || |
1015 | std::is_base_of<NestedNameSpecifier, T>::value || |
1016 | std::is_base_of<NestedNameSpecifierLoc, T>::value || |
1017 | std::is_base_of<TypeLoc, T>::value || |
1018 | std::is_base_of<QualType, T>::value, |
1019 | "unsupported type for recursive matching"); |
1020 | return matchesDescendantOf(ast_type_traits::DynTypedNode::create(Node), |
1021 | Matcher, Builder, Bind); |
1022 | } |
1023 | |
1024 | |
1025 | template <typename T> |
1026 | bool matchesAncestorOf(const T &Node, |
1027 | const DynTypedMatcher &Matcher, |
1028 | BoundNodesTreeBuilder *Builder, |
1029 | AncestorMatchMode MatchMode) { |
1030 | static_assert(std::is_base_of<Decl, T>::value || |
1031 | std::is_base_of<NestedNameSpecifierLoc, T>::value || |
1032 | std::is_base_of<Stmt, T>::value || |
1033 | std::is_base_of<TypeLoc, T>::value, |
1034 | "type not allowed for recursive matching"); |
1035 | return matchesAncestorOf(ast_type_traits::DynTypedNode::create(Node), |
1036 | Matcher, Builder, MatchMode); |
1037 | } |
1038 | |
1039 | virtual ASTContext &getASTContext() const = 0; |
1040 | |
1041 | protected: |
1042 | virtual bool matchesChildOf(const ast_type_traits::DynTypedNode &Node, |
1043 | const DynTypedMatcher &Matcher, |
1044 | BoundNodesTreeBuilder *Builder, |
1045 | TraversalKind Traverse, |
1046 | BindKind Bind) = 0; |
1047 | |
1048 | virtual bool matchesDescendantOf(const ast_type_traits::DynTypedNode &Node, |
1049 | const DynTypedMatcher &Matcher, |
1050 | BoundNodesTreeBuilder *Builder, |
1051 | BindKind Bind) = 0; |
1052 | |
1053 | virtual bool matchesAncestorOf(const ast_type_traits::DynTypedNode &Node, |
1054 | const DynTypedMatcher &Matcher, |
1055 | BoundNodesTreeBuilder *Builder, |
1056 | AncestorMatchMode MatchMode) = 0; |
1057 | }; |
1058 | |
1059 | |
1060 | |
1061 | |
1062 | |
1063 | template <typename... Ts> struct TypeList {}; |
1064 | |
1065 | template <typename T1, typename... Ts> struct TypeList<T1, Ts...> { |
1066 | |
1067 | using head = T1; |
1068 | |
1069 | |
1070 | |
1071 | |
1072 | |
1073 | using tail = TypeList<Ts...>; |
1074 | }; |
1075 | |
1076 | |
1077 | using EmptyTypeList = TypeList<>; |
1078 | |
1079 | |
1080 | |
1081 | template <typename AnyTypeList, typename T> |
1082 | struct TypeListContainsSuperOf { |
1083 | static const bool value = |
1084 | std::is_base_of<typename AnyTypeList::head, T>::value || |
1085 | TypeListContainsSuperOf<typename AnyTypeList::tail, T>::value; |
1086 | }; |
1087 | template <typename T> |
1088 | struct TypeListContainsSuperOf<EmptyTypeList, T> { |
1089 | static const bool value = false; |
1090 | }; |
1091 | |
1092 | |
1093 | |
1094 | |
1095 | using AllNodeBaseTypes = |
1096 | TypeList<Decl, Stmt, NestedNameSpecifier, NestedNameSpecifierLoc, QualType, |
1097 | Type, TypeLoc, CXXCtorInitializer>; |
1098 | |
1099 | |
1100 | |
1101 | |
1102 | |
1103 | template <class T> struct ; |
1104 | template <class T> struct <void(T)> { |
1105 | using = T; |
1106 | }; |
1107 | |
1108 | |
1109 | using AdaptativeDefaultFromTypes = AllNodeBaseTypes; |
1110 | using AdaptativeDefaultToTypes = |
1111 | TypeList<Decl, Stmt, NestedNameSpecifier, NestedNameSpecifierLoc, TypeLoc, |
1112 | QualType>; |
1113 | |
1114 | |
1115 | using HasDeclarationSupportedTypes = |
1116 | TypeList<CallExpr, CXXConstructExpr, CXXNewExpr, DeclRefExpr, EnumType, |
1117 | ElaboratedType, InjectedClassNameType, LabelStmt, AddrLabelExpr, |
1118 | MemberExpr, QualType, RecordType, TagType, |
1119 | TemplateSpecializationType, TemplateTypeParmType, TypedefType, |
1120 | UnresolvedUsingType, ObjCIvarRefExpr>; |
1121 | |
1122 | |
1123 | |
1124 | |
1125 | |
1126 | |
1127 | |
1128 | |
1129 | |
1130 | |
1131 | |
1132 | |
1133 | |
1134 | |
1135 | template <template <typename ToArg, typename FromArg> class ArgumentAdapterT, |
1136 | typename FromTypes = AdaptativeDefaultFromTypes, |
1137 | typename ToTypes = AdaptativeDefaultToTypes> |
1138 | struct ArgumentAdaptingMatcherFunc { |
1139 | template <typename T> class Adaptor { |
1140 | public: |
1141 | explicit Adaptor(const Matcher<T> &InnerMatcher) |
1142 | : InnerMatcher(InnerMatcher) {} |
1143 | |
1144 | using ReturnTypes = ToTypes; |
1145 | |
1146 | template <typename To> operator Matcher<To>() const { |
1147 | return Matcher<To>(new ArgumentAdapterT<To, T>(InnerMatcher)); |
1148 | } |
1149 | |
1150 | private: |
1151 | const Matcher<T> InnerMatcher; |
1152 | }; |
1153 | |
1154 | template <typename T> |
1155 | static Adaptor<T> create(const Matcher<T> &InnerMatcher) { |
1156 | return Adaptor<T>(InnerMatcher); |
1157 | } |
1158 | |
1159 | template <typename T> |
1160 | Adaptor<T> operator()(const Matcher<T> &InnerMatcher) const { |
1161 | return create(InnerMatcher); |
1162 | } |
1163 | }; |
1164 | |
1165 | |
1166 | |
1167 | |
1168 | |
1169 | |
1170 | |
1171 | |
1172 | |
1173 | |
1174 | |
1175 | |
1176 | |
1177 | template <template <typename T> class MatcherT, |
1178 | typename ReturnTypesF = void(AllNodeBaseTypes)> |
1179 | class PolymorphicMatcherWithParam0 { |
1180 | public: |
1181 | using ReturnTypes = typename ExtractFunctionArgMeta<ReturnTypesF>::type; |
1182 | |
1183 | template <typename T> |
1184 | operator Matcher<T>() const { |
1185 | static_assert(TypeListContainsSuperOf<ReturnTypes, T>::value, |
1186 | "right polymorphic conversion"); |
1187 | return Matcher<T>(new MatcherT<T>()); |
1188 | } |
1189 | }; |
1190 | |
1191 | template <template <typename T, typename P1> class MatcherT, |
1192 | typename P1, |
1193 | typename ReturnTypesF = void(AllNodeBaseTypes)> |
1194 | class PolymorphicMatcherWithParam1 { |
1195 | public: |
1196 | explicit PolymorphicMatcherWithParam1(const P1 &Param1) |
1197 | : Param1(Param1) {} |
1198 | |
1199 | using ReturnTypes = typename ExtractFunctionArgMeta<ReturnTypesF>::type; |
1200 | |
1201 | template <typename T> |
1202 | operator Matcher<T>() const { |
1203 | static_assert(TypeListContainsSuperOf<ReturnTypes, T>::value, |
1204 | "right polymorphic conversion"); |
1205 | return Matcher<T>(new MatcherT<T, P1>(Param1)); |
1206 | } |
1207 | |
1208 | private: |
1209 | const P1 Param1; |
1210 | }; |
1211 | |
1212 | template <template <typename T, typename P1, typename P2> class MatcherT, |
1213 | typename P1, typename P2, |
1214 | typename ReturnTypesF = void(AllNodeBaseTypes)> |
1215 | class PolymorphicMatcherWithParam2 { |
1216 | public: |
1217 | PolymorphicMatcherWithParam2(const P1 &Param1, const P2 &Param2) |
1218 | : Param1(Param1), Param2(Param2) {} |
1219 | |
1220 | using ReturnTypes = typename ExtractFunctionArgMeta<ReturnTypesF>::type; |
1221 | |
1222 | template <typename T> |
1223 | operator Matcher<T>() const { |
1224 | static_assert(TypeListContainsSuperOf<ReturnTypes, T>::value, |
1225 | "right polymorphic conversion"); |
1226 | return Matcher<T>(new MatcherT<T, P1, P2>(Param1, Param2)); |
1227 | } |
1228 | |
1229 | private: |
1230 | const P1 Param1; |
1231 | const P2 Param2; |
1232 | }; |
1233 | |
1234 | |
1235 | |
1236 | |
1237 | |
1238 | class TrueMatcher { |
1239 | public: |
1240 | using ReturnTypes = AllNodeBaseTypes; |
1241 | |
1242 | template <typename T> |
1243 | operator Matcher<T>() const { |
1244 | return DynTypedMatcher::trueMatcher( |
1245 | ast_type_traits::ASTNodeKind::getFromNodeKind<T>()) |
1246 | .template unconditionalConvertTo<T>(); |
1247 | } |
1248 | }; |
1249 | |
1250 | |
1251 | |
1252 | |
1253 | |
1254 | template <typename T> |
1255 | class BindableMatcher : public Matcher<T> { |
1256 | public: |
1257 | explicit BindableMatcher(const Matcher<T> &M) : Matcher<T>(M) {} |
1258 | explicit BindableMatcher(MatcherInterface<T> *Implementation) |
1259 | : Matcher<T>(Implementation) {} |
1260 | |
1261 | |
1262 | |
1263 | |
1264 | |
1265 | Matcher<T> bind(StringRef ID) const { |
1266 | return DynTypedMatcher(*this) |
1267 | .tryBind(ID) |
1268 | ->template unconditionalConvertTo<T>(); |
1269 | } |
1270 | |
1271 | |
1272 | |
1273 | operator DynTypedMatcher() const { |
1274 | DynTypedMatcher Result = static_cast<const Matcher<T>&>(*this); |
1275 | Result.setAllowBind(true); |
1276 | return Result; |
1277 | } |
1278 | }; |
1279 | |
1280 | |
1281 | |
1282 | |
1283 | |
1284 | template <typename T, typename ChildT> |
1285 | class HasMatcher : public WrapperMatcherInterface<T> { |
1286 | public: |
1287 | explicit HasMatcher(const Matcher<ChildT> &ChildMatcher) |
1288 | : HasMatcher::WrapperMatcherInterface(ChildMatcher) {} |
1289 | |
1290 | bool matches(const T &Node, ASTMatchFinder *Finder, |
1291 | BoundNodesTreeBuilder *Builder) const override { |
1292 | return Finder->matchesChildOf(Node, this->InnerMatcher, Builder, |
1293 | ASTMatchFinder::TK_AsIs, |
1294 | ASTMatchFinder::BK_First); |
1295 | } |
1296 | }; |
1297 | |
1298 | |
1299 | |
1300 | |
1301 | |
1302 | |
1303 | template <typename T, typename ChildT> |
1304 | class ForEachMatcher : public WrapperMatcherInterface<T> { |
1305 | static_assert(IsBaseType<ChildT>::value, |
1306 | "for each only accepts base type matcher"); |
1307 | |
1308 | public: |
1309 | explicit ForEachMatcher(const Matcher<ChildT> &ChildMatcher) |
1310 | : ForEachMatcher::WrapperMatcherInterface(ChildMatcher) {} |
1311 | |
1312 | bool matches(const T& Node, ASTMatchFinder* Finder, |
1313 | BoundNodesTreeBuilder* Builder) const override { |
1314 | return Finder->matchesChildOf( |
1315 | Node, this->InnerMatcher, Builder, |
1316 | ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses, |
1317 | ASTMatchFinder::BK_All); |
1318 | } |
1319 | }; |
1320 | |
1321 | |
1322 | |
1323 | |
1324 | |
1325 | |
1326 | |
1327 | |
1328 | |
1329 | |
1330 | template <typename... Ps> class VariadicOperatorMatcher { |
1331 | public: |
1332 | VariadicOperatorMatcher(DynTypedMatcher::VariadicOperator Op, Ps &&... Params) |
1333 | : Op(Op), Params(std::forward<Ps>(Params)...) {} |
1334 | |
1335 | template <typename T> operator Matcher<T>() const { |
1336 | return DynTypedMatcher::constructVariadic( |
1337 | Op, ast_type_traits::ASTNodeKind::getFromNodeKind<T>(), |
1338 | getMatchers<T>(llvm::index_sequence_for<Ps...>())) |
1339 | .template unconditionalConvertTo<T>(); |
1340 | } |
1341 | |
1342 | private: |
1343 | |
1344 | template <typename T, std::size_t... Is> |
1345 | std::vector<DynTypedMatcher> getMatchers(llvm::index_sequence<Is...>) const { |
1346 | return {Matcher<T>(std::get<Is>(Params))...}; |
1347 | } |
1348 | |
1349 | const DynTypedMatcher::VariadicOperator Op; |
1350 | std::tuple<Ps...> Params; |
1351 | }; |
1352 | |
1353 | |
1354 | |
1355 | template <unsigned MinCount, unsigned MaxCount> |
1356 | struct VariadicOperatorMatcherFunc { |
1357 | DynTypedMatcher::VariadicOperator Op; |
1358 | |
1359 | template <typename... Ms> |
1360 | VariadicOperatorMatcher<Ms...> operator()(Ms &&... Ps) const { |
1361 | static_assert(MinCount <= sizeof...(Ms) && sizeof...(Ms) <= MaxCount, |
1362 | "invalid number of parameters for variadic matcher"); |
1363 | return VariadicOperatorMatcher<Ms...>(Op, std::forward<Ms>(Ps)...); |
1364 | } |
1365 | }; |
1366 | |
1367 | |
1368 | |
1369 | template <typename T> |
1370 | inline Matcher<T> DynTypedMatcher::unconditionalConvertTo() const { |
1371 | return Matcher<T>(*this); |
1372 | } |
1373 | |
1374 | |
1375 | template<typename T> |
1376 | BindableMatcher<T> makeAllOfComposite( |
1377 | ArrayRef<const Matcher<T> *> InnerMatchers) { |
1378 | |
1379 | if (InnerMatchers.empty()) { |
1380 | return BindableMatcher<T>(TrueMatcher()); |
1381 | } |
1382 | |
1383 | |
1384 | if (InnerMatchers.size() == 1) { |
1385 | return BindableMatcher<T>(*InnerMatchers[0]); |
1386 | } |
1387 | |
1388 | using PI = llvm::pointee_iterator<const Matcher<T> *const *>; |
1389 | |
1390 | std::vector<DynTypedMatcher> DynMatchers(PI(InnerMatchers.begin()), |
1391 | PI(InnerMatchers.end())); |
1392 | return BindableMatcher<T>( |
1393 | DynTypedMatcher::constructVariadic( |
1394 | DynTypedMatcher::VO_AllOf, |
1395 | ast_type_traits::ASTNodeKind::getFromNodeKind<T>(), |
1396 | std::move(DynMatchers)) |
1397 | .template unconditionalConvertTo<T>()); |
1398 | } |
1399 | |
1400 | |
1401 | |
1402 | |
1403 | |
1404 | |
1405 | |
1406 | template<typename T, typename InnerT> |
1407 | BindableMatcher<T> makeDynCastAllOfComposite( |
1408 | ArrayRef<const Matcher<InnerT> *> InnerMatchers) { |
1409 | return BindableMatcher<T>( |
1410 | makeAllOfComposite(InnerMatchers).template dynCastTo<T>()); |
1411 | } |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | |
1417 | template <typename T, typename DescendantT> |
1418 | class HasDescendantMatcher : public WrapperMatcherInterface<T> { |
1419 | static_assert(IsBaseType<DescendantT>::value, |
1420 | "has descendant only accepts base type matcher"); |
1421 | |
1422 | public: |
1423 | explicit HasDescendantMatcher(const Matcher<DescendantT> &DescendantMatcher) |
1424 | : HasDescendantMatcher::WrapperMatcherInterface(DescendantMatcher) {} |
1425 | |
1426 | bool matches(const T &Node, ASTMatchFinder *Finder, |
1427 | BoundNodesTreeBuilder *Builder) const override { |
1428 | return Finder->matchesDescendantOf(Node, this->InnerMatcher, Builder, |
1429 | ASTMatchFinder::BK_First); |
1430 | } |
1431 | }; |
1432 | |
1433 | |
1434 | |
1435 | |
1436 | |
1437 | template <typename T, typename ParentT> |
1438 | class HasParentMatcher : public WrapperMatcherInterface<T> { |
1439 | static_assert(IsBaseType<ParentT>::value, |
1440 | "has parent only accepts base type matcher"); |
1441 | |
1442 | public: |
1443 | explicit HasParentMatcher(const Matcher<ParentT> &ParentMatcher) |
1444 | : HasParentMatcher::WrapperMatcherInterface(ParentMatcher) {} |
1445 | |
1446 | bool matches(const T &Node, ASTMatchFinder *Finder, |
1447 | BoundNodesTreeBuilder *Builder) const override { |
1448 | return Finder->matchesAncestorOf(Node, this->InnerMatcher, Builder, |
1449 | ASTMatchFinder::AMM_ParentOnly); |
1450 | } |
1451 | }; |
1452 | |
1453 | |
1454 | |
1455 | |
1456 | |
1457 | template <typename T, typename AncestorT> |
1458 | class HasAncestorMatcher : public WrapperMatcherInterface<T> { |
1459 | static_assert(IsBaseType<AncestorT>::value, |
1460 | "has ancestor only accepts base type matcher"); |
1461 | |
1462 | public: |
1463 | explicit HasAncestorMatcher(const Matcher<AncestorT> &AncestorMatcher) |
1464 | : HasAncestorMatcher::WrapperMatcherInterface(AncestorMatcher) {} |
1465 | |
1466 | bool matches(const T &Node, ASTMatchFinder *Finder, |
1467 | BoundNodesTreeBuilder *Builder) const override { |
1468 | return Finder->matchesAncestorOf(Node, this->InnerMatcher, Builder, |
1469 | ASTMatchFinder::AMM_All); |
1470 | } |
1471 | }; |
1472 | |
1473 | |
1474 | |
1475 | |
1476 | |
1477 | |
1478 | |
1479 | template <typename T, typename DescendantT> |
1480 | class ForEachDescendantMatcher : public WrapperMatcherInterface<T> { |
1481 | static_assert(IsBaseType<DescendantT>::value, |
1482 | "for each descendant only accepts base type matcher"); |
1483 | |
1484 | public: |
1485 | explicit ForEachDescendantMatcher( |
1486 | const Matcher<DescendantT> &DescendantMatcher) |
1487 | : ForEachDescendantMatcher::WrapperMatcherInterface(DescendantMatcher) {} |
1488 | |
1489 | bool matches(const T &Node, ASTMatchFinder *Finder, |
1490 | BoundNodesTreeBuilder *Builder) const override { |
1491 | return Finder->matchesDescendantOf(Node, this->InnerMatcher, Builder, |
1492 | ASTMatchFinder::BK_All); |
1493 | } |
1494 | }; |
1495 | |
1496 | |
1497 | |
1498 | template <typename T, typename ValueT> |
1499 | class ValueEqualsMatcher : public SingleNodeMatcherInterface<T> { |
1500 | static_assert(std::is_base_of<CharacterLiteral, T>::value || |
1501 | std::is_base_of<CXXBoolLiteralExpr, T>::value || |
1502 | std::is_base_of<FloatingLiteral, T>::value || |
1503 | std::is_base_of<IntegerLiteral, T>::value, |
1504 | "the node must have a getValue method"); |
1505 | |
1506 | public: |
1507 | explicit ValueEqualsMatcher(const ValueT &ExpectedValue) |
1508 | : ExpectedValue(ExpectedValue) {} |
1509 | |
1510 | bool matchesNode(const T &Node) const override { |
1511 | return Node.getValue() == ExpectedValue; |
1512 | } |
1513 | |
1514 | private: |
1515 | const ValueT ExpectedValue; |
1516 | }; |
1517 | |
1518 | |
1519 | |
1520 | template <> |
1521 | inline bool ValueEqualsMatcher<FloatingLiteral, double>::matchesNode( |
1522 | const FloatingLiteral &Node) const { |
1523 | if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle()) |
1524 | return Node.getValue().convertToFloat() == ExpectedValue; |
1525 | if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble()) |
1526 | return Node.getValue().convertToDouble() == ExpectedValue; |
1527 | return false; |
1528 | } |
1529 | template <> |
1530 | inline bool ValueEqualsMatcher<FloatingLiteral, float>::matchesNode( |
1531 | const FloatingLiteral &Node) const { |
1532 | if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle()) |
1533 | return Node.getValue().convertToFloat() == ExpectedValue; |
1534 | if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble()) |
1535 | return Node.getValue().convertToDouble() == ExpectedValue; |
1536 | return false; |
1537 | } |
1538 | template <> |
1539 | inline bool ValueEqualsMatcher<FloatingLiteral, llvm::APFloat>::matchesNode( |
1540 | const FloatingLiteral &Node) const { |
1541 | return ExpectedValue.compare(Node.getValue()) == llvm::APFloat::cmpEqual; |
1542 | } |
1543 | |
1544 | |
1545 | |
1546 | |
1547 | |
1548 | |
1549 | |
1550 | |
1551 | |
1552 | |
1553 | |
1554 | |
1555 | template <typename SourceT, typename TargetT> |
1556 | class VariadicDynCastAllOfMatcher |
1557 | : public VariadicFunction<BindableMatcher<SourceT>, Matcher<TargetT>, |
1558 | makeDynCastAllOfComposite<SourceT, TargetT>> { |
1559 | public: |
1560 | VariadicDynCastAllOfMatcher() {} |
1561 | }; |
1562 | |
1563 | |
1564 | |
1565 | |
1566 | |
1567 | |
1568 | |
1569 | |
1570 | |
1571 | |
1572 | |
1573 | template <typename T> |
1574 | class VariadicAllOfMatcher |
1575 | : public VariadicFunction<BindableMatcher<T>, Matcher<T>, |
1576 | makeAllOfComposite<T>> { |
1577 | public: |
1578 | VariadicAllOfMatcher() {} |
1579 | }; |
1580 | |
1581 | |
1582 | |
1583 | template <typename TLoc, typename T> |
1584 | class LocMatcher : public WrapperMatcherInterface<TLoc> { |
1585 | public: |
1586 | explicit LocMatcher(const Matcher<T> &InnerMatcher) |
1587 | : LocMatcher::WrapperMatcherInterface(InnerMatcher) {} |
1588 | |
1589 | bool matches(const TLoc &Node, ASTMatchFinder *Finder, |
1590 | BoundNodesTreeBuilder *Builder) const override { |
1591 | if (!Node) |
1592 | return false; |
1593 | return this->InnerMatcher.matches(extract(Node), Finder, Builder); |
1594 | } |
1595 | |
1596 | private: |
1597 | static ast_type_traits::DynTypedNode |
1598 | (const NestedNameSpecifierLoc &Loc) { |
1599 | return ast_type_traits::DynTypedNode::create(*Loc.getNestedNameSpecifier()); |
1600 | } |
1601 | }; |
1602 | |
1603 | |
1604 | |
1605 | |
1606 | |
1607 | class TypeLocTypeMatcher : public WrapperMatcherInterface<TypeLoc> { |
1608 | public: |
1609 | explicit TypeLocTypeMatcher(const Matcher<QualType> &InnerMatcher) |
1610 | : TypeLocTypeMatcher::WrapperMatcherInterface(InnerMatcher) {} |
1611 | |
1612 | bool matches(const TypeLoc &Node, ASTMatchFinder *Finder, |
1613 | BoundNodesTreeBuilder *Builder) const override { |
1614 | if (!Node) |
1615 | return false; |
1616 | return this->InnerMatcher.matches( |
1617 | ast_type_traits::DynTypedNode::create(Node.getType()), Finder, Builder); |
1618 | } |
1619 | }; |
1620 | |
1621 | |
1622 | |
1623 | |
1624 | template <typename T> |
1625 | class TypeTraverseMatcher : public WrapperMatcherInterface<T> { |
1626 | public: |
1627 | explicit TypeTraverseMatcher(const Matcher<QualType> &InnerMatcher, |
1628 | QualType (T::*TraverseFunction)() const) |
1629 | : TypeTraverseMatcher::WrapperMatcherInterface(InnerMatcher), |
1630 | TraverseFunction(TraverseFunction) {} |
1631 | |
1632 | bool matches(const T &Node, ASTMatchFinder *Finder, |
1633 | BoundNodesTreeBuilder *Builder) const override { |
1634 | QualType NextNode = (Node.*TraverseFunction)(); |
1635 | if (NextNode.isNull()) |
1636 | return false; |
1637 | return this->InnerMatcher.matches( |
1638 | ast_type_traits::DynTypedNode::create(NextNode), Finder, Builder); |
1639 | } |
1640 | |
1641 | private: |
1642 | QualType (T::*TraverseFunction)() const; |
1643 | }; |
1644 | |
1645 | |
1646 | |
1647 | |
1648 | template <typename T> |
1649 | class TypeLocTraverseMatcher : public WrapperMatcherInterface<T> { |
1650 | public: |
1651 | explicit TypeLocTraverseMatcher(const Matcher<TypeLoc> &InnerMatcher, |
1652 | TypeLoc (T::*TraverseFunction)() const) |
1653 | : TypeLocTraverseMatcher::WrapperMatcherInterface(InnerMatcher), |
1654 | TraverseFunction(TraverseFunction) {} |
1655 | |
1656 | bool matches(const T &Node, ASTMatchFinder *Finder, |
1657 | BoundNodesTreeBuilder *Builder) const override { |
1658 | TypeLoc NextNode = (Node.*TraverseFunction)(); |
1659 | if (!NextNode) |
1660 | return false; |
1661 | return this->InnerMatcher.matches( |
1662 | ast_type_traits::DynTypedNode::create(NextNode), Finder, Builder); |
1663 | } |
1664 | |
1665 | private: |
1666 | TypeLoc (T::*TraverseFunction)() const; |
1667 | }; |
1668 | |
1669 | |
1670 | |
1671 | |
1672 | |
1673 | |
1674 | |
1675 | template <typename InnerTBase, |
1676 | template <typename OuterT> class Getter, |
1677 | template <typename OuterT> class MatcherImpl, |
1678 | typename ReturnTypesF> |
1679 | class TypeTraversePolymorphicMatcher { |
1680 | private: |
1681 | using Self = TypeTraversePolymorphicMatcher<InnerTBase, Getter, MatcherImpl, |
1682 | ReturnTypesF>; |
1683 | |
1684 | static Self create(ArrayRef<const Matcher<InnerTBase> *> InnerMatchers); |
1685 | |
1686 | public: |
1687 | using ReturnTypes = typename ExtractFunctionArgMeta<ReturnTypesF>::type; |
1688 | |
1689 | explicit TypeTraversePolymorphicMatcher( |
1690 | ArrayRef<const Matcher<InnerTBase> *> InnerMatchers) |
1691 | : InnerMatcher(makeAllOfComposite(InnerMatchers)) {} |
1692 | |
1693 | template <typename OuterT> operator Matcher<OuterT>() const { |
1694 | return Matcher<OuterT>( |
1695 | new MatcherImpl<OuterT>(InnerMatcher, Getter<OuterT>::value())); |
1696 | } |
1697 | |
1698 | struct Func |
1699 | : public VariadicFunction<Self, Matcher<InnerTBase>, &Self::create> { |
1700 | Func() {} |
1701 | }; |
1702 | |
1703 | private: |
1704 | const Matcher<InnerTBase> InnerMatcher; |
1705 | }; |
1706 | |
1707 | |
1708 | |
1709 | |
1710 | |
1711 | template <typename Matcher, Matcher (*Func)()> class MemoizedMatcher { |
1712 | struct Wrapper { |
1713 | Wrapper() : M(Func()) {} |
1714 | |
1715 | Matcher M; |
1716 | }; |
1717 | |
1718 | public: |
1719 | static const Matcher &getInstance() { |
1720 | static llvm::ManagedStatic<Wrapper> Instance; |
1721 | return Instance->M; |
1722 | } |
1723 | }; |
1724 | |
1725 | |
1726 | |
1727 | |
1728 | template <typename InnerTBase, template <typename OuterT> class Getter, |
1729 | template <typename OuterT> class MatcherImpl, typename ReturnTypesF> |
1730 | TypeTraversePolymorphicMatcher<InnerTBase, Getter, MatcherImpl, ReturnTypesF> |
1731 | TypeTraversePolymorphicMatcher< |
1732 | InnerTBase, Getter, MatcherImpl, |
1733 | ReturnTypesF>::create(ArrayRef<const Matcher<InnerTBase> *> InnerMatchers) { |
1734 | return Self(InnerMatchers); |
1735 | } |
1736 | |
1737 | |
1738 | |
1739 | inline ArrayRef<TemplateArgument> |
1740 | getTemplateSpecializationArgs(const ClassTemplateSpecializationDecl &D) { |
1741 | return D.getTemplateArgs().asArray(); |
1742 | } |
1743 | |
1744 | inline ArrayRef<TemplateArgument> |
1745 | getTemplateSpecializationArgs(const TemplateSpecializationType &T) { |
1746 | return llvm::makeArrayRef(T.getArgs(), T.getNumArgs()); |
1747 | } |
1748 | |
1749 | inline ArrayRef<TemplateArgument> |
1750 | getTemplateSpecializationArgs(const FunctionDecl &FD) { |
1751 | if (const auto* TemplateArgs = FD.getTemplateSpecializationArgs()) |
1752 | return TemplateArgs->asArray(); |
1753 | return ArrayRef<TemplateArgument>(); |
1754 | } |
1755 | |
1756 | struct NotEqualsBoundNodePredicate { |
1757 | bool operator()(const internal::BoundNodesMap &Nodes) const { |
1758 | return Nodes.getNode(ID) != Node; |
1759 | } |
1760 | |
1761 | std::string ID; |
1762 | ast_type_traits::DynTypedNode Node; |
1763 | }; |
1764 | |
1765 | template <typename Ty> |
1766 | struct GetBodyMatcher { |
1767 | static const Stmt *get(const Ty &Node) { |
1768 | return Node.getBody(); |
1769 | } |
1770 | }; |
1771 | |
1772 | template <> |
1773 | inline const Stmt *GetBodyMatcher<FunctionDecl>::get(const FunctionDecl &Node) { |
1774 | return Node.doesThisDeclarationHaveABody() ? Node.getBody() : nullptr; |
1775 | } |
1776 | |
1777 | template <typename Ty> |
1778 | struct HasSizeMatcher { |
1779 | static bool hasSize(const Ty &Node, unsigned int N) { |
1780 | return Node.getSize() == N; |
1781 | } |
1782 | }; |
1783 | |
1784 | template <> |
1785 | inline bool HasSizeMatcher<StringLiteral>::hasSize( |
1786 | const StringLiteral &Node, unsigned int N) { |
1787 | return Node.getLength() == N; |
1788 | } |
1789 | |
1790 | template <typename Ty> |
1791 | struct GetSourceExpressionMatcher { |
1792 | static const Expr *get(const Ty &Node) { |
1793 | return Node.getSubExpr(); |
1794 | } |
1795 | }; |
1796 | |
1797 | template <> |
1798 | inline const Expr *GetSourceExpressionMatcher<OpaqueValueExpr>::get( |
1799 | const OpaqueValueExpr &Node) { |
1800 | return Node.getSourceExpr(); |
1801 | } |
1802 | |
1803 | template <typename Ty> |
1804 | struct CompoundStmtMatcher { |
1805 | static const CompoundStmt *get(const Ty &Node) { |
1806 | return &Node; |
1807 | } |
1808 | }; |
1809 | |
1810 | template <> |
1811 | inline const CompoundStmt * |
1812 | CompoundStmtMatcher<StmtExpr>::get(const StmtExpr &Node) { |
1813 | return Node.getSubStmt(); |
1814 | } |
1815 | |
1816 | } |
1817 | |
1818 | } |
1819 | |
1820 | } |
1821 | |
1822 | #endif |
1823 | |