Author: Armin Rigo <ar...@tunes.org>
Branch: 
Changeset: r100:1d714ead0a23
Date: 2014-11-28 13:10 +0100
http://bitbucket.org/cffi/creflect/changeset/1d714ead0a23/

Log:    Support '#define FOO <some integer constant>'

diff --git a/creflect/cparser.py b/creflect/cparser.py
--- a/creflect/cparser.py
+++ b/creflect/cparser.py
@@ -30,6 +30,9 @@
 r_comment3 = re.compile(r"[*]/")
 r_empty_braces = re.compile(r"{(\s*)}")     # after comments have been removed
 r_typedef_dotdodot = re.compile(r"\btypedef(\s*)[.][.][.]")
+r_define_var = re.compile(
+    r"^[ \t]*#[ \t]*define[ \t]+([A-Za-z_][A-Za-z0-9_]+)[ \t][^\n]+",
+    re.MULTILINE)
 
 def remove_comments(csource):
     csource = r_comment1.sub('', csource)     # remove the '//' comments
@@ -56,6 +59,9 @@
 def expand_typedef_dotdotdot(csource):
     return r_typedef_dotdodot.sub(r'typedef\1 __crx_unknown_t ', csource)
 
+def expand_define_var(csource):
+    return r_define_var.sub(r'const int \1;', csource)
+
 
 class CSource(object):
 
@@ -68,6 +74,7 @@
         csource = remove_comments(self.cdefblock)
         csource = expand_empty_braces(csource)
         csource = expand_typedef_dotdotdot(csource)
+        csource = expand_define_var(csource)
         return 'typedef int __crx_unknown_t; ' + csource
 
     def to_ast(self):
diff --git a/test/codegen/macro-001.c b/test/codegen/macro-001.c
new file mode 100644
--- /dev/null
+++ b/test/codegen/macro-001.c
@@ -0,0 +1,23 @@
+#define FOO  42
+#define BAR  (40U + 2)
+
+# ____________________________________________________________
+
+void testmacro_001(crx_builder_t *cb)
+{
+    crx_type_t *t1, *t2;
+    {
+        crx_num_const_t v;
+        (void)((FOO) << 1);  /* check that 'FOO' is an integer */
+        t1 = CRX_INT_CONST(cb, FOO, &v, 1);
+        cb->define_num_const(cb, "FOO", t1, &v);
+#expect NUMCONST FOO = int 42
+    }
+    {
+        crx_num_const_t v;
+        (void)((BAR) << 1);  /* check that 'BAR' is an integer */
+        t2 = CRX_INT_CONST(cb, BAR, &v, 1);
+        cb->define_num_const(cb, "BAR", t2, &v);
+#expect NUMCONST BAR = unsigned int 42
+    }
+}
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to