dump_format_style.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/env python
  2. # A tool to parse the FormatStyle struct from Format.h and update the
  3. # documentation in ../ClangFormatStyleOptions.rst automatically.
  4. # Run from the directory in which this file is located to update the docs.
  5. import collections
  6. import os
  7. import re
  8. CLANG_DIR = os.path.join(os.path.dirname(__file__), '../..')
  9. FORMAT_STYLE_FILE = os.path.join(CLANG_DIR, 'include/clang/Format/Format.h')
  10. INCLUDE_STYLE_FILE = os.path.join(CLANG_DIR, 'include/clang/Tooling/Inclusions/IncludeStyle.h')
  11. DOC_FILE = os.path.join(CLANG_DIR, 'docs/ClangFormatStyleOptions.rst')
  12. def substitute(text, tag, contents):
  13. replacement = '\n.. START_%s\n\n%s\n\n.. END_%s\n' % (tag, contents, tag)
  14. pattern = r'\n\.\. START_%s\n.*\n\.\. END_%s\n' % (tag, tag)
  15. return re.sub(pattern, '%s', text, flags=re.S) % replacement
  16. def doxygen2rst(text):
  17. text = re.sub(r'<tt>\s*(.*?)\s*<\/tt>', r'``\1``', text)
  18. text = re.sub(r'\\c ([^ ,;\.]+)', r'``\1``', text)
  19. text = re.sub(r'\\\w+ ', '', text)
  20. return text
  21. def indent(text, columns, indent_first_line=True):
  22. indent = ' ' * columns
  23. s = re.sub(r'\n([^\n])', '\n' + indent + '\\1', text, flags=re.S)
  24. if not indent_first_line or s.startswith('\n'):
  25. return s
  26. return indent + s
  27. class Option(object):
  28. def __init__(self, name, type, comment):
  29. self.name = name
  30. self.type = type
  31. self.comment = comment.strip()
  32. self.enum = None
  33. self.nested_struct = None
  34. def __str__(self):
  35. s = '**%s** (``%s``)\n%s' % (self.name, self.type,
  36. doxygen2rst(indent(self.comment, 2)))
  37. if self.enum:
  38. s += indent('\n\nPossible values:\n\n%s\n' % self.enum, 2)
  39. if self.nested_struct:
  40. s += indent('\n\nNested configuration flags:\n\n%s\n' %self.nested_struct,
  41. 2)
  42. return s
  43. class NestedStruct(object):
  44. def __init__(self, name, comment):
  45. self.name = name
  46. self.comment = comment.strip()
  47. self.values = []
  48. def __str__(self):
  49. return '\n'.join(map(str, self.values))
  50. class NestedField(object):
  51. def __init__(self, name, comment):
  52. self.name = name
  53. self.comment = comment.strip()
  54. def __str__(self):
  55. return '\n* ``%s`` %s' % (
  56. self.name,
  57. doxygen2rst(indent(self.comment, 2, indent_first_line=False)))
  58. class Enum(object):
  59. def __init__(self, name, comment):
  60. self.name = name
  61. self.comment = comment.strip()
  62. self.values = []
  63. def __str__(self):
  64. return '\n'.join(map(str, self.values))
  65. class EnumValue(object):
  66. def __init__(self, name, comment):
  67. self.name = name
  68. self.comment = comment
  69. def __str__(self):
  70. return '* ``%s`` (in configuration: ``%s``)\n%s' % (
  71. self.name,
  72. re.sub('.*_', '', self.name),
  73. doxygen2rst(indent(self.comment, 2)))
  74. def clean_comment_line(line):
  75. match = re.match(r'^/// \\code(\{.(\w+)\})?$', line)
  76. if match:
  77. lang = match.groups()[1]
  78. if not lang:
  79. lang = 'c++'
  80. return '\n.. code-block:: %s\n\n' % lang
  81. if line == '/// \\endcode':
  82. return ''
  83. return line[4:] + '\n'
  84. def read_options(header):
  85. class State(object):
  86. BeforeStruct, Finished, InStruct, InNestedStruct, InNestedFieldComent, \
  87. InFieldComment, InEnum, InEnumMemberComment = range(8)
  88. state = State.BeforeStruct
  89. options = []
  90. enums = {}
  91. nested_structs = {}
  92. comment = ''
  93. enum = None
  94. nested_struct = None
  95. for line in header:
  96. line = line.strip()
  97. if state == State.BeforeStruct:
  98. if line == 'struct FormatStyle {' or line == 'struct IncludeStyle {':
  99. state = State.InStruct
  100. elif state == State.InStruct:
  101. if line.startswith('///'):
  102. state = State.InFieldComment
  103. comment = clean_comment_line(line)
  104. elif line == '};':
  105. state = State.Finished
  106. break
  107. elif state == State.InFieldComment:
  108. if line.startswith('///'):
  109. comment += clean_comment_line(line)
  110. elif line.startswith('enum'):
  111. state = State.InEnum
  112. name = re.sub(r'enum\s+(\w+)\s*\{', '\\1', line)
  113. enum = Enum(name, comment)
  114. elif line.startswith('struct'):
  115. state = State.InNestedStruct
  116. name = re.sub(r'struct\s+(\w+)\s*\{', '\\1', line)
  117. nested_struct = NestedStruct(name, comment)
  118. elif line.endswith(';'):
  119. state = State.InStruct
  120. field_type, field_name = re.match(r'([<>:\w(,\s)]+)\s+(\w+);',
  121. line).groups()
  122. option = Option(str(field_name), str(field_type), comment)
  123. options.append(option)
  124. else:
  125. raise Exception('Invalid format, expected comment, field or enum')
  126. elif state == State.InNestedStruct:
  127. if line.startswith('///'):
  128. state = State.InNestedFieldComent
  129. comment = clean_comment_line(line)
  130. elif line == '};':
  131. state = State.InStruct
  132. nested_structs[nested_struct.name] = nested_struct
  133. elif state == State.InNestedFieldComent:
  134. if line.startswith('///'):
  135. comment += clean_comment_line(line)
  136. else:
  137. state = State.InNestedStruct
  138. nested_struct.values.append(NestedField(line.replace(';', ''), comment))
  139. elif state == State.InEnum:
  140. if line.startswith('///'):
  141. state = State.InEnumMemberComment
  142. comment = clean_comment_line(line)
  143. elif line == '};':
  144. state = State.InStruct
  145. enums[enum.name] = enum
  146. else:
  147. raise Exception('Invalid format, expected enum field comment or };')
  148. elif state == State.InEnumMemberComment:
  149. if line.startswith('///'):
  150. comment += clean_comment_line(line)
  151. else:
  152. state = State.InEnum
  153. enum.values.append(EnumValue(line.replace(',', ''), comment))
  154. if state != State.Finished:
  155. raise Exception('Not finished by the end of file')
  156. for option in options:
  157. if not option.type in ['bool', 'unsigned', 'int', 'std::string',
  158. 'std::vector<std::string>',
  159. 'std::vector<IncludeCategory>',
  160. 'std::vector<RawStringFormat>']:
  161. if option.type in enums:
  162. option.enum = enums[option.type]
  163. elif option.type in nested_structs:
  164. option.nested_struct = nested_structs[option.type]
  165. else:
  166. raise Exception('Unknown type: %s' % option.type)
  167. return options
  168. options = read_options(open(FORMAT_STYLE_FILE))
  169. options += read_options(open(INCLUDE_STYLE_FILE))
  170. options = sorted(options, key=lambda x: x.name)
  171. options_text = '\n\n'.join(map(str, options))
  172. contents = open(DOC_FILE).read()
  173. contents = substitute(contents, 'FORMAT_STYLE_OPTIONS', options_text)
  174. with open(DOC_FILE, 'wb') as output:
  175. output.write(contents)