Debian Codegraph @

share/analysis-scripts/list_files.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 parses a compile_commands.json file and lists the C files in it."""
26
27 # See: http://clang.llvm.org/docs/JSONCompilationDatabase.html
28
29 import sys
30 import os
31 import json
32 import re
33 from pathlib import Path
34
35 if len(sys.argv) < 2:
36 # no argument, assume default name
37 arg = Path("compile_commands.json")
38 else:
39 arg = Path(sys.argv[1])
40
41 if not arg.exists():
42 sys.exit(f"error: file '{arg}' not found")
43
44 # check if arg has a known extension
45 def is_known_c_extension(ext):
46 return ext in (".c", ".i", ".ci", ".h")
47
48
49 pwd = os.getcwd()
50 fcmake_pwd = Path(pwd) / ".frama-c" # pwd as seen by the Frama-C makefile
51 with open(arg) as data:
52 jcdb = json.load(data)
53 jcdb_dir = arg.parent
54 includes = set()
55 defines = set()
56 files = set() # set of pairs of (file, file_for_fcmake)
57 for entry in jcdb:
58 arg_includes = [] # before normalization
59 if not "file" in entry:
60 # ignore entries without a filename
61 continue
62 filename = Path(entry["file"])
63 directory = Path(entry["directory"]) if "directory" in entry else None
64 if filename.is_absolute():
65 filepath = filename
66 elif directory and directory.is_absolute():
67 filepath = directory / filename
68 elif directory:
69 filepath = jcdb_dir / directory / filename
70 else:
71 filepath = jcdb_dir / filename
72 if not is_known_c_extension(filepath.suffix):
73 print(f"warning: ignoring file of unknown type: {filepath}")
74 else:
75 files.add((os.path.relpath(filepath, pwd), os.path.relpath(filepath, fcmake_pwd)))
76
77 files_for_fcmake = map(lambda x: (x[1]), files)
78 print("# Paths as seen by a makefile inside subdirectory '.frama-c':")
79 print("SRCS=\\\n" + " \\\n".join(sorted(files_for_fcmake)) + " \\")
80 print("")
81
82 files_defining_main = set()
83 re_main = re.compile(r"(int|void)\s+main\s*\([^)]*\)\s*\{")
84 for (filename, file_for_fcmake) in files:
85 assert os.path.exists(filename), "file does not exist: %s" % filename
86 with open(filename, "r") as content_file:
87 content = content_file.read()
88 res = re.search(re_main, content)
89 if res is not None:
90 files_defining_main.add(file_for_fcmake)
91
92 if files_defining_main != []:
93 print("")
94 print(
95 "# Possible definition of main function in the following file(s), as seen from '.frama-c':"
96 )
97 print("\n".join(sorted(files_defining_main)))