summaryrefslogtreecommitdiff
path: root/one-off/find-expert-in-tree
blob: 59e3d24b7bf05a08cdaf06d833df26377160cd29 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
# Searches for functions adding expert items which are located in a if(tree)
# guard. Tested using clang-query 3.7.0 (part of clang-tools-extra).
#
# Usage:
#
#   Ensure that builddir and srcdir variables below are set
#   Ensure that compile_commands.json exists in builddir (see below).
#   Run `./find-expert-in-tree > scan.log` to write matching contexts to file
#   Use the displayed awk command for further processing (see bottom).
#
# Author: Peter Wu <peter@lekensteyn.nl>

# path to builddir (should contain compile_commands.json, use
# cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1)
builddir=/tmp/wsbuild
# path to source directory (will be scanned for files matching the function
srcdir=/tmp/wireshark

set -e -u

# Condition which should match "if(tree)" and "if(tree && foo())" but not
# "if(somefunc(tree))".
cond='
hasCondition(
    # Do not use hasDescendant, it matches if(somefunc(tree)) too
    has(
        declRefExpr(
            hasType(
                asString("proto_tree *")
            )
        )
    )
)
'

# matches callers of expert_add_info[_format] and proto_tree_add_export[_format]
body='
callee(
    functionDecl(
        hasAnyParameter(
            matchesName("eiindex")
        )
    )
)
'

# print line with "if"
matcher="
ifStmt(
    allOf(
        $cond,
        hasDescendant(
            callExpr($body)
        )
    )
)
"

# print line with caller (comment next line to enable it)
: || \
matcher="
callExpr(allOf(
    $body,
    hasAncestor(
        ifStmt($cond)
    )
))
"

matcher="$(sed '/^ *#/d;s/^ *//;s/ *$//' <<<"$matcher" | tr -d '\n')"
args=(
    # Add this if you get an error about missing std headers
    #-extra-arg=-I/usr/lib/clang/3.7.0/include/
    -p "$builddir"

    # Use "print" to show the full processed line. Use "diag" for a short line.
    # Use "dump" for a raw AST tree.
    -c "set output print"
    #-c "set output dump"
    -c "set output diag"

    -c "match ${matcher}"
)

# Write file names matching the function names to a file (cache it in case this
# script is interrupted).
tmp=/tmp/files.txt
[ -s "$tmp" ] ||
grep -rl --exclude=\* --include=\*.c --exclude-dir=asn1 "$srcdir" \
    -e 'expert_add_info\|proto_tree_add_expert' > "$tmp"

# Start the hunt!
cat "$tmp" |
#head | grep ssl|
xargs -rt -P$(nproc) -n10 clang-query "${args[@]}"
# add -t to xargs for verbose debugging (print commands as they are executed)
# -P is used for parallel jobs
# -n limits files per command. Use this to tune memory usage.

rm "$tmp"
cat <<'EOF' >&2

# Use this for analysis:
awk '/"[r]oot" binds here/{i=3} !/clang-query/&&i>0{printf("%s\033[m\n", $0);i--}' scan.log | less -r
EOF

# vim: set sw=4 ts=4 et: