Skip to content
Snippets Groups Projects
Commit 49eaa67c authored by Andre Maroneze's avatar Andre Maroneze
Browse files

[Analysis-scripts] update list-files

parent 8bcc8742
No related branches found
No related tags found
No related merge requests found
...@@ -31,58 +31,69 @@ import sys ...@@ -31,58 +31,69 @@ import sys
import os import os
import json import json
import re import re
from pathlib import Path
MIN_PYTHON = (3, 6) # for glob(recursive) and automatic Path conversions
if sys.version_info < MIN_PYTHON:
sys.exit("Python %s.%s or later is required.\n" % MIN_PYTHON)
arg = ""
if len(sys.argv) < 2: if len(sys.argv) < 2:
# no argument, assume default name # no argument, assume default name
arg = "compile_commands.json" arg = Path("compile_commands.json")
else: else:
arg = sys.argv[1] arg = Path(sys.argv[1])
if not os.path.exists(arg): if not arg.exists():
print(f"error: file '{arg}' not found") print(f"error: file '{arg}' not found")
sys.exit(f"usage: {sys.argv[0]} [compile_commands.json]") sys.exit(f"usage: {sys.argv[0]} [compile_commands.json]")
# check if arg has a known extension # check if arg has a known extension
def has_known_c_extension(arg): def is_known_c_extension(ext):
return arg.endswith(".c") or arg.endswith(".i") or arg.endswith(".h") return ext == ".c" or ext == ".i" or ext == ".h"
pwd = os.getcwd() pwd = os.getcwd()
fcmake_pwd = pwd / Path(".frama-c") # pwd as seen by the Frama-C makefile
json = json.loads(open(arg).read()) json = json.loads(open(arg).read())
jcdb_dir = os.path.dirname(arg) jcdb_dir = arg.parent
includes = set() includes = set()
defines = set() defines = set()
files = set() files = set() # set of pairs of (file, file_for_fcmake)
for entry in json: for entry in json:
arg_includes = [] # before normalization arg_includes = [] # before normalization
dir = entry["directory"] if not "file" in entry:
file = entry["file"] # ignore entries without a filename
# json compile spec says either command or arguments are mandatory continue
if os.path.isabs(file): file = Path(entry["file"])
dir = Path(entry["directory"]) if "directory" in entry else None
if file.is_absolute():
filepath = file filepath = file
elif os.path.isabs(dir): elif dir and dir.is_absolute():
filepath = os.path.join(dir, file) filepath = dir / file
elif dir:
filepath = jcdb_dir / dir / file
else: else:
filepath = os.path.join(jcdb_dir, dir, file) filepath = jcdb_dir / file
if not has_known_c_extension(filepath): if not is_known_c_extension(filepath.suffix):
print(f"warning: ignoring file of unknown type: {filepath}") print(f"warning: ignoring file of unknown type: {filepath}")
else: else:
files.add(os.path.relpath(filepath, pwd)) files.add((os.path.relpath(filepath, pwd), os.path.relpath(filepath, fcmake_pwd)))
print("SRCS=\\\n" + " \\\n".join(sorted(files)) + " \\") files_for_fcmake = map (lambda x:(x[1]), files)
print("# Paths as seen by a makefile inside subdirectory '.frama-c':")
print("SRCS=\\\n" + " \\\n".join(sorted(files_for_fcmake)) + " \\")
print("") print("")
files_defining_main = set() files_defining_main = set()
re_main = re.compile("(int|void)\s+main\s*\([^)]*\)\s*\{") re_main = re.compile("(int|void)\s+main\s*\([^)]*\)\s*\{")
for file in files: for (file, file_for_fcmake) in files:
assert os.path.exists(file), "file does not exist: %s" % file assert os.path.exists(file), "file does not exist: %s" % file
with open(file, 'r') as content_file: with open(file, 'r') as content_file:
content = content_file.read() content = content_file.read()
res = re.search(re_main, content) res = re.search(re_main, content)
if res is not None: if res is not None:
files_defining_main.add(file) files_defining_main.add(file_for_fcmake)
if files_defining_main != []: if files_defining_main != []:
print("") print("")
print("# Possible definition of main function in the following file(s):") print("# Possible definition of main function in the following file(s), as seen from '.frama-c':")
print("\n".join(sorted(files_defining_main))) print("\n".join(sorted(files_defining_main)))
# Paths as seen by a makefile inside subdirectory '.frama-c':
SRCS=\ SRCS=\
tests/fc_script/main.c \ ../tests/fc_script/main.c \
tests/fc_script/main2.c \ ../tests/fc_script/main2.c \
tests/fc_script/main3.c \ ../tests/fc_script/main3.c \
# Possible definition of main function in the following file(s): # Possible definition of main function in the following file(s), as seen from '.frama-c':
tests/fc_script/main.c ../tests/fc_script/main.c
tests/fc_script/main3.c ../tests/fc_script/main3.c
# Paths as seen by a makefile inside subdirectory '.frama-c':
SRCS=\ SRCS=\
tests/jcdb/file_without_main.c \ ../tests/jcdb/file_without_main.c \
tests/jcdb/jcdb.c \ ../tests/jcdb/jcdb.c \
# Possible definition of main function in the following file(s): # Possible definition of main function in the following file(s), as seen from '.frama-c':
tests/jcdb/jcdb.c ../tests/jcdb/jcdb.c
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment