Commits

Armin Rigo committed 64647cd

Verifying the typedefs, precisely enough to be able to
distinguish between "typedef int x" and "typedef long x"
even if the sizes of "int" and "long" are the same.

  • Participants
  • Parent commits 1420a9f
  • Branches linux-only

Comments (0)

Files changed (5)

         { "ptrdiff_t",     sizeof(ptrdiff_t) },
         { "size_t",        sizeof(size_t) | UNSIGNED },
         { "ssize_t",       sizeof(ssize_t) },
-        { "wchar_t",       sizeof(wchar_t) | UNSIGNED },
+        /*{ "wchar_t",       sizeof(wchar_t) | UNSIGNED },*/
         { NULL }
     };
 #undef UNSIGNED

cffi/backend_ctypes.py

             if size == ctypes.sizeof(ctypes.c_size_t):
                 result['size_t'] = size | UNSIGNED
                 result['ssize_t'] = size
-            if size == ctypes.sizeof(ctypes.c_wchar):
-                result['wchar_t'] = size | UNSIGNED
+            #if size == ctypes.sizeof(ctypes.c_wchar):
+            #    result['wchar_t'] = size | UNSIGNED
         return result
 
     def load_library(self, path):
         except KeyError:
             return self.new_backend_type(ffi, *args)
 
-    def verifier_declare(self, verifier, kind, name):
-        # nothing to see here
-        pass
+    def verifier_declare_typedef(self, verifier, name):
+        verifier.write('{ %s = (%s**)0; }' % (
+            self.get_c_name('** result'), name))
+
 
 class VoidType(BaseType):
     _attrs_ = ()
     def new_backend_type(self, ffi, result, *args):
         return ffi._backend.new_function_type(args, result, self.ellipsis)
 
-    def verifier_declare(self, verifier, kind, name):
-        if kind == 'function':
-            verifier.write('  { %s = %s; }\n' % (
-                self.get_c_name('result'), name))
+    def verifier_declare_function(self, verifier, name):
+        verifier.write('{ %s = %s; }' % (
+            self.get_c_name('result'), name))
 
 
 class PointerType(BaseType):
         self.check_not_partial()
         return ffi._backend.new_struct_type(self.name)
 
-    def verifier_declare(self, verifier, kind, name):
-        if kind != 'struct':
-            return
+    def verifier_declare_struct(self, verifier, name):
         if self.fldnames is None:
             assert not self.partial
             return
             f.write('#include <stdio.h>\n')
             f.write('#include <stdint.h>\n')
             f.write('#include <stddef.h>\n')
+            f.write('#include <unistd.h>\n')
             f.write(preamble + "\n\n")
             f.write('int main() {\n')
             self.f = f
             for name, tp in ffi._parser._declarations.iteritems():
                 kind, realname = name.split(' ', 1)
-                tp.verifier_declare(self, kind, realname)
+                method = getattr(tp, 'verifier_declare_' + kind)
+                method(self, realname)
             del self.f
             f.write('  return 0;\n')
             f.write('}\n')

testing/test_verify.py

     ffi.verify("#include <string.h>")
 
 
+def test_verify_typedefs():
+    types = ['signed char', 'unsigned char', 'int', 'long']
+    for cdefed in types:
+        for real in types:
+            ffi = FFI()
+            ffi.cdef("typedef %s foo_t;" % cdefed)
+            if cdefed == real:
+                ffi.verify("typedef %s foo_t;" % real)
+            else:
+                py.test.raises(VerificationError, ffi.verify,
+                               "typedef %s foo_t;" % real)
+
+
 def test_ffi_nonfull_struct():
     py.test.skip("XXX")
     ffi = FFI()