-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_import
executable file
·80 lines (57 loc) · 2.14 KB
/
check_import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# This script check the use of modules, and reports when a module is imported withoug being used
# License MIT
SOURCE=$REFRESCO_CODE_DIR/src
SCRIPT=check_import_cleanup.sh
gen_line() {
printf "%${1}s\n" "" | sed 's/\ /-/g'
}
if [[ "$1" ]]
then
fileList=$(echo "$@" | sed 's/ /\n/g')
else
fileList=$(find $SOURCE -maxdepth 1 -name "*.F90" | sort)
fi
echo '#!/bin/bash' > $SCRIPT
while read file
do
gen_line 110
echo $file
USEModule=$(grep "^USE " $file | awk '{print $2}')
localVariableList=$(ctags -f - -x "$file" | awk '{print $2 " " $1}' | grep -E '^variable' | awk '{print $2}')
while read module
do
modulefile=$SOURCE/${module}.F90
if [[ ! -e "$modulefile" ]]
then
modulefile=$SOURCE/automatic/${module}.F90
if [[ ! -e "$modulefile" ]]
then
echo $module not found
continue
fi
fi
functionList=$(ctags -f - -x "$modulefile" | awk '{print $2 " " $1}' | grep -E '^subroutine|^function' | awk '{print " "$2}')
interfaceList=$(sed 's/\!.*$//' "$modulefile" | grep "^INTERFACE" | awk '{print " "$NF}' | sed 's/)//;s/\ (//')
variableList=$(ctags -f - -x "$modulefile" | awk '{print $2 " " $1}' | grep -E '^variable' | awk '{print $2}' | grep -v -wFf <(echo -e "$localVariableList"))
functionFound=$(grep -iFf <(echo -e "$functionList\n$interfaceList" | sed '/^$/d') <(sed s/\!.*$// "$file"))
variableFound=$(grep -iFf <(echo -e "$variableList") <(sed s/\!.*$// "$file"))
if [[ -z "$functionFound" ]] && [[ -z "$variableFound" ]]
then
echo $module
echo sed -i s/\\^USE\\ $module/\\!check_import\\ USE\\ $module/ \"$file\" >> "$SCRIPT"
fi
done <<<"$USEModule"
echo
done <<<"$fileList"
echo
echo ----
echo
echo "To apply the changes run:"
echo "bash $SCRIPT"
echo
echo "To revert back to the original files run:"
echo sed -i s/\\^\\!check_import\\ // $SOURCE/\*.F90
echo
echo "Finally, to definitively remove the check_import lines (once everything compiles), run:"
echo sed -i /\\^\\!check_import\\ /d $SOURCE/\*.F90