Matcher proposed in the review of checker misc-assign-operator (name pending). Its goal is to find the direct enclosing function declaration of a statement and run the inner matcher on it. Two version is attached in this patch (thus it will not compile), to be decided which approach to take. The second one always chooses one single parent while the first one does a depth-first search upwards (thus a height-first search) and returns the first positive match of the inner matcher (thus it always returns zero or one matches, not more). Further questions: is it enough to implement it in-place, or ASTMatchersInternals or maybe ASTMatchFinder should be involved?
Details
Diff Detail
Event Timeline
Please be sure to run clang\docs\tools\dump_ast_matchers.py to regenerate the documentation as well.
I will run it, once we are approaching the final version. This one is more of a question than a real patch.
Thanks for doing this!
I think we want the version that iterates all parents.
Otherwise it will have problems with templates.
That is, you won't know which FunctionDecl you will get: the template or the instantiation. And you might need one or the other specifically.
include/clang/ASTMatchers/ASTMatchers.h | ||
---|---|---|
5124 | I think this matcher works for Decls too, but we can leave it like this until we need the polymorphism. | |
5127 | You should not assert() this. | |
5132 | If you are taking from the back, use a vector<> instead. | |
5134 | Just call CurNode.get<FunctionDecl>. It'll do the dyn_cast for you. | |
5139 | We should check for LambdaExpr here too, and try to match against LambdaExpr::getCallOperator() | |
5141 | You don't need to get a Stmt. getParents() works with DynTypedNodes. | |
5142 | We should also not traverse FunctionDecls. void F() { struct S { void F2() { } }; } So, on each node: if is a FunctionDecl or a LambdaExpr run the matcher, otherwise traverse. | |
unittests/ASTMatchers/ASTMatchersTest.cpp | ||
5581 | All of this is kind of unnecessary for the test. PosVec& operator=(const PosVec&) { auto x = [] { return 1; }; return *this; } | |
5596 | EXPECT_TRUE(notMatches(... | |
5601 | Please add test cases for the changes suggested to the matcher. |
I think this matcher works for Decls too, but we can leave it like this until we need the polymorphism.