Fix rendering of aliases

This commit is contained in:
Victor Zverovich 2024-06-01 14:40:39 -07:00
parent e3910b8a9c
commit f18c2b65c4
2 changed files with 51 additions and 35 deletions

View File

@ -80,9 +80,7 @@ Unused arguments are allowed as in Python's `str.format` and ordinary functions.
::: basic_format_string ::: basic_format_string
:: {.doxygentypedef} ::: format_string
fmt::format_string
::
::: runtime(string_view) ::: runtime(string_view)
@ -107,10 +105,13 @@ Example ([run](https://godbolt.org/z/nvME4arz8)):
#include <fmt/format.h> #include <fmt/format.h>
namespace kevin_namespacy { namespace kevin_namespacy {
enum class film { enum class film {
house_of_cards, american_beauty, se7en = 7 house_of_cards, american_beauty, se7en = 7
}; };
auto format_as(film f) { return fmt::underlying(f); } auto format_as(film f) { return fmt::underlying(f); }
} }
int main() { int main() {
@ -272,17 +273,17 @@ Named arguments are not supported in compile-time checks at the moment.
You can create your own formatting function with compile-time checks and You can create your own formatting function with compile-time checks and
small binary footprint, for example (<https://godbolt.org/z/vajfWEG4b>): small binary footprint, for example (<https://godbolt.org/z/vajfWEG4b>):
``` c++ ```c++
#include <fmt/base.h> #include <fmt/base.h>
void vlog(const char* file, int line, fmt::string_view format, void vlog(const char* file, int line,
fmt::format_args args) { fmt::string_view format, fmt::format_args args) {
fmt::print("{}: {}: ", file, line); fmt::print("{}: {}: {}", file, line, fmt::vformat(format, args));
fmt::vprint(format, args);
} }
template <typename... T> template <typename... T>
void log(const char* file, int line, fmt::format_string<T...> format, T&&... args) { void log(const char* file, int line,
fmt::format_string<T...> format, T&&... args) {
vlog(file, line, format, fmt::make_format_args(args...)); vlog(file, line, format, fmt::make_format_args(args...));
} }

View File

@ -9,8 +9,9 @@ from subprocess import CalledProcessError, PIPE, Popen, STDOUT
class Definition: class Definition:
'''A definition extracted by Doxygen.''' '''A definition extracted by Doxygen.'''
def __init__(self, name: str): def __init__(self, name: str, kind: str):
self.name = name self.name = name
self.kind = kind
self.params = None self.params = None
self.members = None self.members = None
@ -58,7 +59,7 @@ def get_template_params(node: et.Element) -> Optional[list[Definition]]:
params = [] params = []
for param_node in templateparamlist.findall('param'): for param_node in templateparamlist.findall('param'):
name = param_node.find('declname') name = param_node.find('declname')
param = Definition(name.text if name is not None else '') param = Definition(name.text if name is not None else '', 'param')
param.type = param_node.find('type').text param.type = param_node.find('type').text
params.append(param) params.append(param)
return params return params
@ -71,16 +72,18 @@ def clean_type(type: str) -> str:
type = type.replace('< ', '<').replace(' >', '>') type = type.replace('< ', '<').replace(' >', '>')
return type.replace(' &', '&').replace(' *', '*') return type.replace(' &', '&').replace(' *', '*')
def convert_param(param: et.Element) -> Definition: def convert_type(type: et.Element) -> str:
d = Definition(param.find('declname').text) result = type.text if type.text else ''
type = param.find('type')
type_str = type.text if type.text else ''
for ref in type: for ref in type:
type_str += ref.text result += ref.text
if ref.tail: if ref.tail:
type_str += ref.tail result += ref.tail
type_str += type.tail.strip() result += type.tail.strip()
d.type = clean_type(type_str) return clean_type(result)
def convert_param(param: et.Element) -> Definition:
d = Definition(param.find('declname').text, 'param')
d.type = convert_type(param.find('type'))
return d return d
def render_decl(d: Definition) -> None: def render_decl(d: Definition) -> None:
@ -90,13 +93,22 @@ def render_decl(d: Definition) -> None:
text += ', '.join( text += ', '.join(
[f'{p.type} {p.name}'.rstrip() for p in d.template_params]) [f'{p.type} {p.name}'.rstrip() for p in d.template_params])
text += '&gt;\n' text += '&gt;\n'
text += d.type + ' ' + d.name if d.kind == 'function' or d.kind == 'variable':
text += d.type
elif d.kind == 'typedef':
text += 'using'
else:
text += d.kind
text += ' ' + d.name
if d.params is not None: if d.params is not None:
params = ', '.join([f'{p.type} {p.name}' for p in d.params]) params = ', '.join([f'{p.type} {p.name}' for p in d.params])
text += '(' + escape_html(params) + ')' text += '(' + escape_html(params) + ')'
if d.trailing_return_type: if d.trailing_return_type:
text += '\n ' if len(d.name) + len(params) > 60 else '' text += '\n ' \
if len(d.name) + len(params) + len(d.trailing_return_type) > 74 else ''
text += ' -> ' + escape_html(d.trailing_return_type) text += ' -> ' + escape_html(d.trailing_return_type)
elif d.kind == 'typedef':
text += ' = ' + escape_html(d.type)
text += ';' text += ';'
text += '</code></pre>\n' text += '</code></pre>\n'
return text return text
@ -166,20 +178,24 @@ class CxxHandler(BaseHandler):
f"compounddef/sectiondef/memberdef/name[.='{name}']/..") f"compounddef/sectiondef/memberdef/name[.='{name}']/..")
candidates = [] candidates = []
for node in nodes: for node in nodes:
# Process a function. # Process a function or a typedef.
params = [convert_param(p) for p in node.findall('param')] params = None
node_param_str = ', '.join([p.type for p in params]) kind = node.get('kind')
if param_str and param_str != node_param_str: if kind == 'function':
candidates.append(f'{name}({node_param_str})') params = [convert_param(p) for p in node.findall('param')]
continue node_param_str = ', '.join([p.type for p in params])
d = Definition(name) if param_str and param_str != node_param_str:
d.type = node.find('type').text candidates.append(f'{name}({node_param_str})')
continue
d = Definition(name, kind)
d.type = convert_type(node.find('type'))
d.template_params = get_template_params(node) d.template_params = get_template_params(node)
d.params = params d.params = params
d.trailing_return_type = None d.trailing_return_type = None
if d.type == 'auto': if d.type == 'auto' or d.type == 'constexpr auto':
d.trailing_return_type = clean_type( parts = node.find('argsstring').text.split(' -> ')
node.find('argsstring').text.split(' -> ')[1]) if len(parts) > 1:
d.trailing_return_type = clean_type(parts[1])
d.desc = get_description(node) d.desc = get_description(node)
return d return d
@ -190,14 +206,13 @@ class CxxHandler(BaseHandler):
with open(os.path.join(self._doxyxml_dir, cls[0].get('refid') + '.xml')) as f: with open(os.path.join(self._doxyxml_dir, cls[0].get('refid') + '.xml')) as f:
xml = et.parse(f) xml = et.parse(f)
node = xml.find('compounddef') node = xml.find('compounddef')
d = Definition(name) d = Definition(name, node.get('kind'))
d.type = node.get('kind')
d.template_params = get_template_params(node) d.template_params = get_template_params(node)
d.desc = get_description(node) d.desc = get_description(node)
d.members = [] d.members = []
for m in node.findall('sectiondef[@kind="public-attrib"]/memberdef'): for m in node.findall('sectiondef[@kind="public-attrib"]/memberdef'):
name = m.find('name').text name = m.find('name').text
member = Definition(name if name else '') member = Definition(name if name else '', m.get('kind'))
type = m.find('type').text type = m.find('type').text
member.type = type if type else '' member.type = type if type else ''
member.template_params = None member.template_params = None