[cfe-dev] Match callback invocation order

Stephen Kelly via cfe-dev cfe-dev at lists.llvm.org
Wed Jun 17 15:21:13 PDT 2020


On 15/06/2020 14:28, Billy O'Mahony via cfe-dev wrote:
> I was expecting the matches to be in line-number order. In fact I'm 
> really depending on that for the custom tool I want to build as it 
> needs to analyze the order in which certain fns are called.


I'm not sure line numbers are the thing to rely on if that is your goal. 
Consider for example a lambda inside a function (though maybe you are 
only working on C, not C++?). Your matcher for callExpr() would need to 
distinguish between calls inside and outside the lambda.

Regardless of whether C++ is relevant to you, as David pointed out, this 
appears to be an optimization, so an alternative implementation might 
make sense anyway.

I would recommend you match functions as you are with functionDecl() and 
then in your match callback, use something like (not tested)

ast_matchers::match(functionDecl(

forEachDescendant(callExpr(forFunction(functionDecl(equalsNode(FN)))))

), *FN, *Result.Context);


The forFunction() matcher is the important part which will ensure that 
for example callExpr() in nested lambdas are not part of your result set.

Then order the resulting nodes by their getBeginLoc() perhaps. (Macros 
may need to be accounted for).

Thanks,

Stephen.



More information about the cfe-dev mailing list