cov-display 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/python
  2. import sys, re, os
  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. file_args = sys.argv[1:]
  18. files = []
  19. for fn in file_args:
  20. if os.path.isdir(fn):
  21. files.extend(os.path.join(fn, f) for f in os.listdir(fn))
  22. else:
  23. files.append(fn)
  24. for fn in files:
  25. none = some = branchTaken = branchNot = 0
  26. inFunc = ""
  27. for line in open(fn, 'r'):
  28. m = re.match(r'^[^:]*:([^:]*):(.*)', line)
  29. if m:
  30. body = m.group(2).rstrip()
  31. lineno = m.group(1).strip()
  32. else:
  33. body = ""
  34. lineno = "?"
  35. m = re.match(r'^([A-Za-z_][A-Za-z0-9_]*)(?:, *)?\(', body)
  36. if m:
  37. inFunc = "%s:%s %s" %(fn,lineno,m.group(1))
  38. elif body == "}":
  39. if FUNC and inFunc:
  40. show(inFunc, none, some)
  41. none = some = 0
  42. inFunc = None
  43. if re.match(r'^ *###', line):
  44. none += 1
  45. elif re.match(r'^ *\d', line):
  46. some += 1
  47. else:
  48. m = re.match(r'^branch.*taken (\d+)%', line)
  49. if m:
  50. if int(m.group(1)) == 0:
  51. branchNot += 1
  52. else:
  53. branchTaken += 1
  54. none0 += none
  55. some0 += some
  56. branchTaken0 += branchTaken
  57. branchNot0 += branchNot
  58. if FUNC:
  59. pass
  60. elif BRANCH:
  61. if branchTaken or branchNot:
  62. show(fn, branchNot, branchTaken)
  63. else:
  64. if some or none:
  65. show(fn, none, some)
  66. if BRANCH:
  67. if branchTaken0 or branchNot0:
  68. show("TOTAL", branchNot0, branchTaken0)
  69. else:
  70. if some0 or none0:
  71. show("TOTAL", none0, some0)