2021-09-24 08:14:53 +01:00
|
|
|
#!/usr/bin/env python3
|
2013-09-04 15:09:13 +00:00
|
|
|
# A tool to parse the FormatStyle struct from Format.h and update the
|
|
|
|
# documentation in ../ClangFormatStyleOptions.rst automatically.
|
|
|
|
# Run from the directory in which this file is located to update the docs.
|
|
|
|
|
2021-09-24 08:14:53 +01:00
|
|
|
import inspect
|
2016-02-23 16:11:43 +00:00
|
|
|
import os
|
2013-09-04 15:09:13 +00:00
|
|
|
import re
|
2021-09-24 08:14:53 +01:00
|
|
|
from typing import Set
|
2013-09-04 15:09:13 +00:00
|
|
|
|
2016-02-23 16:11:43 +00:00
|
|
|
CLANG_DIR = os.path.join(os.path.dirname(__file__), '../..')
|
|
|
|
FORMAT_STYLE_FILE = os.path.join(CLANG_DIR, 'include/clang/Format/Format.h')
|
2018-07-25 10:21:47 +00:00
|
|
|
INCLUDE_STYLE_FILE = os.path.join(CLANG_DIR, 'include/clang/Tooling/Inclusions/IncludeStyle.h')
|
2016-02-23 16:11:43 +00:00
|
|
|
DOC_FILE = os.path.join(CLANG_DIR, 'docs/ClangFormatStyleOptions.rst')
|
2013-09-04 15:09:13 +00:00
|
|
|
|
2021-09-24 08:14:53 +01:00
|
|
|
PLURALS_FILE = os.path.join(os.path.dirname(__file__), 'plurals.txt')
|
|
|
|
|
|
|
|
plurals: Set[str] = set()
|
|
|
|
with open(PLURALS_FILE, 'a+') as f:
|
|
|
|
f.seek(0)
|
|
|
|
plurals = set(f.read().splitlines())
|
2013-09-04 15:09:13 +00:00
|
|
|
|
|
|
|
def substitute(text, tag, contents):
|
|
|
|
replacement = '\n.. START_%s\n\n%s\n\n.. END_%s\n' % (tag, contents, tag)
|
|
|
|
pattern = r'\n\.\. START_%s\n.*\n\.\. END_%s\n' % (tag, tag)
|
|
|
|
return re.sub(pattern, '%s', text, flags=re.S) % replacement
|
|
|
|
|
2021-09-24 08:14:53 +01:00
|
|
|
def register_plural(singular: str, plural: str):
|
|
|
|
if plural not in plurals:
|
|
|
|
if not hasattr(register_plural, "generated_new_plural"):
|
|
|
|
print('Plural generation: you can use '
|
|
|
|
f'`git checkout -- {os.path.relpath(PLURALS_FILE)}` '
|
|
|
|
'to reemit warnings or `git add` to include new plurals\n')
|
|
|
|
register_plural.generated_new_plural = True
|
|
|
|
|
|
|
|
plurals.add(plural)
|
|
|
|
with open(PLURALS_FILE, 'a') as f:
|
|
|
|
f.write(plural + '\n')
|
|
|
|
cf = inspect.currentframe()
|
|
|
|
lineno = ''
|
|
|
|
if cf and cf.f_back:
|
|
|
|
lineno = ':' + str(cf.f_back.f_lineno)
|
|
|
|
print(f'{__file__}{lineno} check if plural of {singular} is {plural}', file=os.sys.stderr)
|
|
|
|
return plural
|
|
|
|
|
|
|
|
def pluralize(word: str):
|
|
|
|
lword = word.lower()
|
|
|
|
if len(lword) >= 2 and lword[-1] == 'y' and lword[-2] not in 'aeiou':
|
|
|
|
return register_plural(word, word[:-1] + 'ies')
|
|
|
|
elif lword.endswith(('s', 'sh', 'ch', 'x', 'z')):
|
|
|
|
return register_plural(word, word[:-1] + 'es')
|
|
|
|
elif lword.endswith('fe'):
|
|
|
|
return register_plural(word, word[:-2] + 'ves')
|
|
|
|
elif lword.endswith('f') and not lword.endswith('ff'):
|
|
|
|
return register_plural(word, word[:-1] + 'ves')
|
|
|
|
else:
|
|
|
|
return register_plural(word, word + 's')
|
|
|
|
|
|
|
|
|
|
|
|
def to_yaml_type(typestr: str):
|
|
|
|
if typestr == 'bool':
|
|
|
|
return 'Boolean'
|
|
|
|
elif typestr == 'int':
|
|
|
|
return 'Integer'
|
|
|
|
elif typestr == 'unsigned':
|
|
|
|
return 'Unsigned'
|
|
|
|
elif typestr == 'std::string':
|
|
|
|
return 'String'
|
|
|
|
|
|
|
|
subtype, napplied = re.subn(r'^std::vector<(.*)>$', r'\1', typestr)
|
|
|
|
if napplied == 1:
|
|
|
|
return 'List of ' + pluralize(to_yaml_type(subtype))
|
|
|
|
|
|
|
|
return typestr
|
|
|
|
|
2013-09-04 15:09:13 +00:00
|
|
|
def doxygen2rst(text):
|
|
|
|
text = re.sub(r'<tt>\s*(.*?)\s*<\/tt>', r'``\1``', text)
|
|
|
|
text = re.sub(r'\\c ([^ ,;\.]+)', r'``\1``', text)
|
|
|
|
text = re.sub(r'\\\w+ ', '', text)
|
|
|
|
return text
|
|
|
|
|
2017-06-23 11:29:40 +00:00
|
|
|
def indent(text, columns, indent_first_line=True):
|
2013-09-04 15:09:13 +00:00
|
|
|
indent = ' ' * columns
|
|
|
|
s = re.sub(r'\n([^\n])', '\n' + indent + '\\1', text, flags=re.S)
|
2017-06-23 11:29:40 +00:00
|
|
|
if not indent_first_line or s.startswith('\n'):
|
2013-09-04 15:09:13 +00:00
|
|
|
return s
|
|
|
|
return indent + s
|
|
|
|
|
2018-12-03 12:12:48 +00:00
|
|
|
class Option(object):
|
2021-09-28 11:42:19 +01:00
|
|
|
def __init__(self, name, type, comment, version):
|
2013-09-04 15:09:13 +00:00
|
|
|
self.name = name
|
|
|
|
self.type = type
|
|
|
|
self.comment = comment.strip()
|
|
|
|
self.enum = None
|
2015-09-29 14:57:55 +00:00
|
|
|
self.nested_struct = None
|
2021-09-28 11:42:19 +01:00
|
|
|
self.version = version
|
2013-09-04 15:09:13 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2021-09-28 11:42:19 +01:00
|
|
|
if self.version:
|
|
|
|
s = '**%s** (``%s``) :versionbadge:`clang-format %s`\n%s' % (self.name, to_yaml_type(self.type), self.version,
|
|
|
|
doxygen2rst(indent(self.comment, 2)))
|
|
|
|
else:
|
|
|
|
s = '**%s** (``%s``)\n%s' % (self.name, to_yaml_type(self.type),
|
2013-09-04 15:09:13 +00:00
|
|
|
doxygen2rst(indent(self.comment, 2)))
|
2021-01-25 09:30:02 +01:00
|
|
|
if self.enum and self.enum.values:
|
2013-09-04 15:09:13 +00:00
|
|
|
s += indent('\n\nPossible values:\n\n%s\n' % self.enum, 2)
|
2015-09-29 14:57:55 +00:00
|
|
|
if self.nested_struct:
|
|
|
|
s += indent('\n\nNested configuration flags:\n\n%s\n' %self.nested_struct,
|
|
|
|
2)
|
2013-09-04 15:09:13 +00:00
|
|
|
return s
|
|
|
|
|
2018-12-03 12:12:48 +00:00
|
|
|
class NestedStruct(object):
|
2015-09-29 14:57:55 +00:00
|
|
|
def __init__(self, name, comment):
|
|
|
|
self.name = name
|
|
|
|
self.comment = comment.strip()
|
|
|
|
self.values = []
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '\n'.join(map(str, self.values))
|
|
|
|
|
2018-12-03 12:12:48 +00:00
|
|
|
class NestedField(object):
|
2015-09-29 14:57:55 +00:00
|
|
|
def __init__(self, name, comment):
|
|
|
|
self.name = name
|
|
|
|
self.comment = comment.strip()
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-06-23 11:29:40 +00:00
|
|
|
return '\n* ``%s`` %s' % (
|
|
|
|
self.name,
|
|
|
|
doxygen2rst(indent(self.comment, 2, indent_first_line=False)))
|
2015-09-29 14:57:55 +00:00
|
|
|
|
2018-12-03 12:12:48 +00:00
|
|
|
class Enum(object):
|
2013-09-04 15:09:13 +00:00
|
|
|
def __init__(self, name, comment):
|
|
|
|
self.name = name
|
|
|
|
self.comment = comment.strip()
|
|
|
|
self.values = []
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '\n'.join(map(str, self.values))
|
|
|
|
|
2019-11-08 14:34:07 +00:00
|
|
|
class NestedEnum(object):
|
|
|
|
def __init__(self, name, enumtype, comment, values):
|
|
|
|
self.name = name
|
|
|
|
self.comment = comment
|
|
|
|
self.values = values
|
|
|
|
self.type = enumtype
|
|
|
|
|
|
|
|
def __str__(self):
|
2021-09-24 08:14:53 +01:00
|
|
|
s = '\n* ``%s %s``\n%s' % (to_yaml_type(self.type), self.name,
|
2019-11-08 14:34:07 +00:00
|
|
|
doxygen2rst(indent(self.comment, 2)))
|
|
|
|
s += indent('\nPossible values:\n\n', 2)
|
|
|
|
s += indent('\n'.join(map(str, self.values)),2)
|
|
|
|
return s;
|
|
|
|
|
2018-12-03 12:12:48 +00:00
|
|
|
class EnumValue(object):
|
2019-11-06 20:02:16 +00:00
|
|
|
def __init__(self, name, comment, config):
|
2013-09-04 15:09:13 +00:00
|
|
|
self.name = name
|
2016-02-23 16:11:55 +00:00
|
|
|
self.comment = comment
|
2019-11-06 20:02:16 +00:00
|
|
|
self.config = config
|
2013-09-04 15:09:13 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '* ``%s`` (in configuration: ``%s``)\n%s' % (
|
|
|
|
self.name,
|
2019-11-06 20:02:16 +00:00
|
|
|
re.sub('.*_', '', self.config),
|
2013-09-04 15:09:13 +00:00
|
|
|
doxygen2rst(indent(self.comment, 2)))
|
|
|
|
|
|
|
|
def clean_comment_line(line):
|
2021-01-25 09:30:02 +01:00
|
|
|
match = re.match(r'^/// (?P<indent> +)?\\code(\{.(?P<lang>\w+)\})?$', line)
|
2016-02-23 16:11:55 +00:00
|
|
|
if match:
|
2021-01-25 09:30:02 +01:00
|
|
|
indent = match.group('indent')
|
|
|
|
if not indent:
|
|
|
|
indent = ''
|
|
|
|
lang = match.group('lang')
|
2016-02-23 16:11:55 +00:00
|
|
|
if not lang:
|
|
|
|
lang = 'c++'
|
2021-01-25 09:30:02 +01:00
|
|
|
return '\n%s.. code-block:: %s\n\n' % (indent, lang)
|
|
|
|
|
|
|
|
endcode_match = re.match(r'^/// +\\endcode$', line)
|
|
|
|
if endcode_match:
|
2015-10-06 11:54:18 +00:00
|
|
|
return ''
|
2021-09-23 20:00:33 +01:00
|
|
|
|
|
|
|
match = re.match(r'^/// \\warning$', line)
|
|
|
|
if match:
|
|
|
|
return '\n.. warning:: \n\n'
|
|
|
|
|
|
|
|
endwarning_match = re.match(r'^/// +\\endwarning$', line)
|
|
|
|
if endwarning_match:
|
|
|
|
return ''
|
2015-10-06 11:54:18 +00:00
|
|
|
return line[4:] + '\n'
|
2013-09-04 15:09:13 +00:00
|
|
|
|
|
|
|
def read_options(header):
|
2018-12-03 12:12:48 +00:00
|
|
|
class State(object):
|
2021-09-24 08:14:53 +01:00
|
|
|
BeforeStruct, Finished, InStruct, InNestedStruct, InNestedFieldComment, \
|
2015-09-29 14:57:55 +00:00
|
|
|
InFieldComment, InEnum, InEnumMemberComment = range(8)
|
2013-09-04 15:09:13 +00:00
|
|
|
state = State.BeforeStruct
|
|
|
|
|
|
|
|
options = []
|
|
|
|
enums = {}
|
2015-09-29 14:57:55 +00:00
|
|
|
nested_structs = {}
|
2013-09-04 15:09:13 +00:00
|
|
|
comment = ''
|
|
|
|
enum = None
|
2015-09-29 14:57:55 +00:00
|
|
|
nested_struct = None
|
2021-09-28 11:42:19 +01:00
|
|
|
version = None
|
2013-09-04 15:09:13 +00:00
|
|
|
|
|
|
|
for line in header:
|
|
|
|
line = line.strip()
|
|
|
|
if state == State.BeforeStruct:
|
2018-07-25 10:21:47 +00:00
|
|
|
if line == 'struct FormatStyle {' or line == 'struct IncludeStyle {':
|
2013-09-04 15:09:13 +00:00
|
|
|
state = State.InStruct
|
|
|
|
elif state == State.InStruct:
|
|
|
|
if line.startswith('///'):
|
|
|
|
state = State.InFieldComment
|
|
|
|
comment = clean_comment_line(line)
|
|
|
|
elif line == '};':
|
|
|
|
state = State.Finished
|
|
|
|
break
|
|
|
|
elif state == State.InFieldComment:
|
2021-09-28 11:42:19 +01:00
|
|
|
if line.startswith(r'/// \version'):
|
|
|
|
match = re.match(r'/// \\version\s*(?P<version>[0-9.]+)*',line)
|
|
|
|
if match:
|
|
|
|
version = match.group('version')
|
|
|
|
elif line.startswith('///'):
|
2013-09-04 15:09:13 +00:00
|
|
|
comment += clean_comment_line(line)
|
|
|
|
elif line.startswith('enum'):
|
|
|
|
state = State.InEnum
|
2020-12-23 20:27:43 +00:00
|
|
|
name = re.sub(r'enum\s+(\w+)\s*(:((\s*\w+)+)\s*)?\{', '\\1', line)
|
2013-09-04 15:09:13 +00:00
|
|
|
enum = Enum(name, comment)
|
2015-09-29 14:57:55 +00:00
|
|
|
elif line.startswith('struct'):
|
|
|
|
state = State.InNestedStruct
|
|
|
|
name = re.sub(r'struct\s+(\w+)\s*\{', '\\1', line)
|
|
|
|
nested_struct = NestedStruct(name, comment)
|
2013-09-04 15:09:13 +00:00
|
|
|
elif line.endswith(';'):
|
|
|
|
state = State.InStruct
|
2015-09-29 14:57:55 +00:00
|
|
|
field_type, field_name = re.match(r'([<>:\w(,\s)]+)\s+(\w+);',
|
|
|
|
line).groups()
|
2021-09-28 11:42:19 +01:00
|
|
|
|
|
|
|
if not version:
|
|
|
|
print('Warning missing version for ', field_name)
|
|
|
|
option = Option(str(field_name), str(field_type), comment, version)
|
2013-09-04 15:09:13 +00:00
|
|
|
options.append(option)
|
2021-09-28 11:42:19 +01:00
|
|
|
version=None
|
2013-09-04 15:09:13 +00:00
|
|
|
else:
|
2021-09-28 11:42:19 +01:00
|
|
|
raise Exception('Invalid format, expected comment, field or enum\n'+line)
|
2015-09-29 14:57:55 +00:00
|
|
|
elif state == State.InNestedStruct:
|
|
|
|
if line.startswith('///'):
|
2021-09-24 08:14:53 +01:00
|
|
|
state = State.InNestedFieldComment
|
2015-09-29 14:57:55 +00:00
|
|
|
comment = clean_comment_line(line)
|
|
|
|
elif line == '};':
|
|
|
|
state = State.InStruct
|
|
|
|
nested_structs[nested_struct.name] = nested_struct
|
2021-09-24 08:14:53 +01:00
|
|
|
elif state == State.InNestedFieldComment:
|
2015-09-29 14:57:55 +00:00
|
|
|
if line.startswith('///'):
|
|
|
|
comment += clean_comment_line(line)
|
|
|
|
else:
|
|
|
|
state = State.InNestedStruct
|
2019-11-08 14:34:07 +00:00
|
|
|
field_type, field_name = re.match(r'([<>:\w(,\s)]+)\s+(\w+);',line).groups()
|
|
|
|
if field_type in enums:
|
|
|
|
nested_struct.values.append(NestedEnum(field_name,field_type,comment,enums[field_type].values))
|
|
|
|
else:
|
|
|
|
nested_struct.values.append(NestedField(field_type + " " + field_name, comment))
|
|
|
|
|
2013-09-04 15:09:13 +00:00
|
|
|
elif state == State.InEnum:
|
|
|
|
if line.startswith('///'):
|
|
|
|
state = State.InEnumMemberComment
|
|
|
|
comment = clean_comment_line(line)
|
|
|
|
elif line == '};':
|
|
|
|
state = State.InStruct
|
|
|
|
enums[enum.name] = enum
|
|
|
|
else:
|
2021-01-25 09:30:02 +01:00
|
|
|
# Enum member without documentation. Must be documented where the enum
|
|
|
|
# is used.
|
|
|
|
pass
|
2013-09-04 15:09:13 +00:00
|
|
|
elif state == State.InEnumMemberComment:
|
|
|
|
if line.startswith('///'):
|
|
|
|
comment += clean_comment_line(line)
|
|
|
|
else:
|
|
|
|
state = State.InEnum
|
2019-11-06 20:02:16 +00:00
|
|
|
val = line.replace(',', '')
|
|
|
|
pos = val.find(" // ")
|
|
|
|
if (pos != -1):
|
|
|
|
config = val[pos+4:]
|
|
|
|
val = val[:pos]
|
|
|
|
else:
|
|
|
|
config = val;
|
|
|
|
enum.values.append(EnumValue(val, comment,config))
|
2013-09-04 15:09:13 +00:00
|
|
|
if state != State.Finished:
|
|
|
|
raise Exception('Not finished by the end of file')
|
|
|
|
|
|
|
|
for option in options:
|
2014-04-09 14:05:49 +00:00
|
|
|
if not option.type in ['bool', 'unsigned', 'int', 'std::string',
|
2015-09-29 14:57:55 +00:00
|
|
|
'std::vector<std::string>',
|
2017-11-09 15:41:23 +00:00
|
|
|
'std::vector<IncludeCategory>',
|
|
|
|
'std::vector<RawStringFormat>']:
|
2018-12-18 08:22:47 +00:00
|
|
|
if option.type in enums:
|
2013-09-04 15:09:13 +00:00
|
|
|
option.enum = enums[option.type]
|
2018-12-18 08:22:47 +00:00
|
|
|
elif option.type in nested_structs:
|
2017-06-23 11:29:40 +00:00
|
|
|
option.nested_struct = nested_structs[option.type]
|
2013-09-04 15:09:13 +00:00
|
|
|
else:
|
|
|
|
raise Exception('Unknown type: %s' % option.type)
|
|
|
|
return options
|
|
|
|
|
|
|
|
options = read_options(open(FORMAT_STYLE_FILE))
|
2018-07-25 10:21:47 +00:00
|
|
|
options += read_options(open(INCLUDE_STYLE_FILE))
|
2013-09-04 15:09:13 +00:00
|
|
|
|
|
|
|
options = sorted(options, key=lambda x: x.name)
|
|
|
|
options_text = '\n\n'.join(map(str, options))
|
|
|
|
|
|
|
|
contents = open(DOC_FILE).read()
|
|
|
|
|
|
|
|
contents = substitute(contents, 'FORMAT_STYLE_OPTIONS', options_text)
|
|
|
|
|
2015-11-20 07:46:19 +00:00
|
|
|
with open(DOC_FILE, 'wb') as output:
|
2020-05-07 19:52:12 +01:00
|
|
|
output.write(contents.encode())
|