Search code examples
cfunction-pointersabstract-syntax-treeclang-static-analyzerclang-query

Matching Function Pointers in Clang-Query


What is the query to match a function pointer, e.g., in the following code:

int foo(struct A *a, int b)
{
    return a->x->y(b);
}

y is the function pointer to match.


Solution

  • Finally, this is the answer for a call to a function pointer. The main difficulty was to detect the type of function pointers using the query. So I added the ignoringParens(functionType()) traversal matcher to solve the problem. A simple usage of functionType() will not work.

    match callExpr(hasDescendant(memberExpr(hasType(pointerType(pointee(ignoringParens(functionType()))))).bind("member")))