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.
11 CLANG_DIR = os.path.join(os.path.dirname(__file__), '../..')
12 FORMAT_STYLE_FILE = os.path.join(CLANG_DIR, 'include/clang/Format/Format.h')
13 INCLUDE_STYLE_FILE = os.path.join(CLANG_DIR, 'include/clang/Tooling/Inclusions/IncludeStyle.h')
14 DOC_FILE = os.path.join(CLANG_DIR, 'docs/ClangFormatStyleOptions.rst')
17 def substitute(text, tag, contents):
18 replacement = '\n.. START_%s\n\n%s\n\n.. END_%s\n' % (tag, contents, tag)
19 pattern = r'\n\.\. START_%s\n.*\n\.\. END_%s\n' % (tag, tag)
20 return re.sub(pattern, '%s', text, flags=re.S) % replacement
22 def doxygen2rst(text):
23 text = re.sub(r'<tt>\s*(.*?)\s*<\/tt>', r'``\1``', text)
24 text = re.sub(r'\\c ([^ ,;\.]+)', r'``\1``', text)
25 text = re.sub(r'\\\w+ ', '', text)
28 def indent(text, columns, indent_first_line=True):
29 indent = ' ' * columns
30 s = re.sub(r'\n([^\n])', '\n' + indent + '\\1', text, flags=re.S)
31 if not indent_first_line or s.startswith('\n'):
36 def __init__(self, name, type, comment):
39 self.comment = comment.strip()
41 self.nested_struct = None
44 s = '**%s** (``%s``)\n%s' % (self.name, self.type,
45 doxygen2rst(indent(self.comment, 2)))
47 s += indent('\n\nPossible values:\n\n%s\n' % self.enum, 2)
48 if self.nested_struct:
49 s += indent('\n\nNested configuration flags:\n\n%s\n' %self.nested_struct,
54 def __init__(self, name, comment):
56 self.comment = comment.strip()
60 return '\n'.join(map(str, self.values))
63 def __init__(self, name, comment):
65 self.comment = comment.strip()
68 return '\n* ``%s`` %s' % (
70 doxygen2rst(indent(self.comment, 2, indent_first_line=False)))
73 def __init__(self, name, comment):
75 self.comment = comment.strip()
79 return '\n'.join(map(str, self.values))
82 def __init__(self, name, comment):
84 self.comment = comment
87 return '* ``%s`` (in configuration: ``%s``)\n%s' % (
89 re.sub('.*_', '', self.name),
90 doxygen2rst(indent(self.comment, 2)))
92 def clean_comment_line(line):
93 match = re.match(r'^/// \\code(\{.(\w+)\})?$', line)
95 lang = match.groups()[1]
98 return '\n.. code-block:: %s\n\n' % lang
99 if line == '/// \\endcode':
101 return line[4:] + '\n'
103 def read_options(header):
105 BeforeStruct, Finished, InStruct, InNestedStruct, InNestedFieldComent, \
106 InFieldComment, InEnum, InEnumMemberComment = range(8)
107 state = State.BeforeStruct
118 if state == State.BeforeStruct:
119 if line == 'struct FormatStyle {' or line == 'struct IncludeStyle {':
120 state = State.InStruct
121 elif state == State.InStruct:
122 if line.startswith('///'):
123 state = State.InFieldComment
124 comment = clean_comment_line(line)
126 state = State.Finished
128 elif state == State.InFieldComment:
129 if line.startswith('///'):
130 comment += clean_comment_line(line)
131 elif line.startswith('enum'):
133 name = re.sub(r'enum\s+(\w+)\s*\{', '\\1', line)
134 enum = Enum(name, comment)
135 elif line.startswith('struct'):
136 state = State.InNestedStruct
137 name = re.sub(r'struct\s+(\w+)\s*\{', '\\1', line)
138 nested_struct = NestedStruct(name, comment)
139 elif line.endswith(';'):
140 state = State.InStruct
141 field_type, field_name = re.match(r'([<>:\w(,\s)]+)\s+(\w+);',
143 option = Option(str(field_name), str(field_type), comment)
144 options.append(option)
146 raise Exception('Invalid format, expected comment, field or enum')
147 elif state == State.InNestedStruct:
148 if line.startswith('///'):
149 state = State.InNestedFieldComent
150 comment = clean_comment_line(line)
152 state = State.InStruct
153 nested_structs[nested_struct.name] = nested_struct
154 elif state == State.InNestedFieldComent:
155 if line.startswith('///'):
156 comment += clean_comment_line(line)
158 state = State.InNestedStruct
159 nested_struct.values.append(NestedField(line.replace(';', ''), comment))
160 elif state == State.InEnum:
161 if line.startswith('///'):
162 state = State.InEnumMemberComment
163 comment = clean_comment_line(line)
165 state = State.InStruct
166 enums[enum.name] = enum
168 raise Exception('Invalid format, expected enum field comment or };')
169 elif state == State.InEnumMemberComment:
170 if line.startswith('///'):
171 comment += clean_comment_line(line)
174 enum.values.append(EnumValue(line.replace(',', ''), comment))
175 if state != State.Finished:
176 raise Exception('Not finished by the end of file')
178 for option in options:
179 if not option.type in ['bool', 'unsigned', 'int', 'std::string',
180 'std::vector<std::string>',
181 'std::vector<IncludeCategory>',
182 'std::vector<RawStringFormat>']:
183 if enums.has_key(option.type):
184 option.enum = enums[option.type]
185 elif nested_structs.has_key(option.type):
186 option.nested_struct = nested_structs[option.type]
188 raise Exception('Unknown type: %s' % option.type)
191 options = read_options(open(FORMAT_STYLE_FILE))
192 options += read_options(open(INCLUDE_STYLE_FILE))
194 options = sorted(options, key=lambda x: x.name)
195 options_text = '\n\n'.join(map(str, options))
197 contents = open(DOC_FILE).read()
199 contents = substitute(contents, 'FORMAT_STYLE_OPTIONS', options_text)
201 with open(DOC_FILE, 'wb') as output:
202 output.write(contents)