Debian Codegraph @

share/analysis-scripts/print_callgraph.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 ##########################################################################
4 # #
5 # This file is part of Frama-C. #
6 # #
7 # Copyright (C) 2007-2022 #
8 # CEA (Commissariat à l'énergie atomique et aux énergies #
9 # alternatives) #
10 # #
11 # you can redistribute it and/or modify it under the terms of the GNU #
12 # Lesser General Public License as published by the Free Software #
13 # Foundation, version 2.1. #
14 # #
15 # It is distributed in the hope that it will be useful, #
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
18 # GNU Lesser General Public License for more details. #
19 # #
20 # See the GNU Lesser General Public License version 2.1 #
21 # for more details (enclosed in the file licenses/LGPLv2.1). #
22 # #
23 ##########################################################################
24
25 """This script finds files containing likely declarations and definitions
26 for a given function name, via heuristic syntactic matching."""
27
28 import sys
29 import build_callgraph
30
31 dotfile = None
32 args = sys.argv[1:]
33 if "--dot" in args:
34 dotarg = args.index("--dot")
35 dotfile = args[dotarg + 1]
36 args_before = args[: dotarg - 1] if dotarg > 0 else []
37 args_after = args[dotarg + 2 :]
38 args = args_before + args_after
39 if not args:
40 sys.exit(
41 f"""\
42 usage: {sys.argv[0]} [--dot outfile] file1 file2 ...
43 prints a heuristic callgraph for the specified files.
44 If --dot is specified, print in DOT (Graphviz) format
45 to file outfile, or to stdout if outfile is '-'."""
46 )
47
48 cg = build_callgraph.compute(args)
49 if dotfile:
50 if dotfile == "-":
51 out = sys.stdout
52 else:
53 out = open(dotfile, "w")
54 build_callgraph.print_cg_dot(cg, out)
55 if dotfile != "-":
56 out.close()
57 print(f"wrote {dotfile}")
58 else:
59 build_callgraph.print_cg(cg)