def test_typedef(self): self.assertPrettyPrint(typedef_type("INT", int_type("int", 4, True)), "typedef int INT") self.assertPrettyPrint( typedef_type("CINT", int_type("int", 4, True, Qualifiers.CONST)), "typedef const int CINT", ) self.assertPrettyPrint( typedef_type("INT", int_type("int", 4, True), Qualifiers.CONST), "const typedef int INT", ) self.assertPrettyPrint( typedef_type("string", pointer_type(8, int_type("char", 1, True))), "typedef char *string", ) t = typedef_type( "Point", struct_type( None, 8, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 4), ), ), ) self.assertPrettyPrint( t, """\ typedef struct { int x; int y; } Point""", )
def test_typedef(self): self.assertPrettyPrint(typedef_type('INT', int_type('int', 4, True)), 'typedef int INT') self.assertPrettyPrint( typedef_type('CINT', int_type('int', 4, True, Qualifiers.CONST)), 'typedef const int CINT') self.assertPrettyPrint( typedef_type('INT', int_type('int', 4, True), Qualifiers.CONST), 'const typedef int INT') self.assertPrettyPrint( typedef_type('string', pointer_type(8, int_type('char', 1, True))), 'typedef char *string') t = typedef_type( 'Point', struct_type(None, 8, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 4), ))) self.assertPrettyPrint( t, """\ typedef struct { int x; int y; } Point""")
def test_default_primitive_types(self): def spellings(tokens, num_optional=0): for i in range(len(tokens) - num_optional, len(tokens) + 1): for perm in itertools.permutations(tokens[:i]): yield " ".join(perm) for word_size in [8, 4]: prog = mock_program( MOCK_PLATFORM if word_size == 8 else MOCK_32BIT_PLATFORM ) self.assertEqual(prog.type("_Bool"), bool_type("_Bool", 1)) self.assertEqual(prog.type("char"), int_type("char", 1, True)) for spelling in spellings(["signed", "char"]): self.assertEqual(prog.type(spelling), int_type("signed char", 1, True)) for spelling in spellings(["unsigned", "char"]): self.assertEqual( prog.type(spelling), int_type("unsigned char", 1, False) ) for spelling in spellings(["short", "signed", "int"], 2): self.assertEqual(prog.type(spelling), int_type("short", 2, True)) for spelling in spellings(["short", "unsigned", "int"], 1): self.assertEqual( prog.type(spelling), int_type("unsigned short", 2, False) ) for spelling in spellings(["int", "signed"], 1): self.assertEqual(prog.type(spelling), int_type("int", 4, True)) for spelling in spellings(["unsigned", "int"]): self.assertEqual( prog.type(spelling), int_type("unsigned int", 4, False) ) for spelling in spellings(["long", "signed", "int"], 2): self.assertEqual(prog.type(spelling), int_type("long", word_size, True)) for spelling in spellings(["long", "unsigned", "int"], 1): self.assertEqual( prog.type(spelling), int_type("unsigned long", word_size, False) ) for spelling in spellings(["long", "long", "signed", "int"], 2): self.assertEqual(prog.type(spelling), int_type("long long", 8, True)) for spelling in spellings(["long", "long", "unsigned", "int"], 1): self.assertEqual( prog.type(spelling), int_type("unsigned long long", 8, False) ) self.assertEqual(prog.type("float"), float_type("float", 4)) self.assertEqual(prog.type("double"), float_type("double", 8)) for spelling in spellings(["long", "double"]): self.assertEqual(prog.type(spelling), float_type("long double", 16)) self.assertEqual( prog.type("size_t"), typedef_type("size_t", int_type("unsigned long", word_size, False)), ) self.assertEqual( prog.type("ptrdiff_t"), typedef_type("ptrdiff_t", int_type("long", word_size, True)), )
def test_default_primitive_types(self): def spellings(tokens, num_optional=0): for i in range(len(tokens) - num_optional, len(tokens) + 1): for perm in itertools.permutations(tokens[:i]): yield ' '.join(perm) for word_size in [8, 4]: prog = mock_program(MOCK_ARCH if word_size == 8 else MOCK_32BIT_ARCH) self.assertEqual(prog.type('_Bool'), bool_type('_Bool', 1)) self.assertEqual(prog.type('char'), int_type('char', 1, True)) for spelling in spellings(['signed', 'char']): self.assertEqual(prog.type(spelling), int_type('signed char', 1, True)) for spelling in spellings(['unsigned', 'char']): self.assertEqual(prog.type(spelling), int_type('unsigned char', 1, False)) for spelling in spellings(['short', 'signed', 'int'], 2): self.assertEqual(prog.type(spelling), int_type('short', 2, True)) for spelling in spellings(['short', 'unsigned', 'int'], 1): self.assertEqual(prog.type(spelling), int_type('unsigned short', 2, False)) for spelling in spellings(['int', 'signed'], 1): self.assertEqual(prog.type(spelling), int_type('int', 4, True)) for spelling in spellings(['unsigned', 'int']): self.assertEqual(prog.type(spelling), int_type('unsigned int', 4, False)) for spelling in spellings(['long', 'signed', 'int'], 2): self.assertEqual(prog.type(spelling), int_type('long', word_size, True)) for spelling in spellings(['long', 'unsigned', 'int'], 1): self.assertEqual(prog.type(spelling), int_type('unsigned long', word_size, False)) for spelling in spellings(['long', 'long', 'signed', 'int'], 2): self.assertEqual(prog.type(spelling), int_type('long long', 8, True)) for spelling in spellings(['long', 'long', 'unsigned', 'int'], 1): self.assertEqual(prog.type(spelling), int_type('unsigned long long', 8, False)) self.assertEqual(prog.type('float'), float_type('float', 4)) self.assertEqual(prog.type('double'), float_type('double', 8)) for spelling in spellings(['long', 'double']): self.assertEqual(prog.type(spelling), float_type('long double', 16)) self.assertEqual( prog.type('size_t'), typedef_type('size_t', int_type('unsigned long', word_size, False))) self.assertEqual( prog.type('ptrdiff_t'), typedef_type('ptrdiff_t', int_type('long', word_size, True)))
def test_size_t_and_ptrdiff_t(self): # 64-bit architecture with 4-byte long/unsigned long. prog = mock_program( types=[int_type("long", 4, True), int_type("unsigned long", 4, False),] ) self.assertEqual( prog.type("size_t"), typedef_type("size_t", prog.type("unsigned long long")) ) self.assertEqual( prog.type("ptrdiff_t"), typedef_type("ptrdiff_t", prog.type("long long")) ) # 32-bit architecture with 8-byte long/unsigned long. prog = mock_program( MOCK_32BIT_PLATFORM, types=[int_type("long", 8, True), int_type("unsigned long", 8, False),], ) self.assertEqual( prog.type("size_t"), typedef_type("size_t", prog.type("unsigned int")) ) self.assertEqual( prog.type("ptrdiff_t"), typedef_type("ptrdiff_t", prog.type("int")) ) # Nonsense sizes. prog = mock_program( types=[ int_type("int", 1, True), int_type("unsigned int", 1, False), int_type("long", 1, True), int_type("unsigned long", 1, False), int_type("long long", 2, True), int_type("unsigned long long", 2, False), ] ) self.assertRaisesRegex( ValueError, "no suitable integer type for size_t", prog.type, "size_t" ) self.assertRaisesRegex( ValueError, "no suitable integer type for ptrdiff_t", prog.type, "ptrdiff_t" )
def test_size_t_and_ptrdiff_t(self): # 64-bit architecture with 4-byte long/unsigned long. prog = mock_program(types=[ int_type('long', 4, True), int_type('unsigned long', 4, False), ]) self.assertEqual( prog.type('size_t'), typedef_type('size_t', prog.type('unsigned long long'))) self.assertEqual(prog.type('ptrdiff_t'), typedef_type('ptrdiff_t', prog.type('long long'))) # 32-bit architecture with 8-byte long/unsigned long. prog = mock_program(MOCK_32BIT_ARCH, types=[ int_type('long', 8, True), int_type('unsigned long', 8, False), ]) self.assertEqual(prog.type('size_t'), typedef_type('size_t', prog.type('unsigned int'))) self.assertEqual(prog.type('ptrdiff_t'), typedef_type('ptrdiff_t', prog.type('int'))) # Nonsense sizes. prog = mock_program(types=[ int_type('int', 1, True), int_type('unsigned int', 1, False), int_type('long', 1, True), int_type('unsigned long', 1, False), int_type('long long', 2, True), int_type('unsigned long long', 2, False), ]) self.assertRaisesRegex(ValueError, 'no suitable integer type for size_t', prog.type, 'size_t') self.assertRaisesRegex(ValueError, 'no suitable integer type for ptrdiff_t', prog.type, 'ptrdiff_t')
def test_typedef(self): t = typedef_type("INT", int_type("int", 4, True)) self.assertEqual(t.kind, TypeKind.TYPEDEF) self.assertIsNone(t.primitive) self.assertEqual(t.language, DEFAULT_LANGUAGE) self.assertEqual(t.name, "INT") self.assertEqual(t.type, int_type("int", 4, True)) self.assertTrue(t.is_complete()) self.assertEqual(t, typedef_type("INT", int_type("int", 4, True))) # Qualified type argument. self.assertEqual(t, typedef_type("INT", int_type("int", 4, True))) # Different name. self.assertNotEqual(t, typedef_type("integer", int_type("int", 4, True))) # Different type. self.assertNotEqual( t, typedef_type("integer", int_type("unsigned int", 4, False))) self.assertNotEqual( t, typedef_type("INT", int_type("int", 4, True, Qualifiers.CONST))) self.assertEqual( repr(t), "typedef_type(name='INT', type=int_type(name='int', size=4, is_signed=True))", ) self.assertEqual(sizeof(t), 4) t = typedef_type("VOID", void_type()) self.assertFalse(t.is_complete()) self.assertRaises(TypeError, typedef_type, None, int_type("int", 4, True)) self.assertRaises(TypeError, typedef_type, "INT", 4) self.assertEqual( typedef_type("size_t", int_type("unsigned long", 8, False)).primitive, PrimitiveType.C_SIZE_T, ) self.assertEqual( typedef_type("ptrdiff_t", int_type("long", 8, True)).primitive, PrimitiveType.C_PTRDIFF_T, )
def test_typedef(self): t = typedef_type('INT', int_type('int', 4, True)) self.assertEqual(t.kind, TypeKind.TYPEDEF) self.assertIsNone(t.primitive) self.assertEqual(t.name, 'INT') self.assertEqual(t.type, int_type('int', 4, True)) self.assertTrue(t.is_complete()) self.assertEqual(t, typedef_type('INT', int_type('int', 4, True))) # Qualified type argument. self.assertEqual(t, typedef_type('INT', int_type('int', 4, True))) # Different name. self.assertNotEqual(t, typedef_type('integer', int_type('int', 4, True))) # Different type. self.assertNotEqual( t, typedef_type('integer', int_type('unsigned int', 4, False))) self.assertNotEqual( t, typedef_type('INT', int_type('int', 4, True, Qualifiers.CONST))) self.assertEqual( repr(t), "typedef_type(name='INT', type=int_type(name='int', size=4, is_signed=True))" ) self.assertEqual(sizeof(t), 4) t = typedef_type('VOID', void_type()) self.assertFalse(t.is_complete()) self.assertRaises(TypeError, typedef_type, None, int_type('int', 4, True)) self.assertRaises(TypeError, typedef_type, 'INT', 4) self.assertEqual( typedef_type('size_t', int_type('unsigned long', 8, False)).primitive, PrimitiveType.C_SIZE_T) self.assertEqual( typedef_type('ptrdiff_t', int_type('long', 8, True)).primitive, PrimitiveType.C_PTRDIFF_T)
def test_function_typedef(self): self.assertPrettyPrint( typedef_type("fn", function_type(int_type("int", 4, True), (), False)), "typedef int fn(void)", )
(TypeMember(point_type, "a"), TypeMember(point_type, "b", 64))) option_type = union_type( "option", 4, ( TypeMember(int_type("int", 4, True), "i"), TypeMember(float_type("float", 4), "f"), ), ) color_type = enum_type( "color", int_type("unsigned int", 4, False), (TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1), TypeEnumerator("BLUE", 2)), ) pid_type = typedef_type("pid_t", int_type("int", 4, True)) MOCK_32BIT_PLATFORM = Platform(Architecture.UNKNOWN, PlatformFlags.IS_LITTLE_ENDIAN) MOCK_PLATFORM = Platform( Architecture.UNKNOWN, PlatformFlags.IS_64_BIT | PlatformFlags.IS_LITTLE_ENDIAN) class MockMemorySegment(NamedTuple): buf: bytes virt_addr: Optional[int] = None phys_addr: Optional[int] = None def mock_memory_read(data, address, count, offset, physical):
point_type = struct_type('point', 8, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 32), )) line_segment_type = struct_type('line_segment', 16, ( (point_type, 'a'), (point_type, 'b', 64), )) option_type = union_type('option', 4, ( (int_type('int', 4, True), 'i'), (float_type('float', 4), 'f'), )) color_type = enum_type('color', int_type('unsigned int', 4, False), (('RED', 0), ('GREEN', 1), ('BLUE', 2))) pid_type = typedef_type('pid_t', int_type('int', 4, True)) MOCK_32BIT_PLATFORM = Platform(Architecture.UNKNOWN, PlatformFlags.IS_LITTLE_ENDIAN) MOCK_PLATFORM = Platform( Architecture.UNKNOWN, PlatformFlags.IS_64_BIT | PlatformFlags.IS_LITTLE_ENDIAN) class MockMemorySegment(NamedTuple): buf: bytes virt_addr: Optional[int] = None phys_addr: Optional[int] = None def mock_memory_read(data, address, count, offset, physical):
def test_function_typedef(self): self.assertPrettyPrint( typedef_type('fn', function_type(int_type('int', 4, True), (), False)), 'typedef int fn(void)')