diff options
author | Justin Boffemmyer | 2018-02-20 17:34:37 +0900 |
---|---|---|
committer | Justin Boffemmyer | 2018-02-20 17:34:37 +0900 |
commit | 3a7d6c03f93e86cb514530938648df81ac587b0f (patch) | |
tree | ac79339a5174cfdf260da6e40e77392e4ff93381 | |
parent | 069d0384e9c3e4e7c3eff87d70917ca4b7367d74 (diff) |
ward: allow specifying function prefixes
Add new functionality for limiting the functions that are tested by ward to
those matching a given prefix.
Currently, ward-functions with no arguments picks up some false positives that
are hard to eliminate in the general case. Re-running it with a specified
prefix can help test for specific problems without running into false
positives.
-rw-r--r-- | ward/libsource | 52 | ||||
-rwxr-xr-x | ward/ward-functions | 25 |
2 files changed, 51 insertions, 26 deletions
diff --git a/ward/libsource b/ward/libsource index e27d9f2..b82563f 100644 --- a/ward/libsource +++ b/ward/libsource @@ -410,6 +410,7 @@ function ward_error_variables() { ## @param -p base path to sources (optional) ## @param -l base path to libraries (optional) ## @param -e path to enchanter functions (optional) +## @param -P prefix to check function names for (optional) ## @param source1 [source2 source3...] (required) ## ## Tests whether any functions used in source1 (source2, source3, etc.) are @@ -418,7 +419,8 @@ function ward_error_variables() { ## found in via libcolor_file. Both base paths default to the current working ## directory if not supplied. Enchanter-specific functions are tested against ## the corresponding enchanter functions file, if supplied as an argument to the -## -e option. +## -e option. If a prefix is given via -P, then only functions beginning with +## the given prefix are checked. ## #------------------------------------------------------------------------------- function ward_functions() { @@ -453,6 +455,10 @@ function ward_functions() { epath="${2%/}" shift 2 ;; + "P") + prefix="${2}" + shift 2 + ;; *) libcolor_error "error: invalid function parameter $1" echo "" @@ -480,8 +486,7 @@ function ward_functions() { local aznum="[[:alnum:]]" local az_num="[[:alnum:]_]" local space="[[:space:]]" - #local needle="^${space}*${az}${aznum}_${az_num}\+\b[^[:punct:]]\?" - local needle="^${space}*${az}${aznum}\+_${az_num}\+$space\+" + local needle="^${space}*${prefix:-${az}${aznum}\+_}${az_num}\+$space\+" while [[ $# -gt 0 ]] do file="$1" @@ -506,7 +511,7 @@ function ward_functions() { local mangle="$function" # check against the prefixes, one at a time - while $(echo $mangle | grep -q '^[a-z][a-z0-9]*_[a-z0-9_]\+') + while $(echo $mangle | grep -q "^${az}${aznum}*_${az_num}\+") do prefix="${mangle%%_*}" @@ -528,21 +533,20 @@ function ward_functions() { esac # check for the function in a library file matching the prefix - if [[ -f "$lpath/lib$prefix" ]] - then - if grep -q "^function $function() {" "$lpath/lib$prefix" + for lib in "$lpath/lib"{,.}"$prefix" + do + if [[ -f "$lib" ]] then - matched="yes" - break - fi - elif [[ -f "$lpath/lib.$prefix" ]] - then - if grep -q "^function $function() {" "$lpath/lib.$prefix" - then - matched="yes" - break + if grep -q "^function $function() {" "$lib" + then + matched="yes" + break + fi fi - elif [[ -f "${epath:+$epath/functions}" ]] + done + + # check for the function in a given enchanter function file + if [[ -f "${epath:+$epath/functions}" ]] then if grep -q "^function $function() {" "$epath/functions" then @@ -557,13 +561,17 @@ function ward_functions() { if [[ "$matched" != "yes" ]] then - if [[ -f "$lpath/lib.${function#*_}" ]] - then - if grep -q "^function $function() {" "$lpath/lib.${function#*_}" + for lib in "$lpath/lib"{.,}"${function#*_}" + do + if [[ -f "$lib" ]] then - matched="yes" + if grep -q "^function $function() {" "$lib" + then + matched="yes" + break + fi fi - fi + done fi # if the function isn't defined in the corresponding file, warn the user diff --git a/ward/ward-functions b/ward/ward-functions index f744521..0a035e4 100755 --- a/ward/ward-functions +++ b/ward/ward-functions @@ -42,7 +42,7 @@ fi function ward_print_usage() { cat << EndUsage -Usage: $(basename $0) [-n] +Usage: $(basename $0) [-n] [-p PREFIX] [cauldron | enchantment] Calls the functions delinter to check whether there are functions called in the known cauldron and enchantment source files that aren't defined in the @@ -55,13 +55,24 @@ function ward_usage() { exit $ERR_FATAL } -while [[ "$#" -gt 0 ]] +PREFIXES=() +PREFIX= +while [[ -n "$@" ]] do case "$1" in # check if color should be disabled "-n") ward_color "no" ;; + # function name prefix to check against + "-p") + PREFIX="${1#-?}" + if [ -z "$PREFIX" ] ;then + shift + PREFIX="$1" + fi + PREFIXES[${#PREFIXES[@]}+1]="$PREFIX" + ;; # check if the user only wants to process cauldron "cauldron") PROCESS_ENCHANTMENT="no" @@ -78,6 +89,12 @@ do shift done +if [[ "${#PREFIXES[@]}" -gt 0 ]] +then + PREFIX=$(printf '%s\|' "${PREFIXES[@]}") + PREFIX="\\(${PREFIX%\\|}\\)" +fi + PROCESS_CAULDRON="${PROCESS_CAULDRON:-yes}" PROCESS_ENCHANTMENT="${PROCESS_ENCHANTMENT:-yes}" @@ -89,7 +106,7 @@ then # check cauldron files libcolor_notice "Checking for undefined cauldron functions..." echo "" - ward_functions -p "$cauldron" -l "$cauldron/lib" "${sources[@]}" + ward_functions -P "$PREFIX" -p "$cauldron" -l "$cauldron/lib" "${sources[@]}" fi if [[ "$PROCESS_ENCHANTMENT" == "yes" ]] @@ -101,7 +118,7 @@ then # check enchantment files libcolor_notice "Checking for undefined enchantment functions..." echo "" - ward_functions -p "$enchantment" -l "$enchantment/lib" -e "$enchantment/$enchanter" "${sources[@]}" + ward_functions -P "$PREFIX" -p "$enchantment" -l "$enchantment/lib" -e "$enchantment/$enchanter" "${sources[@]}" fi #------------------------------------------------------------------------------- |