fix_cocci_output.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python
  2. # StarPU --- Runtime system for heterogeneous multicore architectures.
  3. #
  4. # Copyright (C) 2012 INRIA
  5. #
  6. # StarPU is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU Lesser General Public License as published by
  8. # the Free Software Foundation; either version 2.1 of the License, or (at
  9. # your option) any later version.
  10. #
  11. # StarPU is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. #
  15. # See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. import sys
  17. import re
  18. # XXX Could be something else, like a file.
  19. output = sys.stdout
  20. def get_indentation_level(s):
  21. cnt = 0
  22. for c in s:
  23. if c != '\t':
  24. return -1
  25. cnt = cnt + 1
  26. return cnt
  27. def fix(f):
  28. modes = {}
  29. trailing_comma = False
  30. indentation_level = -1
  31. for line in f.readlines():
  32. # This regexp could be more precise, but it should be good
  33. # enough
  34. regexp = "((\s)*)\.modes\[(\d)+\](\s)*=(\s)*(.*)"
  35. m = re.search(regexp, line)
  36. if not m:
  37. if modes:
  38. output.write("".join(["\t" for i in range(indentation_level)]))
  39. output.write(".modes = { ")
  40. idx = 0
  41. while modes.get(str(idx)):
  42. if idx != 0:
  43. output.write(", ")
  44. output.write(modes[str(idx)])
  45. idx = idx+1
  46. if trailing_comma:
  47. output.write(" },\n")
  48. else:
  49. output.write(" }\n")
  50. # Resetting these..
  51. modes.clear()
  52. trailing_comma = False
  53. indentation_level = -1
  54. output.write(line)
  55. else:
  56. idx = m.group(3)
  57. mode = m.group(6)
  58. # Remove traling comma
  59. if mode[-1] == ',':
  60. mode = mode[:-1]
  61. # This is the last mode for this
  62. # codelet. Was this also the last
  63. # field ?
  64. if int(idx) == 0:
  65. trailing_comma = True
  66. # Try and guess the level of indentation
  67. if int(idx) == 0:
  68. s = m.group(1)
  69. indentation_level = get_indentation_level(s)
  70. modes[idx] = mode
  71. def fix_file(filename):
  72. with open(filename, 'r') as f:
  73. fix(f)
  74. def usage():
  75. print "%s <filename>" % sys.argv[0]
  76. sys.exit(1)
  77. if __name__ == '__main__':
  78. if len(sys.argv) != 2:
  79. usage()
  80. sys.exit(1)
  81. fix_file(sys.argv[1])