Author: Armin Rigo <[email protected]>
Branch:
Changeset: r42:52a14bdda091
Date: 2014-09-16 10:58 +0200
http://bitbucket.org/cffi/creflect/changeset/52a14bdda091/
Log: Starting to fix the struct tests
diff --git a/creflect/model.py b/creflect/model.py
--- a/creflect/model.py
+++ b/creflect/model.py
@@ -1,7 +1,7 @@
from .codegen import CodeBlock
-class BaseTypeByIdentity(object):
+class BaseType(object):
is_array_type = False
def _get_c_name(self):
@@ -17,9 +17,6 @@
def inspect_type(self, block, inspect):
block.sprintf_add_both_sides(self.c_name_with_marker)
-
-class BaseType(BaseTypeByIdentity):
-
def __eq__(self, other):
return (self.__class__ == other.__class__ and
self._get_items() == other._get_items())
@@ -197,6 +194,7 @@
class ArrayType(BaseType):
_attrs_ = ('item', 'length')
is_array_type = True
+ const = True # not really applicable, but must have one
def __init__(self, item, length):
self.item = item
@@ -236,57 +234,16 @@
extralength=20)
-class StructOrUnionOrEnum(BaseTypeByIdentity):
- _attrs_ = ('name',)
+class StructOrUnionOrEnum(BaseType):
+ _attrs_ = ('kind', 'name')
-
-class StructOrUnion(StructOrUnionOrEnum):
-
- def __init__(self, name, fldnames, fldtypes, fldbitsize):
+ def __init__(self, kind, name, const):
+ self.kind = kind
self.name = name
- self.fldnames = fldnames
- self.fldtypes = fldtypes
- self.fldbitsize = fldbitsize
+ self.const = const
self.c_name_with_marker = '%s %s&' % (self.kind, self.name)
-
- def write_declaration(self, tr):
- tr.reset_r_output()
- typename = "%s %s" % (self.kind, self.name)
- tr.write_msg(typename)
- tr.write_section("a[%d] = (void *)sizeof(%s); /* size */"
- % (tr.fetch_next_a_index(), typename), indent=4)
- tr.write_section("a[%d] = &((struct{char a; %s b;}*)0)->b; /* align
*/"
- % (tr.fetch_next_a_index(), typename), indent=4)
- tr.flush()
- for fldname, fldtype in zip(self.fldnames, self.fldtypes):
- tr.reset_r_output()
- tr.write_msg("{%s:" % (fldname,))
- tr.fetch_next_a_index() # ignored
- outer = OuterDecl(tr, typename, fldname)
- outer.start()
- #
- xtra = ''
- checktype = fldtype
- while isinstance(checktype, ArrayType):
- xtra += '[0]'
- checktype = checktype.item
- if xtra:
- tr.add_include('stddef.h')
- outer.decllines.append("void *o = (void *)offsetof(%s, %s%s);"
- " /* offset */" %
- (typename, fldname, xtra))
- else:
- outer.decllines.append("void *o = &((%s *)0)->%s; /* offset
*/"
- % (typename, fldname))
- outer.codelines.append("a[%d] = o;" % (tr.fetch_next_a_index(),))
- typeexpr = fldtype.outer_decl(outer)
- outer.stop()
- tr.write_msg(typeexpr.replace('&', ''))
- tr.flush()
-
-
-class StructType(StructOrUnion):
- kind = 'struct'
+ if const:
+ self.c_name_with_marker = 'const ' + self.c_name_with_marker
# ____________________________________________________________
@@ -384,6 +341,50 @@
del self.started
+class StructOrUnionDecl(object):
+
+ def __init__(self, type, fldnames, fldtypes, fldbitsize):
+ self.type = type
+ self.fldnames = fldnames
+ self.fldtypes = fldtypes
+ self.fldbitsize = fldbitsize
+
+ def write_declaration(self, tr):
+ tr.reset_r_output()
+ typename = "%s %s" % (self.kind, self.name)
+ tr.write_msg(typename)
+ tr.write_section("a[%d] = (void *)sizeof(%s); /* size */"
+ % (tr.fetch_next_a_index(), typename), indent=4)
+ tr.write_section("a[%d] = &((struct{char a; %s b;}*)0)->b; /* align
*/"
+ % (tr.fetch_next_a_index(), typename), indent=4)
+ tr.flush()
+ for fldname, fldtype in zip(self.fldnames, self.fldtypes):
+ tr.reset_r_output()
+ tr.write_msg("{%s:" % (fldname,))
+ tr.fetch_next_a_index() # ignored
+ outer = OuterDecl(tr, typename, fldname)
+ outer.start()
+ #
+ xtra = ''
+ checktype = fldtype
+ while isinstance(checktype, ArrayType):
+ xtra += '[0]'
+ checktype = checktype.item
+ if xtra:
+ tr.add_include('stddef.h')
+ outer.decllines.append("void *o = (void *)offsetof(%s, %s%s);"
+ " /* offset */" %
+ (typename, fldname, xtra))
+ else:
+ outer.decllines.append("void *o = &((%s *)0)->%s; /* offset
*/"
+ % (typename, fldname))
+ outer.codelines.append("a[%d] = o;" % (tr.fetch_next_a_index(),))
+ typeexpr = fldtype.outer_decl(outer)
+ outer.stop()
+ tr.write_msg(typeexpr.replace('&', ''))
+ tr.flush()
+
+
class TypeDef(object):
def __init__(self, name, type):
self.name = name
diff --git a/test/codegen/struct-001.c b/test/codegen/struct-001.c
--- a/test/codegen/struct-001.c
+++ b/test/codegen/struct-001.c
@@ -1,40 +1,95 @@
struct foo_s {
- int aa, bb;
+ int aa;
+ int bb;
};
# ____________________________________________________________
-static void __creflect1(void r(char *, void**, int))
+int teststruct_001(char *r)
{
- void *a[3];
- __CREFLECT_PREV(r);
- a[0] = (void *)sizeof(struct foo_s); /* size */
- a[1] = &((struct{char a; struct foo_s b;}*)0)->b; /* align */
- r("struct foo_s", a, 2);
+ if (!r)
+ return 57 + 8 + 18 + 16;
+ r += sprintf(r, "struct foo_s {/*%lld,%lld*/\n", (long long)sizeof(struct
foo_s), (long long)(((char *)&((struct{char a; struct foo_s b;} *)0)->b) -
(char *)0));
{
+ long long o = ((char *)&((struct foo_s *)0)->aa) - (char *)0; /*
check that 'struct foo_s::aa' is not an array */
struct foo_s *p1;
- void *o = &((struct foo_s *)0)->aa; /* offset */
char b[sizeof(p1->aa)];
- a[1] = o;
- p1 = (void *)(((char *)b) - (long)o);
+ p1 = (void *)(((char *)b) - o);
(void)(p1->aa << 1); /* check that 'struct foo_s::aa' is an integer
type */
- p1->aa = -1;
- a[2] = (void *)(p1->aa > 0 ? sizeof(p1->aa) : -sizeof(p1->aa));
+ r += sprintf(r, " /*%lld*/", o);
+ p1->aa = -1; /* check that 'struct foo_s::aa' is not declared 'const'
*/
+ if (p1->aa > 0) {
+ if (sizeof(p1->aa) == 1 && p1->aa == 1)
+ r += sprintf(r, "_Bool");
+ else if (sizeof(p1->aa) == sizeof(unsigned int))
+ r += sprintf(r, "unsigned int");
+ else if (sizeof(p1->aa) == sizeof(unsigned short))
+ r += sprintf(r, "unsigned short");
+ else if (sizeof(p1->aa) == sizeof(unsigned char))
+ r += sprintf(r, "unsigned char");
+ else if (sizeof(p1->aa) == sizeof(unsigned long))
+ r += sprintf(r, "unsigned long");
+ else if (sizeof(p1->aa) == sizeof(unsigned long long))
+ r += sprintf(r, "unsigned long long");
+ else
+ r += sprintf(r, "uint%u_t", (int)sizeof(p1->aa) * 8);
+ }
+ else {
+ if (sizeof(p1->aa) == sizeof(int))
+ r += sprintf(r, "int");
+ else if (sizeof(p1->aa) == sizeof(short))
+ r += sprintf(r, "short");
+ else if (sizeof(p1->aa) == sizeof(signed char))
+ r += sprintf(r, "signed char");
+ else if (sizeof(p1->aa) == sizeof(long))
+ r += sprintf(r, "long");
+ else if (sizeof(p1->aa) == sizeof(long long))
+ r += sprintf(r, "long long");
+ else
+ r += sprintf(r, "int%u_t", (int)sizeof(p1->aa) * 8);
+ }
+ r += sprintf(r, " aa;\n");
}
- r("{aa:int?", a, 3);
{
+ long long o = ((char *)&((struct foo_s *)0)->bb) - (char *)0; /*
check that 'struct foo_s::bb' is not an array */
struct foo_s *p1;
- void *o = &((struct foo_s *)0)->bb; /* offset */
char b[sizeof(p1->bb)];
- a[1] = o;
- p1 = (void *)(((char *)b) - (long)o);
+ p1 = (void *)(((char *)b) - o);
(void)(p1->bb << 1); /* check that 'struct foo_s::bb' is an integer
type */
- p1->bb = -1;
- a[2] = (void *)(p1->bb > 0 ? sizeof(p1->bb) : -sizeof(p1->bb));
+ r += sprintf(r, " /*%lld*/", o);
+ p1->bb = -1; /* check that 'struct foo_s::bb' is not declared 'const'
*/
+ if (p1->bb > 0) {
+ if (sizeof(p1->bb) == 1 && p1->bb == 1)
+ r += sprintf(r, "_Bool");
+ else if (sizeof(p1->bb) == sizeof(unsigned int))
+ r += sprintf(r, "unsigned int");
+ else if (sizeof(p1->bb) == sizeof(unsigned short))
+ r += sprintf(r, "unsigned short");
+ else if (sizeof(p1->bb) == sizeof(unsigned char))
+ r += sprintf(r, "unsigned char");
+ else if (sizeof(p1->bb) == sizeof(unsigned long))
+ r += sprintf(r, "unsigned long");
+ else if (sizeof(p1->bb) == sizeof(unsigned long long))
+ r += sprintf(r, "unsigned long long");
+ else
+ r += sprintf(r, "uint%u_t", (int)sizeof(p1->bb) * 8);
+ }
+ else {
+ if (sizeof(p1->bb) == sizeof(int))
+ r += sprintf(r, "int");
+ else if (sizeof(p1->bb) == sizeof(short))
+ r += sprintf(r, "short");
+ else if (sizeof(p1->bb) == sizeof(signed char))
+ r += sprintf(r, "signed char");
+ else if (sizeof(p1->bb) == sizeof(long))
+ r += sprintf(r, "long");
+ else if (sizeof(p1->bb) == sizeof(long long))
+ r += sprintf(r, "long long");
+ else
+ r += sprintf(r, "int%u_t", (int)sizeof(p1->bb) * 8);
+ }
+ r += sprintf(r, " bb;\n");
}
- r("{bb:int?", a, 3);
+ r += sprintf(r, "};\n");
+ return 0;
}
-
-#expect struct foo_s 8 4
-#expect {aa:int? 99 0 -4
-#expect {bb:int? 99 4 -4
diff --git a/test/test_cgcompile.py b/test/test_cgcompile.py
--- a/test/test_cgcompile.py
+++ b/test/test_cgcompile.py
@@ -63,7 +63,7 @@
lines = g.readlines()
err = g.close()
assert not err
- r_remove_addr = re.compile(r"/[*]-?0x[0-9a-f]+[*]/")
+ r_remove_addr = re.compile(r"/[*][-0-9a-fx,]+[*]/")
got = [r_remove_addr.sub('', line.strip()) for line in lines]
compare_lists(got, expected)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit