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

[Analysis-scripts] fix and update normalize-jcdb

parent 49eaa67c
No related branches found
No related tags found
No related merge requests found
......@@ -270,6 +270,7 @@ DISTRIB_FILES:=\
share/analysis-scripts/list_files.py \
share/analysis-scripts/make_template.py \
share/analysis-scripts/make_wrapper.py \
share/analysis-scripts/normalize_jcdb.py \
share/analysis-scripts/parse-coverage.sh \
share/analysis-scripts/prologue.mk \
share/analysis-scripts/README.md \
......@@ -1955,6 +1956,7 @@ install:: install-lib-$(OCAMLBEST)
share/analysis-scripts/list_files.py \
share/analysis-scripts/make_template.py \
share/analysis-scripts/make_wrapper.py \
share/analysis-scripts/normalize_jcdb.py \
share/analysis-scripts/parse-coverage.sh \
share/analysis-scripts/prologue.mk \
share/analysis-scripts/README.md \
......
......@@ -208,38 +208,6 @@ configure_for_frama_c() {
CPP="gcc -E -nostdinc -fno-builtin -I${FRAMAC_SHARE}/libc -D__FC_MACHDEP_${MACHDEP}" ./configure "$@"
}
normalize_jcdb() {
path=""
if [ "$#" -eq 0 ]; then
path="./compile_commands.json"
else
path="$1"
fi
if [ ! -e "$path" ]; then
echo "error: cannot find file: $path";
exit 1
fi
sed "s|$PWD/||g" "$path" > "${path}.tmp"
cmp -s "$path" "${path}.tmp"
if [ $? -eq 0 ]; then
echo "No changes to be applied to $path"
rm "${path}.tmp"
else
echo "Differences to be applied to $path:"
diff -u0 "$path" "${path}.tmp"
read -p "Normalize $path? [y/N] " yn
case $yn in
[Yy])
mv "${path}.tmp" "$path"
echo "Normalization applied to $path"
;;
*)
echo "Exiting without overwriting."
exit 0;;
esac
fi
}
case "$command" in
"help" | "-help" | "--help" | "-h")
usage 0;
......@@ -279,7 +247,7 @@ case "$command" in
;;
"normalize-jcdb")
shift;
normalize_jcdb "$@";
${FRAMAC_SHARE}/analysis-scripts/normalize_jcdb.py "$@";
;;
*)
echo "error: unrecognized command: $command";
......
......@@ -127,6 +127,7 @@ share/analysis-scripts/git_utils.py: .ignore
share/analysis-scripts/list_files.py: .ignore
share/analysis-scripts/make_template.py: .ignore
share/analysis-scripts/make_wrapper.py: .ignore
share/analysis-scripts/normalize_jcdb.py: .ignore
share/analysis-scripts/parse-coverage.sh: .ignore
share/analysis-scripts/prologue.mk: CEA_LGPL
share/analysis-scripts/README.md: .ignore
......
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
##########################################################################
# #
# This file is part of Frama-C. #
# #
# Copyright (C) 2007-2020 #
# CEA (Commissariat à l'énergie atomique et aux énergies #
# alternatives) #
# #
# you can redistribute it and/or modify it under the terms of the GNU #
# Lesser General Public License as published by the Free Software #
# Foundation, version 2.1. #
# #
# It is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU Lesser General Public License for more details. #
# #
# See the GNU Lesser General Public License version 2.1 #
# for more details (enclosed in the file licenses/LGPLv2.1). #
# #
##########################################################################
# This script removes absolute path references in a JSON Compilation Database.
#
# See: http://clang.llvm.org/docs/JSONCompilationDatabase.html
import sys
import os
import json
import re
from pathlib import Path
MIN_PYTHON = (3, 6) # for automatic Path conversions
if sys.version_info < MIN_PYTHON:
sys.exit("Python %s.%s or later is required.\n" % MIN_PYTHON)
if len(sys.argv) < 2:
# no argument, assume default name
arg = Path("compile_commands.json")
else:
arg = Path(sys.argv[1])
if not arg.exists():
print(f"error: file '{arg}' not found")
sys.exit(f"usage: {sys.argv[0]} [compile_commands.json]")
jcdb_json = json.loads(open(arg).read())
jcdb_dir = arg.parent
out_json = {}
nb_diffs = 0
for entry in jcdb_json:
if "file" in entry and os.path.isabs(entry["file"]):
old_entry = entry["file"]
entry["file"] = os.path.relpath(entry["file"], jcdb_dir)
if old_entry != entry["file"]:
nb_diffs += 1
else:
print(f"warning: absolute path could not be normalized: {entry['file']}")
elif "directory" in entry and os.path.isabs(entry["directory"]):
old_entry = entry["directory"]
entry["directory"] = os.path.relpath(entry["directory"], jcdb_dir)
if old_entry != entry["directory"]:
nb_diffs += 1
else:
print(f"warning: absolute path could not be normalized: {entry['directory']}")
if nb_diffs == 0:
print(f"No changes to be applied to {arg}")
else:
yn = input(f"{nb_diffs} replacements to be applied. Normalize {arg}? [y/N] ")
if yn.lower() == "y":
with open(arg, 'w', encoding='utf-8') as outfile:
json.dump(jcdb_json, outfile, ensure_ascii=False, indent=4)
print(f"Normalization applied to {arg}")
else:
print("Exiting without overwriting.")
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