diff mbox series

[v3,35/53] qdev: Rename Property.name to Property.name_template

Message ID 20201112214350.872250-36-ehabkost@redhat.com
State New
Headers show
Series Make qdev static property API usable by any QOM type | expand

Commit Message

Eduardo Habkost Nov. 12, 2020, 9:43 p.m. UTC
The Property.name field won't always be set, but it is very
easy to miss this detail when we see code using prop->name.

Rename the field to "name_template", to indicate it is just a
template used when creating new properties in some cases, but is
not always set.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
Changes v2 -> v3:
* Change name from .qdev_prop_name to .name_template.
  The property won't be qdev-specific, but it won't
  be always set.

This is a patch added in v2 of the series
---
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Cc: qemu-devel@nongnu.org
---
 include/hw/qdev-properties.h | 11 +++++++++--
 hw/core/qdev-properties.c    | 23 ++++++++++++-----------
 softmmu/qdev-monitor.c       |  8 ++++----
 3 files changed, 25 insertions(+), 17 deletions(-)
diff mbox series

Patch

diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h
index 97bb9494ae..1208e12b9b 100644
--- a/include/hw/qdev-properties.h
+++ b/include/hw/qdev-properties.h
@@ -13,7 +13,14 @@ 
  *     is true.
  */
 struct Property {
-    const char   *name;
+    /**
+     * @name_template: Property name template
+     *
+     * This is a string containing the template to be used when
+     * creating the property.  It can be NULL, and code shouldn't
+     * assume it will contain the actual property name.
+     */
+    const char   *name_template;
     const PropertyInfo *info;
     ptrdiff_t    offset;
     uint8_t      bitnr;
@@ -62,7 +69,7 @@  extern const PropertyInfo prop_info_arraylen;
 extern const PropertyInfo prop_info_link;
 
 #define DEFINE_PROP(_name, _state, _field, _prop, _type, ...) {  \
-        .name      = (_name),                                    \
+        .name_template = (_name),                           \
         .info      = &(_prop),                                   \
         .offset    = offsetof(_state, _field)                    \
             + type_check(_type, typeof_field(_state, _field)),   \
diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
index 638daf1ebf..03cfad1e74 100644
--- a/hw/core/qdev-properties.c
+++ b/hw/core/qdev-properties.c
@@ -593,7 +593,7 @@  static void set_prop_arraylen(Object *obj, Visitor *v, const char *name,
         arrayprop->release = prop->arrayinfo->release;
         arrayprop->propname = propname;
         arrayprop->prop.info = prop->arrayinfo;
-        arrayprop->prop.name = propname;
+        arrayprop->prop.name_template = propname;
         /* This ugly piece of pointer arithmetic sets up the offset so
          * that when the underlying get/set hooks call qdev_get_prop_ptr
          * they get the right answer despite the array element not actually
@@ -625,8 +625,8 @@  static Property *qdev_prop_walk(Property *props, const char *name)
     if (!props) {
         return NULL;
     }
-    while (props->name) {
-        if (strcmp(props->name, name) == 0) {
+    while (props->name_template) {
+        if (strcmp(props->name_template, name) == 0) {
             return props;
         }
         props++;
@@ -903,15 +903,16 @@  void object_class_add_field_properties(ObjectClass *oc, Property *props,
 {
     Property *prop;
 
-    for (prop = props; prop && prop->name; prop++) {
-        object_class_property_add_field_static(oc, prop->name, prop, allow_set);
+    for (prop = props; prop && prop->name_template; prop++) {
+        object_class_property_add_field_static(oc, prop->name_template, prop,
+                                               allow_set);
     }
 }
 
 
 void qdev_property_add_static(DeviceState *dev, Property *prop)
 {
-    object_property_add_field(OBJECT(dev), prop->name, prop,
+    object_property_add_field(OBJECT(dev), prop->name_template, prop,
                               qdev_prop_allow_set);
 }
 
@@ -955,7 +956,7 @@  static void qdev_class_add_legacy_property(DeviceClass *dc, Property *prop)
         return;
     }
 
-    name = g_strdup_printf("legacy-%s", prop->name);
+    name = g_strdup_printf("legacy-%s", prop->name_template);
     object_class_property_add(OBJECT_CLASS(dc), name, "str",
         prop->info->print ? qdev_get_legacy_property : prop->info->get,
         NULL, NULL, prop);
@@ -967,7 +968,7 @@  void device_class_set_props(DeviceClass *dc, Property *props)
     Property *prop;
 
     dc->props_ = props;
-    for (prop = props; prop && prop->name; prop++) {
+    for (prop = props; prop && prop->name_template; prop++) {
         qdev_class_add_legacy_property(dc, prop);
     }
 
@@ -983,9 +984,9 @@  void qdev_alias_all_properties(DeviceState *target, Object *source)
     do {
         DeviceClass *dc = DEVICE_CLASS(class);
 
-        for (prop = dc->props_; prop && prop->name; prop++) {
-            object_property_add_alias(source, prop->name,
-                                      OBJECT(target), prop->name);
+        for (prop = dc->props_; prop && prop->name_template; prop++) {
+            object_property_add_alias(source, prop->name_template,
+                                      OBJECT(target), prop->name_template);
         }
         class = object_class_get_parent(class);
     } while (class != object_class_by_name(TYPE_DEVICE));
diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c
index 79164e4a3f..0d196a169a 100644
--- a/softmmu/qdev-monitor.c
+++ b/softmmu/qdev-monitor.c
@@ -697,14 +697,14 @@  static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
 {
     if (!props)
         return;
-    for (; props->name; props++) {
+    for (; props->name_template; props++) {
         char *value;
-        char *legacy_name = g_strdup_printf("legacy-%s", props->name);
+        char *legacy_name = g_strdup_printf("legacy-%s", props->name_template);
 
         if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
             value = object_property_get_str(OBJECT(dev), legacy_name, NULL);
         } else {
-            value = object_property_print(OBJECT(dev), props->name, true,
+            value = object_property_print(OBJECT(dev), props->name_template, true,
                                           NULL);
         }
         g_free(legacy_name);
@@ -712,7 +712,7 @@  static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
         if (!value) {
             continue;
         }
-        qdev_printf("%s = %s\n", props->name,
+        qdev_printf("%s = %s\n", props->name_template,
                     *value ? value : "<null>");
         g_free(value);
     }