Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding is incorrect if array typef is used #18

Open
OlgierdSobinski opened this issue Sep 26, 2018 · 1 comment
Open

Decoding is incorrect if array typef is used #18

OlgierdSobinski opened this issue Sep 26, 2018 · 1 comment

Comments

@OlgierdSobinski
Copy link

typedef u32 TAmbr[2];
struct SAmbrParams
{
TAmbr ambrUl;
TAmbr ambrDl;
};
In such case the ambrUl (and ambrDl) is treated as 1 x u32 instead of 2 x u32.

@aurzenligl
Copy link
Owner

That must be a shortcoming of prophy C++ (so called: "sack") parser:

def get_type_name(self, tp):
decl = tp.get_declaration()
def dive_deeper(method):
name = Builder.alphanumeric_name(decl)
if name not in self.tree.known:
method(decl)
return name
if tp.kind is TypeKind.TYPEDEF:
return self.get_type_name(decl.underlying_typedef_type)
elif tp.kind in (TypeKind.UNEXPOSED, TypeKind.ELABORATED, TypeKind.RECORD):
if decl.kind in (CursorKind.STRUCT_DECL, CursorKind.CLASS_DECL):
return dive_deeper(self.add_struct)
elif decl.kind is CursorKind.UNION_DECL:
return dive_deeper(self.add_union)
elif decl.kind is CursorKind.ENUM_DECL:
return self.get_type_name(decl.type)
elif decl.kind is CursorKind.TYPEDEF_DECL:
return self.get_type_name(decl.underlying_typedef_type)
else:
raise SackParserError("Unknown declaration, {} {}".format(tp.spelling, decl.kind))
elif tp.kind in (TypeKind.CONSTANTARRAY, TypeKind.INCOMPLETEARRAY):
return self.get_type_name(tp.element_type)
elif tp.kind is TypeKind.ENUM:
return dive_deeper(self.add_enum)
if tp.kind in (TypeKind.USHORT, TypeKind.UINT, TypeKind.ULONG, TypeKind.ULONGLONG):
return 'u%d' % (tp.get_size() * 8)
elif tp.kind in (TypeKind.SHORT, TypeKind.INT, TypeKind.LONG, TypeKind.LONGLONG):
return 'i%d' % (tp.get_size() * 8)
return self.unambiguous_builtins[tp.kind]
def add_enum(self, cursor):
def enum_member(cursor):
name = cursor.spelling.decode()
value = cursor.enum_value
if value < 0:
value = "0x%X" % (0x100000000 + value)
else:
value = str(value)
return model.EnumMember(name, value)
members = [enum_member(x) for x in cursor.get_children()]
node = model.Enum(Builder.alphanumeric_name(cursor), members)
self.tree.add_node(node)
def add_struct(self, cursor):
def array_length(tp):
if tp.kind is TypeKind.CONSTANTARRAY:
return tp.element_count
return None
def struct_member(cursor_):
name = cursor_.spelling.decode()
type_name = self.get_type_name(cursor_.type)
array_len = array_length(cursor_.type)
return model.StructMember(name, type_name, size=array_len)
members = [struct_member(x) for x in cursor.get_children()
if x.kind is CursorKind.FIELD_DECL and not x.is_bitfield()]
node = model.Struct(Builder.alphanumeric_name(cursor), members)
self.tree.add_node(node)
def add_union(self, cursor):
def union_member(cursor, disc):
name = cursor.spelling.decode()
type_name = self.get_type_name(cursor.type)
return model.UnionMember(name, type_name, str(disc))
members = [union_member(x, i) for i, x in enumerate(cursor.get_children())
if x.kind is CursorKind.FIELD_DECL]
node = model.Union(Builder.alphanumeric_name(cursor), members)
self.tree.add_node(node)
def build_model(self, translation_unit):
for cursor in translation_unit.cursor.get_children():
if cursor.kind is CursorKind.UNEXPOSED_DECL:
for in_cursor in cursor.get_children():
if in_cursor.kind is CursorKind.STRUCT_DECL and in_cursor.spelling and in_cursor.is_definition():
self.add_struct(in_cursor)
if cursor.spelling and cursor.is_definition():
if cursor.kind is CursorKind.STRUCT_DECL:
self.add_struct(cursor)
if cursor.kind is CursorKind.ENUM_DECL:
self.add_enum(cursor)

I'll gladly accept PR with bugfix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants