universalisos/kernel/add_doxygen.py

67 lines
2.6 KiB
Python

import os
import re
def add_doxygen_stubs(directory):
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(('.h', '.cpp')):
filepath = os.path.join(root, file)
process_file(filepath)
def process_file(filepath):
with open(filepath, 'r') as f:
content = f.read()
lines = content.split('\n')
new_lines = []
in_block_comment = False
func_regex = re.compile(r'^([\w\s\*]+)\s+(\w+)\s*\((.*?)\)\s*(?:{|;)')
struct_regex = re.compile(r'^(?:typedef\s+)?(?:struct|enum|union)\s+(\w+)?\s*(?:{|;)')
i = 0
while i < len(lines):
line = lines[i]
if '/*' in line:
in_block_comment = True
if '*/' in line:
in_block_comment = False
stripped = line.strip()
prev_is_doc = i > 0 and (lines[i-1].strip().startswith('*/') or lines[i-1].strip().startswith('///'))
if not in_block_comment and not prev_is_doc and not stripped.startswith('//') and not stripped.startswith('#') and stripped:
m_func = func_regex.match(line)
if m_func and 'typedef' not in line and 'return' not in line and 'else' not in line:
ret_type, func_name, args = m_func.groups()
if func_name not in ('if', 'while', 'for', 'switch', 'return', 'sizeof'):
new_lines.append('/**')
new_lines.append(f' * @brief {func_name}')
if args and args.strip() != 'void':
arg_parts = args.split(',')
for arg in arg_parts:
arg_name = arg.strip().split()[-1].replace('*', '').replace('&', '')
if arg_name and arg_name != '...':
new_lines.append(f' * @param {arg_name} Description for {arg_name}')
if ret_type.strip() != 'void' and 'void' not in ret_type:
new_lines.append(' * @return Description of return value')
new_lines.append(' */')
else:
m_struct = struct_regex.match(line)
if m_struct:
name = m_struct.group(1) or "anonymous"
new_lines.append('/**')
new_lines.append(f' * @brief {name}')
new_lines.append(' */')
new_lines.append(line)
i += 1
with open(filepath, 'w') as f:
f.write('\n'.join(new_lines))
if __name__ == '__main__':
add_doxygen_stubs('/home/fabiorafaelcoutada/portugalfuturista/universalisos/kernel/src')