40 #ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHFINDER_H
41 #define LLVM_CLANG_ASTMATCHERS_ASTMATCHFINDER_H
44 #include "llvm/ADT/SmallPtrSet.h"
45 #include "llvm/ADT/StringMap.h"
46 #include "llvm/Support/Timer.h"
51 namespace ast_matchers {
113 virtual StringRef
getID()
const;
213 std::vector<std::pair<internal::DynTypedMatcher, MatchCallback *>>
215 std::vector<std::pair<TypeMatcher, MatchCallback *>>
Type;
216 std::vector<std::pair<NestedNameSpecifierMatcher, MatchCallback *>>
218 std::vector<std::pair<NestedNameSpecifierLocMatcher, MatchCallback *>>
220 std::vector<std::pair<TypeLocMatcher, MatchCallback *>>
TypeLoc;
221 std::vector<std::pair<CXXCtorInitializerMatcher, MatchCallback *>>
CtorInit;
222 std::vector<std::pair<TemplateArgumentLocMatcher, MatchCallback *>>
224 std::vector<std::pair<AttrMatcher, MatchCallback *>>
Attr;
252 template <
typename MatcherT,
typename NodeT>
256 template <
typename MatcherT>
263 template <
typename MatcherT>
276 template <
typename NodeT>
280 if (
const NodeT *
Node = N.getNodeAs<NodeT>(BoundTo))
290 Nodes.push_back(Result.Nodes);
301 template <
typename MatcherT>
306 Finder.addMatcher(Matcher, &Callback);
307 Finder.match(
Node, Context);
308 return std::move(Callback.Nodes);
311 template <
typename MatcherT,
typename NodeT>
317 template <
typename MatcherT>
322 Finder.addMatcher(Matcher, &Callback);
323 Finder.matchAST(Context);
324 return std::move(Callback.Nodes);
332 Finder.addDynamicMatcher(Matcher, &Callback);
333 Finder.match(
Node, Context);
334 return std::move(Callback.Nodes);
337 template <
typename NodeT>
348 Finder.addDynamicMatcher(Matcher, &Callback);
349 Finder.matchAST(Context);
350 return std::move(Callback.Nodes);
llvm::MachO::Records Records
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
A dynamically typed AST node container.
static DynTypedNode create(const T &Node)
Creates a DynTypedNode from Node.
This class handles loading and caching of source files into memory.
Maps string IDs to AST nodes matched by parts of a matcher.
Called when the Match registered for it was successfully found in the AST.
virtual std::optional< TraversalKind > getCheckTraversalKind() const
TraversalKind to use while matching and processing the result nodes.
virtual StringRef getID() const
An id used to group the matchers.
virtual void onEndOfTranslationUnit()
Called at the end of each translation unit.
virtual void run(const MatchResult &Result)=0
Called on every match by the MatchFinder.
virtual void onStartOfTranslationUnit()
Called at the start of each translation unit.
Called when parsing is finished. Intended for testing only.
virtual ~ParsingDoneTestCallback()
A class to allow finding matches over the Clang AST.
MatchFinder(MatchFinderOptions Options=MatchFinderOptions())
bool addDynamicMatcher(const internal::DynTypedMatcher &NodeMatch, MatchCallback *Action)
Adds a matcher to execute when running over the AST.
void addMatcher(const DeclarationMatcher &NodeMatch, MatchCallback *Action)
Adds a matcher to execute when running over the AST.
void match(const T &Node, ASTContext &Context)
Calls the registered callbacks on all matches on the given Node.
void registerTestCallbackAfterParsing(ParsingDoneTestCallback *ParsingDone)
Registers a callback to notify the end of parsing.
std::unique_ptr< clang::ASTConsumer > newASTConsumer()
Creates a clang ASTConsumer that finds all matches.
void matchAST(ASTContext &Context)
Finds all matches in the given AST.
std::optional< TraversalKind > getCheckTraversalKind() const override
TraversalKind to use while matching and processing the result nodes.
void run(const MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
SmallVector< BoundNodes, 1 > Nodes
internal::Matcher< QualType > TypeMatcher
internal::Matcher< Decl > DeclarationMatcher
Types of matchers for the top-level classes in the AST class hierarchy.
internal::Matcher< NestedNameSpecifier > NestedNameSpecifierMatcher
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
const NodeT * selectFirst(StringRef BoundTo, const SmallVectorImpl< BoundNodes > &Results)
Returns the first result of type NodeT bound to BoundTo.
internal::Matcher< CXXCtorInitializer > CXXCtorInitializerMatcher
internal::Matcher< Stmt > StatementMatcher
internal::Matcher< TypeLoc > TypeLocMatcher
internal::Matcher< TemplateArgumentLoc > TemplateArgumentLocMatcher
internal::Matcher< Attr > AttrMatcher
internal::Matcher< NestedNameSpecifierLoc > NestedNameSpecifierLocMatcher
SmallVector< BoundNodes, 1 > matchDynamic(internal::DynTypedMatcher Matcher, const DynTypedNode &Node, ASTContext &Context)
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
Profiling(llvm::StringMap< llvm::TimeRecord > &Records)
llvm::StringMap< llvm::TimeRecord > & Records
Per bucket timing information.
std::optional< Profiling > CheckProfiling
Enables per-check timers.
Contains all information for a given match.
clang::SourceManager *const SourceManager
MatchResult(const BoundNodes &Nodes, clang::ASTContext *Context)
const BoundNodes Nodes
Contains the nodes bound on the current match.
clang::ASTContext *const Context
Utilities for interpreting the matched AST structures.
For each Matcher<> a MatchCallback that will be called when it matches.
std::vector< std::pair< CXXCtorInitializerMatcher, MatchCallback * > > CtorInit
std::vector< std::pair< TemplateArgumentLocMatcher, MatchCallback * > > TemplateArgumentLoc
std::vector< std::pair< internal::DynTypedMatcher, MatchCallback * > > DeclOrStmt
std::vector< std::pair< NestedNameSpecifierMatcher, MatchCallback * > > NestedNameSpecifier
std::vector< std::pair< TypeMatcher, MatchCallback * > > Type
std::vector< std::pair< NestedNameSpecifierLocMatcher, MatchCallback * > > NestedNameSpecifierLoc
llvm::SmallPtrSet< MatchCallback *, 16 > AllCallbacks
All the callbacks in one container to simplify iteration.
std::vector< std::pair< TypeLocMatcher, MatchCallback * > > TypeLoc
std::vector< std::pair< AttrMatcher, MatchCallback * > > Attr