Skip to contentSkip to conversation

Generate a literal qobject for introspection

PATCH Superseded Marc-André Lureau 4 participants 32 messages Aug 24, 2017 – Sep 1, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Generate a literal qobject for introspection

Hi,

This series is based on patches 2-5 from "[PATCH v2 00/54] qapi: add #if pre-processor conditions to generated code". Generating a literal qobject will allow to easily introduce #if conditionals from the original series.

Marc-André Lureau (14):
qdict: add qdict_put_null() helper
qlit: move qlit from check-qjson to qobject/
qlit: use QLit prefix consistently
qlit: remove needless type cast
qlit: rename compare_litqobj_to_qobj
qlit: make qlit_equal_qobject return a bool
qlit: make qlit_equal_qobject() take const arguments
qlit: add QLIT_QNULL and QLIT_BOOL
qlit: replace assert(qnum_get_try_int)
tests: add qlit tests
qlit: improve QLit dict vs qdict comparison
qlit: improve QLit list vs qlist comparison
qlit: add qobject_form_qlit()
qapi: generate a literal qobject for introspection

 scripts/qapi-introspect.py         |  87 +++++++++++---------
 include/qapi/qmp/qdict.h           |   4 +-
 include/qapi/qmp/qlit.h            |  56 +++++++++++++
 monitor.c                          |   2 +-
 qobject/qlit.c                     | 157 +++++++++++++++++++++++++++++++++++++
 target/i386/cpu.c                  |   4 +-
 tests/check-qjson.c                | 150 +++++++----------------------------
 tests/check-qlit.c                 | 100 +++++++++++++++++++++++
 tests/test-qobject-input-visitor.c |  11 ++-
 docs/devel/qapi-code-gen.txt       |  29 ++++---
 qobject/Makefile.objs              |   2 +-
 tests/Makefile.include             |   5 +-
 12 files changed, 429 insertions(+), 178 deletions(-)
 create mode 100644 include/qapi/qmp/qlit.h
 create mode 100644 qobject/qlit.c
 create mode 100644 tests/check-qlit.c
Signature · 1 line
2.14.1.146.gd35faa819
#1 lore raw

PATCH 1/14 qdict: add qdict_put_null() helper

A step towards completeness.

---
 include/qapi/qmp/qdict.h | 4 +++-
 target/i386/cpu.c        | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)
include/qapi/qmp/qdict.h +3 -1
index 363e431106..6588c7f0c8 100644
@@ -53,13 +53,15 @@ void qdict_destroy_obj(QObject *obj);
 #define qdict_put(qdict, key, obj) \
         qdict_put_obj(qdict, key, QOBJECT(obj))
 
-/* Helpers for int, bool, and string */
+/* Helpers for int, bool, null, and string */
 #define qdict_put_int(qdict, key, value) \
         qdict_put(qdict, key, qnum_from_int(value))
 #define qdict_put_bool(qdict, key, value) \
         qdict_put(qdict, key, qbool_from_bool(value))
 #define qdict_put_str(qdict, key, value) \
         qdict_put(qdict, key, qstring_from_str(value))
+#define qdict_put_null(qdict, key) \
+        qdict_put(qdict, key, qnull())
 
 /* High level helpers */
 double qdict_get_double(const QDict *qdict, const char *key);
target/i386/cpu.c +2 -2
index ddc45abd70..bec2009a9c 100644
@@ -2454,7 +2454,7 @@ static QDict *x86_cpu_static_props(void)
 
     d = qdict_new();
     for (i = 0; props[i]; i++) {
-        qdict_put(d, props[i], qnull());
+        qdict_put_null(d, props[i]);
     }
 
     for (w = 0; w < FEATURE_WORDS; w++) {
@@ -2464,7 +2464,7 @@ static QDict *x86_cpu_static_props(void)
             if (!fi->feat_names[bit]) {
                 continue;
             }
-            qdict_put(d, fi->feat_names[bit], qnull());
+            qdict_put_null(d, fi->feat_names[bit]);
         }
     }
 
Signature · 1 line
2.14.1.146.gd35faa819
#4 lore raw

PATCH 2/14 qlit: move qlit from check-qjson to qobject/

In reply to Marc-André Lureau

Fix code style issues while at it, to please check-patch.

---
 include/qapi/qmp/qlit.h | 49 +++++++++++++++++++++++++
 qobject/qlit.c          | 89 +++++++++++++++++++++++++++++++++++++++++++++
 tests/check-qjson.c     | 96 +------------------------------------------------
 qobject/Makefile.objs   |  2 +-
 4 files changed, 140 insertions(+), 96 deletions(-)
 create mode 100644 include/qapi/qmp/qlit.h
 create mode 100644 qobject/qlit.c
include/qapi/qmp/qlit.h new file +49 -0
new file mode 100644
index 0000000000..4e2e760ef1
@@ -0,0 +1,49 @@
+/*
+ * Copyright IBM, Corp. 2009
+ * Copyright (c) 2013, 2015, 2017 Red Hat Inc.
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *  Markus Armbruster <[email protected]>
+ *  Marc-André Lureau <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+#ifndef QLIT_H_
+#define QLIT_H_
+
+#include "qapi-types.h"
+#include "qobject.h"
+
+typedef struct LiteralQDictEntry LiteralQDictEntry;
+typedef struct LiteralQObject LiteralQObject;
+
+struct LiteralQObject {
+    int type;
+    union {
+        int64_t qnum;
+        const char *qstr;
+        LiteralQDictEntry *qdict;
+        LiteralQObject *qlist;
+    } value;
+};
+
+struct LiteralQDictEntry {
+    const char *key;
+    LiteralQObject value;
+};
+
+#define QLIT_QNUM(val) \
+    (LiteralQObject){.type = QTYPE_QNUM, .value.qnum = (val)}
+#define QLIT_QSTR(val) \
+    (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
+#define QLIT_QDICT(val) \
+    (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
+#define QLIT_QLIST(val) \
+    (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}
+
+int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs);
+
+#endif /* QLIT_H_ */
qobject/qlit.c new file +89 -0

89 lines not on this page · raw message

tests/check-qjson.c +1 -95

108 lines not on this page · raw message

qobject/Makefile.objs +1 -1
index fc8885c9a4..002d25873a 100644
@@ -1,2 +1,2 @@
-util-obj-y = qnull.o qnum.o qstring.o qdict.o qlist.o qbool.o
+util-obj-y = qnull.o qnum.o qstring.o qdict.o qlist.o qbool.o qlit.o
 util-obj-y += qjson.o qobject.o json-lexer.o json-streamer.o json-parser.o
Signature · 1 line
2.14.1.146.gd35faa819
Reviewed-by on v1 2/14
#5 lore raw

Re: [Qemu-devel] [PATCH 02/14] qlit: move qlit from check-qjson to qobject/

Quoted 1 line · Marc-André Lureau

Fix code style issues while at it, to please check-patch.

It's spelled checkpatch. Can touch up on commit.

Quoted 21 lines
---
 include/qapi/qmp/qlit.h | 49 +++++++++++++++++++++++++
 qobject/qlit.c          | 89 +++++++++++++++++++++++++++++++++++++++++++++
 tests/check-qjson.c     | 96 +------------------------------------------------
 qobject/Makefile.objs   |  2 +-
 4 files changed, 140 insertions(+), 96 deletions(-)
 create mode 100644 include/qapi/qmp/qlit.h
 create mode 100644 qobject/qlit.c
