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();
88 virtual void run(const MatchResult &Result) = 0;
91 /// \brief Called when parsing is finished. Intended for testing only.
92 class ParsingDoneTestCallback {
94 virtual ~ParsingDoneTestCallback();
95 virtual void run() = 0;
101 /// \brief Adds a matcher to execute when running over the AST.
103 /// Calls 'Action' with the BoundNodes on every match.
104 /// Adding more than one 'NodeMatch' allows finding different matches in a
105 /// single pass over the AST.
107 /// Does not take ownership of 'Action'.
109 void addMatcher(const DeclarationMatcher &NodeMatch,
110 MatchCallback *Action);
111 void addMatcher(const TypeMatcher &NodeMatch,
112 MatchCallback *Action);
113 void addMatcher(const StatementMatcher &NodeMatch,
114 MatchCallback *Action);
115 void addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
116 MatchCallback *Action);
117 void addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
118 MatchCallback *Action);
121 /// \brief Creates a clang ASTConsumer that finds all matches.
122 clang::ASTConsumer *newASTConsumer();
124 /// \brief Registers a callback to notify the end of parsing.
126 /// The provided closure is called after parsing is done, before the AST is
127 /// traversed. Useful for benchmarking.
128 /// Each call to FindAll(...) will call the closure once.
129 void registerTestCallbackAfterParsing(ParsingDoneTestCallback *ParsingDone);
132 /// \brief For each \c DynTypedMatcher a \c MatchCallback that will be called
134 std::vector<std::pair<const internal::DynTypedMatcher*, MatchCallback*> >
135 MatcherCallbackPairs;
137 /// \brief Called when parsing is done.
138 ParsingDoneTestCallback *ParsingDone;
141 } // end namespace ast_matchers
142 } // end namespace clang
144 #endif // LLVM_CLANG_AST_MATCHERS_AST_MATCH_FINDER_H