Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1 | //===--- ASTMatchersInternal.cpp - Structural query framework -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implements the base layer of the matcher framework. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 15 | #include "clang/ASTMatchers/ASTMatchersInternal.h" |
Samuel Benzaquen | 96039d7 | 2014-10-09 19:28:18 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ManagedStatic.h" |
Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 17 | |
| 18 | namespace clang { |
| 19 | namespace ast_matchers { |
| 20 | namespace internal { |
| 21 | |
Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 22 | void BoundNodesTreeBuilder::visitMatches(Visitor *ResultVisitor) { |
| 23 | if (Bindings.empty()) |
| 24 | Bindings.push_back(BoundNodesMap()); |
| 25 | for (unsigned i = 0, e = Bindings.size(); i != e; ++i) { |
| 26 | ResultVisitor->visitMatch(BoundNodes(Bindings[i])); |
Manuel Klimek | 021d56f | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | |
Samuel Benzaquen | f28d997 | 2014-10-01 15:08:07 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | class VariadicMatcher : public DynMatcherInterface { |
| 33 | public: |
| 34 | VariadicMatcher(VariadicOperatorFunction Func, |
| 35 | std::vector<DynTypedMatcher> InnerMatchers) |
| 36 | : Func(Func), InnerMatchers(std::move(InnerMatchers)) {} |
| 37 | |
| 38 | bool dynMatches(const ast_type_traits::DynTypedNode &DynNode, |
| 39 | ASTMatchFinder *Finder, |
| 40 | BoundNodesTreeBuilder *Builder) const override { |
| 41 | return Func(DynNode, Finder, Builder, InnerMatchers); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | VariadicOperatorFunction Func; |
| 46 | std::vector<DynTypedMatcher> InnerMatchers; |
| 47 | }; |
| 48 | |
| 49 | class IdDynMatcher : public DynMatcherInterface { |
| 50 | public: |
| 51 | IdDynMatcher(StringRef ID, |
| 52 | const IntrusiveRefCntPtr<DynMatcherInterface> &InnerMatcher) |
| 53 | : ID(ID), InnerMatcher(InnerMatcher) {} |
| 54 | |
| 55 | bool dynMatches(const ast_type_traits::DynTypedNode &DynNode, |
| 56 | ASTMatchFinder *Finder, |
| 57 | BoundNodesTreeBuilder *Builder) const override { |
| 58 | bool Result = InnerMatcher->dynMatches(DynNode, Finder, Builder); |
| 59 | if (Result) Builder->setBinding(ID, DynNode); |
| 60 | return Result; |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | const std::string ID; |
| 65 | const IntrusiveRefCntPtr<DynMatcherInterface> InnerMatcher; |
| 66 | }; |
| 67 | |
Samuel Benzaquen | 96039d7 | 2014-10-09 19:28:18 +0000 | [diff] [blame] | 68 | /// \brief A matcher that always returns true. |
| 69 | /// |
| 70 | /// We only ever need one instance of this matcher, so we create a global one |
| 71 | /// and reuse it to reduce the overhead of the matcher and increase the chance |
| 72 | /// of cache hits. |
| 73 | struct TrueMatcherImpl { |
| 74 | TrueMatcherImpl() : Instance(new Impl) {} |
| 75 | const IntrusiveRefCntPtr<DynMatcherInterface> Instance; |
| 76 | |
| 77 | class Impl : public DynMatcherInterface { |
| 78 | public: |
| 79 | bool dynMatches(const ast_type_traits::DynTypedNode &, ASTMatchFinder *, |
| 80 | BoundNodesTreeBuilder *) const override { |
| 81 | return true; |
| 82 | } |
| 83 | }; |
| 84 | }; |
| 85 | static llvm::ManagedStatic<TrueMatcherImpl> TrueMatcherInstance; |
| 86 | |
Samuel Benzaquen | f28d997 | 2014-10-01 15:08:07 +0000 | [diff] [blame] | 87 | } // namespace |
| 88 | |
| 89 | DynTypedMatcher DynTypedMatcher::constructVariadic( |
| 90 | VariadicOperatorFunction Func, std::vector<DynTypedMatcher> InnerMatchers) { |
| 91 | assert(InnerMatchers.size() > 0 && "Array must not be empty."); |
Samuel Benzaquen | 2009960 | 2014-10-13 17:38:12 +0000 | [diff] [blame] | 92 | assert(std::all_of(InnerMatchers.begin(), InnerMatchers.end(), |
| 93 | [&InnerMatchers](const DynTypedMatcher &M) { |
| 94 | return InnerMatchers[0].SupportedKind.isSame(M.SupportedKind); |
| 95 | }) && |
| 96 | "SupportedKind must match!"); |
| 97 | |
| 98 | // We must relax the restrict kind here. |
| 99 | // The different operators might deal differently with a mismatch. |
| 100 | // Make it the same as SupportedKind, since that is the broadest type we are |
| 101 | // allowed to accept. |
Samuel Benzaquen | 193d87f | 2014-10-13 18:17:11 +0000 | [diff] [blame^] | 102 | auto SupportedKind = InnerMatchers[0].SupportedKind; |
| 103 | return DynTypedMatcher(SupportedKind, SupportedKind, |
Samuel Benzaquen | 2009960 | 2014-10-13 17:38:12 +0000 | [diff] [blame] | 104 | new VariadicMatcher(Func, std::move(InnerMatchers))); |
Samuel Benzaquen | f28d997 | 2014-10-01 15:08:07 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Samuel Benzaquen | 96039d7 | 2014-10-09 19:28:18 +0000 | [diff] [blame] | 107 | DynTypedMatcher DynTypedMatcher::trueMatcher( |
| 108 | ast_type_traits::ASTNodeKind NodeKind) { |
| 109 | return DynTypedMatcher(NodeKind, NodeKind, TrueMatcherInstance->Instance); |
| 110 | } |
| 111 | |
Samuel Benzaquen | f28d997 | 2014-10-01 15:08:07 +0000 | [diff] [blame] | 112 | DynTypedMatcher DynTypedMatcher::dynCastTo( |
| 113 | const ast_type_traits::ASTNodeKind Kind) const { |
| 114 | auto Copy = *this; |
| 115 | Copy.SupportedKind = Kind; |
Samuel Benzaquen | a117002 | 2014-10-06 13:14:30 +0000 | [diff] [blame] | 116 | Copy.RestrictKind = |
| 117 | ast_type_traits::ASTNodeKind::getMostDerivedType(Kind, RestrictKind); |
Samuel Benzaquen | f28d997 | 2014-10-01 15:08:07 +0000 | [diff] [blame] | 118 | return Copy; |
| 119 | } |
| 120 | |
| 121 | bool DynTypedMatcher::matches(const ast_type_traits::DynTypedNode &DynNode, |
| 122 | ASTMatchFinder *Finder, |
| 123 | BoundNodesTreeBuilder *Builder) const { |
| 124 | if (RestrictKind.isBaseOf(DynNode.getNodeKind()) && |
| 125 | Implementation->dynMatches(DynNode, Finder, Builder)) { |
| 126 | return true; |
| 127 | } |
| 128 | // Delete all bindings when a matcher does not match. |
| 129 | // This prevents unexpected exposure of bound nodes in unmatches |
| 130 | // branches of the match tree. |
| 131 | Builder->removeBindings([](const BoundNodesMap &) { return true; }); |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | llvm::Optional<DynTypedMatcher> DynTypedMatcher::tryBind(StringRef ID) const { |
| 136 | if (!AllowBind) return llvm::None; |
| 137 | auto Result = *this; |
| 138 | Result.Implementation = new IdDynMatcher(ID, Result.Implementation); |
| 139 | return Result; |
| 140 | } |
| 141 | |
Samuel Benzaquen | ab005ed | 2014-09-04 14:13:58 +0000 | [diff] [blame] | 142 | bool DynTypedMatcher::canConvertTo(ast_type_traits::ASTNodeKind To) const { |
| 143 | const auto From = getSupportedKind(); |
| 144 | auto QualKind = ast_type_traits::ASTNodeKind::getFromNodeKind<QualType>(); |
| 145 | auto TypeKind = ast_type_traits::ASTNodeKind::getFromNodeKind<Type>(); |
| 146 | /// Mimic the implicit conversions of Matcher<>. |
| 147 | /// - From Matcher<Type> to Matcher<QualType> |
| 148 | if (From.isSame(TypeKind) && To.isSame(QualKind)) return true; |
| 149 | /// - From Matcher<Base> to Matcher<Derived> |
| 150 | return From.isBaseOf(To); |
| 151 | } |
| 152 | |
Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 153 | void BoundNodesTreeBuilder::addMatch(const BoundNodesTreeBuilder &Other) { |
| 154 | for (unsigned i = 0, e = Other.Bindings.size(); i != e; ++i) { |
| 155 | Bindings.push_back(Other.Bindings[i]); |
Daniel Jasper | 94a5685 | 2012-11-16 18:39:22 +0000 | [diff] [blame] | 156 | } |
Manuel Klimek | 021d56f | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Samuel Benzaquen | 4d05874 | 2013-11-22 14:41:48 +0000 | [diff] [blame] | 159 | bool NotUnaryOperator(const ast_type_traits::DynTypedNode DynNode, |
| 160 | ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder, |
| 161 | ArrayRef<DynTypedMatcher> InnerMatchers) { |
| 162 | if (InnerMatchers.size() != 1) |
| 163 | return false; |
| 164 | |
| 165 | // The 'unless' matcher will always discard the result: |
| 166 | // If the inner matcher doesn't match, unless returns true, |
| 167 | // but the inner matcher cannot have bound anything. |
| 168 | // If the inner matcher matches, the result is false, and |
| 169 | // any possible binding will be discarded. |
| 170 | // We still need to hand in all the bound nodes up to this |
| 171 | // point so the inner matcher can depend on bound nodes, |
| 172 | // and we need to actively discard the bound nodes, otherwise |
| 173 | // the inner matcher will reset the bound nodes if it doesn't |
| 174 | // match, but this would be inversed by 'unless'. |
| 175 | BoundNodesTreeBuilder Discard(*Builder); |
| 176 | return !InnerMatchers[0].matches(DynNode, Finder, &Discard); |
| 177 | } |
| 178 | |
Samuel Benzaquen | 85ec25d | 2013-08-27 15:11:16 +0000 | [diff] [blame] | 179 | bool AllOfVariadicOperator(const ast_type_traits::DynTypedNode DynNode, |
| 180 | ASTMatchFinder *Finder, |
| 181 | BoundNodesTreeBuilder *Builder, |
Samuel Benzaquen | f34ac3e | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 182 | ArrayRef<DynTypedMatcher> InnerMatchers) { |
Samuel Benzaquen | 85ec25d | 2013-08-27 15:11:16 +0000 | [diff] [blame] | 183 | // allOf leads to one matcher for each alternative in the first |
| 184 | // matcher combined with each alternative in the second matcher. |
| 185 | // Thus, we can reuse the same Builder. |
| 186 | for (size_t i = 0, e = InnerMatchers.size(); i != e; ++i) { |
Samuel Benzaquen | f34ac3e | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 187 | if (!InnerMatchers[i].matches(DynNode, Finder, Builder)) |
Samuel Benzaquen | 85ec25d | 2013-08-27 15:11:16 +0000 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | bool EachOfVariadicOperator(const ast_type_traits::DynTypedNode DynNode, |
| 194 | ASTMatchFinder *Finder, |
| 195 | BoundNodesTreeBuilder *Builder, |
Samuel Benzaquen | f34ac3e | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 196 | ArrayRef<DynTypedMatcher> InnerMatchers) { |
Samuel Benzaquen | 85ec25d | 2013-08-27 15:11:16 +0000 | [diff] [blame] | 197 | BoundNodesTreeBuilder Result; |
| 198 | bool Matched = false; |
| 199 | for (size_t i = 0, e = InnerMatchers.size(); i != e; ++i) { |
| 200 | BoundNodesTreeBuilder BuilderInner(*Builder); |
Samuel Benzaquen | f34ac3e | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 201 | if (InnerMatchers[i].matches(DynNode, Finder, &BuilderInner)) { |
Samuel Benzaquen | 85ec25d | 2013-08-27 15:11:16 +0000 | [diff] [blame] | 202 | Matched = true; |
| 203 | Result.addMatch(BuilderInner); |
| 204 | } |
| 205 | } |
Benjamin Kramer | d9c9162 | 2014-08-29 11:22:47 +0000 | [diff] [blame] | 206 | *Builder = std::move(Result); |
Samuel Benzaquen | 85ec25d | 2013-08-27 15:11:16 +0000 | [diff] [blame] | 207 | return Matched; |
| 208 | } |
| 209 | |
| 210 | bool AnyOfVariadicOperator(const ast_type_traits::DynTypedNode DynNode, |
| 211 | ASTMatchFinder *Finder, |
| 212 | BoundNodesTreeBuilder *Builder, |
Samuel Benzaquen | f34ac3e | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 213 | ArrayRef<DynTypedMatcher> InnerMatchers) { |
Samuel Benzaquen | 85ec25d | 2013-08-27 15:11:16 +0000 | [diff] [blame] | 214 | for (size_t i = 0, e = InnerMatchers.size(); i != e; ++i) { |
| 215 | BoundNodesTreeBuilder Result = *Builder; |
Samuel Benzaquen | f34ac3e | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 216 | if (InnerMatchers[i].matches(DynNode, Finder, &Result)) { |
Benjamin Kramer | d9c9162 | 2014-08-29 11:22:47 +0000 | [diff] [blame] | 217 | *Builder = std::move(Result); |
Samuel Benzaquen | 85ec25d | 2013-08-27 15:11:16 +0000 | [diff] [blame] | 218 | return true; |
| 219 | } |
| 220 | } |
| 221 | return false; |
| 222 | } |
| 223 | |
Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 224 | } // end namespace internal |
| 225 | } // end namespace ast_matchers |
| 226 | } // end namespace clang |