fix_cocci_output.py 2.2 KB

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