From c1b4a0fd13edd08a9d63c2eaaadf4d55a208ad2c Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 9 Apr 2020 16:57:19 -0400 Subject: [PATCH 019/179] FIXME: more tests --- first-field-1.c | 24 ++++++++++++++++++++++++ first-field-2.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 first-field-1.c create mode 100644 first-field-2.c diff --git a/first-field-1.c b/first-field-1.c new file mode 100644 index 00000000000..0754cf7f884 --- /dev/null +++ b/first-field-1.c @@ -0,0 +1,24 @@ +/* A toy re-implementation of CPython's object model. */ + +typedef struct base_obj +{ + int m_first; + int m_second; +} base_obj; + +typedef struct sub_obj +{ + base_obj base; +} sub_obj; + +void test (sub_obj *sub) +{ + sub->base.m_first = 1; + sub->base.m_second = 2; + __analyzer_eval (sub->base.m_first == 1); + __analyzer_eval (sub->base.m_second == 2); + + base_obj *base = (struct base_obj *)sub; + __analyzer_eval (base->m_first == 1); + __analyzer_eval (base->m_second == 2); +} diff --git a/first-field-2.c b/first-field-2.c new file mode 100644 index 00000000000..59e73ae2585 --- /dev/null +++ b/first-field-2.c @@ -0,0 +1,46 @@ +/* A toy re-implementation of CPython's object model. */ + +#include +#include +#include + +typedef struct base_obj base_obj; +typedef struct string_obj string_obj; + +struct base_obj +{ + int ob_refcnt; +}; + +struct type_obj +{ + base_obj tp_base; +}; + +struct string_obj +{ + base_obj str_base; + size_t str_len; + char str_buf[]; +}; + +type_obj type_type = { + { &type_type, 1}, +}; + +type_obj str_type = { + { &type_type, 1}, +}; + +base_obj *alloc_obj (type_obj *ob_type, size_t sz) +{ + size_t len = strlen (str); + base_obj *obj = (base_obj *)malloc (sizeof (string_obj) + len + 1); + if (!obj) + return NULL; + obj->ob_type = &str_type; + obj->ob_refcnt = 1; + string_obj *str_obj = (string_obj *)obj; + __analyzer_eval (str_obj->str_base.ob_type == &str_type); + __analyzer_eval (str_obj->str_base.ob_refcnt == 1); +} -- 2.21.0