I'm using libclang with it's python binding and I'm trying to get the number of arguments for CallExpr, I found that if a member function has default value and being called with this argument missing, libclang returns the number of arguments in the function declaration and not as in the call expression.
For example :
class SomeClass
{
public:
int sum(int x, int y)
{
return x + y;
}
int sum_def(int x, int y = 0)
{
return x + y;
}
};
int main()
{
SomeClass x;
x.sum(10, 100);
x.sum_def(20);
return 0;
}
traverse ast using liblcang :
def traverse(node):
if node.kind == clang.cindex.CursorKind.CALL_EXPR:
print('%-35s %-20s %-10s [%-6s:%s - %-6s:%s] %s %s ' % (
node.kind, node.spelling, node.type.spelling, node.extent.start.line, node.extent.start.column,
node.extent.end.line, node.extent.end.column, node.location.file, node.mangled_name))
print("%d " % node.get_num_arguments())
for arg in node.get_arguments():
print("ARG=%s %s" % (arg.kind, arg.spelling))
for child in node.get_children():
traverse(child)
The output :
CursorKind.CALL_EXPR sum int [20 :5 - 20 :19] main.cpp
2
ARG=CursorKind.INTEGER_LITERAL
ARG=CursorKind.INTEGER_LITERAL
CursorKind.CALL_EXPR sum_def int [21 :5 - 21 :18] main.cpp
2
ARG=CursorKind.INTEGER_LITERAL
ARG=CursorKind.UNEXPOSED_EXPR
I would expect to get 1 as number of arguments in the CALL_EXPR for sum_def, it is possible to get it or should I parse the code myself, something I don't really want to do. Thank you.