File indexing completed on 2026-05-10 08:36:48
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 #ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHFINDER_H
0041 #define LLVM_CLANG_ASTMATCHERS_ASTMATCHFINDER_H
0042
0043 #include "clang/ASTMatchers/ASTMatchers.h"
0044 #include "llvm/ADT/SmallPtrSet.h"
0045 #include "llvm/ADT/StringMap.h"
0046 #include "llvm/Support/Timer.h"
0047 #include <optional>
0048
0049 namespace clang {
0050
0051 namespace ast_matchers {
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 class MatchFinder {
0070 public:
0071
0072
0073
0074
0075 struct MatchResult {
0076 MatchResult(const BoundNodes &Nodes, clang::ASTContext *Context);
0077
0078
0079
0080
0081 const BoundNodes Nodes;
0082
0083
0084
0085 clang::ASTContext * const Context;
0086 clang::SourceManager * const SourceManager;
0087
0088 };
0089
0090
0091
0092 class MatchCallback {
0093 public:
0094 virtual ~MatchCallback();
0095
0096
0097 virtual void run(const MatchResult &Result) = 0;
0098
0099
0100
0101
0102 virtual void onStartOfTranslationUnit() {}
0103
0104
0105
0106
0107 virtual void onEndOfTranslationUnit() {}
0108
0109
0110
0111
0112
0113 virtual StringRef getID() const;
0114
0115
0116
0117
0118
0119 virtual std::optional<TraversalKind> getCheckTraversalKind() const;
0120 };
0121
0122
0123 class ParsingDoneTestCallback {
0124 public:
0125 virtual ~ParsingDoneTestCallback();
0126 virtual void run() = 0;
0127 };
0128
0129 struct MatchFinderOptions {
0130 struct Profiling {
0131 Profiling(llvm::StringMap<llvm::TimeRecord> &Records)
0132 : Records(Records) {}
0133
0134
0135 llvm::StringMap<llvm::TimeRecord> &Records;
0136 };
0137
0138
0139
0140
0141 std::optional<Profiling> CheckProfiling;
0142 };
0143
0144 MatchFinder(MatchFinderOptions Options = MatchFinderOptions());
0145 ~MatchFinder();
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155 void addMatcher(const DeclarationMatcher &NodeMatch,
0156 MatchCallback *Action);
0157 void addMatcher(const TypeMatcher &NodeMatch,
0158 MatchCallback *Action);
0159 void addMatcher(const StatementMatcher &NodeMatch,
0160 MatchCallback *Action);
0161 void addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
0162 MatchCallback *Action);
0163 void addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
0164 MatchCallback *Action);
0165 void addMatcher(const TypeLocMatcher &NodeMatch,
0166 MatchCallback *Action);
0167 void addMatcher(const CXXCtorInitializerMatcher &NodeMatch,
0168 MatchCallback *Action);
0169 void addMatcher(const TemplateArgumentLocMatcher &NodeMatch,
0170 MatchCallback *Action);
0171 void addMatcher(const AttrMatcher &NodeMatch, MatchCallback *Action);
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 bool addDynamicMatcher(const internal::DynTypedMatcher &NodeMatch,
0183 MatchCallback *Action);
0184
0185
0186 std::unique_ptr<clang::ASTConsumer> newASTConsumer();
0187
0188
0189
0190
0191
0192
0193
0194 template <typename T> void match(const T &Node, ASTContext &Context) {
0195 match(clang::DynTypedNode::create(Node), Context);
0196 }
0197 void match(const clang::DynTypedNode &Node, ASTContext &Context);
0198
0199
0200
0201 void matchAST(ASTContext &Context);
0202
0203
0204
0205
0206
0207
0208 void registerTestCallbackAfterParsing(ParsingDoneTestCallback *ParsingDone);
0209
0210
0211
0212 struct MatchersByType {
0213 std::vector<std::pair<internal::DynTypedMatcher, MatchCallback *>>
0214 DeclOrStmt;
0215 std::vector<std::pair<TypeMatcher, MatchCallback *>> Type;
0216 std::vector<std::pair<NestedNameSpecifierMatcher, MatchCallback *>>
0217 NestedNameSpecifier;
0218 std::vector<std::pair<NestedNameSpecifierLocMatcher, MatchCallback *>>
0219 NestedNameSpecifierLoc;
0220 std::vector<std::pair<TypeLocMatcher, MatchCallback *>> TypeLoc;
0221 std::vector<std::pair<CXXCtorInitializerMatcher, MatchCallback *>> CtorInit;
0222 std::vector<std::pair<TemplateArgumentLocMatcher, MatchCallback *>>
0223 TemplateArgumentLoc;
0224 std::vector<std::pair<AttrMatcher, MatchCallback *>> Attr;
0225
0226 llvm::SmallPtrSet<MatchCallback *, 16> AllCallbacks;
0227 };
0228
0229 private:
0230 MatchersByType Matchers;
0231
0232 MatchFinderOptions Options;
0233
0234
0235 ParsingDoneTestCallback *ParsingDone;
0236 };
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252 template <typename MatcherT, typename NodeT>
0253 SmallVector<BoundNodes, 1>
0254 match(MatcherT Matcher, const NodeT &Node, ASTContext &Context);
0255
0256 template <typename MatcherT>
0257 SmallVector<BoundNodes, 1> match(MatcherT Matcher, const DynTypedNode &Node,
0258 ASTContext &Context);
0259
0260
0261
0262
0263 template <typename MatcherT>
0264 SmallVector<BoundNodes, 1> match(MatcherT Matcher, ASTContext &Context);
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 template <typename NodeT>
0277 const NodeT *
0278 selectFirst(StringRef BoundTo, const SmallVectorImpl<BoundNodes> &Results) {
0279 for (const BoundNodes &N : Results) {
0280 if (const NodeT *Node = N.getNodeAs<NodeT>(BoundTo))
0281 return Node;
0282 }
0283 return nullptr;
0284 }
0285
0286 namespace internal {
0287 class CollectMatchesCallback : public MatchFinder::MatchCallback {
0288 public:
0289 void run(const MatchFinder::MatchResult &Result) override {
0290 Nodes.push_back(Result.Nodes);
0291 }
0292
0293 std::optional<TraversalKind> getCheckTraversalKind() const override {
0294 return std::nullopt;
0295 }
0296
0297 SmallVector<BoundNodes, 1> Nodes;
0298 };
0299 }
0300
0301 template <typename MatcherT>
0302 SmallVector<BoundNodes, 1> match(MatcherT Matcher, const DynTypedNode &Node,
0303 ASTContext &Context) {
0304 internal::CollectMatchesCallback Callback;
0305 MatchFinder Finder;
0306 Finder.addMatcher(Matcher, &Callback);
0307 Finder.match(Node, Context);
0308 return std::move(Callback.Nodes);
0309 }
0310
0311 template <typename MatcherT, typename NodeT>
0312 SmallVector<BoundNodes, 1>
0313 match(MatcherT Matcher, const NodeT &Node, ASTContext &Context) {
0314 return match(Matcher, DynTypedNode::create(Node), Context);
0315 }
0316
0317 template <typename MatcherT>
0318 SmallVector<BoundNodes, 1>
0319 match(MatcherT Matcher, ASTContext &Context) {
0320 internal::CollectMatchesCallback Callback;
0321 MatchFinder Finder;
0322 Finder.addMatcher(Matcher, &Callback);
0323 Finder.matchAST(Context);
0324 return std::move(Callback.Nodes);
0325 }
0326
0327 inline SmallVector<BoundNodes, 1>
0328 matchDynamic(internal::DynTypedMatcher Matcher, const DynTypedNode &Node,
0329 ASTContext &Context) {
0330 internal::CollectMatchesCallback Callback;
0331 MatchFinder Finder;
0332 Finder.addDynamicMatcher(Matcher, &Callback);
0333 Finder.match(Node, Context);
0334 return std::move(Callback.Nodes);
0335 }
0336
0337 template <typename NodeT>
0338 SmallVector<BoundNodes, 1> matchDynamic(internal::DynTypedMatcher Matcher,
0339 const NodeT &Node,
0340 ASTContext &Context) {
0341 return matchDynamic(Matcher, DynTypedNode::create(Node), Context);
0342 }
0343
0344 inline SmallVector<BoundNodes, 1>
0345 matchDynamic(internal::DynTypedMatcher Matcher, ASTContext &Context) {
0346 internal::CollectMatchesCallback Callback;
0347 MatchFinder Finder;
0348 Finder.addDynamicMatcher(Matcher, &Callback);
0349 Finder.matchAST(Context);
0350 return std::move(Callback.Nodes);
0351 }
0352
0353 }
0354 }
0355
0356 #endif