@@ -239,17 +239,26 @@ void visit_start_struct(Visitor *v, const char *name, void **obj,
size_t size, Error **errp);
/*
- * Complete an object visit started earlier.
+ * Prepare for completing an object visit.
*
* @errp must be NULL-initialized, and is set if an error is detected
* (such as unparsed keys remaining in the input stream).
*
+ * Should be called prior to visit_end_struct() if all other
+ * intermediate visit steps were successful, to allow the visitor one
+ * last chance to report errors.
+ */
+void visit_check_struct(Visitor *v, Error **errp);
+
+/*
+ * Complete an object visit started earlier.
+ *
* Must be called after any successful use of visit_start_struct(),
* even if intermediate processing was skipped due to errors, to allow
* the backend to release any resources. Destroying the visitor may
* behave as if this was implicitly called.
*/
-void visit_end_struct(Visitor *v, Error **errp);
+void visit_end_struct(Visitor *v);
/* === Visiting lists */
@@ -39,8 +39,11 @@ struct Visitor
void (*start_struct)(Visitor *v, const char *name, void **obj,
size_t size, Error **errp);
+ /* Optional; intended for input visitors. */
+ void (*check_struct)(Visitor *v, Error **errp);
+
/* Must be set to visit structs. */
- void (*end_struct)(Visitor *v, Error **errp);
+ void (*end_struct)(Visitor *v);
/* Must be set. */
void (*start_list)(Visitor *v, const char *name, Error **errp);
@@ -71,8 +71,9 @@ def gen_event_send(name, arg_type):
ret += gen_visit_members(arg_type.members, need_cast=True,
label='out_obj')
ret += mcgen('''
+ visit_check_struct(v, &err);
out_obj:
- visit_end_struct(v, err ? NULL : &err);
+ visit_end_struct(v);
if (err) {
goto out;
}
@@ -188,9 +188,10 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error
break;
}
visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
- error_propagate(errp, err);
- err = NULL;
- visit_end_struct(v, &err);
+ if (!err) {
+ visit_check_struct(v, &err);
+ }
+ visit_end_struct(v);
''',
c_type=var.type.c_name(),
c_name=c_name(var.name))
@@ -240,10 +241,12 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error
goto out_obj;
}
visit_type_%(c_name)s_members(v, *obj, &err);
- error_propagate(errp, err);
- err = NULL;
+ if (err) {
+ goto out_obj;
+ }
+ visit_check_struct(v, &err);
out_obj:
- visit_end_struct(v, &err);
+ visit_end_struct(v);
out:
error_propagate(errp, err);
}
@@ -29,9 +29,16 @@ void visit_start_struct(Visitor *v, const char *name, void **obj,
v->start_struct(v, name, obj, size, errp);
}
-void visit_end_struct(Visitor *v, Error **errp)
+void visit_check_struct(Visitor *v, Error **errp)
{
- v->end_struct(v, errp);
+ if (v->check_struct) {
+ v->check_struct(v, errp);
+ }
+}
+
+void visit_end_struct(Visitor *v)
+{
+ v->end_struct(v);
}
void visit_start_list(Visitor *v, const char *name, Error **errp)
@@ -288,7 +288,8 @@ static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
case FDT_END_NODE:
/* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
g_assert(fdt_depth > 0);
- visit_end_struct(v, &err);
+ visit_check_struct(v, &err);
+ visit_end_struct(v);
if (err) {
error_propagate(errp, err);
return;
@@ -136,17 +136,18 @@ static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
if (err) {
- break;
+ goto out_nested;
}
}
- error_propagate(errp, err);
- err = NULL;
- visit_end_struct(v, &err);
+ visit_check_struct(v, &err);
+out_nested:
+ visit_end_struct(v);
+ if (!err) {
+ visit_check_struct(v, &err);
+ }
out_end:
- error_propagate(errp, err);
- err = NULL;
- visit_end_struct(v, &err);
+ visit_end_struct(v);
out:
error_propagate(errp, err);
}
@@ -158,13 +158,13 @@ opts_start_struct(Visitor *v, const char *name, void **obj,
static void
-opts_end_struct(Visitor *v, Error **errp)
+opts_check_struct(Visitor *v, Error **errp)
{
OptsVisitor *ov = to_ov(v);
GHashTableIter iter;
GQueue *any;
- if (--ov->depth > 0) {
+ if (ov->depth > 0) {
return;
}
@@ -176,6 +176,18 @@ opts_end_struct(Visitor *v, Error **errp)
first = g_queue_peek_head(any);
error_setg(errp, QERR_INVALID_PARAMETER, first->name);
}
+}
+
+
+static void
+opts_end_struct(Visitor *v)
+{
+ OptsVisitor *ov = to_ov(v);
+
+ if (--ov->depth > 0) {
+ return;
+ }
+
g_hash_table_destroy(ov->unprocessed_opts);
ov->unprocessed_opts = NULL;
if (ov->fake_id_opt) {
@@ -510,6 +522,7 @@ opts_visitor_new(const QemuOpts *opts)
ov->visitor.type = VISITOR_INPUT;
ov->visitor.start_struct = &opts_start_struct;
+ ov->visitor.check_struct = &opts_check_struct;
ov->visitor.end_struct = &opts_end_struct;
ov->visitor.start_list = &opts_start_list;
@@ -67,7 +67,7 @@ static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
qapi_dealloc_push(qov, obj);
}
-static void qapi_dealloc_end_struct(Visitor *v, Error **errp)
+static void qapi_dealloc_end_struct(Visitor *v)
{
QapiDeallocVisitor *qov = to_qov(v);
void **obj = qapi_dealloc_pop(qov);
@@ -123,8 +123,10 @@ static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj,
}
-static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
+static void qmp_input_check_struct(Visitor *v, Error **errp)
{
+ QmpInputVisitor *qiv = to_qiv(v);
+
assert(qiv->nb_stack > 0);
if (qiv->strict) {
@@ -137,6 +139,19 @@ static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
error_setg(errp, QERR_QMP_EXTRA_MEMBER, key);
}
+ }
+ }
+}
+
+static void qmp_input_pop(Visitor *v)
+{
+ QmpInputVisitor *qiv = to_qiv(v);
+
+ assert(qiv->nb_stack > 0);
+
+ if (qiv->strict) {
+ GHashTable * const top_ht = qiv->stack[qiv->nb_stack - 1].h;
+ if (top_ht) {
g_hash_table_unref(top_ht);
}
}
@@ -171,12 +186,6 @@ static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
}
}
-static void qmp_input_end_struct(Visitor *v, Error **errp)
-{
- QmpInputVisitor *qiv = to_qiv(v);
-
- qmp_input_pop(qiv, errp);
-}
static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
{
@@ -216,12 +225,6 @@ static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
return entry;
}
-static void qmp_input_end_list(Visitor *v)
-{
- QmpInputVisitor *qiv = to_qiv(v);
-
- qmp_input_pop(qiv, &error_abort);
-}
static void qmp_input_start_alternate(Visitor *v, const char *name,
GenericAlternate **obj, size_t size,
@@ -381,10 +384,11 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
v->visitor.type = VISITOR_INPUT;
v->visitor.start_struct = qmp_input_start_struct;
- v->visitor.end_struct = qmp_input_end_struct;
+ v->visitor.check_struct = qmp_input_check_struct;
+ v->visitor.end_struct = qmp_input_pop;
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.end_list = qmp_input_pop;
v->visitor.start_alternate = qmp_input_start_alternate;
v->visitor.type_int64 = qmp_input_type_int64;
v->visitor.type_uint64 = qmp_input_type_uint64;
@@ -113,7 +113,7 @@ static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
qmp_output_push(qov, dict);
}
-static void qmp_output_end_struct(Visitor *v, Error **errp)
+static void qmp_output_end_struct(Visitor *v)
{
QmpOutputVisitor *qov = to_qov(v);
QObject *value = qmp_output_pop(qov);
@@ -2035,10 +2035,9 @@ static void property_get_tm(Object *obj, Visitor *v, const char *name,
if (err) {
goto out_end;
}
+ visit_check_struct(v, &err);
out_end:
- error_propagate(errp, err);
- err = NULL;
- visit_end_struct(v, errp);
+ visit_end_struct(v);
out:
error_propagate(errp, err);
@@ -41,7 +41,7 @@ Object *user_creatable_add(const QDict *qdict,
char *type = NULL;
char *id = NULL;
Object *obj = NULL;
- Error *local_err = NULL, *end_err = NULL;
+ Error *local_err = NULL;
QDict *pdict;
pdict = qdict_clone_shallow(qdict);
@@ -68,16 +68,14 @@ Object *user_creatable_add(const QDict *qdict,
goto out_visit;
}
- out_visit:
- visit_end_struct(v, &end_err);
- if (end_err) {
- error_propagate(&local_err, end_err);
- if (obj) {
- user_creatable_del(id, NULL);
- }
- goto out;
+ visit_check_struct(v, &local_err);
+ if (local_err) {
+ user_creatable_del(id, NULL);
}
+out_visit:
+ visit_end_struct(v);
+
out:
QDECREF(pdict);
g_free(id);
@@ -289,7 +289,8 @@ static void test_visitor_in_null(TestInputVisitorData *data,
v = visitor_input_test_init(data, "{ 'a': null }");
visit_start_struct(v, NULL, NULL, 0, &error_abort);
visit_type_null(v, "a", &error_abort);
- visit_end_struct(v, &error_abort);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v);
}
static void test_visitor_in_union_flat(TestInputVisitorData *data,
@@ -898,10 +898,12 @@ Example:
goto out_obj;
}
visit_type_UserDefOne_members(v, *obj, &err);
- error_propagate(errp, err);
- err = NULL;
+ if (err) {
+ goto out_obj;
+ }
+ visit_check_struct(v, &err);
out_obj:
- visit_end_struct(v, &err);
+ visit_end_struct(v);
out:
error_propagate(errp, err);
}
As mentioned in previous patches, we want to call visit_end_struct() functions unconditionally, so that visitors can release resources tied up since the matching visit_start_struct() without also having to worry about error priority if more than one error occurs. Even though error_propagate() can be safely used to ignore a second error during cleanup caused by a first error, it is simpler if the cleanup cannot set an error. So, split out the error checking portion (basically, input visitors checking for unvisited keys) into a new function visit_check_struct(), which can be safely skipped if any earlier errors are encountered, and leave the cleanup portion (which never fails, but must be called unconditionally if visit_start_struct() succeeded) in visit_end_struct(). Generated code has diffs resembling: |@@ -59,10 +59,12 @@ void visit_type_ACPIOSTInfo(Visitor *v, | goto out_obj; | } | visit_type_ACPIOSTInfo_members(v, obj, &err); |- error_propagate(errp, err); |- err = NULL; |+ if (err) { |+ goto out_obj; |+ } |+ visit_check_struct(v, &err); | out_obj: |- visit_end_struct(v, &err); |+ visit_end_struct(v); | out: Signed-off-by: Eric Blake <eblake@redhat.com> --- v12: rebase to earlier changes, fix bug in spapr_drc not calling visit_end_struct, fold in docs, fix stray DO_UPCAST from sneaking back in [no v10, v11] v9: rebase to earlier changes, drop Marc-Andre's R-b v8: rebase to 'name' motion v7: rebase to earlier changes v6: new patch, revised version of RFC based on discussion of v5 7/46 --- include/qapi/visitor.h | 13 +++++++++++-- include/qapi/visitor-impl.h | 5 ++++- scripts/qapi-event.py | 3 ++- scripts/qapi-visit.py | 15 +++++++++------ qapi/qapi-visit-core.c | 11 +++++++++-- hw/ppc/spapr_drc.c | 3 ++- hw/virtio/virtio-balloon.c | 15 ++++++++------- qapi/opts-visitor.c | 17 +++++++++++++++-- qapi/qapi-dealloc-visitor.c | 2 +- qapi/qmp-input-visitor.c | 34 +++++++++++++++++++--------------- qapi/qmp-output-visitor.c | 2 +- qom/object.c | 5 ++--- qom/object_interfaces.c | 16 +++++++--------- tests/test-qmp-input-visitor.c | 3 ++- docs/qapi-code-gen.txt | 8 +++++--- 15 files changed, 97 insertions(+), 55 deletions(-)