def setUp(self): super(TestRedirectionToDeclaration, self).setUp() name = type(self).__name__ private_h = Header("private.h") private_h.add_type(Structure("Private")) public_h = Header("public.h") public_h.add_types([ Type["Private"].gen_forward_declaration(), Function("public_func") ]) private_c = Source("private.c") public_func_impl = Type["public_func"].gen_definition() private_c.add_type(public_func_impl) src = Source(name.lower() + ".c").add_global_variable( # It must internally re-direct pointer from type "Private" # to "Private.declaration", its forward declaration. Pointer(Type["Private"])("handler")) src.add_type(Pointer(public_func_impl, name="cb_ptr")) src_content = """\ /* %s */ #include "public.h" typedef void (*cb_ptr)(void); Private *handler __attribute__((unused)); """ % (name.lower() + ".c") self.files = [(src, src_content)]
def setUp(self): super(TestReferencingToSelfDefinedType, self).setUp() name = type(self).__name__ hdr = Header(name.lower() + ".h") m = Macro("M_OTHER_TYPE", text="int") hdr.add_type(m) ht = Header("macro_types.h") ht.add_type(Macro("M_TYPE", text="M_OTHER_TYPE")) ht.add_reference(m) hdr.add_global_variable(Type["M_TYPE"]("var")) hdr_content = """\ /* {path} */ #ifndef INCLUDE_{fname_upper}_H #define INCLUDE_{fname_upper}_H #define M_OTHER_TYPE int #include "macro_types.h" extern M_TYPE var; #endif /* INCLUDE_{fname_upper}_H */ """.format(path=hdr.path, fname_upper=name.upper()) self.files = [(hdr, hdr_content)]
def setUp(self): super(TestMacroType, self).setUp() name = type(self).__name__ hdr = Header(name.lower() + ".h") hdr.add_type(Macro("QTAIL_ENTRY", args=["type"])) struct = Structure("StructA") struct.append_fields([ Type["QTAIL_ENTRY"]("entry", macro_initializer=Initializer({"type": struct})), Pointer(struct)("next") ]) hdr.add_type(struct) hdr_content = """\ /* {path} */ #ifndef INCLUDE_{fname_upper}_H #define INCLUDE_{fname_upper}_H #define QTAIL_ENTRY(type) typedef struct StructA StructA; struct StructA {{ QTAIL_ENTRY(StructA) entry; StructA *next; }}; #endif /* INCLUDE_{fname_upper}_H */ """.format(path=hdr.path, fname_upper=name.upper()) self.files = [(hdr, hdr_content)]
def setUp(self): super(TestPointerReferences, self).setUp() name = type(self).__name__ try: h = Header["type_a.h"] except: h = Header("type_a.h") h.add_type(Type("a", incomplete=False, base=False)) src = Source(name.lower() + ".c").add_type( Structure("s", Pointer(Type["a"])("next"))) src_content = """\ /* {} */ #include "type_a.h" typedef struct s {{ a *next; }} s; """.format(src.path) self.files = [(src, src_content)]
def setUp(self): super(TestAddingTypeToLockedHeader, self).setUp() name = type(self).__name__ Header("some_types.h").add_type(Type("t")) # Without locking "some_types.h" header will be included in # "lockedheader.h". Header("lockedheader.h", locked=True).add_type(Structure("S", Pointer(Type["t"])("f"))) hdr = Header(name.lower() + ".h") hdr.add_type(Pointer(Type["S"], name="ps")) hdr_content = """\ /* {path} */ #ifndef INCLUDE_{fname_upper}_H #define INCLUDE_{fname_upper}_H #include "some_types.h" #include "lockedheader.h" typedef S *ps; #endif /* INCLUDE_{fname_upper}_H */ """.format(path=hdr.path, fname_upper=name.upper()) self.files = [(hdr, hdr_content)]
def setUp(self): super(TestEnumerations, self).setUp() name = type(self).__name__ try: h = Header["enums.h"] except: h = Header("enums.h") h.add_type(Enumeration("A", [("one", 1), ("two", 2)])) a = Type["int"]("a") b = Type["int"]("b") c = Type["int"]("c") src = Source(name.lower() + ".c").add_types([ Enumeration("B", [("three", 3), ("four", 4)], "B"), Function(name="main", body=BodyTree()(Declare(a, b, c), OpAssign(a, Type["A"].one), OpAssign(b, Type["B"].three), OpAssign(c, Type["four"]))) ]) src_content = """\ /* {} */ #include "enums.h" enum B {{ three = 3, four = 4 }}; void main(void) {{ int a, b, c; a = one; b = three; c = four; }} """.format(src.path) self.files = [(src, src_content)]
def setUp(self): super(TestGlobalHeadersInclusion, self).setUp() name = type(self).__name__ hg = Header("global_types.h", is_global=True) hl = Header("local_types.h") hg.add_type(Type("GT", incomplete=False)) hl.add_type(Type("LT", incomplete=False)) hdr = Header(name.lower() + ".h").add_type( Structure("Fields", Type["GT"]("f1"), Type["LT"]("f2"))) hdr_content = """\ /* {path} */ #ifndef INCLUDE_{fname_upper}_H #define INCLUDE_{fname_upper}_H #include "local_types.h" typedef struct Fields {{ GT f1; LT f2; }} Fields; #endif /* INCLUDE_{fname_upper}_H */ """.format(path=hdr.path, fname_upper=name.upper()) src = Source(name.lower() + ".c").add_global_variable( Type["Fields"]("fv")) src_content = """\ /* {} */ #include <global_types.h> #include "{}" Fields fv __attribute__((unused)); """.format(src.path, hdr.path) self.files = [(hdr, hdr_content), (src, src_content)]
def setUp(self): super(TestMacroType, self).setUp() name = type(self).__name__ Header("entry_macro.h").add_type(Macro("QTAIL_ENTRY", args=["type"])) Header("struct_end.h").add_type(Macro("END_STRUCT")) Header("header_init.h").add_type(Macro("INIT_HEADER")) hdr = Header(name.lower() + ".h") struct = Structure("StructA") struct.append_fields([ Type["QTAIL_ENTRY"]("entry", macro_initializer=Initializer({"type": struct})), Pointer(struct)("next"), Type["END_STRUCT"].gen_type() ]) hdr.add_type(struct) hdr.add_type(Type["INIT_HEADER"].gen_type()) hdr_content = """\ /* {path} */ #ifndef INCLUDE_{fname_upper}_H #define INCLUDE_{fname_upper}_H #include "entry_macro.h" #include "header_init.h" #include "struct_end.h" typedef struct StructA StructA; struct StructA {{ QTAIL_ENTRY(StructA) entry; StructA *next; END_STRUCT }}; INIT_HEADER #endif /* INCLUDE_{fname_upper}_H */ """.format(path=hdr.path, fname_upper=name.upper()) self.files = [(hdr, hdr_content)]
def setUp(self): super(TestForwardDeclarationHeader, self).setUp() name = type(self).__name__ hdr = Header(name.lower() + ".h") src = Source(name.lower() + ".c") a = Structure("A") a.append_field(Pointer(a)("next")) b = Structure("B") b.append_field(Pointer(a)("next")) hdr.add_type(a) hdr_content = """\ /* {path} */ #ifndef INCLUDE_{fname_upper}_H #define INCLUDE_{fname_upper}_H typedef struct A A; struct A {{ A *next; }}; #endif /* INCLUDE_{fname_upper}_H */ """.format(path=hdr.path, fname_upper=name.upper()) src.add_type(b) src_content = """\ /* {} */ #include "{}" typedef struct B {{ A *next; }} B; """.format(src.path, hdr.path) self.files = [(hdr, hdr_content), (src, src_content)]
def setUp(self): super(TestOptimizeInclusions, self).setUp() name = type(self).__name__ src = Source(name.lower() + ".c") ah = Header("a.h") bh = Header("b.h") ch = Header("c.h") ah.add_type(Type("a")) bh.add_type(Type("b")).add_reference(Type["a"]) ch.add_type(Type("c")).add_reference(Type["b"]).add_inclusion(ah) src.add_type(Pointer(Type["c"], "cpointer")) # c.h includes a.h but inclusion of a.h cannot be substituted with c.h # inclusion because it creating reference loop between inclusions of # c.h and b.h. This test checks inclusions optimization correctness and # ordering of chunks. src_content = """\ /* {} */ #include "a.h" #include "b.h" #include "c.h" typedef c *cpointer; """.format(src.path) self.files = [(src, src_content)]
def setUp(self): super(TestNamelessStructure, self).setUp() name = type(self).__name__ hdr = Header(name.lower() + ".h") hdr.add_type(Structure("a")) hdr.add_type(Structure("b", Type["int"]("f"))) hdr.add_type( Structure( "c", Structure(None, Type["int"]("f2"), Structure()("f3"))("f1"))) hdr_content = """\ /* {path} */ #ifndef INCLUDE_{fname_upper}_H #define INCLUDE_{fname_upper}_H typedef struct a {{}} a; typedef struct b {{ int f; }} b; typedef struct c {{ struct {{ int f2; struct {{}} f3; }} f1; }} c; #endif /* INCLUDE_{fname_upper}_H */ """.format(path=hdr.path, fname_upper=name.upper()) self.files = [(hdr, hdr_content)]
def setUp(self): super(TestReferencingToTypeInAnotherHeader, self).setUp() name = type(self).__name__ hdr = Header(name.lower() + ".h") h1 = Header("some_types.h") h2 = Header("another_some_types.h") h1.add_type(Type("f1", incomplete=False)) h2.add_type(Type("f2", incomplete=False)) # Without reference headers `another_some_types` and `some_types` would # be in alphabetical order. h2.add_reference(Type["f1"]) s = Structure("S", Type["f1"]("field1"), Type["f2"]("field2")) hdr.add_type(s) hdr_content = """\ /* {path} */ #ifndef INCLUDE_{fname_upper}_H #define INCLUDE_{fname_upper}_H #include "some_types.h" #include "another_some_types.h" typedef struct S {{ f1 field1; f2 field2; }} S; #endif /* INCLUDE_{fname_upper}_H */ """.format(path=hdr.path, fname_upper=name.upper()) self.files = [(hdr, hdr_content)]
def setUp(self): super(TestEnumerations, self).setUp() name = type(self).__name__ try: h = Header["enums.h"] except: h = Header("enums.h") h.add_type(Enumeration([("EXT", 1)])) src = Source(name.lower() + ".c").add_types([ Enumeration([("ONE", 1)]), Enumeration([("TWO", 2)], enum_name="A"), Enumeration([("THREE", 3)], typedef_name="B"), Enumeration([("FOUR", 4)], enum_name="C", typedef_name="D") ]) a = Type["int"]("a") b = Type["int"]("b") c = Type["A"]("c") d = Type["B"]("d") e = Type["D"]("e") src.add_types([ Function(name="main", body=BodyTree()(Declare(a, b), OpAssign(a, Type["EXT"]), OpAssign(b, Type["ONE"]), Declare(c), OpAssign(c, Type["TWO"]), Declare(d), OpAssign(d, Type["THREE"]), Declare(e), OpAssign(e, Type["FOUR"]))) ]) src_content = """\ /* {} */ #include "enums.h" enum A {{ TWO = 2 }}; typedef enum {{ THREE = 3 }} B; typedef enum C {{ FOUR = 4 }} D; enum {{ ONE = 1 }}; void main(void) {{ int a, b; a = EXT; b = ONE; enum A c; c = TWO; B d; d = THREE; D e; e = FOUR; }} """.format(src.path) self.files = [(src, src_content)]