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