struct-flags.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  2. import re
  3. import sys
  4. import os
  5. from string import Template
  6. if len(sys.argv) == 2:
  7. SRC_DIR = sys.argv[1]
  8. else:
  9. SRC_DIR = "../../src/"
  10. class Structure:
  11. def __init__(self, string):
  12. (self.struct, self.flags, self.values) = string.split(":")
  13. class SetterGetter:
  14. def __init__(self, string):
  15. (function, params, self.value) = string.split(":")
  16. self.function = function.strip("()")
  17. self.params = [int(a) for a in params.split(",")]
  18. cmd = "grep -h coccinelle ../../src/*[ch] | sed -e 's/.*coccinelle: \(.*\) \*\//\1/'"
  19. struct_list = []
  20. setter_getter_list = []
  21. dirList = os.listdir(SRC_DIR)
  22. for fname in dirList:
  23. if re.search("\.[ch]$", fname):
  24. for line in open(os.path.join(SRC_DIR, fname)):
  25. if "coccinelle:" in line:
  26. m = re.search("coccinelle: (.*) \*\/", line)
  27. if "()" not in m.group(1):
  28. struct = Structure(m.group(1))
  29. struct_list.append(struct)
  30. else:
  31. function = SetterGetter(m.group(1))
  32. setter_getter_list.append(function)
  33. header = "@flags@"
  34. body = []
  35. # Handle setter and getter
  36. setter_getter = [x.function for x in setter_getter_list]
  37. if len(setter_getter):
  38. header += "\nidentifier NotSetterGetter !~ \"^(%s)$\";" % ("|".join(setter_getter))
  39. i = 0
  40. for struct in struct_list:
  41. header += """
  42. %s *struct%d;
  43. identifier struct_flags%d =~ "^(?!%s).+";""" % (struct.struct, i, i, struct.values)
  44. body.append("""
  45. struct%d->%s@p1 |= struct_flags%d
  46. |
  47. struct%d->%s@p1 & struct_flags%d
  48. |
  49. struct%d->%s@p1 &= ~struct_flags%d
  50. """ % (i, struct.flags, i, i, struct.flags, i, i, struct.flags, i))
  51. i += 1
  52. print(header)
  53. print("position p1;")
  54. print("@@")
  55. print("")
  56. print("""
  57. NotSetterGetter(...)
  58. {
  59. <...
  60. """)
  61. print("")
  62. print("(" + "|".join(body) + ")")
  63. print("")
  64. print("""
  65. ...>
  66. }
  67. @script:python@
  68. p1 << flags.p1;
  69. @@
  70. print "Invalid usage of flags field at %s:%s, flags value is incorrect (wrong family)." % (p1[0].file, p1[0].line)
  71. import sys
  72. sys.exit(1)""")
  73. i = 1
  74. setter_template = """
  75. @settergetter${i}@
  76. identifier SetterGetter =~ "${function}";
  77. identifier f_flags =~ "^(?!${value}).+";
  78. identifier ${params_line};
  79. position p1;
  80. @@
  81. SetterGetter@p1(${prefix_param}f_flags${suffix_param})
  82. @script:python@
  83. p1 << settergetter${i}.p1;
  84. @@
  85. print "Invalid usage of ${function} at %s:%s, flags value is incorrect (wrong family)." % (p1[0].file, p1[0].line)
  86. import sys
  87. sys.exit(1)
  88. """
  89. for sg in setter_getter_list:
  90. prefix_param = ""
  91. for index in list(range(1, sg.params[1])):
  92. prefix_param += "param%d, " % (index)
  93. if sg.params[1] < sg.params[0]:
  94. suffix_param = ", " + ", ".join(["param%d" % (index + 1) for index in list(range(sg.params[1], sg.params[0]))])
  95. else:
  96. suffix_param = ""
  97. params_elts = list(range(1, sg.params[1])) + list(range(sg.params[1] + 1, sg.params[0] + 1))
  98. params_line = ", ".join(["param%d" % (x) for x in params_elts])
  99. print(Template(setter_template).substitute(i=i, function=sg.function, value=sg.value,
  100. prefix_param=prefix_param, suffix_param=suffix_param, params_line=params_line))
  101. i += 1