coverage 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/sh
  2. #
  3. # Calculate coverage of source files
  4. #
  5. gcovflags=-n
  6. usage ()
  7. {
  8. cat <<EOF
  9. usage: coverage [OPTIONS] [output-files]
  10. where OPTIONS are:
  11. --output generate .gcov files
  12. --help -h -? print this message
  13. EOF
  14. exit $1
  15. }
  16. while true; do
  17. case "$1" in
  18. --output ) gcovflags= ; shift ;;
  19. '-?' | -h | --help ) usage ;;
  20. - ) shift ; break ;;
  21. -* ) usage 1 ;;
  22. * ) break ;;
  23. esac
  24. done
  25. {
  26. for f in *.bb
  27. do
  28. test "$f" = "*.bb" && { echo "run 'make check' first"; exit 1; }
  29. gcov $gcovflags "$f" 2>/dev/null
  30. done
  31. for f in $@
  32. do
  33. gcov $gcovflags "$f" 2>/dev/null
  34. done
  35. } |
  36. awk '
  37. BEGIN {
  38. '"$(for f in "$@"; do echo 'include["'$f'"] = 1;'; done)"'
  39. }
  40. /^File/ {
  41. if (sub(/^File `\/home\/ppessi\/sofia-proxy\/(proxy\/..\/)?/, "")) {
  42. sub(/.$/, "");
  43. file=$0;
  44. if (file ~ /torture/) file = "";
  45. }
  46. else
  47. file = "";
  48. }
  49. file ~ /\.[hc]$/ && /Lines executed/ {
  50. sub(/Lines executed:/, "");
  51. covered = int($3*$1/100 + 0.5);
  52. # printf "%-5u %-5u %-5u %-7s %s\n", $3, covered, $3 - covered, $1, file;
  53. if ($3 == 0) {
  54. /* skip */
  55. }
  56. else if (!lines[file]) {
  57. names[i++] = file; lines[file] = $3; coverage[file] = covered;
  58. coverage[file] = $3*$1/100;
  59. }
  60. else if (coverage[file] < covered) {
  61. lines[file] = $3; coverage[file] = covered;
  62. coverage[file] = $3*$1/100;
  63. }
  64. }
  65. /torture/ { next; }
  66. /test/ { next; }
  67. /\/usr\/include/ { next; }
  68. /(source )?lines executed in file (.*\/)?[-A-Za-z_0-9]+\.[ch]/ {
  69. file = $0;
  70. sub(/.* in file /, "", file);
  71. sub(/.*\//, "", file);
  72. covered = int($3*$1/100 + 0.5);
  73. #printf "%-5u %-5u %-5u %-7s %s\n", $3, covered, $3 - covered, $1, file;
  74. if ($3 == 0) {
  75. /* skip */
  76. }
  77. else if (!file in lines) {
  78. lines[file] = $3; coverage[file] = $3*$1/100;
  79. }
  80. else if (coverage[file] <= $3*$1/100) {
  81. lines[file] = $3; coverage[file] = $3*$1/100;
  82. }
  83. }
  84. END {
  85. for (file in lines) {
  86. if (!(file in include))
  87. continue;
  88. l = lines[file]; c = coverage[file];
  89. covered = int(c + 0.5);
  90. total += l; total_coverage += c;
  91. printf "%-5u %-5u %-5u %6.2f%% %s\n",
  92. l, covered, l - covered, 100.0 * c / l, file;
  93. }
  94. if (total) {
  95. module = ENVIRON["PWD"];
  96. gsub(/\/+$/, "", module);
  97. gsub(/.*\//, "", module);
  98. printf "%-5u %-5u %-5u %6.2f%% %s in %s\n",
  99. total, total_coverage, total - total_coverage,
  100. 100.0 * total_coverage / total, "TOTAL", module;
  101. }
  102. }
  103. ' |
  104. sort -n