Skip to contentSkip to conversation

qdev deconstruction, command-line episode

PATCH In review Paolo Bonzini 3 participants 16 messages Feb 9, 2012 – Feb 22, 2012 Kbuild

lore
Share & tools
lore
#0 lore raw

cover qdev deconstruction, command-line episode

This patch substitutes more qdev code with shared infrastructure. The code is now ripe enough that we can attack command-line parsing.

Parsing to a string is replaced with a StringInputVisitor (there is also a StringOutputVisitor, but it's not used yet). This lets us remove most integer parsing/printing code; command-line parsing accesses the integer properties with object_property_parse rather than object_property_set_int, and that's enough in almost all cases.

Similar "type-casting visitors" could be created also for integer and boolean and could replace the use of QObject in qom/object.c.

In addition, the limited polymorphism allowed by visitors is exploited for PCI devfn properties. These will always read as an integer (the 8-bit DDDDDFFF), but they can be set both as strings and integers.

While there are still a few legacy getters for "info qtree", the only remaining legacy setters are for hex properties. But even here we can lay down the foundations for future simplification. In general, they are rarely used and their printed form is more interesting than the parsing. For example you'd usually set isa-serial.index instead of isa-serial.iobase. Plus (luckily) our main client, libvirt, only cares about few of these, and always sets them with a 0x prefix. So the series stops accepting bare hexadecimal numbers, preparing for making legacy properties read-only in 1.3 or so.

Patches 1 to 4 refactor some QAPI code and introduce the string visitors. Patches 5 uses them in new generic property accessors. Patches 6 to 9 put the shiny new accessors to use in qdev.

Paolo Bonzini (9):
qapi: allow sharing enum implementation across visitors
qapi: drop qmp_input_end_optional
qapi: add string-based visitors
qapi: add tests for string-based visitors
qom: add generic string parsing/printing
qdev: accept both strings and integers for PCI addresses
qdev: accept hex properties only if prefixed by 0x
qdev: use built-in QOM string parser
qdev: drop unnecessary parse/print methods

 .gitignore                   |    2 +
 Makefile.objs                |    5 +-
 hw/qdev-properties.c         |  186 +++++++++---------------------------------
 include/qemu/object.h        |   24 ++++++
 qapi/qapi-visit-core.c       |   52 ++++++++++++
 qapi/qapi-visit-impl.h       |   23 +++++
 qapi/qmp-input-visitor.c     |   39 +--------
 qapi/qmp-output-visitor.c    |   22 +-----
 qapi/string-input-visitor.c  |  136 ++++++++++++++++++++++++++++++
 qapi/string-input-visitor.h  |   25 ++++++
 qapi/string-output-visitor.c |   89 ++++++++++++++++++++
 qapi/string-output-visitor.h |   26 ++++++
 qom/object.c                 |   24 ++++++
 test-string-input-visitor.c  |  160 +++++++++++++++++++++++++++++++++++
 test-string-output-visitor.c |  188 ++++++++++++++++++++++++++++++++++++++++++
 tests/Makefile               |   12 ++-
 16 files changed, 805 insertions(+), 208 deletions(-)
 create mode 100644 qapi/qapi-visit-impl.h
 create mode 100644 qapi/string-input-visitor.c
 create mode 100644 qapi/string-input-visitor.h
 create mode 100644 qapi/string-output-visitor.c
 create mode 100644 qapi/string-output-visitor.h
 create mode 100644 test-string-input-visitor.c
 create mode 100644 test-string-output-visitor.c
Signature · 1 line
1.7.7.6
#1 lore raw

PATCH 1/9 qapi: allow sharing enum implementation across visitors

Most visitors will use the same code for enum parsing. Move it to the core.

---
 qapi/qapi-visit-core.c    |   51 +++++++++++++++++++++++++++++++++++++++++++++
 qapi/qapi-visit-impl.h    |   23 ++++++++++++++++++++
 qapi/qmp-input-visitor.c  |   34 +----------------------------
 qapi/qmp-output-visitor.c |   22 +-----------------
 4 files changed, 78 insertions(+), 52 deletions(-)
 create mode 100644 qapi/qapi-visit-impl.h
qapi/qapi-visit-core.c +51 -0
index ddef3ed..a4e088c 100644
@@ -12,6 +12,7 @@
  */
 
 #include "qapi/qapi-visit-core.h"
+#include "qapi/qapi-visit-impl.h"
 
 void visit_start_handle(Visitor *v, void **obj, const char *kind,
                         const char *name, Error **errp)
@@ -116,3 +117,53 @@ void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
         v->type_number(v, obj, name, errp);
     }
 }
+
+void output_type_enum(Visitor *v, int *obj, const char *strings[],
+                      const char *kind, const char *name,
+                      Error **errp)
+{
+    int i = 0;
+    int value = *obj;
+    char *enum_str;
+
+    assert(strings);
+    while (strings[i++] != NULL);
+    if (value < 0 || value >= i - 1) {
+        error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
+        return;
+    }
+
+    enum_str = (char *)strings[value];
+    visit_type_str(v, &enum_str, name, errp);
+}
+
+void input_type_enum(Visitor *v, int *obj, const char *strings[],
+                     const char *kind, const char *name,
+                     Error **errp)
+{
+    int64_t value = 0;
+    char *enum_str;
+
+    assert(strings);
+
+    visit_type_str(v, &enum_str, name, errp);
+    if (error_is_set(errp)) {
+        return;
+    }
+
+    while (strings[value] != NULL) {
+        if (strcmp(strings[value], enum_str) == 0) {
+            break;
+        }
+        value++;
+    }
+
+    if (strings[value] == NULL) {
+        error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
+        g_free(enum_str);
+        return;
+    }
+
+    g_free(enum_str);
+    *obj = value;
+}
qapi/qapi-visit-impl.h new file +23 -0
new file mode 100644
index 0000000..0f3a189
@@ -0,0 +1,23 @@
+/*
+ * Core Definitions for QAPI Visitor implementations
+ *
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * Author: Paolo Bonizni <[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 QAPI_VISITOR_IMPL_H
+#define QAPI_VISITOR_IMPL_H
+
+#include "qapi/qapi-types-core.h"
+#include "qapi/qapi-visit-core.h"
+
+void input_type_enum(Visitor *v, int *obj, const char *strings[],
+                     const char *kind, const char *name, Error **errp);
+void output_type_enum(Visitor *v, int *obj, const char *strings[],
+                      const char *kind, const char *name, Error **errp);
+
+#endif
qapi/qmp-input-visitor.c +2 -32
index c78022b..012cd0a 100644
@@ -12,6 +12,7 @@
  */
 
 #include "qmp-input-visitor.h"
