Commit Graph

24 Commits

Author SHA1 Message Date
Manuel Klimek b64d6b7fee Implements memoization for ancestor matching.
This yields a log(#ast_nodes) worst-case improvement with matchers like
stmt(unless(hasAncestor(...))).

Also made the order of visitation for ancestor matches BFS, as the most
common use cases (for example finding the closest enclosing function
definition) rely on that.

llvm-svn: 177081
2013-03-14 16:33:21 +00:00
Manuel Klimek d4be4084b4 First step towards adding a parent map to the ASTContext.
This does not yet implement the LimitNode approach discussed.

The impact of this is an O(n) in the number of nodes in the AST
reduction of complexity for certain kinds of matchers (as otherwise the
parent map gets recreated for every new MatchFinder).

See FIXMEs in the comments for the direction of future work.

llvm-svn: 176251
2013-02-28 13:21:39 +00:00
Manuel Klimek 191c093af1 Re-design the convenience interfaces on MatchFinder.
First, this implements a match() method on MatchFinder; this allows us
to get rid of the findAll implementation, as findAll is really a special
case of recursive matchers on match.

Instead of findAll, provide a convenience function match() that lets
users iterate easily over the results instead of needing to implement
callbacks.

llvm-svn: 174172
2013-02-01 13:41:35 +00:00
Dmitri Gribenko f857950d39 Remove useless 'llvm::' qualifier from names like StringRef and others that are
brought into 'clang' namespace by clang/Basic/LLVM.h

llvm-svn: 172323
2013-01-12 19:30:44 +00:00
Manuel Klimek c844a46e77 Implements multiple parents in the parent map.
Previously we would match the last visited parent, which in the
case of template instantiations was the last instantiated template.

llvm-svn: 169508
2012-12-06 14:42:48 +00:00
Manuel Klimek 5472a52c20 Fixes crash in isDerivedFrom for recursive templates.
llvm-svn: 169262
2012-12-04 13:40:29 +00:00
Manuel Klimek 9d0412334e Allow matchers to access the ASTContext.
Patch by Edwin Vane.

llvm-svn: 169000
2012-11-30 13:45:19 +00:00
Daniel Jasper 0f9f019ff8 Do not use data recursion in ASTMatchFinder.
The matchers rely on the complete AST being traversed as shown by the new test cases.

llvm-svn: 168022
2012-11-15 03:29:05 +00:00
Daniel Jasper a05bb6bd70 Fix AST-matcher descendant visiting for Types, TypeLocs and NestedNamespecifierLocs.
The RecursiveASTVisitor assumes that any given Traverse-method can be called with a NULL-node. So the subclass needs to handle these appropriately.

llvm-svn: 167850
2012-11-13 17:14:11 +00:00
Manuel Klimek bd0e2b7111 Insert interception point onStartOfTranslationUnit.
Often users of the ASTMatchers want to add tasks that are done once per
translation unit, for example, cleaning up caches. Combined with the
interception point for the end of source file one can add to the factory
creation, this covers the cases we've seen users need.

llvm-svn: 167271
2012-11-02 01:31:03 +00:00
Daniel Jasper 6fc3433b15 Implement descendant matchers for NestedNamespecifiers
This implements has(), hasDescendant(), forEach() and
forEachDescendant() for NestedNameSpecifier and NestedNameSpecifierLoc
matchers.

Review: http://llvm-reviews.chandlerc.com/D86
llvm-svn: 167017
2012-10-30 15:42:00 +00:00
Daniel Jasper d29d5fa3f2 Implement has(), hasDescendant(), forEach() and forEachDescendant() for
Types, QualTypes and TypeLocs.

Review: http://llvm-reviews.chandlerc.com/D83
llvm-svn: 166917
2012-10-29 10:14:44 +00:00
Manuel Klimek c268745011 Adds the possibility to run ASTMatchFinder over arbitrary AST nodes.
llvm-svn: 166567
2012-10-24 14:47:44 +00:00
Daniel Jasper 632aea92a5 Implement hasParent()-matcher.
llvm-svn: 166421
2012-10-22 16:26:51 +00:00
Daniel Jasper 516b02e548 First version of matchers for Types and TypeLocs.
Review: http://llvm-reviews.chandlerc.com/D47
llvm-svn: 166094
2012-10-17 08:52:59 +00:00
Daniel Jasper 83dafaf3db Fix isDerivedFrom matcher.
Without this patch, the isDerivedFrom matcher asserts in the
"assert(ClassDecl != NULL);" in the new test, as a
DependentTemplateSpecilizationType is not a sub-type of
TemplateSpecializationType and also does not offer getAsCXXRecordDecl().

I am not sure why this did not cause problems before. It is now (after
the changed implementation of isDerivedFrom) easier to write a matcher
that actually gets into this branch of the code.

llvm-svn: 164127
2012-09-18 14:17:42 +00:00
Daniel Jasper a6bc1f6d35 Create initial support for matching and binding NestedNameSpecifier(Loc)s.
Review: http://llvm-reviews.chandlerc.com/D39
llvm-svn: 163794
2012-09-13 13:11:25 +00:00
Daniel Jasper f49d1e0070 Change the behavior of the isDerivedFrom-matcher to not match on the
class itself. This caused some confusion (intuitively, a class is not
derived from itself) and makes it hard to write certain matchers, e.g.
"match and bind any pair of base and subclass".

The original behavior can be achieved with a new isA-matcher.  Similar
to all other matchers, this matcher has the same behavior and name as
the corresponding AST-entity - in this case the isa<>() function.

llvm-svn: 163385
2012-09-07 12:48:17 +00:00
Manuel Klimek 3ca12c5b54 Implements hasAncestor.
Implements the hasAncestor matcher. This builds
on the previous patch that introduced DynTypedNode to build up
a parent map for an additional degree of freedom in the AST traversal.

The map is only built once we hit an hasAncestor matcher, in order
to not slow down matching for cases where this is not needed.

We could implement some speed-ups for special cases, like building up
the parent map as we go and only building up the full map if we break
out of the already visited part of the tree, but that is probably
not going to be worth it, and would make the code significantly more
complex.

Major TODOs are:
- implement hasParent
- implement type traversal
- implement memoization in hasAncestor

llvm-svn: 163382
2012-09-07 09:26:10 +00:00
Manuel Klimek eb958ded72 Introduces DynTypedMatcher as a new concept that replaces the UntypedBaseMatcher and TypedMatcher.
Due to DynTypedNode the basic dynamically typed matcher interface can now be simplified.

Also switches the traversal interfaces to use DynTypedNode;
this is in preperation for the hasAncestor implementation, and
also allows us to need fewer changes when we want to add new
nodes to traverse, thus making the code a little more decoupled.

Main design concerns: I went back towards the original design
of getNodeAs to return a pointer, and switched DynTypedNode::get
to always return a pointer (in case of value types like QualType
the pointer points into the storage of DynTypedNode, thus allowing
us to treat all the nodes the same from the point of view of a
user of the DynTypedNodes.

Adding the QualType implementation for DynTypedNode was needed
for the recursive traversal interface changes.

llvm-svn: 163212
2012-09-05 12:12:07 +00:00
Daniel Jasper 3cb72b476d Fix for ASTMatchFinder to visit a functions parameter declarations.
llvm-svn: 160947
2012-07-30 05:03:25 +00:00
Daniel Jasper 2b3c7d414b Make the isDerivedFrom matcher more generic.
It now accepts an arbitrary inner matcher but is fully backwards
compatible.

llvm-svn: 160348
2012-07-17 07:39:27 +00:00
Daniel Jasper 1dad183b38 Add more matchers and do cleanups.
Reviewers: klimek

    Differential Revision: http://ec2-50-18-127-156.us-west-1.compute.amazonaws.com/D2

llvm-svn: 160013
2012-07-10 20:20:19 +00:00
Manuel Klimek 04616e4776 Adds the AST Matcher library, which provides a in-C++ DSL to express
matches on interesting parts of the AST, and callback mechanisms to
act on them.

llvm-svn: 159805
2012-07-06 05:48:52 +00:00