fix: fix union forward decl translator.

This commit is contained in:
2025-01-30 02:21:30 +08:00
parent 58d6f92802
commit b0e517452b
3 changed files with 34 additions and 41 deletions

View File

@@ -54,8 +54,10 @@ def _find_definition(decl: str, in_types: list) -> ClassDefineRecord | None:
if not CppUtil.is_full_type_required_for_typeset(namespace, clazz, in_types):
return None
assert namespace in _class_defs_record, f'namespace not recorded, {namespace}'
assert clazz in _class_defs_record[namespace], f'{clazz} not recorded, in {namespace}'
assert namespace in _class_defs_record, f'namespace = "{namespace}" is not recorded.'
assert clazz in _class_defs_record[namespace], (
f'namespace = "{namespace}", class = {clazz} is not recorded.'
)
return _class_defs_record[namespace][find_decl.class_decl]

View File

@@ -4,6 +4,7 @@ import re
import header_postprocessor as HeaderPostProcessor
import util.cpp_language as CppUtil
import util.string as StrUtil
class Options:
@@ -199,18 +200,14 @@ def process(path_to_file: str, args: Options):
if stripped_line.startswith('// auto generated forward declare list'):
in_forward_declaration_list = True
if in_forward_declaration_list:
if (
stripped_line.startswith('class ')
or stripped_line.startswith('struct ')
or stripped_line.startswith('namespace ')
):
if StrUtil.startswith_m(stripped_line, 'class ', 'struct ', 'union ', 'namespace '):
forward_declarations.append(stripped_line)
if stripped_line.startswith('// clang-format on') and in_forward_declaration_list:
in_forward_declaration_list = False
# record namespace & classes
if not in_forward_declaration_list:
if line.startswith('class ') or line.startswith('struct '): # ignore nested class
if StrUtil.startswith_m(line, 'class ', 'struct ', 'union '): # ignore nested class
founded = CppUtil.find_class_definition(line)
if founded:
is_template = (

View File

@@ -5,14 +5,7 @@ C++ Language Utility
import re
class ForwardDeclaration:
namespace_decl = str()
class_decl = str()
def __init__(self, namespace_decl: str, class_decl: str):
self.namespace_decl = namespace_decl
self.class_decl = class_decl
import util.string as StrUtil
def is_header_file(path: str):
@@ -20,25 +13,21 @@ def is_header_file(path: str):
def find_class_definition(line: str) -> str | None:
# class A (no quotation mark)
# class A :
# class A {
# class A { ... }; (in single line)
# KEYWORD A (no quotation mark)
# KEYWORD A :
# KEYWORD A {
# KEYWORD A { ... }; (in single line)
specifier_size = len('class')
class_pos = line.find('class ')
struct_pos = line.find('struct ')
assert class_pos == -1 or struct_pos == -1, f'line = {line}, c = {class_pos}, s = {struct_pos}'
keyword_pos, keyword = StrUtil.find_m(line, 'class ', 'struct ', 'union ')
if keyword_pos == -1:
return None
keyword_size = len(keyword) - 1 # not class defs
left_brace_pos = line.find('{')
semicolon_pos = line.find(';')
if semicolon_pos != -1 and (left_brace_pos == -1 or semicolon_pos < left_brace_pos):
return None # is forward decl
if class_pos == -1:
if struct_pos == -1:
return None # is not class defs
specifier_size = len('struct')
class_pos = struct_pos
end_pos = len(line)
colon_pos = line.find(':')
@@ -48,9 +37,18 @@ def find_class_definition(line: str) -> str | None:
if colon_pos != -1:
end_pos = min(end_pos, colon_pos)
if l_angle_bracket_pos != -1 and l_angle_bracket_pos < colon_pos:
return None # template specialization (is not currently supported)
return None # template specialization (is not supported)
return line[class_pos + specifier_size : end_pos].strip()
return line[keyword_pos + keyword_size : end_pos].strip()
class ForwardDeclaration:
namespace_decl = str()
class_decl = str()
def __init__(self, namespace_decl: str, class_decl: str):
self.namespace_decl = namespace_decl
self.class_decl = class_decl
def find_class_forward_declaration(line: str) -> ForwardDeclaration | None:
@@ -65,21 +63,17 @@ def find_class_forward_declaration(line: str) -> ForwardDeclaration | None:
if namespace_pos != -1 and left_brace_pos != -1:
namespace_decl = line[namespace_pos + len('namespace') : left_brace_pos].strip()
specifier_size = len('class')
class_pos = line.find('class ')
struct_pos = line.find('struct ')
assert class_pos == -1 or struct_pos == -1
keyword_pos, keyword = StrUtil.find_m(line, 'class ', 'struct ', 'union ')
if keyword_pos == -1:
return None
keyword_size = len(keyword) - 1 # not class defs
semicolon_pos = line.find(';')
if semicolon_pos == -1:
return None
if class_pos == -1:
if struct_pos == -1:
return None
specifier_size = len('struct')
class_pos = struct_pos
class_decl = line[class_pos + specifier_size : semicolon_pos].strip()
class_decl = line[keyword_pos + keyword_size : semicolon_pos].strip()
return ForwardDeclaration(namespace_decl, class_decl)