1 //===--- ASTMatchFinder.h - Structural query framework ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Provides a way to construct an ASTConsumer that runs given matchers
11 // over the AST and invokes a given callback on every match.
13 // The general idea is to construct a matcher expression that describes a
14 // subtree match on the AST. Next, a callback that is executed every time the
15 // expression matches is registered, and the matcher is run over the AST of
16 // some code. Matched subexpressions can be bound to string IDs and easily
17 // be accessed from the registered callback. The callback can than use the
18 // AST nodes that the subexpressions matched on to output information about
19 // the match or construct changes that can be applied to the code.
22 // class HandleMatch : public MatchFinder::MatchCallback {
24 // virtual void Run(const MatchFinder::MatchResult &Result) {
25 // const CXXRecordDecl *Class =
26 // Result.Nodes.GetDeclAs<CXXRecordDecl>("id");
31 // int main(int argc, char **argv) {
32 // ClangTool Tool(argc, argv);
33 // MatchFinder finder;
34 // finder.AddMatcher(Id("id", record(hasName("::a_namespace::AClass"))),
36 // return Tool.Run(newFrontendActionFactory(&finder));
39 //===----------------------------------------------------------------------===//
41 #ifndef LLVM_CLANG_AST_MATCHERS_AST_MATCH_FINDER_H
42 #define LLVM_CLANG_AST_MATCHERS_AST_MATCH_FINDER_H
44 #include "clang/ASTMatchers/ASTMatchers.h"
48 namespace ast_matchers {
50 /// \brief A class to allow finding matches over the Clang AST.
52 /// After creation, you can add multiple matchers to the MatchFinder via
53 /// calls to addMatcher(...).
55 /// Once all matchers are added, newASTConsumer() returns an ASTConsumer
56 /// that will trigger the callbacks specified via addMatcher(...) when a match
59 /// See ASTMatchers.h for more information about how to create matchers.
61 /// Not intended to be subclassed.
64 /// \brief Contains all information for a given match.
66 /// Every time a match is found, the MatchFinder will invoke the registered
67 /// MatchCallback with a MatchResult containing information about the match.
69 MatchResult(const BoundNodes &Nodes, clang::ASTContext *Context);
71 /// \brief Contains the nodes bound on the current match.
73 /// This allows user code to easily extract matched AST nodes.
74 const BoundNodes Nodes;
76 /// \brief Utilities for interpreting the matched AST structures.
78 clang::ASTContext * const Context;
79 clang::SourceManager * const SourceManager;
83 /// \brief Called when the Match registered for it was successfully found
87 virtual ~MatchCallback();
89 /// \brief Called on every match by the \c MatchFinder.
90 virtual void run(const MatchResult &Result) = 0;
92 /// \brief Called at the start of each translation unit.
94 /// Optionally override to do per translation unit tasks.
95 virtual void onStartOfTranslationUnit() {}
98 /// \brief Called when parsing is finished. Intended for testing only.
99 class ParsingDoneTestCallback {
101 virtual ~ParsingDoneTestCallback();
102 virtual void run() = 0;
108 /// \brief Adds a matcher to execute when running over the AST.
110 /// Calls 'Action' with the BoundNodes on every match.
111 /// Adding more than one 'NodeMatch' allows finding different matches in a
112 /// single pass over the AST.
114 /// Does not take ownership of 'Action'.
116 void addMatcher(const DeclarationMatcher &NodeMatch,
117 MatchCallback *Action);
118 void addMatcher(const TypeMatcher &NodeMatch,
119 MatchCallback *Action);
120 void addMatcher(const StatementMatcher &NodeMatch,
121 MatchCallback *Action);
122 void addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
123 MatchCallback *Action);
124 void addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
125 MatchCallback *Action);
126 void addMatcher(const TypeLocMatcher &NodeMatch,
127 MatchCallback *Action);
130 /// \brief Creates a clang ASTConsumer that finds all matches.
131 clang::ASTConsumer *newASTConsumer();
133 /// \brief Finds all matches on the given \c Node.
136 void findAll(const Decl &Node, ASTContext &Context);
137 void findAll(const Stmt &Node, ASTContext &Context);
140 /// \brief Registers a callback to notify the end of parsing.
142 /// The provided closure is called after parsing is done, before the AST is
143 /// traversed. Useful for benchmarking.
144 /// Each call to FindAll(...) will call the closure once.
145 void registerTestCallbackAfterParsing(ParsingDoneTestCallback *ParsingDone);
148 /// \brief For each \c DynTypedMatcher a \c MatchCallback that will be called
150 std::vector<std::pair<const internal::DynTypedMatcher*, MatchCallback*> >
151 MatcherCallbackPairs;
153 /// \brief Called when parsing is done.
154 ParsingDoneTestCallback *ParsingDone;
157 } // end namespace ast_matchers
158 } // end namespace clang
160 #endif // LLVM_CLANG_AST_MATCHERS_AST_MATCH_FINDER_H