cov-display 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/python
  2. import sys, re
  3. none0, some0 = 0,0
  4. branchTaken0, branchNot0 = 0,0
  5. BRANCH = False
  6. FUNC = False
  7. if sys.argv[1] == '-b':
  8. BRANCH = True
  9. del sys.argv[1]
  10. if sys.argv[1] == '-f':
  11. FUNC = True
  12. del sys.argv[1]
  13. def show(name, none, some):
  14. if some+none == 0:
  15. none = 1
  16. print name, none, some, "%.02f"%(100*(float(some)/(some+none)))
  17. for fn in sys.argv[1:]:
  18. none = some = branchTaken = branchNot = 0
  19. inFunc = ""
  20. for line in open(fn, 'r'):
  21. m = re.match(r'^[^:]*:([^:]*):(.*)', line)
  22. if m:
  23. body = m.group(2).rstrip()
  24. lineno = m.group(1).strip()
  25. else:
  26. body = ""
  27. lineno = "?"
  28. m = re.match(r'^([A-Za-z_][A-Za-z0-9_]*)(?:, *)?\(', body)
  29. if m:
  30. inFunc = "%s:%s %s" %(fn,lineno,m.group(1))
  31. elif body == "}":
  32. if FUNC and inFunc:
  33. show(inFunc, none, some)
  34. none = some = 0
  35. inFunc = None
  36. if re.match(r'^ *###', line):
  37. none += 1
  38. elif re.match(r'^ *\d', line):
  39. some += 1
  40. else:
  41. m = re.match(r'^branch.*taken (\d+)%', line)
  42. if m:
  43. if int(m.group(1)) == 0:
  44. branchNot += 1
  45. else:
  46. branchTaken += 1
  47. none0 += none
  48. some0 += some
  49. branchTaken0 += branchTaken
  50. branchNot0 += branchNot
  51. if FUNC:
  52. pass
  53. elif BRANCH:
  54. if branchTaken or branchNot:
  55. show(fn, branchNot, branchTaken)
  56. else:
  57. if some or none:
  58. show(fn, none, some)
  59. if BRANCH:
  60. if branchTaken0 or branchNot0:
  61. show("TOTAL", branchNot0, branchTaken0)
  62. else:
  63. if some0 or none0:
  64. show("TOTAL", none0, some0)