after some initial probing on IRC it seems like we might want a unified parent map (lazily) cached in the ASTContext.
** Context **
For the ASTMatchers we have come up with a 2nd implementation of a ParentMap in clang , which has slightly different requirements from the first one .
The lifetime of the ASTMatcher’s map is currently bound to the lifetime of the MatchFinder - the map is lazily created, if we have any matchers that need it, and re-used for all subsequent matches.
Recently we added support to match on arbitrary nodes, which has led to a pattern where one uses the ASTMatchers to find nodes to refactor, and then uses the match-on-node functionality to figure out what’s going on around that node.
The problem is that currently we don’t have a way to re-use the ParentMap that was already built between those calls, leading to an N^2 algorithm in the number of AST nodes.
When thinking about where this information would fit best, we thought it’s strongly coupled to the ASTContext, so before running off and creating an ASTMatchContext that contains an ASTContext and a ParentMap, I thought I’d through out the idea of using a unified ParentMap in the ASTContext.
** Proposal **
The proposal would be to add methods to the ASTContext that allow querying the parents of a node.
Knowing that this might not at all be what we want, I’m proposing a straw-man:
/// \brief Returns an iterator to the parents of the given node.
/// Note that this will lazily compute the parents of all nodes
/// and store them for later retrieval. Thus, the first call is O(n)
/// in the number of AST nodes.
/// ‘NodeT’ can be one of Decl, Stmt, Type, TypeLoc,
/// NestedNameSpecifier or NestedNameSpecifierLoc.
ParentIterator ASTContext::parents_begin(const NodeT &Node);
ParentIterator ASTContext::parents_end(const NodeT &Node);
An alternative would be to use DynTypedNode in the interface instead of templating the method and hiding it, but I’m not sure how much exposure to that we want in general.
I’m also fine with putting up a solution just for the matchers, but wanted to make sure to pursue a more generally useful solution if there’s interest…