blob: f56f153f7e69d1e56d2bcd7b37f212f1ea034b04 [file] [log] [blame]
Eli Benderskya2915862012-06-23 06:25:53 +03001#-----------------------------------------------------------------
2# pycparser: func_defs.py
3#
4# Using pycparser for printing out all the calls of some function
5# in a C file.
6#
7# Copyright (C) 2008-2011, Eli Bendersky
8# License: BSD
9#-----------------------------------------------------------------
10from __future__ import print_function
11import sys
12
13# This is not required if you've installed pycparser into
14# your site-packages/ with setup.py
15#
16sys.path.extend(['.', '..'])
17
18from pycparser import c_parser, c_ast, parse_file
19
20
21# A visitor with some state information (the funcname it's
22# looking for)
23#
24class FuncCallVisitor(c_ast.NodeVisitor):
Eli Bendersky3921e8e2010-05-21 09:05:39 +030025 def __init__(self, funcname):
Eli Benderskya2915862012-06-23 06:25:53 +030026 self.funcname = funcname
27
28 def visit_FuncCall(self, node):
29 if node.name.name == self.funcname:
30 print('%s called at %s' % (
31 self.funcname, node.name.coord))
32
33
34def show_func_calls(filename, funcname):
35 ast = parse_file(filename, use_cpp=True)
36 v = FuncCallVisitor(funcname)
37 v.visit(ast)
38
39
40if __name__ == "__main__":
41 if len(sys.argv) > 2:
42 filename = sys.argv[1]
43 func = sys.argv[2]
44 else:
45 filename = 'c_files/hash.c'
46 func = 'malloc'
47
48 show_func_calls(filename, func)