1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-13 11:29:51 +03:00

datacopy.cpp automatic generation

This commit is contained in:
Bertrand Songis 2019-11-18 16:09:08 +01:00
parent 8e4ed180cb
commit f1b715743a
No known key found for this signature in database
GPG key ID: F189F79290FEC50F
4 changed files with 21 additions and 383 deletions

View file

@ -22,6 +22,7 @@ if sys.platform == "linux2":
structs = []
extrastructs = []
def build_struct(cursor, anonymousUnion=False):
if not anonymousUnion:
structs.append(cursor.spelling)
@ -32,7 +33,7 @@ def build_struct(cursor, anonymousUnion=False):
if c.spelling:
raise "Cannot handle non anonymous unions"
copiedUnionMember = False
copied_union_member = False
for uc in c.get_children():
if not uc.spelling or uc.kind == clang.cindex.CursorKind.PACKED_ATTR:
# Ignore
@ -40,11 +41,11 @@ def build_struct(cursor, anonymousUnion=False):
else:
# per default we copy only the first member of a union and warn if there are more
# members (declare the other members NOBACKUP)
if copiedUnionMember:
print ("Warning more than one union member (%s) in anynomous union inside struct %s, consider NOBACKUP statements" % (uc.spelling, cursor.spelling), file=sys.stderr)
if copied_union_member:
print("Warning more than one union member (%s) in anynomous union inside struct %s, consider NOBACKUP statements" % (uc.spelling, cursor.spelling), file=sys.stderr)
else:
copy_decl(uc, uc.spelling)
copiedUnionMember = True
copied_union_member = True
elif c.kind == clang.cindex.CursorKind.FIELD_DECL:
copy_decl(c, c.spelling)
@ -52,6 +53,7 @@ def build_struct(cursor, anonymousUnion=False):
if not anonymousUnion:
print("}\n")
def build(cursor):
result = []
for c in cursor.get_children():
@ -61,13 +63,13 @@ def build(cursor):
for c, spelling in extrastructs:
print("template <class A, class B>\nvoid copy%s(A * dest, B * src)\n{" % spelling)
build_struct(c, True)
print ("}\n")
print("}\n")
return result
def copy_decl(c, spelling):
childs = [ch for ch in c.get_children()]
children = [ch for ch in c.get_children()]
if c.type.get_array_size() > 0:
if c.type.get_array_element_type().spelling in structs:
print(" for (int i=0; i<%d; i++) {" % c.type.get_array_size())
@ -75,16 +77,15 @@ def copy_decl(c, spelling):
print(" }")
else:
print(" memcpy(dest->%s, src->%s, sizeof(dest->%s));" % (spelling, spelling, spelling))
elif len(childs)==1 and childs[0].kind == clang.cindex.CursorKind.STRUCT_DECL and not childs[0].spelling:
elif len(children) == 1 and children[0].kind == clang.cindex.CursorKind.STRUCT_DECL and not children[0].spelling:
# inline declared structs
if c.semantic_parent.spelling:
spellingFunc = c.semantic_parent.spelling + "_" + spelling
spelling_func = c.semantic_parent.spelling + "_" + spelling
else:
spellingFunc = c.semantic_parent.semantic_parent.spelling + "_" + spelling
extrastructs.append((childs[0], spellingFunc))
print(" copy%s(&dest->%s, &src->%s);" % (spellingFunc, spelling, spelling))
spelling_func = c.semantic_parent.semantic_parent.spelling + "_" + spelling
extrastructs.append((children[0], spelling_func))
print(" copy%s(&dest->%s, &src->%s);" % (spelling_func, spelling, spelling))
elif c.type.get_declaration().spelling in structs:
print(" copy%s(&dest->%s, &src->%s);" % (c.type.get_declaration().spelling, spelling, spelling))
else:
@ -92,7 +93,8 @@ def copy_decl(c, spelling):
def header():
print("//This file was auto-generated by %s script on %s. Do not edit this file!\n\n\n" % (os.path.basename(sys.argv[0]), time.asctime()))
print("// This file was auto-generated by %s script on %s. Do not edit this file!\n\n\n" % (os.path.basename(sys.argv[0]), time.asctime()))
index = clang.cindex.Index.create()
translation_unit = index.parse(sys.argv[1], ['-x', 'c++', '-std=c++11'] + sys.argv[2:])