Debian Codegraph @

share/analysis-scripts/heuristic_list_functions.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 uses heuristics to list all function definitions and
26 declarations in a set of files."""
27
28 import sys
29 import os
30 import function_finder
31
32 debug = os.getenv("DEBUG")
33
34 arg = ""
35 if len(sys.argv) < 4:
36 print("usage: %s want_defs want_decls file..." % sys.argv[0])
37 print(" looks for likely function definitions and/or declarations")
38 print(" in the specified files.")
39 sys.exit(1)
40
41
42 def boolish_string(s):
43 if s.lower() == "true" or s == "1":
44 return True
45 if s.lower() == "false" or s == "0":
46 return False
47 sys.exit(f"error: expected 'true', 'false', 0 or 1; got: {s}")
48
49
50 want_defs = boolish_string(sys.argv[1])
51 want_decls = boolish_string(sys.argv[2])
52 files = sys.argv[3:]
53
54 for f in files:
55 with open(f, encoding="ascii", errors="ignore") as data:
56 file_content = data.read()
57 file_lines = file_content.splitlines(keepends=True)
58 newlines = function_finder.compute_newline_offsets(file_lines)
59 defs_and_decls = function_finder.find_definitions_and_declarations(
60 want_defs, want_decls, f, file_content, file_lines, newlines
61 )
62 for (funcname, is_def, start, end, _offset) in defs_and_decls:
63 if is_def:
64 print(f"{os.path.relpath(f)}:{start}:{end}: {funcname} (definition)")
65 else:
66 print(f"{os.path.relpath(f)}:{start}:{end}: {funcname} (declaration)")