include/qapi/qmp/qlit.h new file +5 -0
new file mode 100644
index 0000000000..4e2e760ef1
@@ -0,0 +1,49 @@
+/*
+ * Copyright IBM, Corp. 2009
+ * Copyright (c) 2013, 2015, 2017 Red Hat Inc.
+ *
+ * Authors:

include/qapi/qmp/qlit.h line 15

Quoted 10 lines
include/qapi/qmp/qlit.h new file +10 -0
+ *  Anthony Liguori   <[email protected]>
+ *  Markus Armbruster <[email protected]>
+ *  Marc-André Lureau <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+#ifndef QLIT_H_
+#define QLIT_H_

QLIT_H (no trailing _), to match the other headers in this directory. Can touch up on commit.

[...]

With the two spelling nits corrected:

#6 lore raw

Re: [Qemu-devel] [PATCH 02/14] qlit: move qlit from check-qjson to qobject/

In reply to Marc-André Lureau
Quoted 53 lines · Marc-André Lureau

Fix code style issues while at it, to please check-patch.

---
 include/qapi/qmp/qlit.h | 49 +++++++++++++++++++++++++
 qobject/qlit.c          | 89 +++++++++++++++++++++++++++++++++++++++++++++
 tests/check-qjson.c     | 96 +------------------------------------------------
 qobject/Makefile.objs   |  2 +-
 4 files changed, 140 insertions(+), 96 deletions(-)
 create mode 100644 include/qapi/qmp/qlit.h
 create mode 100644 qobject/qlit.c
include/qapi/qmp/qlit.h new file +35 -0
new file mode 100644
index 0000000000..4e2e760ef1
@@ -0,0 +1,49 @@
+/*
+ * Copyright IBM, Corp. 2009
+ * Copyright (c) 2013, 2015, 2017 Red Hat Inc.
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *  Markus Armbruster <[email protected]>
+ *  Marc-André Lureau <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+#ifndef QLIT_H_
+#define QLIT_H_
+
+#include "qapi-types.h"
+#include "qobject.h"
+
+typedef struct LiteralQDictEntry LiteralQDictEntry;
+typedef struct LiteralQObject LiteralQObject;
+
+struct LiteralQObject {
+    int type;
+    union {
+        int64_t qnum;
+        const char *qstr;
+        LiteralQDictEntry *qdict;
+        LiteralQObject *qlist;
+    } value;
+};
+
+struct LiteralQDictEntry {
+    const char *key;
+    LiteralQObject value;

include/qapi/qmp/qlit.h line 45

Quoted 10 lines
include/qapi/qmp/qlit.h new file +10 -0
+};
+
+#define QLIT_QNUM(val) \
+    (LiteralQObject){.type = QTYPE_QNUM, .value.qnum = (val)}
+#define QLIT_QSTR(val) \
+    (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
+#define QLIT_QDICT(val) \
+    (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
+#define QLIT_QLIST(val) \
+    (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}

I'm still trying to understand why this exists. Doesn't this provide exactly the same functionality as QObject?

Quoted 1 line

[...]

Signature · 1 line
Eduardo
#7 lore raw

Re: [Qemu-devel] [PATCH 02/14] qlit: move qlit from check-qjson to qobject/

Quoted 59 lines · Eduardo Habkost
Quoted 58 lines · Marc-André Lureau

Fix code style issues while at it, to please check-patch.

---
 include/qapi/qmp/qlit.h | 49 +++++++++++++++++++++++++
 qobject/qlit.c          | 89 +++++++++++++++++++++++++++++++++++++++++++++
 tests/check-qjson.c     | 96 +------------------------------------------------
 qobject/Makefile.objs   |  2 +-
 4 files changed, 140 insertions(+), 96 deletions(-)
 create mode 100644 include/qapi/qmp/qlit.h
 create mode 100644 qobject/qlit.c
include/qapi/qmp/qlit.h new file +40 -0
new file mode 100644
index 0000000000..4e2e760ef1
@@ -0,0 +1,49 @@
+/*
+ * Copyright IBM, Corp. 2009
+ * Copyright (c) 2013, 2015, 2017 Red Hat Inc.
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *  Markus Armbruster <[email protected]>
+ *  Marc-André Lureau <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+#ifndef QLIT_H_
+#define QLIT_H_
+
+#include "qapi-types.h"
+#include "qobject.h"
+
+typedef struct LiteralQDictEntry LiteralQDictEntry;
+typedef struct LiteralQObject LiteralQObject;
+
+struct LiteralQObject {
+    int type;
+    union {
+        int64_t qnum;
+        const char *qstr;
+        LiteralQDictEntry *qdict;
+        LiteralQObject *qlist;
+    } value;
+};
+
+struct LiteralQDictEntry {
+    const char *key;
+    LiteralQObject value;
+};
+
+#define QLIT_QNUM(val) \
+    (LiteralQObject){.type = QTYPE_QNUM, .value.qnum = (val)}
+#define QLIT_QSTR(val) \
Quoted 8 lines

include/qapi/qmp/qlit.h line 45

Quoted 5 lines
include/qapi/qmp/qlit.h new file +5 -0
+    (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
+#define QLIT_QDICT(val) \
+    (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
+#define QLIT_QLIST(val) \
+    (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}

I'm still trying to understand why this exists. Doesn't this provide exactly the same functionality as QObject?

The value-add is initializers. Check out check-qjson.c for examples.

Use cases:

* Specify expected output as data (QLitObject initializer), then use qlit_equal_qobject() to verify actual output matches. Example:
check-qjson.c. I think it compares nicely to the qdict_get morass we use elsewhere.

* Specify a QObject as data (QLitObject initializer), build it with qobject_from_qlit(). Example: PATCH 14. I think it beats specifying the QObject in code (qdict_new(), qdict_put(), ...) at least when the QObject is big.

#8 lore raw

Re: [Qemu-devel] [PATCH 02/14] qlit: move qlit from check-qjson to qobject/

Quoted 70 lines · Markus Armbruster
Quoted 59 lines · Eduardo Habkost
Quoted 58 lines · Marc-André Lureau

Fix code style issues while at it, to please check-patch.

---
 include/qapi/qmp/qlit.h | 49 +++++++++++++++++++++++++
 qobject/qlit.c          | 89 +++++++++++++++++++++++++++++++++++++++++++++
 tests/check-qjson.c     | 96 +------------------------------------------------
 qobject/Makefile.objs   |  2 +-
 4 files changed, 140 insertions(+), 96 deletions(-)
 create mode 100644 include/qapi/qmp/qlit.h
 create mode 100644 qobject/qlit.c
include/qapi/qmp/qlit.h new file +40 -0
new file mode 100644
index 0000000000..4e2e760ef1
@@ -0,0 +1,49 @@
+/*
+ * Copyright IBM, Corp. 2009
+ * Copyright (c) 2013, 2015, 2017 Red Hat Inc.
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *  Markus Armbruster <[email protected]>
+ *  Marc-André Lureau <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+#ifndef QLIT_H_
+#define QLIT_H_
+
+#include "qapi-types.h"
+#include "qobject.h"
+
+typedef struct LiteralQDictEntry LiteralQDictEntry;
+typedef struct LiteralQObject LiteralQObject;
+
+struct LiteralQObject {
+    int type;
+    union {
+        int64_t qnum;
+        const char *qstr;
+        LiteralQDictEntry *qdict;
+        LiteralQObject *qlist;
+    } value;
+};
+
+struct LiteralQDictEntry {
+    const char *key;
+    LiteralQObject value;
+};
+
+#define QLIT_QNUM(val) \
+    (LiteralQObject){.type = QTYPE_QNUM, .value.qnum = (val)}
+#define QLIT_QSTR(val) \
Quoted 8 lines

include/qapi/qmp/qlit.h line 45

Quoted 5 lines
include/qapi/qmp/qlit.h new file +5 -0
+    (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
+#define QLIT_QDICT(val) \
+    (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
+#define QLIT_QLIST(val) \
+    (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}

I'm still trying to understand why this exists. Doesn't this provide exactly the same functionality as QObject?

Quoted 1 line

The value-add is initializers. Check out check-qjson.c for examples.

Oh, I see.

QList and QDict seem to be the ones that require special code for literals. Making initializers for QNull, QNum, QString, and QBool seems possible.

Quoted 7 lines

Use cases:

* Specify expected output as data (QLitObject initializer), then use qlit_equal_qobject() to verify actual output matches. Example:
check-qjson.c. I think it compares nicely to the qdict_get morass we use elsewhere.

Is any qlit_equal_qobject() user so performance-critical that it couldn't be replaced by a QObject/QObject comparison function, and implemented as qobject_equals(qobject_from_qlit(a), b)?

Quoted 5 lines

* Specify a QObject as data (QLitObject initializer), build it with qobject_from_qlit(). Example: PATCH 14. I think it beats specifying the QObject in code (qdict_new(), qdict_put(), ...) at least when the QObject is big.

Signature · 1 line
Eduardo
#9 lore raw

PATCH 3/14 qlit: use QLit prefix consistently

In reply to Marc-André Lureau

Rename from LiteralQ to QLit.

---
 include/qapi/qmp/qlit.h | 24 ++++++++++++------------
 qobject/qlit.c          |  4 ++--
 tests/check-qjson.c     | 40 ++++++++++++++++++++--------------------
 3 files changed, 34 insertions(+), 34 deletions(-)
include/qapi/qmp/qlit.h +12 -12
index 4e2e760ef1..f36bca4554 100644
@@ -17,33 +17,33 @@
 #include "qapi-types.h"
 #include "qobject.h"
 
-typedef struct LiteralQDictEntry LiteralQDictEntry;
-typedef struct LiteralQObject LiteralQObject;
+typedef struct QLitDictEntry QLitDictEntry;
+typedef struct QLitObject QLitObject;
 
-struct LiteralQObject {
+struct QLitObject {
     int type;
     union {
         int64_t qnum;
         const char *qstr;
-        LiteralQDictEntry *qdict;
-        LiteralQObject *qlist;
+        QLitDictEntry *qdict;
+        QLitObject *qlist;
     } value;
 };
 
-struct LiteralQDictEntry {
+struct QLitDictEntry {
     const char *key;
-    LiteralQObject value;
+    QLitObject value;
 };
 
 #define QLIT_QNUM(val) \
-    (LiteralQObject){.type = QTYPE_QNUM, .value.qnum = (val)}
+    (QLitObject){.type = QTYPE_QNUM, .value.qnum = (val)}
 #define QLIT_QSTR(val) \
-    (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
+    (QLitObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
 #define QLIT_QDICT(val) \
-    (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
+    (QLitObject){.type = QTYPE_QDICT, .value.qdict = (val)}
 #define QLIT_QLIST(val) \
-    (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}
+    (QLitObject){.type = QTYPE_QLIST, .value.qlist = (val)}
 
-int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs);
+int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs);
 
 #endif /* QLIT_H_ */
qobject/qlit.c +2 -2
index 5917c3584e..262d64988d 100644
@@ -20,7 +20,7 @@
 
 typedef struct QListCompareHelper {
     int index;
-    LiteralQObject *objs;
+    QLitObject *objs;
     int result;
 } QListCompareHelper;
 
@@ -41,7 +41,7 @@ static void compare_helper(QObject *obj, void *opaque)
         compare_litqobj_to_qobj(&helper->objs[helper->index++], obj);
 }
 
