clang 8.0.0

Classes | Public Member Functions | List of all members
clang::ast_matchers::MatchFinder Class Reference

A class to allow finding matches over the Clang AST. More...

`#include "clang/ASTMatchers/ASTMatchFinder.h"`

## Classes
class | MatchCallback
| Called when the Match registered for it was successfully found in the AST. More...
struct | MatchersByType
| For each `Matcher<>` a `MatchCallback` that will be called when it matches. More...
struct | MatchFinderOptions
struct | MatchResult
| Contains all information for a given match. More...
class | ParsingDoneTestCallback
| Called when parsing is finished. Intended for testing only. More...

## Public Member Functions
| MatchFinder (MatchFinderOptions Options=MatchFinderOptions())
| ~MatchFinder ()
bool | addDynamicMatcher (const internal::DynTypedMatcher &NodeMatch, MatchCallback *Action)
| Adds a matcher to execute when running over the AST. More...
std::unique_ptr< clang::ASTConsumer > | newASTConsumer ()
| Creates a clang ASTConsumer that finds all matches. More...
void | matchAST (ASTContext &Context)
| Finds all matches in the given AST. More...
void | registerTestCallbackAfterParsing (ParsingDoneTestCallback *ParsingDone)
| Registers a callback to notify the end of parsing. More...
void | addMatcher (const DeclarationMatcher &NodeMatch, MatchCallback *Action)
| Adds a matcher to execute when running over the AST. More...
void | addMatcher (const TypeMatcher &NodeMatch, MatchCallback *Action)
void | addMatcher (const StatementMatcher &NodeMatch, MatchCallback *Action)
void | addMatcher (const NestedNameSpecifierMatcher &NodeMatch, MatchCallback *Action)
void | addMatcher (const NestedNameSpecifierLocMatcher &NodeMatch, MatchCallback *Action)
void | addMatcher (const TypeLocMatcher &NodeMatch, MatchCallback *Action)
void | addMatcher (const CXXCtorInitializerMatcher &NodeMatch, MatchCallback *Action)
template<typename T >
void | match (const T &Node, ASTContext &Context)
| Calls the registered callbacks on all matches on the given `Node`. More...
void | match (const clang::ast_type_traits::DynTypedNode &Node, ASTContext &Context)

## Detailed Description

A class to allow finding matches over the Clang AST.

After creation, you can add multiple matchers to the MatchFinder via calls to addMatcher(...).

Once all matchers are added, newASTConsumer() returns an ASTConsumer that will trigger the callbacks specified via addMatcher(...) when a match is found.

The order of matches is guaranteed to be equivalent to doing a pre-order traversal on the AST, and applying the matchers in the order in which they were added to the MatchFinder.

See ASTMatchers.h for more information about how to create matchers.

Not intended to be subclassed.

Definition at line 69 of file ASTMatchFinder.h.

## Constructor & Destructor Documentation

## ◆ MatchFinder()

clang::ast_matchers::MatchFinder::MatchFinder | ( | MatchFinderOptions | Options = `MatchFinderOptions()` | ) |

Definition at line 948 of file ASTMatchFinder.cpp.

## ◆ ~MatchFinder()

clang::ast_matchers::MatchFinder::~MatchFinder | ( |  | ) |

Definition at line 951 of file ASTMatchFinder.cpp.

## Member Function Documentation

## ◆ addDynamicMatcher()

bool clang::ast_matchers::MatchFinder::addDynamicMatcher | ( | const internal::DynTypedMatcher & | NodeMatch,
|  | MatchCallback * | Action
| ) |  |

Adds a matcher to execute when running over the AST.

This is similar to `addMatcher()`, but it uses the dynamic interface. It is more flexible, but the lost type information enables a caller to pass a matcher that cannot match anything.

Returns
  `true` if the matcher is a valid top-level matcher, `false` otherwise.

Definition at line 995 of file ASTMatchFinder.cpp.

References addMatcher().

## ◆ addMatcher() [1/7]

void clang::ast_matchers::MatchFinder::addMatcher | ( | const DeclarationMatcher & | NodeMatch,
|  | MatchCallback * | Action
| ) |  |

Adds a matcher to execute when running over the AST.

Calls 'Action' with the BoundNodes on every match. Adding more than one 'NodeMatch' allows finding different matches in a single pass over the AST.

Does not take ownership of 'Action'.

Definition at line 953 of file ASTMatchFinder.cpp.

References clang::ast_matchers::MatchFinder::MatchersByType::AllCallbacks, and clang::ast_matchers::MatchFinder::MatchersByType::DeclOrStmt.

Referenced by addDynamicMatcher().

## ◆ addMatcher() [2/7]

