[cfe-dev] AST matcher: ForEach FunctionDecl -> ParmVarDecl

FarSight Studios via cfe-dev cfe-dev at lists.llvm.org
Mon Dec 31 22:40:56 PST 2018


Hi,
I am really sorry I did not gave a proper explanation of what I am trying
to do.
I want to match the call expressions where at least SOME of its arguments
are the parameters or SOME of the parameters of the outer function.
Not all arguments have to be the parameters of the outer function.
I don't really need to forward all parameters to arguments.

The combination of the built-in matches that I used, only matched the first
parameter because I was unable to do some sort of loop there.
As a result, only first parameter of the outer function is matched, others
are were not matched.

Is there an option to make an iteration over all outer function parameters
and still use the equalsBoundNode("callerArg") narrowing matcher?
Or the only option is to try implementing a custom matcher?

Thank you so much for your help.

On Sun, Dec 30, 2018 at 4:41 PM Stephen Kelly via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> On 30/12/2018 14:00, FarSight Studios via cfe-dev wrote:
> > I am trying to match call expressions where the caller arguments are the
> > parameters of another, outer function.
>
> You need to write your own matcher.
>
> These untested/uncompiled snippets should get you started:
>
> bool callArgsAreFuncParams(CallExpr const& C, FunctionDecl const& F)
> {
>      // TODO: Handle variadic args
>
>      // TODO: Handle default arguments in call
>
>      if (C.argumentCount() != F.parameterCount())
>          return false;
>
>      for (auto i = 0; i < C.argumentCount(); ++i)
>      {
>          if (C.getArg(i).getDecl() != F.getParameter(i))
>              return false;
>      }
>      return true;
> }
>
> AST_MATCHER(CallExpr, forwardsAllParameters) {
>    const auto &Parents = Finder->getASTContext().getParents(Node, true);
>
>    llvm::SmallVector<ast_type_traits::DynTypedNode, 8>
> Stack(Parents.begin(),
>
>  Parents.end());
>    while(!Stack.empty()) {
>      const auto &CurNode = Stack.back();
>      Stack.pop_back();
>      if(const auto *FuncDeclNode = CurNode.get<FunctionDecl>()) {
>        if (callArgsAreFuncParams(Node, *FuncDeclNode))
>          return true;
>      } else if(const auto *LambdaExprNode = CurNode.get<LambdaExpr>()) {
>        if (callArgsAreFuncParams(Node, *LambdaExprNode->getCallOperator()))
>          return true;
>      } else {
>        for (const auto &Parent :
>             Finder->getASTContext().getParents(CurNode, true))
>          Stack.push_back(Parent);
>      }
>    }
>    return false;
> }
>
>
>
> auto fullMatcher =  callExpr(
>      forwardsAllParameters(),
>      forFunction(functionDecl().bind("outerFunction"))
>      ).bind("innerCall")
>
>
> It would also be possible to design the matcher such that it is used
> like this:
>
>   forwardsAllParametersFrom(functionDecl().bind("outerFunction"))
>
>
> That's left as an exercise for the reader.
>
> Thanks,
>
> Stephen.
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>


-- 
-------------------------------------
FarSight Studios
http://design.shadas.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190101/7933e8ec/attachment.html>


More information about the cfe-dev mailing list