KernelNewbies:

The script below can be used for static code analysis. It requires git, sed and grep

# (c) [kernelnewbies.org/roelkluin Roel Kluin], 2009, GPL v2.
#!/bin/bash

# blank_it: replaces spaces with bracket expressions to match
# optional/obligatory spaces for usage with egrep.
bli2()
{
local int="[[:digit:]]"
local hex="[[:xdigit:]]"
local up_="[[:upper:]_]"
local al_="[[:alpha:]_]"
local AN_="[[:upper:][:digit:]_]"
local an_="[[:alnum:]_]"
local sp="[[:space:]]"
local V="$al_+$an_*"
local em='!'

local l="$1"
l="${l//@V/$V}"                 # variable/function/struct name
l="${l//@Q2/[^[:alnum:]_>.]}"   # 1 char left of variable/function/struct name
l="${l//@Q/[^[:alnum:]_]}"      # 1 char right of variable/function/struct name
l="${l//@d/($int+[uUlLfF]?|$int+[uU]?[lL][lL]?|0x$hex+|$int+[lL][lL][uU]|$int*\.$int+[fF]?)}"   # a number
l="${l//@K/$up_+$AN_*}"         # an definition or macro name (uppercase)
l="${l//@s/$sp*}"               # optional space
l="${l//@S/$sp+}"               # obligatory space
l="${l//\\$em/$em}"             # Because bash bangs otherwise
l="${l//@w/($V|\.|->|\[$sp*[^][]+$sp*\]|\($sp*[^)(]*$sp*\))+}"          # a variable/array/member/unnested function or macro
l="${l//\\\(...\\\)/\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)}"  # nested parentheses, 2 backrefs
l="$(echo "$l" | sed -r "s/\\\\\{\.\.\.\\\\\}/\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}/g")" # nested curly brackets, 2 backrefs
 echo "$l" | sed -r "
:a
s/($an_)$sp+($an_)/\1$sp+\2/g
s/(\[\[:space:\]\]\*)*$sp+/$sp*/g
$!{
 N
 ba
}
"
}

# echo a sed script to catch from multiline grep $n (where n=$#) and filter out $1 up to  ${n-1}.
# e.g. grep -E -n -H -B1 -A10 "foo" ~/file | sed -n -r "$(ecsed2 "foo.*bar" "foo")"
# searches for lines with `foo', not followed by `bar'
ecsed()
{
local app;
if [ "${1:0:6}" = "--PrE=" ]; then
 app="${1:6}"; shift;
fi
local sp="[[:space:]]";         # space
local ns="[^[:space:]]";        # no space
local bol="$ns*[:-][0-9]+[:-]"  # begin of line
local not;
while [ -n "$2" ]; do
 not="$not/$(bli2 "$app$1")/b;"
 shift;
done
echo ":start
/(--|$bol}$s)$/!{
N
b start
}
s/$sp*\/\*+([^*]*|\*[^\/])*\*+\// /g    # remove C style comment
s/(\/\/[^\n]*)?\n$bol/\n/g              # remove C99 comment
$not
s/^($ns*)[:-]([0-9]+)[:-]/---[ vi \1 +\2 ]---\n/
s/\n--$//
/$(bli2 "$app$1")/p"
}

# gres does something like
# git grep -E -n [-opts] "[parsed $1]" | sed -n -r "[parsed ${@:2}]"
gres()
{
 local opts; local o; local append; local gq; local sq;
 while [ $# -ne 0 ]; do
  [ "${1:0:1}" != "-" ] && break;
  [ "${1:0:6}" != "--PrE=" ] && opts="$opts $1" || gq="${1:6}";
  shift;
 done
 if [ -z "$gq" ]; then
  gq="$(bli2 "$1")";
  shift;
  sq="$(ecsed2 "$@")"
 else
  gq="$(bli2 "$gq")"
  sq="$(ecsed2 "--PrE=$gq" "$@")";
 fi
 git grep -E -n $opts "$gq" | sed -n -r "$sq"
}

To use, save, e.g. as ~/bin/cvars.sh and:

source ~/bin/cvars.sh

KernelNewbies: roelkluin/cvars (last edited 2009-09-21 13:21:59 by d133062)