-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmpfuncs
executable file
·56 lines (49 loc) · 1.38 KB
/
cmpfuncs
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
#!/usr/bin/env bash
#
# cmpfuncs
# - compares all functions that exist in both source files $1 and $2
# - any remaining args restrict comparison to only those function names
# - system must support "ctags -x" (ie exuberant, universal)
# - outputs universal diff hunks for each differing function, or nothing
# - function signature line (the ctags referent line) is not compared
# - functions must end on a line with trailing brace in column 0
# - only tested with C files
#
# scott@smemsh.net
# https://github.com/smemsh/utilsh/
# https://spdx.org/licenses/GPL-2.0
#
for f in ${1:?} ${2:?}
do test -f $f -a -r $f || exit; done
if (($# > 2)); then
declare -A onlyfuncs
for f in ${@:3}; do onlyfuncs[$f]=1; done
fi
printfuncs ()
{
ctags -x ${1:?} \
| awk '$2 == "function" {print $1, $3}'
}
displayfunc ()
{
echo "$sigline" # always use file2's signature line
tail -n +$(($2 + 1)) $1 | grep -m1 -B999 '^}'
}
eval declare -A funcs1=($(printfuncs $1))
eval declare -A funcs2=($(printfuncs $2))
for func in ${!funcs1[@]}
do
loc1=${funcs1[$func]}
loc2=${funcs2[$func]}
[[ $loc2 ]] || continue
((${onlyfuncs[$func]})) || continue
declare -x sigline="$(sed -n ${loc2}p $2)"
diffout="$(diff -pwu <(displayfunc $1 $loc1) <(displayfunc $2 $loc2))"
if [[ ${diffout:+x} ]]; then
sed -r \
-e "/^-{3}/s,/dev/fd/.*\\>,$1," \
-e "/^\\+{3}/s,/dev/fd/.*\\>,$2," \
<<< "$diffout"
echo
fi
done