Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame^] | 1 | #-----------------------------------------------------------------
|
| 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, Eli Bendersky
|
| 8 | # License: LGPL
|
| 9 | #-----------------------------------------------------------------
|
| 10 | import sys
|
| 11 |
|
| 12 | # This is not required if you've installed pycparser into
|
| 13 | # your site-packages/ with setup.py
|
| 14 | #
|
| 15 | sys.path.insert(0, '..')
|
| 16 |
|
| 17 | from pycparser import c_parser, c_ast, parse_file
|
| 18 | from pycparser.portability import printme
|
| 19 |
|
| 20 |
|
| 21 | # A visitor with some state information (the funcname it's
|
| 22 | # looking for)
|
| 23 | #
|
| 24 | class FuncCallVisitor(c_ast.NodeVisitor):
|
| 25 | def __init__(self, funcname): |
| 26 | self.funcname = funcname
|
| 27 |
|
| 28 | def visit_FuncCall(self, node):
|
| 29 | if node.name.name == self.funcname:
|
| 30 | printme('%s called at %s\n' % (
|
| 31 | self.funcname, node.name.coord))
|
| 32 |
|
| 33 |
|
| 34 | def show_func_calls(filename, funcname):
|
| 35 | ast = parse_file(filename, use_cpp=True)
|
| 36 | v = FuncCallVisitor(funcname)
|
| 37 | v.visit(ast)
|
| 38 |
|
| 39 |
|
| 40 | if __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)
|
| 49 |
|
| 50 |
|
| 51 |
|