[pypy-commit] creflect default: Small reorganizations until all tests pass
arigo
noreply at buildbot.pypy.org
Tue Nov 18 21:23:19 CET 2014
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r93:b04afb735e35
Date: 2014-11-18 21:23 +0100
http://bitbucket.org/cffi/creflect/changeset/b04afb735e35/
Log: Small reorganizations until all tests pass
diff --git a/creflect/codegen.py b/creflect/codegen.py
--- a/creflect/codegen.py
+++ b/creflect/codegen.py
@@ -56,6 +56,7 @@
def transform_cdef(csource, crx_func_name):
outerblock = CodeBlock(parent=None)
+ outerblock.writeline('#include "creflect.h"')
outerblock.crx_func_name = crx_func_name
funcblock = CodeBlock(outerblock)
diff --git a/creflect/driver.py b/creflect/driver.py
--- a/creflect/driver.py
+++ b/creflect/driver.py
@@ -73,22 +73,13 @@
_DEBUG_TEMPLATE1 = '''
/***** CREFLECT debug code *****/
-#include <stdio.h>
-#include <stdlib.h>
+#include "creflect_print.h"
int main(void) {
%s return 0;
}
'''
_DEBUG_TEMPLATE2 = '''\
- {
- int size = %(funcname)s((char *)0);
- char *result = malloc(size);
- int err = %(funcname)s(result);
- printf("%%s", result);
- free(result);
- if (err != 0)
- return 1;
- }
+ %(funcname)s(&maincb);
'''
def get_debug_code(reflection_func_name):
diff --git a/creflect/model.py b/creflect/model.py
--- a/creflect/model.py
+++ b/creflect/model.py
@@ -117,7 +117,10 @@
if not self.const:
block.writeline("%s = -1;%s" % (star_p1, comment3))
if self.is_integer_type():
- expr = 'CRX_INT_TYPE(cb, %s, "%s")' % (star_p1, self.name)
+ hint = self.name.split()
+ if hint[0] in ('signed', 'unsigned'):
+ hint = hint[1:]
+ expr = 'CRX_INT_TYPE(cb, %s, "%s")' % (star_p1, " ".join(hint))
elif self.is_char_type():
errmsg = "numeric type '%s' is not a char" % (
inspect.get_comment_type(0, False),)
diff --git a/creflect/creflect.h b/creflect/src/creflect.h
rename from creflect/creflect.h
rename to creflect/src/creflect.h
--- a/creflect/creflect.h
+++ b/creflect/src/creflect.h
@@ -1,3 +1,6 @@
+#ifndef _CREFLECT_H_
+#define _CREFLECT_H_
+
#include <stddef.h>
#include <string.h>
@@ -123,17 +126,17 @@
else if (fits_int) {
vp->as_unsigned_int = (unsigned int)value;
size = sizeof(unsigned int);
- name = "unsigned int";
+ name = "int";
}
else if (fits_long) {
vp->as_unsigned_long = (unsigned long)value;
size = sizeof(unsigned long);
- name = "unsigned long";
+ name = "long";
}
else {
vp->as_unsigned_long_long = (unsigned long long)value;
size = sizeof(unsigned long long);
- name = "unsigned long long";
+ name = "long long";
}
return cb->get_unsigned_type(cb, size, name);
}
@@ -196,3 +199,5 @@
}
return cb->get_float_type(cb, size, name);
}
+
+#endif /* _CREFLECT_H_ */
diff --git a/test/cgcompile.c b/creflect/src/creflect_print.h
rename from test/cgcompile.c
rename to creflect/src/creflect_print.h
--- a/test/cgcompile.c
+++ b/creflect/src/creflect_print.h
@@ -1,4 +1,5 @@
#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
#include <assert.h>
@@ -43,40 +44,61 @@
static crx_type_t *tst_get_signed_type(crx_builder_t *cb, size_t sz,
const char *g)
{
-#define TT(name) if (strcmp(g, #name) == 0) { found = sizeof(name); }
- int found = 0;
+ int skip = 0;
+ if (sizeof(long) == sizeof(long long))
+ if (strcmp(g, "long long") == 0)
+ skip = 4;
+ if (sizeof(int) == sizeof(long))
+ if (strcmp(g, "long") == 0 || strcmp(g, "long long") == 0)
+ skip = 3;
+
+#define TT(name) if (--skip && sz == sizeof(name)) { return newtype(#name); }
TT(signed char);
TT(short);
TT(int);
TT(long);
TT(long long);
- assert(found == sz);
- return newtype(g);
+
+ printf("cannot find signed type with %zd bytes\n", sz);
+ abort();
}
static crx_type_t *tst_get_unsigned_type(crx_builder_t *cb, size_t sz,
const char *g)
{
- int found = 0;
+ int skip = 0;
+ if (sizeof(long) == sizeof(long long))
+ if (strcmp(g, "long long") == 0)
+ skip = 4;
+ if (sizeof(int) == sizeof(long))
+ if (strcmp(g, "long") == 0 || strcmp(g, "long long") == 0)
+ skip = 3;
+
TT(unsigned char);
TT(unsigned short);
TT(unsigned int);
TT(unsigned long);
TT(unsigned long long);
- assert(found == sz);
- return newtype(g);
+
+ printf("cannot find unsigned type with %zd bytes\n", sz);
+ abort();
}
static crx_type_t *tst_get_float_type(crx_builder_t *cb, size_t sz,
const char *g)
{
- int found = 0;
+ int skip = 0;
+ if (sizeof(double) == sizeof(long double))
+ if (strcmp(g, "long double") == 0)
+ skip = 2;
+
TT(float);
TT(double);
TT(long double);
- assert(found == sz);
#undef TT
- return newtype(g);
+
+ printf("cannot find float type with %zd bytes\n", sz);
+ abort();
}
static crx_type_t *tst_get_function_type(crx_builder_t *cb, crx_type_t *ret,
@@ -238,9 +260,3 @@
tst_define_num_const,
tst_error,
};
-
-int main(void)
-{
- TESTFN(&maincb);
- return 0;
-}
diff --git a/test/codegen/003d.c b/test/codegen/003d.c
--- a/test/codegen/003d.c
+++ b/test/codegen/003d.c
@@ -11,7 +11,7 @@
p1 = (void *)b;
(void)(*p1 << 1); /* check that 'num_t' is an integer type */
*p1 = -1; /* check that 'num_t' is not declared 'const' */
- t1 = CRX_INT_TYPE(cb, *p1, "signed char");
+ t1 = CRX_INT_TYPE(cb, *p1, "char");
cb->define_type(cb, "num_t", t1);
#expect TYPEDEF num_t = signed char
}
diff --git a/test/codegen/003f.c b/test/codegen/003f.c
--- a/test/codegen/003f.c
+++ b/test/codegen/003f.c
@@ -11,7 +11,7 @@
p1 = (void *)b;
(void)(*p1 << 1); /* check that 'num_t' is an integer type */
*p1 = -1; /* check that 'num_t' is not declared 'const' */
- t1 = CRX_INT_TYPE(cb, *p1, "unsigned long long");
+ t1 = CRX_INT_TYPE(cb, *p1, "long long");
cb->define_type(cb, "num_t", t1);
#expect TYPEDEF num_t = unsigned long long
}
diff --git a/test/codegen/003h.c b/test/codegen/003h.c
new file mode 100644
--- /dev/null
+++ b/test/codegen/003h.c
@@ -0,0 +1,22 @@
+typedef long long num_t;
+
+# ____________________________________________________________
+
+#define num_t int
+
+# ____________________________________________________________
+
+void test003h(crx_builder_t *cb)
+{
+ crx_type_t *t1;
+ {
+ num_t *p1;
+ char b[sizeof(*p1)];
+ p1 = (void *)b;
+ (void)(*p1 << 1); /* check that 'num_t' is an integer type */
+ *p1 = -1; /* check that 'num_t' is not declared 'const' */
+ t1 = CRX_INT_TYPE(cb, *p1, "long long");
+ cb->define_type(cb, "num_t", t1);
+#expect TYPEDEF num_t = int
+ }
+}
diff --git a/test/codegen/006.c b/test/codegen/006.c
--- a/test/codegen/006.c
+++ b/test/codegen/006.c
@@ -11,7 +11,7 @@
memset(b, -1, sizeof(b));
p1 = (void *)b;
(void)(*p1 << 1); /* check that 'num_t' is an integer type */
- t1 = CRX_INT_TYPE(cb, *p1, "unsigned int");
+ t1 = CRX_INT_TYPE(cb, *p1, "int");
t2 = cb->get_const_type(cb, t1);
cb->define_type(cb, "num_t", t2);
#expect TYPEDEF num_t = CONST unsigned int
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
@@ -31,7 +31,7 @@
p1 = (void *)(((char *)b) - o);
(void)(p1->bb << 1); /* check that 'struct foo_s::bb' is an integer type */
p1->bb = -1; /* check that 'struct foo_s::bb' is not declared 'const' */
- t3 = CRX_INT_TYPE(cb, p1->bb, "unsigned int");
+ t3 = CRX_INT_TYPE(cb, p1->bb, "int");
d1[1].name = "bb";
d1[1].type = t3;
d1[1].offset = o;
diff --git a/test/test_cgcompile.py b/test/test_cgcompile.py
--- a/test/test_cgcompile.py
+++ b/test/test_cgcompile.py
@@ -21,17 +21,23 @@
assert infile != outfile
f = open(infile, 'w')
f.write('#include <stdio.h>\n')
- f.write('#include "%s/../../creflect/creflect.h"\n' % path)
- f.write('#define TESTFN(cb) test%s(cb)\n' % (basename.replace('-','_'),))
+ f.write('#include "%s/../../creflect/src/creflect.h"\n' % path)
f.write('\n')
for line in inputlines:
if not (line.startswith('# ') or line.startswith('#expect')):
f.write(line)
f.write('\n')
- f.write('#include "%s/../cgcompile.c"\n' % path)
+ f.write('#include "%s/../../creflect/src/creflect_print.h"\n' % path)
+ f.write('''
+int main(void)
+{
+ test%s(&maincb);
+ return 0;
+}
+''' % (basename.replace('-','_'),))
f.close()
#
- err = os.system("gcc -Werror -Wall '%s' -o '%s'" % (infile, outfile))
+ err = os.system("gcc -g -Werror -Wall '%s' -o '%s'" % (infile, outfile))
assert err == 0
#
g = os.popen("'%s'" % (outfile,), 'r')
diff --git a/test/test_driver.py b/test/test_driver.py
--- a/test/test_driver.py
+++ b/test/test_driver.py
@@ -9,9 +9,11 @@
print >> f, get_debug_code(funcname)
f.close()
#
+ from creflect.driver import __file__
+ main_dir = os.path.abspath(os.path.dirname(__file__))
err = os.system(
- "cd '%s' && gcc -fPIC -Werror -Wall %s.c -o %s"
- % (str(udir), funcname, funcname))
+ "cd '%s' && gcc -fPIC -Werror -Wall %s.c -o %s -I%s/src"
+ % (str(udir), funcname, funcname, main_dir))
assert err == 0
#
g = os.popen("cd '%s' && %s" % (str(udir), funcname), 'r')
@@ -26,7 +28,7 @@
gen = expand_cdef("typedef long a_t[20];", "test_expand_cdef")
code = '/* real code */\n%s\n/* generated code */\n%s' % (real_code, gen)
data = compile_and_run(code, "test_expand_cdef")
- assert data == real_code
+ assert data == "TYPEDEF a_t = ARRAY[42] short\n"
def test_copy_file_and_expand():
@@ -41,4 +43,4 @@
copy_file_and_expand(f, g)
#
data = compile_and_run(g.getvalue(), "inspect1")
- assert data == "typedef unsigned short a_t[5];\n"
+ assert data == "TYPEDEF a_t = ARRAY[5] unsigned short\n"
More information about the pypy-commit
mailing list