+#include "qapi/qapi-visit-impl.h"
 #include "qemu-queue.h"
 #include "qemu-common.h"
 #include "qemu-objects.h"
@@ -217,37 +218,6 @@ static void qmp_input_type_number(Visitor *v, double *obj, const char *name,
     *obj = qfloat_get_double(qobject_to_qfloat(qobj));
 }
 
-static void qmp_input_type_enum(Visitor *v, int *obj, const char *strings[],
-                                const char *kind, const char *name,
-                                Error **errp)
-{
-    int64_t value = 0;
-    char *enum_str;
-
-    assert(strings);
-
-    qmp_input_type_str(v, &enum_str, name, errp);
-    if (error_is_set(errp)) {
-        return;
-    }
-
-    while (strings[value] != NULL) {
-        if (strcmp(strings[value], enum_str) == 0) {
-            break;
-        }
-        value++;
-    }
-
-    if (strings[value] == NULL) {
-        error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
-        g_free(enum_str);
-        return;
-    }
-
-    g_free(enum_str);
-    *obj = value;
-}
-
 static void qmp_input_start_optional(Visitor *v, bool *present,
                                      const char *name, Error **errp)
 {
@@ -288,7 +258,7 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
     v->visitor.start_list = qmp_input_start_list;
     v->visitor.next_list = qmp_input_next_list;
     v->visitor.end_list = qmp_input_end_list;
-    v->visitor.type_enum = qmp_input_type_enum;
+    v->visitor.type_enum = input_type_enum;
     v->visitor.type_int = qmp_input_type_int;
     v->visitor.type_bool = qmp_input_type_bool;
     v->visitor.type_str = qmp_input_type_str;
qapi/qmp-output-visitor.c +2 -20
index f76d015..e0697b0 100644
@@ -12,6 +12,7 @@
  */
 
 #include "qmp-output-visitor.h"
+#include "qapi/qapi-visit-impl.h"
 #include "qemu-queue.h"
 #include "qemu-common.h"
 #include "qemu-objects.h"
@@ -180,25 +181,6 @@ static void qmp_output_type_number(Visitor *v, double *obj, const char *name,
     qmp_output_add(qov, name, qfloat_from_double(*obj));
 }
 
-static void qmp_output_type_enum(Visitor *v, int *obj, const char *strings[],
-                                 const char *kind, const char *name,
-                                 Error **errp)
-{
-    int i = 0;
-    int value = *obj;
-    char *enum_str;
-
-    assert(strings);
-    while (strings[i++] != NULL);
-    if (value < 0 || value >= i - 1) {
-        error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
-        return;
-    }
-
-    enum_str = (char *)strings[value];
-    qmp_output_type_str(v, &enum_str, name, errp);
-}
-
 QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
 {
     QObject *obj = qmp_output_first(qov);
@@ -239,7 +221,7 @@ QmpOutputVisitor *qmp_output_visitor_new(void)
     v->visitor.start_list = qmp_output_start_list;
     v->visitor.next_list = qmp_output_next_list;
     v->visitor.end_list = qmp_output_end_list;
-    v->visitor.type_enum = qmp_output_type_enum;
+    v->visitor.type_enum = output_type_enum;
     v->visitor.type_int = qmp_output_type_int;
     v->visitor.type_bool = qmp_output_type_bool;
     v->visitor.type_str = qmp_output_type_str;
Signature · 1 line
1.7.7.6
Reviewed-by on v1 1/9
#2 lore raw

Re: [Qemu-devel] [PATCH 1/9] qapi: allow sharing enum implementation across visitors

Am 09.02.2012 15:31, schrieb Paolo Bonzini:

Quoted 4 lines

Most visitors will use the same code for enum parsing. Move it to the core.

However...

Quoted 16 lines
qapi/qapi-visit-core.c +7 -0
index ddef3ed..a4e088c 100644
@@ -116,3 +117,53 @@ void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
         v->type_number(v, obj, name, errp);
     }
 }
+
+void output_type_enum(Visitor *v, int *obj, const char *strings[],
+                      const char *kind, const char *name,
+                      Error **errp)
+{
+    int i = 0;
+    int value = *obj;

qapi/qapi-visit-core.c void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp) · line 136

Quoted 10 lines
qapi/qapi-visit-core.c +10 -0
+    char *enum_str;
+
+    assert(strings);
+    while (strings[i++] != NULL);
+    if (value < 0 || value >= i - 1) {
+        error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
+        return;
+    }
+
+    enum_str = (char *)strings[value];

This does not take into account non-linear enum values.

Maybe name it output_type_linear_enum to allow for alternative enum lookup implementations? (e.g., hashtable or list of name,value tuples)

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#3 lore raw

Re: [Qemu-devel] [PATCH 1/9] qapi: allow sharing enum implementation across visitors

Quoted 18 lines · Andreas Färber
Quoted 8 lines
Quoted 8 lines
qapi/qapi-visit-core.c +8 -0
+void output_type_enum(Visitor *v, int *obj, const char *strings[],
+                      const char *kind, const char *name,
+                      Error **errp)
+{
+    int i = 0;
+    int value = *obj;
+    char *enum_str;
+
Quoted 8 lines

qapi/qapi-visit-core.c void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp) · line 136

Quoted 8 lines
qapi/qapi-visit-core.c +8 -0
+    assert(strings);
+    while (strings[i++] != NULL);
+    if (value < 0 || value >= i - 1) {
+        error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
+        return;
+    }
+
+    enum_str = (char *)strings[value];

This does not take into account non-linear enum values.

Quoted 2 lines

Maybe name it output_type_linear_enum to allow for alternative enum lookup implementations? (e.g., hashtable or list of name,value tuples)

I would say that this is the common case and the other one should be named output_type_sparse_enum or something like that, if the need arises...

Paolo

#4 lore raw

PATCH 2/9 qapi: drop qmp_input_end_optional

In reply to Paolo Bonzini

This method is optional, do not implement it if it is empty.

---
 qapi/qmp-input-visitor.c |    5 -----
 1 files changed, 0 insertions(+), 5 deletions(-)
