Finding invocations of a certain function in a c++ file using python

I need to find all occurrences of a function call in a C++ file using python, and extract the arguments for each call.
I'm playing with the pygccxml package, and extracting the arguments given a string with the function call is extremely easy:

from pygccxml.declarations import call_invocation

def test_is_call_invocation(call):
    if call_invocation.is_call_invocation(call):
        for arg in call_invocation.args(call):
            print "   ",arg
        print "not a function invocation"

What I couldn't find is a way of getting the calls parsing a file:

from pygccxml import parser
from pygccxml import declarations

decls = parser.parse( ['main.cpp'] )
# ...

Is there a way to find the calls to a certain function using the pygccxml package?
Or maybe that package is an overkill for what I'm trying to do :) and there's a much simpler way? Finding the function calls with a regular expression is, I'm afraid, much trickier than it might look at a first sight...

Asked by: Tara580 | Posted: 27-01-2022

Answer 1

XML-GCC can't do that, because it only reports the data types (and function signatures). It ignores the function bodies. To see that, create

void foo()

void bar()

and then run gccxml -fxml=a.xml. Look at the generated a.xml, to see that the only mentioning of foo (or its id) is in the declaration of foo.

An alternative might be available in codeviz ( It consists of a patch to gcc 3.4.6 that generates call dependency information - plus some perl scripts that generate graphviz input; the latter you can safely ignore.

As yet another alternative (which doesn't need gcc modifications) you could copy the approach from egypt (; this parses GCC RTL dumps. It should work with any recent GCC, however, it might be that you don't get calls to inline functions.

In any case, with these approaches, you won't get "calls" to macros, but that might be actually the better choice.

Answered by: Ted683 | Posted: 28-02-2022

Similar questions

Chained Python class invocations

I am trying to build a set of classes to define hierarchical properties for protocols in the OSI stack... in an abstract sense, I just need to inherit properties from parent python classes, but I need to be able to invoke the entire class chain at once... so, I'm looking for something like this... #!/usr/bin/env python class Foo(object): def __init__(self,fooprop1=None): return None class Bar(F...

python - Safe and lazy method invocations in PySide/PyQt

I'm using PySide to manage some hardware and perform some relatively simple operations depending on (e.g.) button clicks in the interface. The code for running each of these pieces of hardware resides in another thread. For convenience, to all of those hardware drivers I've added a generic invoke_method signal, such that a UI component can use my_driver.invoke_method.emit('method_name', [arg, ....

python - celery - same parameters, multiple invocations

Either I haven't understood how Celery works, or it works strange for me. I have following module: from celery.task import task import time @task def add(x, y): time.sleep(x + y) return "x+y=%s" % (x + y) launched celeryd with command:

python - twisted reactor invocations on separate thread

I am working on an application that involves fetching data over tcp using twisted api. Our process is listener application that keeps listening to events and does the following.. Process event notification and build dictionary to sent it to third party application In order to complete calls a process using twisted api to get some additional data and complete the dictionary. ...

python - Is it possible to maintain state between Postgres trigger invocations?

I am implementing a trigger in PostgreSQL using Python that sends an AMQP message via amqpy. A connection is required to send the message. Currently, the only way I know to do this is, when the trigger is invoked, open a connection, send the message, and close the connection. I would like to not have to keep opening a new connection for every trigger invocation. Is there a way to initialize the connection...

python - Scrapy: saving cookies between invocations

Is there a way to preserve cookies between invocations of a scrapy crawler? The purpose - the site requires log in, and then maintains the session via cookies. I'd rather reuse the session than re-login every time.

parsing - How to parse Unix-like command invocations contained in a text file in Python?

We have a bunch of shell scripts with multiple calls to a specific tool with its respective command line arguments, e.g: some_tool -a param -b param -c param,param,param some_tool -d param -f param some_other_tool -x another_param -Y -z params,params,params etc. How can text files containing these calls be parsed and processed cleanly in Python? Is there a library that is intended specific...

python - Multiple rs.exe invocations

I've a python based SSRS report generation utility that I'm using to generate multiple reports (often 100+). The way it's setup is - Multiple threads are invoked using threading.Thread and each of them is given a dictionary. Each thread parses the dictionary and calls rs.exe passing in relevant arguments via python's Reports get generated with the following caveats -

How to pipe multi-line JSON Objects into separate python invocations

I know the basics of piping stdin to downstream processes in the shell and as long as each line is treated individually, or as one single input, I can get my pipelines to work. But when I want to read 4 lines of stdin, do some processing, read 6 more lines, and do the same, my limited of understanding of pipelines becomes an issue. For example, in the below pipeline, each curl invocation produces an unknow...

python - How to see C++ function invocations behind the SWIG interface, TensorFlow

I'm working on TensorFlow and I want to know the relationship between each Python function and the correspondent C++ functions behind the SWIG interface. In other words, I want to be able to know exactly which C++ functions are invoked for every line of Python code of my TensorFlow application. I already saw how to debug the Python code

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python