qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 05/21] qapi: update the qobject visitor to use QUInt


From: Marc-André Lureau
Subject: [Qemu-devel] [PATCH 05/21] qapi: update the qobject visitor to use QUInt
Date: Sat, 11 Mar 2017 17:22:40 +0400

Signed-off-by: Marc-André Lureau <address@hidden>
---
 scripts/qapi-visit.py               |  3 ++-
 qapi/qobject-input-visitor.c        | 30 ++++++++++++++++++++++++------
 qapi/qobject-output-visitor.c       |  3 +--
 tests/test-qobject-input-visitor.c  | 15 +++++++++++++++
 tests/test-qobject-output-visitor.c | 19 +++++++++++++++----
 5 files changed, 57 insertions(+), 13 deletions(-)

diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index 330b9f321b..532f929e18 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -165,7 +165,8 @@ def gen_visit_alternate(name, variants):
     promote_int = 'true'
     ret = ''
     for var in variants.variants:
-        if var.type.alternate_qtype() == 'QTYPE_QINT':
+        if var.type.alternate_qtype() == 'QTYPE_QINT' \
+            or var.type.alternate_qtype() == 'QTYPE_QUINT':
             promote_int = 'false'
 
     ret += mcgen('''
diff --git a/qapi/qobject-input-visitor.c b/qapi/qobject-input-visitor.c
index 865e948ac0..41a825b06b 100644
--- a/qapi/qobject-input-visitor.c
+++ b/qapi/qobject-input-visitor.c
@@ -21,6 +21,7 @@
 #include "qapi/qmp/qjson.h"
 #include "qapi/qmp/types.h"
 #include "qapi/qmp/qerror.h"
+#include "qemu/error-report.h"
 #include "qemu/cutils.h"
 #include "qemu/option.h"
 
@@ -349,7 +350,8 @@ static void qobject_input_start_alternate(Visitor *v, const 
char *name,
     }
     *obj = g_malloc0(size);
     (*obj)->type = qobject_type(qobj);
-    if (promote_int && (*obj)->type == QTYPE_QINT) {
+    if (promote_int &&
+        ((*obj)->type == QTYPE_QINT || (*obj)->type == QTYPE_QUINT)) {
         (*obj)->type = QTYPE_QFLOAT;
     }
 }
@@ -395,22 +397,38 @@ static void qobject_input_type_int64_keyval(Visitor *v, 
const char *name,
 static void qobject_input_type_uint64(Visitor *v, const char *name,
                                       uint64_t *obj, Error **errp)
 {
-    /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
     QObjectInputVisitor *qiv = to_qiv(v);
     QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
+    QUInt *quint;
     QInt *qint;
+    int64_t val;
 
     if (!qobj) {
         return;
     }
+
+    quint = qobject_to_quint(qobj);
+    if (quint) {
+        *obj = quint_get_uint(quint);
+        return;
+    }
+
     qint = qobject_to_qint(qobj);
     if (!qint) {
-        error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
-                   full_name(qiv, name), "integer");
-        return;
+        goto error;
     }
 
-    *obj = qint_get_int(qint);
+    val = qint_get_int(qint);
+    if (val < 0) {
+        goto error;
+    }
+
+    *obj = val;
+    return;
+
+error:
+    error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
+               full_name(qiv, name), "unsigned integer");
 }
 
 static void qobject_input_type_uint64_keyval(Visitor *v, const char *name,
diff --git a/qapi/qobject-output-visitor.c b/qapi/qobject-output-visitor.c
index 871127079d..7066b3b59b 100644
--- a/qapi/qobject-output-visitor.c
+++ b/qapi/qobject-output-visitor.c
@@ -150,9 +150,8 @@ static void qobject_output_type_int64(Visitor *v, const 
char *name,
 static void qobject_output_type_uint64(Visitor *v, const char *name,
                                        uint64_t *obj, Error **errp)
 {
-    /* FIXME values larger than INT64_MAX become negative */
     QObjectOutputVisitor *qov = to_qov(v);
-    qobject_output_add(qov, name, qint_from_int(*obj));
+    qobject_output_add(qov, name, quint_from_uint(*obj));
 }
 
 static void qobject_output_type_bool(Visitor *v, const char *name, bool *obj,
diff --git a/tests/test-qobject-input-visitor.c 
b/tests/test-qobject-input-visitor.c
index 6eb48fee7b..011366a65b 100644
--- a/tests/test-qobject-input-visitor.c
+++ b/tests/test-qobject-input-visitor.c
@@ -170,6 +170,19 @@ static void 
test_visitor_in_int_str_fail(TestInputVisitorData *data,
     error_free_or_abort(&err);
 }
 
+static void test_visitor_in_uint(TestInputVisitorData *data,
+                                const void *unused)
+{
+    uint64_t res = 0;
+    uint64_t value = G_MAXUINT64;
+    Visitor *v;
+
+    v = visitor_input_test_init(data, "%" PRIu64, value);
+
+    visit_type_uint64(v, NULL, &res, &error_abort);
+    g_assert_cmpint(res, ==, value);
+}
+
 static void test_visitor_in_bool(TestInputVisitorData *data,
                                  const void *unused)
 {
@@ -1233,6 +1246,8 @@ int main(int argc, char **argv)
                            NULL, test_visitor_in_int_str_keyval);
     input_visitor_test_add("/visitor/input/int_str_fail",
                            NULL, test_visitor_in_int_str_fail);
+    input_visitor_test_add("/visitor/input/uint",
+                           NULL, test_visitor_in_uint);
     input_visitor_test_add("/visitor/input/bool",
                            NULL, test_visitor_in_bool);
     input_visitor_test_add("/visitor/input/bool_keyval",
diff --git a/tests/test-qobject-output-visitor.c 
b/tests/test-qobject-output-visitor.c
index 500b452d98..047c6b5c6c 100644
--- a/tests/test-qobject-output-visitor.c
+++ b/tests/test-qobject-output-visitor.c
@@ -597,14 +597,25 @@ static void check_native_list(QObject *qobj,
     qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
 
     switch (kind) {
-    case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
-    case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
-    case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
-    case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
     case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
     case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
     case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
     case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
+        for (i = 0; i < 32; i++) {
+            QObject *tmp;
+            QUInt *qvalue;
+            tmp = qlist_peek(qlist);
+            g_assert(tmp);
+            qvalue = qobject_to_quint(tmp);
+            g_assert_cmpint(quint_get_uint(qvalue), ==, i);
+            qobject_decref(qlist_pop(qlist));
+        }
+        break;
+
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
         /* all integer elements in JSON arrays get stored into QInts when
          * we convert to QObjects, so we can check them all in the same
          * fashion, so simply fall through here
-- 
2.12.0.191.gc5d8de91d




reply via email to

[Prev in Thread] Current Thread [Next in Thread]