qapi/qmp-input-visitor.c +0 -5
index 012cd0a..e6b6152 100644
@@ -232,10 +232,6 @@ static void qmp_input_start_optional(Visitor *v, bool *present,
     *present = true;
 }
 
-static void qmp_input_end_optional(Visitor *v, Error **errp)
-{
-}
-
 Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
 {
     return &v->visitor;
@@ -264,7 +260,6 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
     v->visitor.type_str = qmp_input_type_str;
     v->visitor.type_number = qmp_input_type_number;
     v->visitor.start_optional = qmp_input_start_optional;
-    v->visitor.end_optional = qmp_input_end_optional;
 
     v->obj = obj;
     qobject_incref(v->obj);
Signature · 1 line
1.7.7.6
#5 lore raw

PATCH 3/9 qapi: add string-based visitors

In reply to Paolo Bonzini

String based visitors provide a consistent interface for parsing strings to C values, as well as consuming C values as strings. They will be used to parse command-line options.

---
 Makefile.objs                |    5 +-
 qapi/string-input-visitor.c  |  136 ++++++++++++++++++++++++++++++++++++++++++
 qapi/string-input-visitor.h  |   25 ++++++++
 qapi/string-output-visitor.c |   89 ++++++++++++++++++++++++++++
 qapi/string-output-visitor.h |   26 ++++++++
 6 files changed, 280 insertions(+), 2 deletions(-)
 create mode 100644 qapi/string-input-visitor.c
 create mode 100644 qapi/string-input-visitor.h
 create mode 100644 qapi/string-output-visitor.c
 create mode 100644 qapi/string-output-visitor.h
Makefile.objs +3 -2
index ec35320..8f9e1fb 100644
@@ -414,8 +414,9 @@ libcacard-y = cac.o event.o vcard.o vreader.o vcard_emul_nss.o vcard_emul_type.o
 ######################################################################
 # qapi
 
-qapi-nested-y = qapi-visit-core.o qmp-input-visitor.o qmp-output-visitor.o qapi-dealloc-visitor.o
-qapi-nested-y += qmp-registry.o qmp-dispatch.o
+qapi-nested-y = qapi-visit-core.o qapi-dealloc-visitor.o qmp-input-visitor.o
+qapi-nested-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o
+qapi-nested-y += string-input-visitor.o string-output-visitor.o
 qapi-obj-y = $(addprefix qapi/, $(qapi-nested-y))
 
 common-obj-y += qmp-marshal.o qapi-visit.o qapi-types.o $(qapi-obj-y)
qapi/string-input-visitor.c new file +136 -0

136 lines not on this page · raw message

qapi/string-input-visitor.h new file +25 -0
new file mode 100644
index 0000000..d269d42
@@ -0,0 +1,25 @@
+/*
+ * String parsing Visitor
+ *
+ * Copyright Red Hat, Inc. 2012
+ *
+ * Author: Paolo Bonzini <[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 STRING_INPUT_VISITOR_H
+#define STRING_INPUT_VISITOR_H
+
+#include "qapi-visit-core.h"
+
+typedef struct StringInputVisitor StringInputVisitor;
+
+StringInputVisitor *string_input_visitor_new(const char *str);
+void string_input_visitor_cleanup(StringInputVisitor *v);
+
+Visitor *string_input_get_visitor(StringInputVisitor *v);
+
+#endif
qapi/string-output-visitor.c new file +89 -0

89 lines not on this page · raw message

qapi/string-output-visitor.h new file +26 -0
new file mode 100644
index 0000000..05b3b29
@@ -0,0 +1,26 @@
+/*
+ * String printing Visitor
+ *
+ * Copyright Red Hat, Inc. 2012
+ *
+ * Author: Paolo Bonzini <[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 STRING_OUTPUT_VISITOR_H
+#define STRING_OUTPUT_VISITOR_H
+
+#include "qapi-visit-core.h"
+
+typedef struct StringOutputVisitor StringOutputVisitor;
+
+StringOutputVisitor *string_output_visitor_new(void);
+void string_output_visitor_cleanup(StringOutputVisitor *v);
+
+char *string_output_get_string(StringOutputVisitor *v);
+Visitor *string_output_get_visitor(StringOutputVisitor *v);
+
+#endif
Signature · 1 line
1.7.7.6
#6 lore raw

PATCH 4/9 qapi: add tests for string-based visitors

In reply to Paolo Bonzini
---
 .gitignore                   |    2 +
 test-string-input-visitor.c  |  160 +++++++++++++++++++++++++++++++++++
 test-string-output-visitor.c |  188 ++++++++++++++++++++++++++++++++++++++++++
 tests/Makefile               |   12 ++-
 4 files changed, 360 insertions(+), 2 deletions(-)
 create mode 100644 test-string-input-visitor.c
 create mode 100644 test-string-output-visitor.c
.gitignore +2 -0
index f5aab2c..c72955a 100644
@@ -44,6 +44,8 @@ QMP/qmp-commands.txt
 test-coroutine
 test-qmp-input-visitor
 test-qmp-output-visitor
+test-string-input-visitor
+test-string-output-visitor
 fsdev/virtfs-proxy-helper.1
 fsdev/virtfs-proxy-helper.pod
 .gdbinit
test-string-input-visitor.c new file +160 -0

160 lines not on this page · raw message

test-string-output-visitor.c new file +188 -0

188 lines not on this page · raw message

tests/Makefile +10 -2
index 55e8eb0..74b29dc 100644
@@ -1,6 +1,6 @@
 CHECKS = check-qdict check-qfloat check-qint check-qstring check-qlist
 CHECKS += check-qjson test-qmp-output-visitor test-qmp-input-visitor
-CHECKS += test-coroutine
+CHECKS += test-string-input-visitor test-string-output-visitor test-coroutine
 
 check-qint.o check-qstring.o check-qdict.o check-qlist.o check-qfloat.o check-qjson.o test-coroutine.o: $(GENERATED_HEADERS)
 
@@ -12,7 +12,9 @@ check-qfloat: check-qfloat.o qfloat.o $(tools-obj-y)
 check-qjson: check-qjson.o $(qobject-obj-y) $(tools-obj-y)
 test-coroutine: test-coroutine.o qemu-timer-common.o async.o $(coroutine-obj-y) $(tools-obj-y)
 
-test-qmp-input-visitor.o test-qmp-output-visitor.o test-qmp-commands.o qemu-ga$(EXESUF): QEMU_CFLAGS += -I $(qapi-dir)
+test-qmp-input-visitor.o test-qmp-output-visitor.o \
+test-string-input-visitor.o test-string-output-visitor.o \
+	test-qmp-commands.o qemu-ga$(EXESUF): QEMU_CFLAGS += -I $(qapi-dir)
 
 $(qapi-dir)/test-qapi-types.c $(qapi-dir)/test-qapi-types.h :\
 $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
@@ -25,6 +27,12 @@ $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-commands.py
 	    $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py $(gen-out-type) -o "$(qapi-dir)" -p "test-" < $<, "  GEN   $@")
 
 
+test-string-output-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y)
+test-string-output-visitor: test-string-output-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
+
+test-string-input-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y)
+test-string-input-visitor: test-string-input-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
+
 test-qmp-output-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y)
 test-qmp-output-visitor: test-qmp-output-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
 
Signature · 1 line
1.7.7.6
#7 lore raw

PATCH 5/9 qom: add generic string parsing/printing

In reply to Paolo Bonzini

Add generic property accessors that take a string and parse it appropriately for the property type. All the magic here is done by the new string-based visitors.

---
 include/qemu/object.h |   24 ++++++++++++++++++++++++
 qom/object.c          |   24 ++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 0 deletions(-)
include/qemu/object.h +24 -0
index 7d50da9..af071eb 100644
@@ -730,6 +730,30 @@ void object_property_set(Object *obj, struct Visitor *v, const char *name,
                          struct Error **errp);
 
 /**
+ * object_property_parse:
+ * @obj: the object
+ * @string: the string that will be used to parse the property value.
+ * @name: the name of the property
+ * @errp: returns an error if this function fails
+ *
+ * Parses a string and writes the result into a property of an object.
+ */
+void object_property_parse(Object *obj, const char *string,
+                           const char *name, struct Error **errp);
+
+/**
+ * object_property_set:
+ * @obj: the object
+ * @name: the name of the property
+ * @errp: returns an error if this function fails
+ *
+ * Returns a string representation of the value of the property.  The
+ * caller shall free the string.
+ */
+char *object_property_print(Object *obj, const char *name,
+                            struct Error **errp);
+
+/**
  * @object_property_get_type:
  * @obj: the object
  * @name: the name of the property
qom/object.c +24 -0
index 5e5b261..97d898f 100644
@@ -13,6 +13,8 @@
 #include "qemu/object.h"
 #include "qemu-common.h"
 #include "qapi/qapi-visit-core.h"
+#include "qapi/string-input-visitor.h"
+#include "qapi/string-output-visitor.h"
 
 /* TODO: replace QObject with a simpler visitor to avoid a dependency
  * of the QOM core on QObject?  */
@@ -782,6 +784,28 @@ int64_t object_property_get_int(Object *obj, const char *name,
     return retval;
 }
 
+void object_property_parse(Object *obj, const char *string,
+                           const char *name, Error **errp)
+{
+    StringInputVisitor *mi;
+    mi = string_input_visitor_new(string);
+    object_property_set(obj, string_input_get_visitor(mi), name, errp);
+
+    string_input_visitor_cleanup(mi);
+}
+
+char *object_property_print(Object *obj, const char *name,
+                            Error **errp)
+{
+    StringOutputVisitor *mo;
+    char *string;
+
+    mo = string_output_visitor_new();
+    object_property_get(obj, string_output_get_visitor(mo), name, NULL);
+    string = string_output_get_string(mo);
+    string_output_visitor_cleanup(mo);
+    return string;
+}
 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
 {
     ObjectProperty *prop = object_property_find(obj, name);
Signature · 1 line
1.7.7.6
#8 lore raw

Re: [Qemu-devel] [PATCH 5/9] qom: add generic string parsing/printing

Am 09.02.2012 15:31, schrieb Paolo Bonzini:

Quoted 26 lines

Add generic property accessors that take a string and parse it appropriately for the property type. All the magic here is done by the new string-based visitors.

---
 include/qemu/object.h |   24 ++++++++++++++++++++++++
 qom/object.c          |   24 ++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 0 deletions(-)
include/qemu/object.h +8 -0
index 7d50da9..af071eb 100644
@@ -730,6 +730,30 @@ void object_property_set(Object *obj, struct Visitor *v, const char *name,
                          struct Error **errp);
 
 /**
+ * object_property_parse:
+ * @obj: the object
+ * @string: the string that will be used to parse the property value.
+ * @name: the name of the property
+ * @errp: returns an error if this function fails
+ *
+ * Parses a string and writes the result into a property of an object.
+ */

include/qemu/object.h void object_property_set(Object *obj, struct Visitor *v, const char *name, · line 750

Quoted 10 lines
include/qemu/object.h +10 -0
+void object_property_parse(Object *obj, const char *string,
+                           const char *name, struct Error **errp);
+
+/**
+ * object_property_set:
+ * @obj: the object
+ * @name: the name of the property
+ * @errp: returns an error if this function fails
+ *
+ * Returns a string representation of the value of the property.  The

* Returns: for gtk-doc.

include/qemu/object.h void object_property_set(Object *obj, struct Visitor *v, const char *name, · line 757

Quoted 7 lines
include/qemu/object.h +6 -0
+ * caller shall free the string.
+ */
+char *object_property_print(Object *obj, const char *name,
+                            struct Error **errp);
+
+/**
  * @object_property_get_type:

(This non-PULL version will conflict here.)

Quoted 8 lines
include/qemu/object.h +0 -0
  * @obj: the object
  * @name: the name of the property
qom/object.c +0 -0
index 5e5b261..97d898f 100644

qom/object.c int64_t object_property_get_int(Object *obj, const char *name, · line 790

Quoted 7 lines
qom/object.c +4 -0
@@ -782,6 +784,28 @@ int64_t object_property_get_int(Object *obj, const char *name,
     return retval;
 }
 
+void object_property_parse(Object *obj, const char *string,
+                           const char *name, Error **errp)
+{
+    StringInputVisitor *mi;

Curious: where does mi/mo come from?

Quoted 8 lines
qom/object.c +8 -0
+    mi = string_input_visitor_new(string);
+    object_property_set(obj, string_input_get_visitor(mi), name, errp);
+
+    string_input_visitor_cleanup(mi);
+}
+
+char *object_property_print(Object *obj, const char *name,
+                            Error **errp)

qom/object.c int64_t object_property_get_int(Object *obj, const char *name, · line 808

Quoted 10 lines
qom/object.c +10 -0
+{
+    StringOutputVisitor *mo;
+    char *string;
+
+    mo = string_output_visitor_new();
+    object_property_get(obj, string_output_get_visitor(mo), name, NULL);
+    string = string_output_get_string(mo);
+    string_output_visitor_cleanup(mo);
+    return string;
+}

Empty line please. Otherwise Liked-by: Andreas Färber <[email protected]>

qom/object.c int64_t object_property_get_int(Object *obj, const char *name, · line 811

Quoted 3 lines
qom/object.c +0 -0
 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
 {
     ObjectProperty *prop = object_property_find(obj, name);

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#9 lore raw

Re: [Qemu-devel] [PATCH 5/9] qom: add generic string parsing/printing

Quoted 5 lines · Andreas Färber
Quoted 4 lines

qom/object.c int64_t object_property_get_int(Object *obj, const char *name, · line 790

Quoted 4 lines
qom/object.c +4 -0
+void object_property_parse(Object *obj, const char *string,
+                           const char *name, Error **errp)
+{
+    StringInputVisitor *mi;

Curious: where does mi/mo come from?

Cut-and-paste from the version using QMP visitors.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
qom/object.c +10 -0
+    mi = string_input_visitor_new(string);
+    object_property_set(obj, string_input_get_visitor(mi), name, errp);
+
+    string_input_visitor_cleanup(mi);
+}
+
+char *object_property_print(Object *obj, const char *name,
+                            Error **errp)
+{
+    StringOutputVisitor *mo;
Quoted 8 lines
Quoted 8 lines

qom/object.c int64_t object_property_get_int(Object *obj, const char *name, · line 808

Quoted 8 lines
qom/object.c +8 -0
+    char *string;
+
+    mo = string_output_visitor_new();
+    object_property_get(obj, string_output_get_visitor(mo), name, NULL);
+    string = string_output_get_string(mo);
+    string_output_visitor_cleanup(mo);
+    return string;
+}

Paolo

#10 lore raw

PATCH 6/9 qdev: accept both strings and integers for PCI addresses

In reply to Paolo Bonzini

Visitors allow a limited form of polymorphism. Exploit it to support setting the non-legacy PCI address property both as a DD.F string and as an 8-bit integer.

The 8-bit integer form is just too clumsy, it is unlikely that we will ever drop it.

---
 hw/qdev-properties.c |   36 +++++++++++++++++++++++++-----------
 1 files changed, 25 insertions(+), 11 deletions(-)
hw/qdev-properties.c +25 -11
index b6d6fcf..9a838b0 100644
@@ -943,25 +943,40 @@ PropertyInfo qdev_prop_losttickpolicy = {
 /*
  * bus-local address, i.e. "$slot" or "$slot.$fn"
  */
-static int parse_pci_devfn(DeviceState *dev, Property *prop, const char *str)
+static void set_pci_devfn(Object *obj, Visitor *v, void *opaque,
+                          const char *name, Error **errp)
 {
+    DeviceState *dev = DEVICE(obj);
+    Property *prop = opaque;
     uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
     unsigned int slot, fn, n;
+    Error *local_err = NULL;
+    char *str = (char *)"";
+
+    if (dev->state != DEV_STATE_CREATED) {
+        error_set(errp, QERR_PERMISSION_DENIED);
+        return;
+    }
+
+    visit_type_str(v, &str, name, &local_err);
+    if (local_err) {
+        return set_int32(obj, v, opaque, name, errp);
+    }
 
     if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
         fn = 0;
         if (sscanf(str, "%x%n", &slot, &n) != 1) {
-            return -EINVAL;
+            goto invalid;
         }
     }
-    if (str[n] != '\0')
-        return -EINVAL;
-    if (fn > 7)
-        return -EINVAL;
-    if (slot > 31)
-        return -EINVAL;
+    if (str[n] != '\0' || fn > 7 || slot > 31) {
+        goto invalid;
+    }
     *ptr = slot << 3 | fn;
-    return 0;
+    return;
+
+invalid:
+    error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
 }
 
 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)
@@ -978,10 +993,9 @@ static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t
 PropertyInfo qdev_prop_pci_devfn = {
     .name  = "int32",
     .legacy_name  = "pci-devfn",
-    .parse = parse_pci_devfn,
     .print = print_pci_devfn,
     .get   = get_int32,
-    .set   = set_int32,
+    .set   = set_pci_devfn,
     /* FIXME: this should be -1...255, but the address is stored
      * into an uint32_t rather than int32_t.
      */
Signature · 1 line
1.7.7.6
#11 lore raw

PATCH 7/9 qdev: accept hex properties only if prefixed by 0x

In reply to Paolo Bonzini

Hex properties are an obstacle to removal of old qdev string parsing, but even here we can lay down the foundations for future simplification. In general, they are rarely used and their printed form is more interesting than the parsing. For example you'd usually set isa-serial.index instead of isa-serial.iobase. And luckily our main client, libvirt only cares about few of these, and always sets them with a 0x prefix. So the series stops accepting bare hexadecimal numbers, preparing for making legacy properties read-only in 1.3 or so. The read side will stay as long as "info qtree" is with us.

---
 hw/qdev-properties.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
hw/qdev-properties.c +12 -0
index 9a838b0..d47122a 100644
@@ -164,6 +164,10 @@ static int parse_hex8(DeviceState *dev, Property *prop, const char *str)
     uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
     char *end;
 
+    if (str[0] != '0' || str[1] != 'x') {
+        return -EINVAL;
+    }
+
     *ptr = strtoul(str, &end, 16);
     if ((*end != '\0') || (end == str)) {
         return -EINVAL;
@@ -369,6 +373,10 @@ static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
     uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
     char *end;
 
+    if (str[0] != '0' || str[1] != 'x') {
+        return -EINVAL;
+    }
+
     *ptr = strtoul(str, &end, 16);
     if ((*end != '\0') || (end == str)) {
         return -EINVAL;
@@ -456,6 +464,10 @@ static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
     uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
     char *end;
 
+    if (str[0] != '0' || str[1] != 'x') {
+        return -EINVAL;
+    }
+
     *ptr = strtoull(str, &end, 16);
     if ((*end != '\0') || (end == str)) {
         return -EINVAL;
Signature · 1 line
1.7.7.6
#12 lore raw

PATCH 8/9 qdev: use built-in QOM string parser

In reply to Paolo Bonzini

object_property_parse lets us drop the legacy setters when their task is done just as well by the string visitors.

---
 hw/qdev-properties.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
hw/qdev-properties.c +2 -2
index d47122a..97bda27 100644
@@ -1080,9 +1080,9 @@ int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
 
     legacy_name = g_strdup_printf("legacy-%s", name);
     if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
-        object_property_set_str(OBJECT(dev), value, legacy_name, &err);
+        object_property_parse(OBJECT(dev), value, legacy_name, &err);
     } else {
-        object_property_set_str(OBJECT(dev), value, name, &err);
+        object_property_parse(OBJECT(dev), value, name, &err);
     }
     g_free(legacy_name);
 
Signature · 1 line
1.7.7.6
#13 lore raw

PATCH 9/9 qdev: drop unnecessary parse/print methods

In reply to Paolo Bonzini

More qdev printers could have been removed in the previous series, and object_property_parse also made several parsers unnecessary. In fact, the new code is even more robust with respect to overflows, so clean them up!

---
 hw/qdev-properties.c |  134 --------------------------------------------------
 1 files changed, 0 insertions(+), 134 deletions(-)
hw/qdev-properties.c +0 -134
index 97bda27..a96c628 100644
@@ -27,16 +27,6 @@ static void bit_prop_set(DeviceState *dev, Property *props, bool val)
 }
 
 /* Bit */
-static int parse_bit(DeviceState *dev, Property *prop, const char *str)
-{
-    if (!strcasecmp(str, "on"))
-        bit_prop_set(dev, prop, true);
-    else if (!strcasecmp(str, "off"))
-        bit_prop_set(dev, prop, false);
-    else
-        return -EINVAL;
-    return 0;
-}
 
 static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len)
 {
@@ -79,7 +69,6 @@ static void set_bit(Object *obj, Visitor *v, void *opaque,
 PropertyInfo qdev_prop_bit = {
     .name  = "boolean",
     .legacy_name  = "on/off",
-    .parse = parse_bit,
     .print = print_bit,
     .get   = get_bit,
     .set   = set_bit,
@@ -87,26 +76,6 @@ PropertyInfo qdev_prop_bit = {
 
 /* --- 8bit integer --- */
 
-static int parse_uint8(DeviceState *dev, Property *prop, const char *str)
-{
-    uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
-    char *end;
-
-    /* accept both hex and decimal */
-    *ptr = strtoul(str, &end, 0);
-    if ((*end != '\0') || (end == str)) {
-        return -EINVAL;
-    }
-
-    return 0;
-}
-
-static int print_uint8(DeviceState *dev, Property *prop, char *dest, size_t len)
-{
-    uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
-    return snprintf(dest, len, "%" PRIu8, *ptr);
-}
-
 static void get_int8(Object *obj, Visitor *v, void *opaque,
                      const char *name, Error **errp)
 {
@@ -149,8 +118,6 @@ static void set_int8(Object *obj, Visitor *v, void *opaque,
 
 PropertyInfo qdev_prop_uint8 = {
     .name  = "uint8",
-    .parse = parse_uint8,
-    .print = print_uint8,
     .get   = get_int8,
     .set   = set_int8,
     .min   = 0,
@@ -195,26 +162,6 @@ PropertyInfo qdev_prop_hex8 = {
 
 /* --- 16bit integer --- */
 
-static int parse_uint16(DeviceState *dev, Property *prop, const char *str)
-{
-    uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
-    char *end;
-
-    /* accept both hex and decimal */
-    *ptr = strtoul(str, &end, 0);
-    if ((*end != '\0') || (end == str)) {
-        return -EINVAL;
-    }
-
-    return 0;
-}
-
-static int print_uint16(DeviceState *dev, Property *prop, char *dest, size_t len)
-{
-    uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
-    return snprintf(dest, len, "%" PRIu16, *ptr);
-}
-
 static void get_int16(Object *obj, Visitor *v, void *opaque,
                       const char *name, Error **errp)
 {
@@ -257,8 +204,6 @@ static void set_int16(Object *obj, Visitor *v, void *opaque,
 
 PropertyInfo qdev_prop_uint16 = {
     .name  = "uint16",
-    .parse = parse_uint16,
-    .print = print_uint16,
     .get   = get_int16,
     .set   = set_int16,
     .min   = 0,
@@ -267,26 +212,6 @@ PropertyInfo qdev_prop_uint16 = {
 
 /* --- 32bit integer --- */
 
-static int parse_uint32(DeviceState *dev, Property *prop, const char *str)
-{
-    uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
-    char *end;
-
-    /* accept both hex and decimal */
-    *ptr = strtoul(str, &end, 0);
-    if ((*end != '\0') || (end == str)) {
-        return -EINVAL;
-    }
-
-    return 0;
-}
-
-static int print_uint32(DeviceState *dev, Property *prop, char *dest, size_t len)
-{
-    uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
-    return snprintf(dest, len, "%" PRIu32, *ptr);
-}
-
 static void get_int32(Object *obj, Visitor *v, void *opaque,
                       const char *name, Error **errp)
 {
@@ -329,37 +254,14 @@ static void set_int32(Object *obj, Visitor *v, void *opaque,
 
 PropertyInfo qdev_prop_uint32 = {
     .name  = "uint32",
-    .parse = parse_uint32,
-    .print = print_uint32,
     .get   = get_int32,
     .set   = set_int32,
     .min   = 0,
     .max   = 0xFFFFFFFFULL,
 };
 
-static int parse_int32(DeviceState *dev, Property *prop, const char *str)
-{
-    int32_t *ptr = qdev_get_prop_ptr(dev, prop);
-    char *end;
-
-    *ptr = strtol(str, &end, 10);
-    if ((*end != '\0') || (end == str)) {
-        return -EINVAL;
-    }
-
-    return 0;
-}
-
-static int print_int32(DeviceState *dev, Property *prop, char *dest, size_t len)
-{
-    int32_t *ptr = qdev_get_prop_ptr(dev, prop);
-    return snprintf(dest, len, "%" PRId32, *ptr);
-}
-
 PropertyInfo qdev_prop_int32 = {
     .name  = "int32",
-    .parse = parse_int32,
-    .print = print_int32,
     .get   = get_int32,
     .set   = set_int32,
     .min   = -0x80000000LL,
@@ -404,26 +306,6 @@ PropertyInfo qdev_prop_hex32 = {
 
 /* --- 64bit integer --- */
 
-static int parse_uint64(DeviceState *dev, Property *prop, const char *str)
-{
-    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
-    char *end;
-
-    /* accept both hex and decimal */
-    *ptr = strtoull(str, &end, 0);
-    if ((*end != '\0') || (end == str)) {
-        return -EINVAL;
-    }
-
-    return 0;
-}
-
-static int print_uint64(DeviceState *dev, Property *prop, char *dest, size_t len)
-{
-    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
-    return snprintf(dest, len, "%" PRIu64, *ptr);
-}
-
 static void get_int64(Object *obj, Visitor *v, void *opaque,
                       const char *name, Error **errp)
 {
@@ -451,8 +333,6 @@ static void set_int64(Object *obj, Visitor *v, void *opaque,
 
 PropertyInfo qdev_prop_uint64 = {
     .name  = "uint64",
-    .parse = parse_uint64,
-    .print = print_uint64,
     .get   = get_int64,
     .set   = set_int64,
 };
@@ -749,19 +629,6 @@ PropertyInfo qdev_prop_netdev = {
 
 /* --- vlan --- */
 
-static int parse_vlan(DeviceState *dev, Property *prop, const char *str)
-{
-    VLANState **ptr = qdev_get_prop_ptr(dev, prop);
-    int id;
-
-    if (sscanf(str, "%d", &id) != 1)
-        return -EINVAL;
-    *ptr = qemu_find_vlan(id, 1);
-    if (*ptr == NULL)
-        return -ENOENT;
-    return 0;
-}
-
 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
 {
     VLANState **ptr = qdev_get_prop_ptr(dev, prop);
@@ -820,7 +687,6 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque,
 
 PropertyInfo qdev_prop_vlan = {
     .name  = "vlan",
-    .parse = parse_vlan,
     .print = print_vlan,
     .get   = get_vlan,
     .set   = set_vlan,
Signature · 1 line
1.7.7.6
#14 lore raw
In reply to Paolo Bonzini

Anthony,

I'm sending a pull request since you informally said on IRC that you're okay with the patches.

The following changes since commit 99c7f87826337fa81f2f0f9baa9ca0a44faf90e9:

input: send kbd+mouse events only to running guests. (2012-02-17 11:02:55 -0600)

are available in the git repository at:
git://github.com/bonzini/qemu.git qdev-props-for-anthony

v1->v2:
Fix bug in parsing booleans and strengthen test suite. The interdiff is attached.

Paolo Bonzini (9):
qapi: allow sharing enum implementation across visitors
qapi: drop qmp_input_end_optional
qapi: add string-based visitors
qapi: add tests for string-based visitors
qom: add generic string parsing/printing
qdev: accept both strings and integers for PCI addresses
qdev: accept hex properties only if prefixed by 0x
qdev: use built-in QOM string parser
qdev: drop unnecessary parse/print methods

 .gitignore                   |    2 +
 Makefile.objs                |    5 +-
 hw/qdev-properties.c         |  186 +++++++++-------------------------------
 include/qemu/object.h        |   24 +++++
 qapi/qapi-visit-core.c       |   51 +++++++++++
 qapi/qapi-visit-impl.h       |   23 +++++
 qapi/qmp-input-visitor.c     |   39 +--------
 qapi/qmp-output-visitor.c    |   22 +-----
 qapi/string-input-visitor.c  |  138 +++++++++++++++++++++++++++++
 qapi/string-input-visitor.h  |   25 ++++++
 qapi/string-output-visitor.c |   89 +++++++++++++++++++
 qapi/string-output-visitor.h |   26 ++++++
 qom/object.c                 |   24 +++++
 test-string-input-visitor.c  |  195 ++++++++++++++++++++++++++++++++++++++++++
 test-string-output-visitor.c |  188 ++++++++++++++++++++++++++++++++++++++++
 tests/Makefile               |   12 ++-
 16 files changed, 841 insertions(+), 208 deletions(-)
 create mode 100644 qapi/qapi-visit-impl.h
 create mode 100644 qapi/string-input-visitor.c
 create mode 100644 qapi/string-input-visitor.h
 create mode 100644 qapi/string-output-visitor.c
 create mode 100644 qapi/string-output-visitor.h
 create mode 100644 test-string-input-visitor.c
 create mode 100644 test-string-output-visitor.c
include/qemu/object.h +1 -1
index 2081ca0..ff6be14 100644
@@ -742,7 +742,7 @@ void object_property_parse(Object *obj, const char *string,
                            const char *name, struct Error **errp);
 
 /**
- * object_property_set:
+ * object_property_print:
  * @obj: the object
  * @name: the name of the property
  * @errp: returns an error if this function fails
qapi/string-input-visitor.c +6 -4
index ceee699..497eb9a 100644
@@ -47,13 +47,15 @@ static void parse_type_bool(Visitor *v, bool *obj, const char *name,
     StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
 
     if (siv->string) {
-        if (strcasecmp(siv->string, "on") || strcasecmp(siv->string, "yes") ||
-            strcasecmp(siv->string, "true")) {
+        if (!strcasecmp(siv->string, "on") ||
+            !strcasecmp(siv->string, "yes") ||
+            !strcasecmp(siv->string, "true")) {
             *obj = true;
             return;
         }
-        if (strcasecmp(siv->string, "off") || strcasecmp(siv->string, "no") ||
-            strcasecmp(siv->string, "false")) {
+        if (!strcasecmp(siv->string, "off") ||
+            !strcasecmp(siv->string, "no") ||
+            !strcasecmp(siv->string, "false")) {
             *obj = false;
             return;
         }
test-string-input-visitor.c +35 -0
index ba2cc40..5370e32 100644
@@ -75,6 +75,41 @@ static void test_visitor_in_bool(TestInputVisitorData *data,
     visit_type_bool(v, &res, NULL, &errp);
     g_assert(!error_is_set(&errp));
     g_assert_cmpint(res, ==, true);
+    visitor_input_teardown(data, unused);
+
+    v = visitor_input_test_init(data, "yes");
+
+    visit_type_bool(v, &res, NULL, &errp);
+    g_assert(!error_is_set(&errp));
+    g_assert_cmpint(res, ==, true);
+    visitor_input_teardown(data, unused);
+
+    v = visitor_input_test_init(data, "on");
+
+    visit_type_bool(v, &res, NULL, &errp);
+    g_assert(!error_is_set(&errp));
+    g_assert_cmpint(res, ==, true);
+    visitor_input_teardown(data, unused);
+
+    v = visitor_input_test_init(data, "false");
+
+    visit_type_bool(v, &res, NULL, &errp);
+    g_assert(!error_is_set(&errp));
+    g_assert_cmpint(res, ==, false);
+    visitor_input_teardown(data, unused);
+
+    v = visitor_input_test_init(data, "no");
+
+    visit_type_bool(v, &res, NULL, &errp);
+    g_assert(!error_is_set(&errp));
+    g_assert_cmpint(res, ==, false);
+    visitor_input_teardown(data, unused);
+
+    v = visitor_input_test_init(data, "off");
+
+    visit_type_bool(v, &res, NULL, &errp);
+    g_assert(!error_is_set(&errp));
+    g_assert_cmpint(res, ==, false);
 }
 
 static void test_visitor_in_number(TestInputVisitorData *data,
Signature · 1 line
1.7.7.6
#15 lore raw
Quoted 8 lines · Paolo Bonzini

Anthony,

I'm sending a pull request since you informally said on IRC that you're okay with the patches.

The following changes since commit 99c7f87826337fa81f2f0f9baa9ca0a44faf90e9:

input: send kbd+mouse events only to running guests. (2012-02-17 11:02:55 -0600)

Pulled. Thanks.

Regards,

Anthony Liguori

Quoted 127 lines

are available in the git repository at:
git://github.com/bonzini/qemu.git qdev-props-for-anthony

v1->v2:
Fix bug in parsing booleans and strengthen test suite. The interdiff is attached.

Paolo Bonzini (9):
qapi: allow sharing enum implementation across visitors
qapi: drop qmp_input_end_optional
qapi: add string-based visitors
qapi: add tests for string-based visitors
qom: add generic string parsing/printing
qdev: accept both strings and integers for PCI addresses
qdev: accept hex properties only if prefixed by 0x
qdev: use built-in QOM string parser
qdev: drop unnecessary parse/print methods

  .gitignore                   |    2 +
  Makefile.objs                |    5 +-
  hw/qdev-properties.c         |  186 +++++++++-------------------------------
  include/qemu/object.h        |   24 +++++
  qapi/qapi-visit-core.c       |   51 +++++++++++
  qapi/qapi-visit-impl.h       |   23 +++++
  qapi/qmp-input-visitor.c     |   39 +--------
  qapi/qmp-output-visitor.c    |   22 +-----
  qapi/string-input-visitor.c  |  138 +++++++++++++++++++++++++++++
  qapi/string-input-visitor.h  |   25 ++++++
  qapi/string-output-visitor.c |   89 +++++++++++++++++++
  qapi/string-output-visitor.h |   26 ++++++
  qom/object.c                 |   24 +++++
  test-string-input-visitor.c  |  195 ++++++++++++++++++++++++++++++++++++++++++
  test-string-output-visitor.c |  188 ++++++++++++++++++++++++++++++++++++++++
  tests/Makefile               |   12 ++-
  16 files changed, 841 insertions(+), 208 deletions(-)

create mode 100644 qapi/qapi-visit-impl.h
create mode 100644 qapi/string-input-visitor.c
create mode 100644 qapi/string-input-visitor.h
create mode 100644 qapi/string-output-visitor.c
create mode 100644 qapi/string-output-visitor.h
create mode 100644 test-string-input-visitor.c
create mode 100644 test-string-output-visitor.c

include/qemu/object.h +1 -1
index 2081ca0..ff6be14 100644
@@ -742,7 +742,7 @@ void object_property_parse(Object *obj, const char *string,
                             const char *name, struct Error **errp);
 
  /**
- * object_property_set:
+ * object_property_print:
   * @obj: the object
   * @name: the name of the property
   * @errp: returns an error if this function fails
qapi/string-input-visitor.c +6 -4
index ceee699..497eb9a 100644
@@ -47,13 +47,15 @@ static void parse_type_bool(Visitor *v, bool *obj, const char *name,
      StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
 
      if (siv->string) {
-        if (strcasecmp(siv->string, "on") || strcasecmp(siv->string, "yes") ||
-            strcasecmp(siv->string, "true")) {
+        if (!strcasecmp(siv->string, "on") ||
+            !strcasecmp(siv->string, "yes") ||
+            !strcasecmp(siv->string, "true")) {
              *obj = true;
              return;
          }
-        if (strcasecmp(siv->string, "off") || strcasecmp(siv->string, "no") ||
-            strcasecmp(siv->string, "false")) {
+        if (!strcasecmp(siv->string, "off") ||
+            !strcasecmp(siv->string, "no") ||
+            !strcasecmp(siv->string, "false")) {
              *obj = false;
              return;
          }
test-string-input-visitor.c +35 -0
index ba2cc40..5370e32 100644
@@ -75,6 +75,41 @@ static void test_visitor_in_bool(TestInputVisitorData *data,
      visit_type_bool(v,&res, NULL,&errp);
      g_assert(!error_is_set(&errp));
      g_assert_cmpint(res, ==, true);
+    visitor_input_teardown(data, unused);
+
+    v = visitor_input_test_init(data, "yes");
+
+    visit_type_bool(v,&res, NULL,&errp);
+    g_assert(!error_is_set(&errp));
+    g_assert_cmpint(res, ==, true);
+    visitor_input_teardown(data, unused);
+
+    v = visitor_input_test_init(data, "on");
+
+    visit_type_bool(v,&res, NULL,&errp);
+    g_assert(!error_is_set(&errp));
+    g_assert_cmpint(res, ==, true);
+    visitor_input_teardown(data, unused);
+
+    v = visitor_input_test_init(data, "false");
+
+    visit_type_bool(v,&res, NULL,&errp);
+    g_assert(!error_is_set(&errp));
+    g_assert_cmpint(res, ==, false);
+    visitor_input_teardown(data, unused);
+
+    v = visitor_input_test_init(data, "no");
+
+    visit_type_bool(v,&res, NULL,&errp);
+    g_assert(!error_is_set(&errp));
+    g_assert_cmpint(res, ==, false);
+    visitor_input_teardown(data, unused);
+
+    v = visitor_input_test_init(data, "off");
+
+    visit_type_bool(v,&res, NULL,&errp);
+    g_assert(!error_is_set(&errp));
+    g_assert_cmpint(res, ==, false);
  }
 
  static void test_visitor_in_number(TestInputVisitorData *data,