fix_cocci_output.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. # StarPU --- Runtime system for heterogeneous multicore architectures.
  3. #
  4. # Copyright (C) 2012-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), 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. #
  17. import sys
  18. import re
  19. # XXX Could be something else, like a file.
  20. output = sys.stdout
  21. def get_indentation_level(s):
  22. cnt = 0
  23. for c in s:
  24. if c != '\t':
  25. return -1
  26. cnt = cnt + 1
  27. return cnt
  28. def fix(f):
  29. modes = {}
  30. trailing_comma = False
  31. indentation_level = -1
  32. for line in f.readlines():
  33. # This regexp could be more precise, but it should be good
  34. # enough
  35. regexp = "((\s)*)\.modes\[(\d)+\](\s)*=(\s)*(.*)"
  36. m = re.search(regexp, line)
  37. if not m:
  38. if modes:
  39. output.write("".join(["\t" for i in range(indentation_level)]))
  40. output.write(".modes = { ")
  41. idx = 0
  42. while modes.get(str(idx)):
  43. if idx != 0:
  44. output.write(", ")
  45. output.write(modes[str(idx)])
  46. idx = idx+1
  47. if trailing_comma:
  48. output.write(" },\n")
  49. else:
  50. output.write(" }\n")
  51. # Resetting these..
  52. modes.clear()
  53. trailing_comma = False
  54. indentation_level = -1
  55. output.write(line)
  56. else:
  57. idx = m.group(3)
  58. mode = m.group(6)
  59. # Remove traling comma
  60. if mode[-1] == ',':
  61. mode = mode[:-1]
  62. # This is the last mode for this
  63. # codelet. Was this also the last
  64. # field ?
  65. if int(idx) == 0:
  66. trailing_comma = True
  67. # Try and guess the level of indentation
  68. if int(idx) == 0:
  69. s = m.group(1)
  70. indentation_level = get_indentation_level(s)
  71. modes[idx] = mode
  72. def fix_file(filename):
  73. with open(filename, 'r') as f:
  74. fix(f)
  75. def usage():
  76. print "%s <filename>" % sys.argv[0]
  77. sys.exit(1)
  78. if __name__ == '__main__':
  79. if len(sys.argv) != 2:
  80. usage()
  81. sys.exit(1)
  82. fix_file(sys.argv[1])