From dd76e9d8bf14bcb63c2e127808ab6fda837d21bc Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Fri, 25 Mar 2016 14:04:30 +0100 Subject: [PATCH] Also backup members inside unions, the code is a bit more complicated since unions can have structs or simple types. --- radio/src/storage/datacopy.cpp | 11 ++++- radio/util/generate_datacopy.py | 71 ++++++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/radio/src/storage/datacopy.cpp b/radio/src/storage/datacopy.cpp index 239db541d..1fc1a9730 100644 --- a/radio/src/storage/datacopy.cpp +++ b/radio/src/storage/datacopy.cpp @@ -1,4 +1,4 @@ -//This file was auto-generated by generate_datacopy.py script on Wed Mar 16 20:12:19 2016. Do not edit this file! +//This file was auto-generated by generate_datacopy.py script on Fri Mar 25 14:01:12 2016. Do not edit this file! @@ -77,6 +77,7 @@ void copyCustomFunctionData(A * dest, B * src) { dest->swtch = src->swtch; dest->func = src->func; + copyCustomFunctionData_all(&dest->CustomFunctionData_all, &src->CustomFunctionData_all); dest->active = src->active; } @@ -305,3 +306,11 @@ void copyRadioData(A * dest, B * src) dest->potsConfig = src->potsConfig; } +template +void copyCustomFunctionData_all(A * dest, B * src) +{ + dest->val = src->val; + dest->mode = src->mode; + dest->param = src->param; +} + diff --git a/radio/util/generate_datacopy.py b/radio/util/generate_datacopy.py index 22e87d6d9..9d1d9f7e4 100644 --- a/radio/util/generate_datacopy.py +++ b/radio/util/generate_datacopy.py @@ -12,33 +12,74 @@ if sys.platform == "darwin": structs = [] +extrastructs = [] + +def build_struct(cursor, anonymousUnion=False): + if not anonymousUnion: + structs.append(cursor.spelling) + print("template \nvoid copy%s(A * dest, B * src)\n{" % cursor.spelling) -def build_struct(cursor): - structs.append(cursor.spelling) - print("template \nvoid copy%s(A * dest, B * src)\n{" % cursor.spelling) for c in cursor.get_children(): - if c.kind == clang.cindex.CursorKind.FIELD_DECL: - 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()) - print(" copy%s(&dest->%s[i], &src->%s[i]);" % (c.type.get_array_element_type().spelling, c.spelling, c.spelling)) - print(" }") + if c.kind == clang.cindex.CursorKind.UNION_DECL: + if c.spelling: + raise "Cannot handle non anonymous unions" + + copiedUnionMember = False + for uc in c.get_children(): + if not uc.spelling or uc.kind == clang.cindex.CursorKind.PACKED_ATTR: + # Ignore + pass else: - print(" memcpy(dest->%s, src->%s, sizeof(dest->%s));" % (c.spelling, c.spelling, c.spelling)) - elif c.type.get_declaration().spelling in structs: - print(" copy%s(&dest->%s, &src->%s);" % (c.type.get_declaration().spelling, c.spelling, c.spelling)) - else: - print(" dest->%s = src->%s;" % (c.spelling, c.spelling)) - print("}\n") + # 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) + else: + copy_decl(uc, uc.spelling) + copiedUnionMember = True + + elif c.kind == clang.cindex.CursorKind.FIELD_DECL: + copy_decl(c, c.spelling) + + + if not anonymousUnion: + print("}\n") def build(cursor): result = [] for c in cursor.get_children(): if c.kind == clang.cindex.CursorKind.STRUCT_DECL: build_struct(c) + for c, spelling in extrastructs: + print("template \nvoid copy%s(A * dest, B * src)\n{" % spelling) + build_struct(c, True) + print ("}\n") + return result +def copy_decl(c, spelling): + childs = [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()) + print(" copy%s(&dest->%s[i], &src->%s[i]);" % (c.type.get_array_element_type().spelling, spelling, spelling)) + print(" }") + else: + print(" memcpy(dest->%s, src->%s, sizeof(dest->%s));" % (spelling, spelling, spelling)) + elif c.type.kind == clang.cindex.TypeKind.UNEXPOSED and len(childs)==1 and childs[0].kind == clang.cindex.CursorKind.STRUCT_DECL: + # inline declared structs + spelling = c.semantic_parent.semantic_parent.spelling + "_" + spelling + + extrastructs.append((childs[0], spelling)) + print(" copy%s(&dest->%s, &src->%s);" % (spelling, 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: + print(" dest->%s = src->%s;" % (spelling, 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()))