-int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs)
+int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs)
 {
     int64_t val;
 
tests/check-qjson.c +20 -20
index 525f79e60b..82b3681327 100644
@@ -1065,23 +1065,23 @@ static void simple_dict(void)
     int i;
     struct {
         const char *encoded;
-        LiteralQObject decoded;
+        QLitObject decoded;
     } test_cases[] = {
         {
             .encoded = "{\"foo\": 42, \"bar\": \"hello world\"}",
-            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QDICT(((QLitDictEntry[]){
                         { "foo", QLIT_QNUM(42) },
                         { "bar", QLIT_QSTR("hello world") },
                         { }
                     })),
         }, {
             .encoded = "{}",
-            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QDICT(((QLitDictEntry[]){
                         { }
                     })),
         }, {
             .encoded = "{\"foo\": 43}",
-            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QDICT(((QLitDictEntry[]){
                         { "foo", QLIT_QNUM(43) },
                         { }
                     })),
@@ -1163,11 +1163,11 @@ static void simple_list(void)
     int i;
     struct {
         const char *encoded;
-        LiteralQObject decoded;
+        QLitObject decoded;
     } test_cases[] = {
         {
             .encoded = "[43,42]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
                         QLIT_QNUM(42),
                         { }
@@ -1175,21 +1175,21 @@ static void simple_list(void)
         },
         {
             .encoded = "[43]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
                         { }
                     })),
         },
         {
             .encoded = "[]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         { }
                     })),
         },
         {
             .encoded = "[{}]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
-                        QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
+                        QLIT_QDICT(((QLitDictEntry[]){
                                     {},
                                         })),
                         {},
@@ -1220,11 +1220,11 @@ static void simple_whitespace(void)
     int i;
     struct {
         const char *encoded;
-        LiteralQObject decoded;
+        QLitObject decoded;
     } test_cases[] = {
         {
             .encoded = " [ 43 , 42 ]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
                         QLIT_QNUM(42),
                         { }
@@ -1232,12 +1232,12 @@ static void simple_whitespace(void)
         },
         {
             .encoded = " [ 43 , { 'h' : 'b' }, [ ], 42 ]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
-                        QLIT_QDICT(((LiteralQDictEntry[]){
+                        QLIT_QDICT(((QLitDictEntry[]){
                                     { "h", QLIT_QSTR("b") },
                                     { }})),
-                        QLIT_QLIST(((LiteralQObject[]){
+                        QLIT_QLIST(((QLitObject[]){
                                     { }})),
                         QLIT_QNUM(42),
                         { }
@@ -1245,13 +1245,13 @@ static void simple_whitespace(void)
         },
         {
             .encoded = " [ 43 , { 'h' : 'b' , 'a' : 32 }, [ ], 42 ]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
-                        QLIT_QDICT(((LiteralQDictEntry[]){
+                        QLIT_QDICT(((QLitDictEntry[]){
                                     { "h", QLIT_QSTR("b") },
                                     { "a", QLIT_QNUM(32) },
                                     { }})),
-                        QLIT_QLIST(((LiteralQObject[]){
+                        QLIT_QLIST(((QLitObject[]){
                                     { }})),
                         QLIT_QNUM(42),
                         { }
@@ -1282,10 +1282,10 @@ static void simple_varargs(void)
 {
     QObject *embedded_obj;
     QObject *obj;
-    LiteralQObject decoded = QLIT_QLIST(((LiteralQObject[]){
+    QLitObject decoded = QLIT_QLIST(((QLitObject[]){
             QLIT_QNUM(1),
             QLIT_QNUM(2),
-            QLIT_QLIST(((LiteralQObject[]){
+            QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(32),
                         QLIT_QNUM(42),
                         {}})),
Signature · 1 line
2.14.1.146.gd35faa819
#11 lore raw

PATCH 4/14 qlit: remove needless type cast

In reply to Marc-André Lureau

And misc code style fix.

---
 include/qapi/qmp/qlit.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/qapi/qmp/qlit.h +4 -4
index f36bca4554..1e9696988a 100644
@@ -36,13 +36,13 @@ struct QLitDictEntry {
 };
 
 #define QLIT_QNUM(val) \
-    (QLitObject){.type = QTYPE_QNUM, .value.qnum = (val)}
+    { .type = QTYPE_QNUM, .value.qnum = (val) }
 #define QLIT_QSTR(val) \
-    (QLitObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
+    { .type = QTYPE_QSTRING, .value.qstr = (val) }
 #define QLIT_QDICT(val) \
-    (QLitObject){.type = QTYPE_QDICT, .value.qdict = (val)}
+    { .type = QTYPE_QDICT, .value.qdict = (val) }
 #define QLIT_QLIST(val) \
-    (QLitObject){.type = QTYPE_QLIST, .value.qlist = (val)}
+    { .type = QTYPE_QLIST, .value.qlist = (val) }
 
 int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs);
 
Signature · 1 line
2.14.1.146.gd35faa819
#12 lore raw

Re: [Qemu-devel] [PATCH 04/14] qlit: remove needless type cast

Quoted 18 lines · Marc-André Lureau

And misc code style fix.

---
 include/qapi/qmp/qlit.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/qapi/qmp/qlit.h +1 -1
index f36bca4554..1e9696988a 100644
@@ -36,13 +36,13 @@ struct QLitDictEntry {
 };
 
 #define QLIT_QNUM(val) \
-    (QLitObject){.type = QTYPE_QNUM, .value.qnum = (val)}
+    { .type = QTYPE_QNUM, .value.qnum = (val) }
 #define QLIT_QSTR(val) \

include/qapi/qmp/qlit.h struct QLitDictEntry { · line 47

Quoted 10 lines
include/qapi/qmp/qlit.h +3 -3
-    (QLitObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
+    { .type = QTYPE_QSTRING, .value.qstr = (val) }
 #define QLIT_QDICT(val) \
-    (QLitObject){.type = QTYPE_QDICT, .value.qdict = (val)}
+    { .type = QTYPE_QDICT, .value.qdict = (val) }
 #define QLIT_QLIST(val) \
-    (QLitObject){.type = QTYPE_QLIST, .value.qlist = (val)}
+    { .type = QTYPE_QLIST, .value.qlist = (val) }
 
 int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs);

Technically, this isn't a type cast, it's a compound literal. A compound literal is "an unnamed object whose value is given by the initializer list" (C99 §6.5.2.5). The standard then cautions "that this differs from a cast expression" (ibid).

The previous paragraph probably makes sense only to language-lawyers, so let's consider an example.

The macro

   #define QLIT_QSTR(val) \
       (QLitObject){.type = QTYPE_QSTRING, .value.qstr = (val)}

expands into a compound literal of type QLitObject, which can be used as an initializer for a variable of type QLitObject.

The macro

   #define QLIT_QSTR(val) \
       { .type = QTYPE_QSTRING, .value.qstr = (val) }

expands into something that can be used as an initializer for a variable of *any* structure type with suitable members. Duck typing, if you will.

The original author's choice of compound literals is clearly intentional. I'd prefer to respect it unless there's a compelling reason not to.

#13 lore raw

Re: [Qemu-devel] [PATCH 04/14] qlit: remove needless type cast

Hi

Quoted 29 lines · Markus Armbruster
Quoted 27 lines · Marc-André Lureau

And misc code style fix.

---
 include/qapi/qmp/qlit.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/qapi/qmp/qlit.h +4 -4
index f36bca4554..1e9696988a 100644
@@ -36,13 +36,13 @@ struct QLitDictEntry {
 };
 
 #define QLIT_QNUM(val) \
-    (QLitObject){.type = QTYPE_QNUM, .value.qnum = (val)}
+    { .type = QTYPE_QNUM, .value.qnum = (val) }
 #define QLIT_QSTR(val) \
-    (QLitObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
+    { .type = QTYPE_QSTRING, .value.qstr = (val) }
 #define QLIT_QDICT(val) \
-    (QLitObject){.type = QTYPE_QDICT, .value.qdict = (val)}
+    { .type = QTYPE_QDICT, .value.qdict = (val) }
 #define QLIT_QLIST(val) \
-    (QLitObject){.type = QTYPE_QLIST, .value.qlist = (val)}
+    { .type = QTYPE_QLIST, .value.qlist = (val) }
 
Quoted 7 lines

include/qapi/qmp/qlit.h struct QLitDictEntry { · line 47

Quoted 1 line
include/qapi/qmp/qlit.h +0 -0
 int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs);

Technically, this isn't a type cast, it's a compound literal. A compound literal is "an unnamed object whose value is given by the initializer list" (C99 §6.5.2.5). The standard then cautions "that this differs from a cast expression" (ibid).

You are right, but actually the patch helps with another issue if we keep the compound type:

const QLitObject qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
            QLIT_QNULL,
            {}
        }));

qmp-introspect.c:17:63: error: initializer element is not constant

 const QLitObject qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
                                                               ^
/home/elmarco/src/qemu/include/qapi/qmp/qlit.h:50:57: note: in definition
of macro ‘QLIT_QLIST’
     (QLitObject) { .type = QTYPE_QLIST, .value.qlist = (val) }
                                                         ^~~
qmp-introspect.c:17:63: note: (near initialization for ‘(anonymous).value’)
 const QLitObject qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
                                                               ^
/home/elmarco/src/qemu/include/qapi/qmp/qlit.h:50:57: note: in definition
of macro ‘QLIT_QLIST’
     (QLitObject) { .type = QTYPE_QLIST, .value.qlist = (val) }
                                                         ^~~

Any idea how to fix that?

Quoted 21 lines

The previous paragraph probably makes sense only to language-lawyers, so let's consider an example.

The macro

   #define QLIT_QSTR(val) \
       (QLitObject){.type = QTYPE_QSTRING, .value.qstr = (val)}

expands into a compound literal of type QLitObject, which can be used as an initializer for a variable of type QLitObject.

The macro

   #define QLIT_QSTR(val) \
       { .type = QTYPE_QSTRING, .value.qstr = (val) }

expands into something that can be used as an initializer for a variable of *any* structure type with suitable members. Duck typing, if you will.

Quoted 5 lines

The original author's choice of compound literals is clearly intentional. I'd prefer to respect it unless there's a compelling reason not to.

--

Marc-André Lureau

#14 lore raw

Re: [Qemu-devel] [PATCH 04/14] qlit: remove needless type cast

Quoted 62 lines · Marc-André Lureau

Hi

Quoted 29 lines · Markus Armbruster
Quoted 27 lines · Marc-André Lureau

And misc code style fix.

---
 include/qapi/qmp/qlit.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/qapi/qmp/qlit.h +4 -4
index f36bca4554..1e9696988a 100644
@@ -36,13 +36,13 @@ struct QLitDictEntry {
 };
 
 #define QLIT_QNUM(val) \
-    (QLitObject){.type = QTYPE_QNUM, .value.qnum = (val)}
+    { .type = QTYPE_QNUM, .value.qnum = (val) }
 #define QLIT_QSTR(val) \
-    (QLitObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
+    { .type = QTYPE_QSTRING, .value.qstr = (val) }
 #define QLIT_QDICT(val) \
-    (QLitObject){.type = QTYPE_QDICT, .value.qdict = (val)}
+    { .type = QTYPE_QDICT, .value.qdict = (val) }
 #define QLIT_QLIST(val) \
-    (QLitObject){.type = QTYPE_QLIST, .value.qlist = (val)}
+    { .type = QTYPE_QLIST, .value.qlist = (val) }
 
Quoted 6 lines

include/qapi/qmp/qlit.h struct QLitDictEntry { · line 47

Quoted 1 line
include/qapi/qmp/qlit.h +0 -0
 int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs);

Technically, this isn't a type cast, it's a compound literal. A compound literal is "an unnamed object whose value is given by the initializer list" (C99 §6.5.2.5). The standard then cautions "that this differs from a cast expression" (ibid).

You are right, but actually the patch helps with another issue if we keep the compound type:

const QLitObject qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
            QLIT_QNULL,
            {}
        }));

qmp-introspect.c:17:63: error: initializer element is not constant

 const QLitObject qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
                                                               ^
/home/elmarco/src/qemu/include/qapi/qmp/qlit.h:50:57: note: in definition
of macro ‘QLIT_QLIST’
     (QLitObject) { .type = QTYPE_QLIST, .value.qlist = (val) }
                                                         ^~~
qmp-introspect.c:17:63: note: (near initialization for ‘(anonymous).value’)
 const QLitObject qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
                                                               ^
/home/elmarco/src/qemu/include/qapi/qmp/qlit.h:50:57: note: in definition
of macro ‘QLIT_QLIST’
     (QLitObject) { .type = QTYPE_QLIST, .value.qlist = (val) }
                                                         ^~~
Quoted 1 line

Any idea how to fix that?

This one:

[...]

Quoted 3 lines
Quoted 3 lines

The original author's choice of compound literals is clearly intentional. I'd prefer to respect it unless there's a compelling reason not to.

Rewrite your commit message so it explains your *actual* reason for converting the macro from compound literal to initializer.

#15 lore raw

PATCH 5/14 qlit: rename compare_litqobj_to_qobj

In reply to Marc-André Lureau

Use qlit_ prefix.

---
 include/qapi/qmp/qlit.h |  2 +-
 qobject/qlit.c          |  6 +++---
 tests/check-qjson.c     | 14 +++++++-------
 3 files changed, 11 insertions(+), 11 deletions(-)
include/qapi/qmp/qlit.h +1 -1
index 1e9696988a..e299e8fab0 100644
@@ -44,6 +44,6 @@ struct QLitDictEntry {
 #define QLIT_QLIST(val) \
     { .type = QTYPE_QLIST, .value.qlist = (val) }
 
-int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs);
+int qlit_equal_qobject(QLitObject *lhs, QObject *rhs);
 
 #endif /* QLIT_H_ */
qobject/qlit.c +3 -3
index 262d64988d..0c4101898d 100644
@@ -38,10 +38,10 @@ static void compare_helper(QObject *obj, void *opaque)
     }
 
     helper->result =
-        compare_litqobj_to_qobj(&helper->objs[helper->index++], obj);
+        qlit_equal_qobject(&helper->objs[helper->index++], obj);
 }
 
-int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs)
+int qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
 {
     int64_t val;
 
@@ -63,7 +63,7 @@ int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs)
             QObject *obj = qdict_get(qobject_to_qdict(rhs),
                                      lhs->value.qdict[i].key);
 
-            if (!compare_litqobj_to_qobj(&lhs->value.qdict[i].value, obj)) {
+            if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
                 return 0;
             }
         }
tests/check-qjson.c +7 -7
index 82b3681327..e5ca273cbc 100644
@@ -1094,13 +1094,13 @@ static void simple_dict(void)
         QString *str;
 
         obj = qobject_from_json(test_cases[i].encoded, &error_abort);
-        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
 
         str = qobject_to_json(obj);
         qobject_decref(obj);
 
         obj = qobject_from_json(qstring_get_str(str), &error_abort);
-        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
         qobject_decref(obj);
         QDECREF(str);
     }
@@ -1203,13 +1203,13 @@ static void simple_list(void)
         QString *str;
 
         obj = qobject_from_json(test_cases[i].encoded, &error_abort);
-        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
 
         str = qobject_to_json(obj);
         qobject_decref(obj);
 
         obj = qobject_from_json(qstring_get_str(str), &error_abort);
-        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
         qobject_decref(obj);
         QDECREF(str);
     }
@@ -1265,13 +1265,13 @@ static void simple_whitespace(void)
         QString *str;
 
         obj = qobject_from_json(test_cases[i].encoded, &error_abort);
-        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
 
         str = qobject_to_json(obj);
         qobject_decref(obj);
 
         obj = qobject_from_json(qstring_get_str(str), &error_abort);
-        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
 
         qobject_decref(obj);
         QDECREF(str);
@@ -1295,7 +1295,7 @@ static void simple_varargs(void)
     g_assert(embedded_obj != NULL);
 
     obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
-    g_assert(compare_litqobj_to_qobj(&decoded, obj) == 1);
+    g_assert(qlit_equal_qobject(&decoded, obj) == 1);
 
     qobject_decref(obj);
 }
Signature · 1 line
2.14.1.146.gd35faa819
Reviewed-by on v1 5/14
#16 lore raw

Re: [Qemu-devel] [PATCH 05/14] qlit: rename compare_litqobj_to_qobj

Quoted 1 line · Marc-André Lureau

Use qlit_ prefix.

Let's spell it out:

qlit: rename compare_litqobj_to_qobj() to qlit_equal_qobject()

By the way, not only is your name shorter, it's also more precise:
"compare" suggests -1, 0, +1 for less than, equal and greater than, equal suggests non-zero for equal, zero for unequal.

Quoted 12 lines
---
 include/qapi/qmp/qlit.h |  2 +-
 qobject/qlit.c          |  6 +++---
 tests/check-qjson.c     | 14 +++++++-------
 3 files changed, 11 insertions(+), 11 deletions(-)
include/qapi/qmp/qlit.h +0 -0
index 1e9696988a..e299e8fab0 100644

include/qapi/qmp/qlit.h struct QLitDictEntry { · line 49

Quoted 7 lines
include/qapi/qmp/qlit.h +1 -1
@@ -44,6 +44,6 @@ struct QLitDictEntry {
 #define QLIT_QLIST(val) \
     { .type = QTYPE_QLIST, .value.qlist = (val) }
 
-int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs);
+int qlit_equal_qobject(QLitObject *lhs, QObject *rhs);
 
 #endif /* QLIT_H_ */

Let's use this opportunity to change the return value to bool.

With those changes:

#17 lore raw

Re: [Qemu-devel] [PATCH 05/14] qlit: rename compare_litqobj_to_qobj

Quoted 27 lines · Markus Armbruster
Quoted 1 line · Marc-André Lureau

Use qlit_ prefix.

Let's spell it out:

qlit: rename compare_litqobj_to_qobj() to qlit_equal_qobject()

By the way, not only is your name shorter, it's also more precise:
"compare" suggests -1, 0, +1 for less than, equal and greater than, equal suggests non-zero for equal, zero for unequal.

Quoted 15 lines
---
 include/qapi/qmp/qlit.h |  2 +-
 qobject/qlit.c          |  6 +++---
 tests/check-qjson.c     | 14 +++++++-------
 3 files changed, 11 insertions(+), 11 deletions(-)
include/qapi/qmp/qlit.h +0 -0
index 1e9696988a..e299e8fab0 100644
@@ -44,6 +44,6 @@ struct QLitDictEntry {
 #define QLIT_QLIST(val) \
     { .type = QTYPE_QLIST, .value.qlist = (val) }
 
Quoted 6 lines

include/qapi/qmp/qlit.h struct QLitDictEntry { · line 49

Quoted 4 lines
include/qapi/qmp/qlit.h +1 -1
-int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs);
+int qlit_equal_qobject(QLitObject *lhs, QObject *rhs);
 
 #endif /* QLIT_H_ */

Let's use this opportunity to change the return value to bool.

Maybe I should peek at the next patch once in a while. Nevermind!

Quoted 1 line

With those changes:

Scratch "those changes", just touch up the commit message a bit:

qlit: rename compare_litqobj_to_qobj() to qlit_equal_qobject()

compare_litqobj_to_qobj() lacks a qlit_ prefix. Moreover, "compare" suggests -1, 0, +1 for less than, equal and greater than. The function actually returns non-zero for equal, zero for unequal. Rename to qlit_equal_qobject().

Its return type will be cleaned up in the next patch.

Can do on commit.

Quoted 1 line
#18 lore raw

PATCH 6/14 qlit: make qlit_equal_qobject return a bool

In reply to Marc-André Lureau

Make it more obvious about the expected return values.

---
 include/qapi/qmp/qlit.h |  2 +-
 qobject/qlit.c          | 18 +++++++++---------
 tests/check-qjson.c     | 14 +++++++-------
 3 files changed, 17 insertions(+), 17 deletions(-)
include/qapi/qmp/qlit.h +1 -1
index e299e8fab0..2e22d0f73c 100644
@@ -44,6 +44,6 @@ struct QLitDictEntry {
 #define QLIT_QLIST(val) \
     { .type = QTYPE_QLIST, .value.qlist = (val) }
 
-int qlit_equal_qobject(QLitObject *lhs, QObject *rhs);
+bool qlit_equal_qobject(QLitObject *lhs, QObject *rhs);
 
 #endif /* QLIT_H_ */
qobject/qlit.c +9 -9
index 0c4101898d..a2975bef3f 100644
@@ -21,19 +21,19 @@
 typedef struct QListCompareHelper {
     int index;
     QLitObject *objs;
-    int result;
+    bool result;
 } QListCompareHelper;
 
 static void compare_helper(QObject *obj, void *opaque)
 {
     QListCompareHelper *helper = opaque;
 
-    if (helper->result == 0) {
+    if (!helper->result) {
         return;
     }
 
     if (helper->objs[helper->index].type == QTYPE_NONE) {
-        helper->result = 0;
+        helper->result = false;
         return;
     }
 
@@ -41,12 +41,12 @@ static void compare_helper(QObject *obj, void *opaque)
         qlit_equal_qobject(&helper->objs[helper->index++], obj);
 }
 
-int qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
+bool qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
 {
     int64_t val;
 
     if (!rhs || lhs->type != qobject_type(rhs)) {
-        return 0;
+        return false;
     }
 
     switch (lhs->type) {
@@ -64,18 +64,18 @@ int qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
                                      lhs->value.qdict[i].key);
 
             if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
-                return 0;
+                return false;
             }
         }
 
-        return 1;
+        return true;
     }
     case QTYPE_QLIST: {
         QListCompareHelper helper;
 
         helper.index = 0;
         helper.objs = lhs->value.qlist;
-        helper.result = 1;
+        helper.result = true;
 
         qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
 
@@ -85,5 +85,5 @@ int qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
         break;
     }
 
-    return 0;
+    return false;
 }
tests/check-qjson.c +7 -7
index e5ca273cbc..59227934ce 100644
@@ -1094,13 +1094,13 @@ static void simple_dict(void)
         QString *str;
 
         obj = qobject_from_json(test_cases[i].encoded, &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
 
         str = qobject_to_json(obj);
         qobject_decref(obj);
 
         obj = qobject_from_json(qstring_get_str(str), &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
         qobject_decref(obj);
         QDECREF(str);
     }
@@ -1203,13 +1203,13 @@ static void simple_list(void)
         QString *str;
 
         obj = qobject_from_json(test_cases[i].encoded, &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
 
         str = qobject_to_json(obj);
         qobject_decref(obj);
 
         obj = qobject_from_json(qstring_get_str(str), &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
         qobject_decref(obj);
         QDECREF(str);
     }
@@ -1265,13 +1265,13 @@ static void simple_whitespace(void)
         QString *str;
 
         obj = qobject_from_json(test_cases[i].encoded, &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
 
         str = qobject_to_json(obj);
         qobject_decref(obj);
 
         obj = qobject_from_json(qstring_get_str(str), &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
 
         qobject_decref(obj);
         QDECREF(str);
@@ -1295,7 +1295,7 @@ static void simple_varargs(void)
     g_assert(embedded_obj != NULL);
 
     obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
-    g_assert(qlit_equal_qobject(&decoded, obj) == 1);
+    g_assert(qlit_equal_qobject(&decoded, obj));
 
     qobject_decref(obj);
 }
Signature · 1 line
2.14.1.146.gd35faa819
Reviewed-by on v1 6/14
#19 lore raw

Re: [Qemu-devel] [PATCH 06/14] qlit: make qlit_equal_qobject return a bool

Quoted 75 lines · Marc-André Lureau

Make it more obvious about the expected return values.

---
 include/qapi/qmp/qlit.h |  2 +-
 qobject/qlit.c          | 18 +++++++++---------
 tests/check-qjson.c     | 14 +++++++-------
 3 files changed, 17 insertions(+), 17 deletions(-)
include/qapi/qmp/qlit.h +1 -1
index e299e8fab0..2e22d0f73c 100644
@@ -44,6 +44,6 @@ struct QLitDictEntry {
 #define QLIT_QLIST(val) \
     { .type = QTYPE_QLIST, .value.qlist = (val) }
 
-int qlit_equal_qobject(QLitObject *lhs, QObject *rhs);
+bool qlit_equal_qobject(QLitObject *lhs, QObject *rhs);
 
 #endif /* QLIT_H_ */
qobject/qlit.c +7 -7
index 0c4101898d..a2975bef3f 100644
@@ -21,19 +21,19 @@
 typedef struct QListCompareHelper {
     int index;
     QLitObject *objs;
-    int result;
+    bool result;
 } QListCompareHelper;
 
 static void compare_helper(QObject *obj, void *opaque)
 {
     QListCompareHelper *helper = opaque;
 
-    if (helper->result == 0) {
+    if (!helper->result) {
         return;
     }
 
     if (helper->objs[helper->index].type == QTYPE_NONE) {
-        helper->result = 0;
+        helper->result = false;
         return;
     }
 
@@ -41,12 +41,12 @@ static void compare_helper(QObject *obj, void *opaque)
         qlit_equal_qobject(&helper->objs[helper->index++], obj);
 }
 
-int qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
+bool qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
 {
     int64_t val;
 
     if (!rhs || lhs->type != qobject_type(rhs)) {
-        return 0;
+        return false;
     }
 
     switch (lhs->type) {
@@ -64,18 +64,18 @@ int qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
                                      lhs->value.qdict[i].key);
 
             if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
-                return 0;
+                return false;
             }
         }
 
-        return 1;
+        return true;
     }

qobject/qlit.c int qlit_equal_qobject(QLitObject *lhs, QObject *rhs) · line 81

Quoted 10 lines
qobject/qlit.c +1 -1
     case QTYPE_QLIST: {
         QListCompareHelper helper;
 
         helper.index = 0;
         helper.objs = lhs->value.qlist;
-        helper.result = 1;
+        helper.result = true;
 
         qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
 

Let's use QLIST_FOREACH_ENTRY() rather than qlist_iter() with awkward callback machinery. Could be done before this patch (might reduce churn) or after. Even as a follow-up patch.

Speaking of additional cleanup: qlit_equal_dict() clones its QDict argument for no good reason. Drop the clone and compare qdict_size(dict) == i instead. Same result except when you got duplicates in your QLitObject, but that's a silly programming error I wouldn't bother to cope with or catch.

Quoted 60 lines
qobject/qlit.c +1 -1
@@ -85,5 +85,5 @@ int qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
         break;
     }
 
-    return 0;
+    return false;
 }
tests/check-qjson.c +6 -6
index e5ca273cbc..59227934ce 100644
@@ -1094,13 +1094,13 @@ static void simple_dict(void)
         QString *str;
 
         obj = qobject_from_json(test_cases[i].encoded, &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
 
         str = qobject_to_json(obj);
         qobject_decref(obj);
 
         obj = qobject_from_json(qstring_get_str(str), &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
         qobject_decref(obj);
         QDECREF(str);
     }
@@ -1203,13 +1203,13 @@ static void simple_list(void)
         QString *str;
 
         obj = qobject_from_json(test_cases[i].encoded, &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
 
         str = qobject_to_json(obj);
         qobject_decref(obj);
 
         obj = qobject_from_json(qstring_get_str(str), &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
         qobject_decref(obj);
         QDECREF(str);
     }
@@ -1265,13 +1265,13 @@ static void simple_whitespace(void)
         QString *str;
 
         obj = qobject_from_json(test_cases[i].encoded, &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
 
         str = qobject_to_json(obj);
         qobject_decref(obj);
 
         obj = qobject_from_json(qstring_get_str(str), &error_abort);
-        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj) == 1);
+        g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
 
         qobject_decref(obj);
         QDECREF(str);

tests/check-qjson.c static void simple_varargs(void) · line 1301

Quoted 8 lines
tests/check-qjson.c +1 -1
@@ -1295,7 +1295,7 @@ static void simple_varargs(void)
     g_assert(embedded_obj != NULL);
 
     obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
-    g_assert(qlit_equal_qobject(&decoded, obj) == 1);
+    g_assert(qlit_equal_qobject(&decoded, obj));
 
     qobject_decref(obj);
 }

I encourage you to pursue the additional cleanups I outlined above.

#20 lore raw

PATCH 7/14 qlit: make qlit_equal_qobject() take const arguments

In reply to Marc-André Lureau
---
 include/qapi/qmp/qlit.h | 2 +-
 qobject/qlit.c          | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/qapi/qmp/qlit.h +1 -1
index 2e22d0f73c..8882016199 100644
@@ -44,6 +44,6 @@ struct QLitDictEntry {
 #define QLIT_QLIST(val) \
     { .type = QTYPE_QLIST, .value.qlist = (val) }
 
-bool qlit_equal_qobject(QLitObject *lhs, QObject *rhs);
+bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs);
 
 #endif /* QLIT_H_ */
qobject/qlit.c +1 -1
index a2975bef3f..ae2787ef35 100644
@@ -41,7 +41,7 @@ static void compare_helper(QObject *obj, void *opaque)
         qlit_equal_qobject(&helper->objs[helper->index++], obj);
 }
 
-bool qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
+bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
 {
     int64_t val;
 
Signature · 1 line
2.14.1.146.gd35faa819
Reviewed-by on v1 7/14
#21 lore raw

Re: [Qemu-devel] [PATCH 07/14] qlit: make qlit_equal_qobject() take const arguments

Quoted 23 lines · Marc-André Lureau
---
 include/qapi/qmp/qlit.h | 2 +-
 qobject/qlit.c          | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/qapi/qmp/qlit.h +1 -1
index 2e22d0f73c..8882016199 100644
@@ -44,6 +44,6 @@ struct QLitDictEntry {
 #define QLIT_QLIST(val) \
     { .type = QTYPE_QLIST, .value.qlist = (val) }
 
-bool qlit_equal_qobject(QLitObject *lhs, QObject *rhs);
+bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs);
 
 #endif /* QLIT_H_ */
qobject/qlit.c +0 -0
index a2975bef3f..ae2787ef35 100644

qobject/qlit.c static void compare_helper(QObject *obj, void *opaque) · line 46

Quoted 7 lines
qobject/qlit.c +1 -1
@@ -41,7 +41,7 @@ static void compare_helper(QObject *obj, void *opaque)
         qlit_equal_qobject(&helper->objs[helper->index++], obj);
 }
 
-bool qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
+bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
 {
     int64_t val;

Could be squashed into the previous patch, but since you got the two split, let's leave them split and save us the trouble of wording the squash's commit message.

#22 lore raw

PATCH 8/14 qlit: add QLIT_QNULL and QLIT_BOOL

In reply to Marc-André Lureau

As they are going to be used in the following patches.

---
 include/qapi/qmp/qlit.h | 5 +++++
 qobject/qlit.c          | 4 ++++
 2 files changed, 9 insertions(+)
include/qapi/qmp/qlit.h +5 -0
index 8882016199..7118fc9e32 100644
@@ -23,6 +23,7 @@ typedef struct QLitObject QLitObject;
 struct QLitObject {
     int type;
     union {
+        bool qbool;
         int64_t qnum;
         const char *qstr;
         QLitDictEntry *qdict;
@@ -35,6 +36,10 @@ struct QLitDictEntry {
     QLitObject value;
 };
 
+#define QLIT_QNULL \
+    { .type = QTYPE_QNULL }
+#define QLIT_QBOOL(val) \
+    { .type = QTYPE_QBOOL, .value.qbool = (val) }
 #define QLIT_QNUM(val) \
     { .type = QTYPE_QNUM, .value.qnum = (val) }
 #define QLIT_QSTR(val) \
qobject/qlit.c +4 -0
index ae2787ef35..07ad6b05e8 100644
@@ -50,6 +50,8 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
     }
 
     switch (lhs->type) {
+    case QTYPE_QBOOL:
+        return lhs->value.qbool == qbool_get_bool(qobject_to_qbool(rhs));
     case QTYPE_QNUM:
         g_assert(qnum_get_try_int(qobject_to_qnum(rhs), &val));
         return lhs->value.qnum == val;
@@ -81,6 +83,8 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
 
         return helper.result;
     }
+    case QTYPE_QNULL:
+        return true;
     default:
         break;
     }
Signature · 1 line
2.14.1.146.gd35faa819
#24 lore raw

PATCH 9/14 qlit: replace assert(qnum_get_try_int)

In reply to Marc-André Lureau

qnum_get_int() will assert if underlying type isn't compatible.

---
 qobject/qlit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qobject/qlit.c +1 -1
index 07ad6b05e8..7e4bf92862 100644
@@ -53,7 +53,7 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
     case QTYPE_QBOOL:
         return lhs->value.qbool == qbool_get_bool(qobject_to_qbool(rhs));
     case QTYPE_QNUM:
-        g_assert(qnum_get_try_int(qobject_to_qnum(rhs), &val));
+        val = qnum_get_int(qobject_to_qnum(rhs));
         return lhs->value.qnum == val;
     case QTYPE_QSTRING:
         return (strcmp(lhs->value.qstr,
Signature · 1 line
2.14.1.146.gd35faa819
Reviewed-by on v1 9/14
#25 lore raw

Re: [Qemu-devel] [PATCH 09/14] qlit: replace assert(qnum_get_try_int)

Quoted 12 lines · Marc-André Lureau

qnum_get_int() will assert if underlying type isn't compatible.

---
 qobject/qlit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qobject/qlit.c +0 -0
index 07ad6b05e8..7e4bf92862 100644

qobject/qlit.c bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs) · line 59

Quoted 8 lines
qobject/qlit.c +1 -1
@@ -53,7 +53,7 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
     case QTYPE_QBOOL:
         return lhs->value.qbool == qbool_get_bool(qobject_to_qbool(rhs));
     case QTYPE_QNUM:
-        g_assert(qnum_get_try_int(qobject_to_qnum(rhs), &val));
+        val = qnum_get_int(qobject_to_qnum(rhs));
         return lhs->value.qnum == val;
     case QTYPE_QSTRING:
         return (strcmp(lhs->value.qstr,

Suggest commit message

qlit: Replace open-coded qnum_get_int() by call

Bonus: rids us of a side effect in an assertion.

Can do on commit.

#26 lore raw

PATCH 10/14 tests: add qlit tests

In reply to Marc-André Lureau
---
 tests/check-qlit.c     | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/Makefile.include |  5 +++-
 2 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 tests/check-qlit.c
tests/check-qlit.c new file +64 -0
new file mode 100644
index 0000000000..47fde92a46
@@ -0,0 +1,64 @@
+/*
+ * QLit unit-tests.
+ *
+ * Copyright (C) 2017 Red Hat Inc.
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+
+#include "qapi/qmp/qbool.h"
+#include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qlit.h"
+#include "qapi/qmp/qnum.h"
+#include "qapi/qmp/qstring.h"
+
+static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) {
+    { "foo", QLIT_QNUM(42) },
+    { "bar", QLIT_QSTR("hello world") },
+    { "baz", QLIT_QNULL },
+    { "bee", QLIT_QLIST(((QLitObject[]) {
+        QLIT_QNUM(43),
+        QLIT_QNUM(44),
+        QLIT_QBOOL(true),
+        { },
+    }))},
+    { },
+}));
+
+static QObject *make_qobject(void)
+{
+    QDict *qdict = qdict_new();
+    QList *list = qlist_new();
+
+    qdict_put_int(qdict, "foo", 42);
+    qdict_put_str(qdict, "bar", "hello world");
+    qdict_put_null(qdict, "baz");
+
+    qlist_append_int(list, 43);
+    qlist_append_int(list, 44);
+    qlist_append_bool(list, true);
+    qdict_put(qdict, "bee", list);
+
+    return QOBJECT(qdict);
+}
+
+static void qlit_equal_qobject_test(void)
+{
+    QObject *qobj = make_qobject();
+
+    g_assert(qlit_equal_qobject(&qlit, qobj));
+
+    qobject_decref(qobj);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/qlit/equal_qobject", qlit_equal_qobject_test);
+
+    return g_test_run();
+}
tests/Makefile.include +4 -1
index 37c1bed683..3653c7b40a 100644
@@ -43,6 +43,8 @@ check-unit-y += tests/check-qnull$(EXESUF)
 gcov-files-check-qnull-y = qobject/qnull.c
 check-unit-y += tests/check-qjson$(EXESUF)
 gcov-files-check-qjson-y = qobject/qjson.c
+check-unit-y += tests/check-qlit$(EXESUF)
+gcov-files-check-qlit-y = qobject/qlit.c
 check-unit-y += tests/test-qobject-output-visitor$(EXESUF)
 gcov-files-test-qobject-output-visitor-y = qapi/qobject-output-visitor.c
 check-unit-y += tests/test-clone-visitor$(EXESUF)
@@ -535,7 +537,7 @@ GENERATED_FILES += tests/test-qapi-types.h tests/test-qapi-visit.h \
 
 test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \
 	tests/check-qlist.o tests/check-qnull.o \
-	tests/check-qjson.o \
+	tests/check-qjson.o tests/check-qlit.o \
 	tests/test-coroutine.o tests/test-string-output-visitor.o \
 	tests/test-string-input-visitor.o tests/test-qobject-output-visitor.o \
 	tests/test-clone-visitor.o \
@@ -569,6 +571,7 @@ tests/check-qdict$(EXESUF): tests/check-qdict.o $(test-util-obj-y)
 tests/check-qlist$(EXESUF): tests/check-qlist.o $(test-util-obj-y)
 tests/check-qnull$(EXESUF): tests/check-qnull.o $(test-util-obj-y)
 tests/check-qjson$(EXESUF): tests/check-qjson.o $(test-util-obj-y)
+tests/check-qlit$(EXESUF): tests/check-qlit.o $(test-util-obj-y)
 tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y)
 tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y)
 
Signature · 1 line
2.14.1.146.gd35faa819
#27 lore raw

PATCH 11/14 qlit: improve QLit dict vs qdict comparison

In reply to Marc-André Lureau

Fail if the QLit dict doesn't match exactly the object dict.

To do so, create a copy of the original qdict, and remove the checked elements. Verify that the dict is empty by the end of the comparison.

---
 qobject/qlit.c     | 42 ++++++++++++++++++++++++++++--------------
 tests/check-qlit.c |  7 +++++++
 2 files changed, 35 insertions(+), 14 deletions(-)
qobject/qlit.c +28 -14
index 7e4bf92862..45ae0635c0 100644
@@ -41,6 +41,32 @@ static void compare_helper(QObject *obj, void *opaque)
         qlit_equal_qobject(&helper->objs[helper->index++], obj);
 }
 
+static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict)
+{
+    QDict *dict = qdict_clone_shallow(qdict);
+    bool success = false;
+    int i;
+
+    for (i = 0; lhs->value.qdict[i].key; i++) {
+        QObject *obj = qdict_get(dict, lhs->value.qdict[i].key);
+
+        if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
+            goto end;
+        }
+        qdict_del(dict, lhs->value.qdict[i].key);
+    }
+
+    if (qdict_size(dict) != 0) {
+        goto end;
+    }
+
+    success = true;
+
+end:
+    QDECREF(dict);
+    return success;
+}
+
 bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
 {
     int64_t val;
@@ -58,20 +84,8 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
     case QTYPE_QSTRING:
         return (strcmp(lhs->value.qstr,
                        qstring_get_str(qobject_to_qstring(rhs))) == 0);
-    case QTYPE_QDICT: {
-        int i;
-
-        for (i = 0; lhs->value.qdict[i].key; i++) {
-            QObject *obj = qdict_get(qobject_to_qdict(rhs),
-                                     lhs->value.qdict[i].key);
-
-            if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
-                return false;
-            }
-        }
-
-        return true;
-    }
+    case QTYPE_QDICT:
+        return qlit_equal_qdict(lhs, qobject_to_qdict(rhs));
     case QTYPE_QLIST: {
         QListCompareHelper helper;
 
tests/check-qlit.c +7 -0
index 47fde92a46..5d9558dfd9 100644
@@ -28,6 +28,11 @@ static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) {
     { },
 }));
 
+static QLitObject qlit_foo = QLIT_QDICT(((QLitDictEntry[]) {
+    { "foo", QLIT_QNUM(42) },
+    { },
+}));
+
 static QObject *make_qobject(void)
 {
     QDict *qdict = qdict_new();
@@ -51,6 +56,8 @@ static void qlit_equal_qobject_test(void)
 
     g_assert(qlit_equal_qobject(&qlit, qobj));
 
+    g_assert(!qlit_equal_qobject(&qlit_foo, qobj));
+
     qobject_decref(qobj);
 }
 
Signature · 1 line
2.14.1.146.gd35faa819
#28 lore raw

PATCH 12/14 qlit: improve QLit list vs qlist comparison

In reply to Marc-André Lureau

Check that the QLit list has the same size as the qlist, this should ensure that we have an exact match when iterating over qlist for comparing the elements.

---
 qobject/qlit.c     | 39 ++++++++++++++++++++++++++-------------
 tests/check-qlit.c |  3 +++
 2 files changed, 29 insertions(+), 13 deletions(-)
qobject/qlit.c +26 -13
index 45ae0635c0..0a60802d74 100644
@@ -67,6 +67,28 @@ end:
     return success;
 }
 
+static bool qlit_equal_qlist(const QLitObject *lhs, const QList *qlist)
+{
+    QListCompareHelper helper;
+    int i;
+
+    for (i = 0; lhs->value.qlist[i].type != QTYPE_NONE; i++) {
+        continue;
+    }
+
+    if (qlist_size(qlist) != i) {
+        return false;
+    }
+
+    helper.index = 0;
+    helper.objs = lhs->value.qlist;
+    helper.result = true;
+
+    qlist_iter(qlist, compare_helper, &helper);
+
+    return helper.result;
+}
+
 bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
 {
     int64_t val;
@@ -82,21 +104,12 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
         val = qnum_get_int(qobject_to_qnum(rhs));
         return lhs->value.qnum == val;
     case QTYPE_QSTRING:
-        return (strcmp(lhs->value.qstr,
-                       qstring_get_str(qobject_to_qstring(rhs))) == 0);
+        return g_str_equal(lhs->value.qstr,
+                           qstring_get_str(qobject_to_qstring(rhs)));
     case QTYPE_QDICT:
         return qlit_equal_qdict(lhs, qobject_to_qdict(rhs));
-    case QTYPE_QLIST: {
-        QListCompareHelper helper;
-
-        helper.index = 0;
-        helper.objs = lhs->value.qlist;
-        helper.result = true;
-
-        qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
-
-        return helper.result;
-    }
+    case QTYPE_QLIST:
+        return qlit_equal_qlist(lhs, qobject_to_qlist(rhs));
     case QTYPE_QNULL:
         return true;
     default:
tests/check-qlit.c +3 -0
index 5d9558dfd9..ae74bfcb83 100644
@@ -58,6 +58,9 @@ static void qlit_equal_qobject_test(void)
 
     g_assert(!qlit_equal_qobject(&qlit_foo, qobj));
 
+    qdict_put(qobject_to_qdict(qobj), "bee", qlist_new());
+    g_assert(!qlit_equal_qobject(&qlit, qobj));
+
     qobject_decref(qobj);
 }
 
Signature · 1 line
2.14.1.146.gd35faa819
#29 lore raw

Re: [Qemu-devel] [PATCH 12/14] qlit: improve QLit list vs qlist comparison

----- Original Message -----

Quoted 44 lines

Check that the QLit list has the same size as the qlist, this should ensure that we have an exact match when iterating over qlist for comparing the elements.

---
 qobject/qlit.c     | 39 ++++++++++++++++++++++++++-------------
 tests/check-qlit.c |  3 +++
 2 files changed, 29 insertions(+), 13 deletions(-)
qobject/qlit.c +22 -0
index 45ae0635c0..0a60802d74 100644
@@ -67,6 +67,28 @@ end:
     return success;
 }
 
+static bool qlit_equal_qlist(const QLitObject *lhs, const QList *qlist)
+{
+    QListCompareHelper helper;
+    int i;
+
+    for (i = 0; lhs->value.qlist[i].type != QTYPE_NONE; i++) {
+        continue;
+    }
+
+    if (qlist_size(qlist) != i) {
+        return false;
+    }
+
+    helper.index = 0;
+    helper.objs = lhs->value.qlist;
+    helper.result = true;
+
+    qlist_iter(qlist, compare_helper, &helper);
+
+    return helper.result;
+}
+
 bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
 {
     int64_t val;

qobject/qlit.c bool qlit_equal_qobject(const QLitObject *lhs, const · line 109

Quoted 8 lines
qobject/qlit.c +2 -2
@@ -82,21 +104,12 @@ bool qlit_equal_qobject(const QLitObject *lhs, const
 QObject *rhs)
         val = qnum_get_int(qobject_to_qnum(rhs));
         return lhs->value.qnum == val;
     case QTYPE_QSTRING:
-        return (strcmp(lhs->value.qstr,
-                       qstring_get_str(qobject_to_qstring(rhs))) == 0);
+        return g_str_equal(lhs->value.qstr,
+                           qstring_get_str(qobject_to_qstring(rhs)));

drop this hunk, it slipped in by mistake.

Quoted 36 lines
qobject/qlit.c +2 -11
     case QTYPE_QDICT:
         return qlit_equal_qdict(lhs, qobject_to_qdict(rhs));
-    case QTYPE_QLIST: {
-        QListCompareHelper helper;
-
-        helper.index = 0;
-        helper.objs = lhs->value.qlist;
-        helper.result = true;
-
-        qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
-
-        return helper.result;
-    }
+    case QTYPE_QLIST:
+        return qlit_equal_qlist(lhs, qobject_to_qlist(rhs));
     case QTYPE_QNULL:
         return true;

default:

tests/check-qlit.c +3 -1
index 5d9558dfd9..ae74bfcb83 100644
@@ -58,6 +58,9 @@ static void qlit_equal_qobject_test(void)
 
     g_assert(!qlit_equal_qobject(&qlit_foo, qobj));
 
+    qdict_put(qobject_to_qdict(qobj), "bee", qlist_new());
+    g_assert(!qlit_equal_qobject(&qlit, qobj));
+
     qobject_decref(qobj);
 }
 
--

2.14.1.146.gd35faa819

#30 lore raw

PATCH 13/14 qlit: add qobject_form_qlit()

In reply to Marc-André Lureau
---
 include/qapi/qmp/qlit.h |  2 ++
 qobject/qlit.c          | 37 +++++++++++++++++++++++++++++++++++++
 tests/check-qlit.c      | 26 ++++++++++++++++++++++++++
 3 files changed, 65 insertions(+)
include/qapi/qmp/qlit.h +2 -0
index 7118fc9e32..9b6e06d02f 100644
@@ -51,4 +51,6 @@ struct QLitDictEntry {
 
 bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs);
 
+QObject *qobject_from_qlit(const QLitObject *qlit);
+
 #endif /* QLIT_H_ */
qobject/qlit.c +37 -0
index 0a60802d74..4adc2033a9 100644
@@ -118,3 +118,40 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
 
     return false;
 }
+
+QObject *qobject_from_qlit(const QLitObject *qlit)
+{
+    int i;
+
+    switch (qlit->type) {
+    case QTYPE_QNULL:
+        return QOBJECT(qnull());
+    case QTYPE_QNUM:
+        return QOBJECT(qnum_from_int(qlit->value.qnum));
+    case QTYPE_QSTRING:
+        return QOBJECT(qstring_from_str(qlit->value.qstr));
+    case QTYPE_QDICT: {
+        QDict *qdict = qdict_new();
+        for (i = 0; qlit->value.qdict[i].value.type != QTYPE_NONE; i++) {
+            QLitDictEntry *e = &qlit->value.qdict[i];
+
+            qdict_put_obj(qdict, e->key, qobject_from_qlit(&e->value));
+        }
+        return QOBJECT(qdict);
+    }
+    case QTYPE_QLIST: {
+        QList *qlist = qlist_new();
+
+        for (i = 0; qlit->value.qlist[i].type != QTYPE_NONE; i++) {
+            qlist_append_obj(qlist, qobject_from_qlit(&qlit->value.qlist[i]));
+        }
+        return QOBJECT(qlist);
+    }
+    case QTYPE_QBOOL:
+        return QOBJECT(qbool_from_bool(qlit->value.qbool));
+    case QTYPE_NONE:
+        assert(0);
+    }
+
+    return NULL;
+}
tests/check-qlit.c +26 -0
index ae74bfcb83..135a399c6d 100644
@@ -64,11 +64,37 @@ static void qlit_equal_qobject_test(void)
     qobject_decref(qobj);
 }
 
+static void qobject_from_qlit_test(void)
+{
+    QObject *obj, *qobj = qobject_from_qlit(&qlit);
+    QDict *qdict;
+    QList *bee;
+
+    qdict = qobject_to_qdict(qobj);
+    g_assert_cmpint(qdict_get_int(qdict, "foo"), ==, 42);
+    g_assert_cmpstr(qdict_get_str(qdict, "bar"), ==, "hello world");
+    g_assert(qobject_type(qdict_get(qdict, "baz")) == QTYPE_QNULL);
+
+    bee = qdict_get_qlist(qdict, "bee");
+    obj = qlist_pop(bee);
+    g_assert_cmpint(qnum_get_int(qobject_to_qnum(obj)), ==, 43);
+    qobject_decref(obj);
+    obj = qlist_pop(bee);
+    g_assert_cmpint(qnum_get_int(qobject_to_qnum(obj)), ==, 44);
+    qobject_decref(obj);
+    obj = qlist_pop(bee);
+    g_assert(qbool_get_bool(qobject_to_qbool(obj)));
+    qobject_decref(obj);
+
+    qobject_decref(qobj);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
 
     g_test_add_func("/qlit/equal_qobject", qlit_equal_qobject_test);
+    g_test_add_func("/qlit/qobject_from_qlit", qobject_from_qlit_test);
 
     return g_test_run();
 }
Signature · 1 line
2.14.1.146.gd35faa819
#31 lore raw

PATCH 14/14 qapi: generate a literal qobject for introspection

In reply to Marc-André Lureau

Replace the generated json string with a literal qobject. The later is easier to deal with, at run time as well as compile time: adding #if conditionals will be easier than in a json string.

---
 scripts/qapi-introspect.py         | 87 ++++++++++++++++++++++----------------
 monitor.c                          |  2 +-
 tests/test-qobject-input-visitor.c | 11 +++--
 docs/devel/qapi-code-gen.txt       | 29 ++++++++-----
 4 files changed, 78 insertions(+), 51 deletions(-)
scripts/qapi-introspect.py +51 -36
index 032bcea491..a157a5f91c 100644
@@ -12,31 +12,41 @@
 from qapi import *
 
 
-# Caveman's json.dumps() replacement (we're stuck at Python 2.4)
-# TODO try to use json.dumps() once we get unstuck
-def to_json(obj, level=0):
+def to_qlit(obj, level=0, suppress_first_indent=False):
+
+    def indent(level):
+        return level * 4 * ' '
+
+    ret = ''
+    if not suppress_first_indent:
+        ret += indent(level)
     if obj is None:
-        ret = 'null'
+        ret += 'QLIT_QNULL'
     elif isinstance(obj, str):
-        ret = '"' + obj.replace('"', r'\"') + '"'
+        ret += 'QLIT_QSTR(' + to_c_string(obj) + ')'
     elif isinstance(obj, list):
-        elts = [to_json(elt, level + 1)
+        elts = [to_qlit(elt, level + 1)
                 for elt in obj]
-        ret = '[' + ', '.join(elts) + ']'
+        elts.append(indent(level + 1) + "{}")
+        ret += 'QLIT_QLIST(((QLitObject[]) {\n'
+        ret += ',\n'.join(elts) + '\n'
+        ret += indent(level) + '}))'
     elif isinstance(obj, dict):
-        elts = ['"%s": %s' % (key.replace('"', r'\"'),
-                              to_json(obj[key], level + 1))
-                for key in sorted(obj.keys())]
-        ret = '{' + ', '.join(elts) + '}'
+        elts = []
+        for key, value in sorted(obj.iteritems()):
+            elts.append(indent(level + 1) + '{ %s, %s }' %
+                        (to_c_string(key), to_qlit(value, level + 1, False)))
+        elts.append(indent(level + 1) + '{}')
+        ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
+        ret += ',\n'.join(elts) + '\n'
+        ret += indent(level) + '}))'
     else:
         assert False                # not implemented
-    if level == 1:
-        ret = '\n' + ret
     return ret
 
 
-def to_c_string(string):
-    return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
+def to_c_string(s):
+    return '"' + s.replace('\\', r'\\').replace('"', r'\"') + '"'
 
 
 class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
@@ -45,39 +55,37 @@ class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
         self.defn = None
         self.decl = None
         self._schema = None
-        self._jsons = None
+        self._qlits = None
         self._used_types = None
         self._name_map = None
 
     def visit_begin(self, schema):
         self._schema = schema
-        self._jsons = []
+        self._qlits = []
         self._used_types = []
         self._name_map = {}
 
     def visit_end(self):
         # visit the types that are actually used
-        jsons = self._jsons
-        self._jsons = []
+        qlits = self._qlits
+        self._qlits = []
         for typ in self._used_types:
             typ.visit(self)
         # generate C
         # TODO can generate awfully long lines
-        jsons.extend(self._jsons)
-        name = c_name(prefix, protect=False) + 'qmp_schema_json'
+        qlits.extend(self._qlits)
+        name = c_name(prefix, protect=False) + 'qmp_schema_qlit'
         self.decl = mcgen('''
-extern const char %(c_name)s[];
+extern const QLitObject %(c_name)s;
 ''',
                           c_name=c_name(name))
-        lines = to_json(jsons).split('\n')
-        c_string = '\n    '.join([to_c_string(line) for line in lines])
         self.defn = mcgen('''
-const char %(c_name)s[] = %(c_string)s;
+const QLitObject %(c_name)s = %(c_string)s;
 ''',
                           c_name=c_name(name),
-                          c_string=c_string)
+                          c_string=to_qlit(qlits))
         self._schema = None
-        self._jsons = None
+        self._qlits = None
         self._used_types = None
         self._name_map = None
 
@@ -111,12 +119,12 @@ const char %(c_name)s[] = %(c_string)s;
             return '[' + self._use_type(typ.element_type) + ']'
         return self._name(typ.name)
 
-    def _gen_json(self, name, mtype, obj):
+    def _gen_qlit(self, name, mtype, obj):
         if mtype not in ('command', 'event', 'builtin', 'array'):
             name = self._name(name)
         obj['name'] = name
         obj['meta-type'] = mtype
-        self._jsons.append(obj)
+        self._qlits.append(obj)
 
     def _gen_member(self, member):
         ret = {'name': member.name, 'type': self._use_type(member.type)}
@@ -132,24 +140,24 @@ const char %(c_name)s[] = %(c_string)s;
         return {'case': variant.name, 'type': self._use_type(variant.type)}
 
     def visit_builtin_type(self, name, info, json_type):
-        self._gen_json(name, 'builtin', {'json-type': json_type})
+        self._gen_qlit(name, 'builtin', {'json-type': json_type})
 
     def visit_enum_type(self, name, info, values, prefix):
-        self._gen_json(name, 'enum', {'values': values})
+        self._gen_qlit(name, 'enum', {'values': values})
 
     def visit_array_type(self, name, info, element_type):
         element = self._use_type(element_type)
-        self._gen_json('[' + element + ']', 'array', {'element-type': element})
+        self._gen_qlit('[' + element + ']', 'array', {'element-type': element})
 
     def visit_object_type_flat(self, name, info, members, variants):
         obj = {'members': [self._gen_member(m) for m in members]}
         if variants:
             obj.update(self._gen_variants(variants.tag_member.name,
                                           variants.variants))
-        self._gen_json(name, 'object', obj)
+        self._gen_qlit(name, 'object', obj)
 
     def visit_alternate_type(self, name, info, variants):
-        self._gen_json(name, 'alternate',
+        self._gen_qlit(name, 'alternate',
                        {'members': [{'type': self._use_type(m.type)}
                                     for m in variants.variants]})
 
@@ -157,13 +165,13 @@ const char %(c_name)s[] = %(c_string)s;
                       gen, success_response, boxed):
         arg_type = arg_type or self._schema.the_empty_object_type
         ret_type = ret_type or self._schema.the_empty_object_type
-        self._gen_json(name, 'command',
+        self._gen_qlit(name, 'command',
                        {'arg-type': self._use_type(arg_type),
                         'ret-type': self._use_type(ret_type)})
 
     def visit_event(self, name, info, arg_type, boxed):
         arg_type = arg_type or self._schema.the_empty_object_type
-        self._gen_json(name, 'event', {'arg-type': self._use_type(arg_type)})
+        self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)})
 
 # Debugging aid: unmask QAPI schema's type names
 # We normally mask them, because they're not QMP wire ABI
@@ -205,11 +213,18 @@ h_comment = '''
 
 fdef.write(mcgen('''
 #include "qemu/osdep.h"
+#include "qapi/qmp/qlit.h"
 #include "%(prefix)sqmp-introspect.h"
 
 ''',
                  prefix=prefix))
 
+fdecl.write(mcgen('''
+#include "qemu/osdep.h"
+#include "qapi/qmp/qlit.h"
+
+'''))
+
 schema = QAPISchema(input_file)
 gen = QAPISchemaGenIntrospectVisitor(opt_unmask)
 schema.visit(gen)
monitor.c +1 -1
index e0f880107f..14c27d4b6f 100644
@@ -953,7 +953,7 @@ EventInfoList *qmp_query_events(Error **errp)
 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
                                  Error **errp)
 {
-    *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
+    *ret_data = qobject_from_qlit(&qmp_schema_qlit);
 }
 
 /*
tests/test-qobject-input-visitor.c +7 -4
index bcf02617dc..bdd00f6bd8 100644
@@ -1247,24 +1247,27 @@ static void test_visitor_in_fail_alternate(TestInputVisitorData *data,
 }
 
 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
-                                              const char *schema_json)
+                                              const QLitObject *qlit)
 {
     SchemaInfoList *schema = NULL;
+    QObject *obj = qobject_from_qlit(qlit);
     Visitor *v;
 
-    v = visitor_input_test_init_raw(data, schema_json);
+    v = qobject_input_visitor_new(obj);
 
     visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
     g_assert(schema);
 
     qapi_free_SchemaInfoList(schema);
+    qobject_decref(obj);
+    visit_free(v);
 }
 
 static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
                                            const void *unused)
 {
-    do_test_visitor_in_qmp_introspect(data, test_qmp_schema_json);
-    do_test_visitor_in_qmp_introspect(data, qmp_schema_json);
+    do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);
+    do_test_visitor_in_qmp_introspect(data, &qmp_schema_qlit);
 }
 
 int main(int argc, char **argv)
docs/devel/qapi-code-gen.txt +19 -10
index 9903ac4c19..b653e86bff 100644
@@ -1295,18 +1295,27 @@ Example:
     #ifndef EXAMPLE_QMP_INTROSPECT_H
     #define EXAMPLE_QMP_INTROSPECT_H
 
-    extern const char example_qmp_schema_json[];
+    extern const QLitObject qmp_schema_qlit;
 
     #endif
     $ cat qapi-generated/example-qmp-introspect.c
 [Uninteresting stuff omitted...]
 
-    const char example_qmp_schema_json[] = "["
-        "{\"arg-type\": \"0\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, "
-        "{\"arg-type\": \"1\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"2\"}, "
-        "{\"members\": [], \"meta-type\": \"object\", \"name\": \"0\"}, "
-        "{\"members\": [{\"name\": \"arg1\", \"type\": \"[2]\"}], \"meta-type\": \"object\", \"name\": \"1\"}, "
-        "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"default\": null, \"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"2\"}, "
-        "{\"element-type\": \"2\", \"meta-type\": \"array\", \"name\": \"[2]\"}, "
-        "{\"json-type\": \"int\", \"meta-type\": \"builtin\", \"name\": \"int\"}, "
-        "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}]";
+    const QLitObject example_qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
+        QLIT_QDICT(((QLitDictEntry[]) {
+            { "arg-type", QLIT_QSTR("0") },
+            { "meta-type", QLIT_QSTR("event") },
+            { "name", QLIT_QSTR("Event") },
+            { }
+        })),
+        QLIT_QDICT(((QLitDictEntry[]) {
+            { "members", QLIT_QLIST(((QLitObject[]) {
+                { }
+            })) },
+            { "meta-type", QLIT_QSTR("object") },
+            { "name", QLIT_QSTR("0") },
+            { }
+        })),
+        ...
+        { }
+    }));
Signature · 1 line
2.14.1.146.gd35faa819