This allows us to raise error messages from virEnum*String functions.
FromString failure will report this error for value 'zzzz'
invalid argument: Unknown 'domain type' value 'zzzz'
ToString failure will report this error for unknown type=83
internal error: Unknown 'domain type' internal value '83'
However we disable the error reporting for now. It should only be
enabled when we decide to begin dropping duplicate error reporting.
Note: this patch will be combined with the next patch when pushing
Signed-off-by: Cole Robinson <crobinso(a)redhat.com>
---
docs/apibuild.py | 12 ++++++++++++
src/util/virenum.c | 22 ++++++++++++++++++----
src/util/virenum.h | 15 ++++++++++-----
3 files changed, 40 insertions(+), 9 deletions(-)
diff --git a/docs/apibuild.py b/docs/apibuild.py
index 9e04871220..1450355eeb 100755
--- a/docs/apibuild.py
+++ b/docs/apibuild.py
@@ -1404,6 +1404,18 @@ class CParser:
self.error("parsing VIR_ENUM_IMPL: expecting ','", token)
token = self.token()
+ # The 'label' field
+ if token[0] != "string":
+ self.error("parsing VIR_ENUM_IMPL: expecting string", token)
+ token = self.token()
+
+ if token[0] != "sep":
+ self.error("parsing VIR_ENUM_IMPL: expecting ','", token)
+
+ if token[1] != ',':
+ self.error("parsing VIR_ENUM_IMPL: expecting ','", token)
+ token = self.token()
+
# Now the sentinel name
if token[0] != "name":
self.error("parsing VIR_ENUM_IMPL: expecting name", token)
diff --git a/src/util/virenum.c b/src/util/virenum.c
index 26093bd795..58968e7357 100644
--- a/src/util/virenum.c
+++ b/src/util/virenum.c
@@ -60,16 +60,23 @@ virTristateSwitchFromBool(bool val)
int
virEnumFromString(const char * const *types,
unsigned int ntypes,
- const char *type)
+ const char *type,
+ const char *label ATTRIBUTE_UNUSED)
{
size_t i;
if (!type)
- return -1;
+ goto error;
for (i = 0; i < ntypes; i++)
if (STREQ(types[i], type))
return i;
+ error:
+ /* To be enabled at a later date
+ *
+ * virReportError(VIR_ERR_INVALID_ARG,
+ * _("Unknown '%s' value '%s'"), label,
NULLSTR(type));
+ */
return -1;
}
@@ -77,10 +84,17 @@ virEnumFromString(const char * const *types,
const char *
virEnumToString(const char * const *types,
unsigned int ntypes,
- int type)
+ int type,
+ const char *label ATTRIBUTE_UNUSED)
{
- if (type < 0 || type >= ntypes)
+ if (type < 0 || type >= ntypes) {
+ /* To be enabled at a later date
+ *
+ * virReportError(VIR_ERR_INTERNAL_ERROR,
+ * _("Unknown '%s' internal value %d"), label,
type);
+ */
return NULL;
+ }
return types[type];
}
diff --git a/src/util/virenum.h b/src/util/virenum.h
index 3ae1a70b72..2c68e7f6a0 100644
--- a/src/util/virenum.h
+++ b/src/util/virenum.h
@@ -24,24 +24,29 @@
int
virEnumFromString(const char * const *types,
unsigned int ntypes,
- const char *type);
+ const char *type,
+ const char *label);
const char *
virEnumToString(const char * const *types,
unsigned int ntypes,
- int type);
+ int type,
+ const char *label);
-# define VIR_ENUM_IMPL(name, lastVal, ...) \
+# define VIR_ENUM_IMPL(name, label, lastVal, ...) \
+ static const char *name ## TypeLabel = label; \
static const char *const name ## TypeList[] = { __VA_ARGS__ }; \
const char *name ## TypeToString(int type) { \
return virEnumToString(name ## TypeList, \
ARRAY_CARDINALITY(name ## TypeList), \
- type); \
+ type, \
+ name ## TypeLabel); \
} \
int name ## TypeFromString(const char *type) { \
return virEnumFromString(name ## TypeList, \
ARRAY_CARDINALITY(name ## TypeList), \
- type); \
+ type, \
+ name ## TypeLabel); \
} \
verify(ARRAY_CARDINALITY(name ## TypeList) == lastVal)
--
2.21.0