Debian Codegraph @

pycparser223/examples/func_calls.py

raw symbols SCIP
1 #-----------------------------------------------------------------
2 # pycparser: func_calls.py
3 #
4 # Using pycparser for printing out all the calls of some function
5 # in a C file.
6 #
7 # Eli Bendersky [https://eli.thegreenplace.net/]
8 # License: BSD
9 #-----------------------------------------------------------------
10 import sys
11
12 # This is not required if you've installed pycparser into
13 # your site-packages/ with setup.py
14 sys.path.extend(['.', '..'])
15
16 from pycparser import c_ast, parse_file
17
18 # A visitor with some state information (the funcname it's looking for)
19 class FuncCallVisitor(c_ast.NodeVisitor):
20 def __init__(self, funcname):
21 self.funcname = funcname
22
23 def visit_FuncCall(self, node):
24 if node.name.name == self.funcname:
25 print('%s called at %s' % (self.funcname, node.name.coord))
26 # Visit args in case they contain more func calls.
27 if node.args:
28 self.visit(node.args)
29
30
31 def show_func_calls(filename, funcname):
32 ast = parse_file(filename, use_cpp=True)
33 v = FuncCallVisitor(funcname)
34 v.visit(ast)
35
36
37 if __name__ == "__main__":
38 if len(sys.argv) > 2:
39 filename = sys.argv[1]
40 func = sys.argv[2]
41 else:
42 filename = 'examples/c_files/basic.c'
43 func = 'foo'
44
45 show_func_calls(filename, func)