void clang::ast_matchers::MatchFinder::addMatcher | ( | const TypeMatcher & | NodeMatch,
|  | MatchCallback * | Action
| ) |  |

Definition at line 959 of file ASTMatchFinder.cpp.

References clang::ast_matchers::MatchFinder::MatchersByType::AllCallbacks, and clang::ast_matchers::MatchFinder::MatchersByType::Type.

## ◆ addMatcher() [3/7]

void clang::ast_matchers::MatchFinder::addMatcher | ( | const StatementMatcher & | NodeMatch,
|  | MatchCallback * | Action
| ) |  |

Definition at line 965 of file ASTMatchFinder.cpp.

References clang::ast_matchers::MatchFinder::MatchersByType::AllCallbacks, and clang::ast_matchers::MatchFinder::MatchersByType::DeclOrStmt.

## ◆ addMatcher() [4/7]

void clang::ast_matchers::MatchFinder::addMatcher | ( | const NestedNameSpecifierMatcher & | NodeMatch,
|  | MatchCallback * | Action
| ) |  |

Definition at line 971 of file ASTMatchFinder.cpp.

References clang::ast_matchers::MatchFinder::MatchersByType::AllCallbacks, and clang::ast_matchers::MatchFinder::MatchersByType::NestedNameSpecifier.

## ◆ addMatcher() [5/7]

void clang::ast_matchers::MatchFinder::addMatcher | ( | const NestedNameSpecifierLocMatcher & | NodeMatch,
|  | MatchCallback * | Action
| ) |  |

Definition at line 977 of file ASTMatchFinder.cpp.

References clang::ast_matchers::MatchFinder::MatchersByType::AllCallbacks, and clang::ast_matchers::MatchFinder::MatchersByType::NestedNameSpecifierLoc.

## ◆ addMatcher() [6/7]

void clang::ast_matchers::MatchFinder::addMatcher | ( | const TypeLocMatcher & | NodeMatch,
|  | MatchCallback * | Action
| ) |  |

Definition at line 983 of file ASTMatchFinder.cpp.

References clang::ast_matchers::MatchFinder::MatchersByType::AllCallbacks, and clang::ast_matchers::MatchFinder::MatchersByType::TypeLoc.

## ◆ addMatcher() [7/7]

void clang::ast_matchers::MatchFinder::addMatcher | ( | const CXXCtorInitializerMatcher & | NodeMatch,
|  | MatchCallback * | Action
| ) |  |

Definition at line 989 of file ASTMatchFinder.cpp.

References clang::ast_matchers::MatchFinder::MatchersByType::AllCallbacks, and clang::ast_matchers::MatchFinder::MatchersByType::CtorInit.

## ◆ match() [1/2]

template<typename T >

void clang::ast_matchers::MatchFinder::match | ( | const T & | Node,
|  | ASTContext & | Context
| ) |  |
| inline

Calls the registered callbacks on all matches on the given `Node`.

Note that there can be multiple matches on a single node, for example when using decl(forEachDescendant(stmt())).

Definition at line 185 of file ASTMatchFinder.h.

References clang::ast_matchers::MatchFinder::MatchResult::Context, clang::ast_type_traits::DynTypedNode::create(), matchAST(), Node, and registerTestCallbackAfterParsing().

## ◆ match() [2/2]

void clang::ast_matchers::MatchFinder::match | ( | const clang::ast_type_traits::DynTypedNode & | Node,
|  | ASTContext & | Context
| ) |  |

Definition at line 1026 of file ASTMatchFinder.cpp.

## ◆ matchAST()

void clang::ast_matchers::MatchFinder::matchAST | ( | ASTContext & | Context | ) |

Finds all matches in the given AST.

Definition at line 1033 of file ASTMatchFinder.cpp.

Referenced by match().

## ◆ newASTConsumer()

std::unique_ptr< ASTConsumer > clang::ast_matchers::MatchFinder::newASTConsumer | ( |  | ) |

Creates a clang ASTConsumer that finds all matches.

Definition at line 1022 of file ASTMatchFinder.cpp.

## ◆ registerTestCallbackAfterParsing()

void clang::ast_matchers::MatchFinder::registerTestCallbackAfterParsing | ( | MatchFinder::ParsingDoneTestCallback * | NewParsingDone | ) |

Registers a callback to notify the end of parsing.

The provided closure is called after parsing is done, before the AST is traversed. Useful for benchmarking. Each call to FindAll(...) will call the closure once.

Definition at line 1041 of file ASTMatchFinder.cpp.

Referenced by match().

The documentation for this class was generated from the following files:

- include/clang/ASTMatchers/ASTMatchFinder.h
- lib/ASTMatchers/ASTMatchFinder.cpp