[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 12/14] error: turn QERR_ macros into an enumeration
From: |
Luiz Capitulino |
Subject: |
[Qemu-devel] [PATCH 12/14] error: turn QERR_ macros into an enumeration |
Date: |
Wed, 25 Jul 2012 17:50:29 -0300 |
The enum is called ErrClass, and is autogenerated.
FIXME: also converts error_is_type() and probably breaks other error
functions used to query the error object.
Signed-off-by: Luiz Capitulino <address@hidden>
---
error.c | 24 ++----------------------
error.h | 3 ++-
scripts/qapi-errors.py | 47 ++++++++++++++++++-----------------------------
3 files changed, 22 insertions(+), 52 deletions(-)
diff --git a/error.c b/error.c
index a410cc2..529e75f 100644
--- a/error.c
+++ b/error.c
@@ -102,29 +102,9 @@ void error_free(Error *err)
}
}
-bool error_is_type(Error *err, const char *fmt)
+bool error_is_type(Error *err, ErrClass err_class)
{
- const char *error_class;
- char *ptr;
- char *end;
-
- if (!err) {
- return false;
- }
-
- ptr = strstr(fmt, "'class': '");
- assert(ptr != NULL);
- ptr += strlen("'class': '");
-
- end = strchr(ptr, '\'');
- assert(end != NULL);
-
- error_class = error_get_field(err, "class");
- if (strlen(error_class) != end - ptr) {
- return false;
- }
-
- return strncmp(ptr, error_class, end - ptr) == 0;
+ return (err && (err->err_class == err_class));
}
void error_propagate(Error **dst_err, Error *local_err)
diff --git a/error.h b/error.h
index de98e48..19116ef 100644
--- a/error.h
+++ b/error.h
@@ -13,6 +13,7 @@
#define ERROR_H
#include "compiler.h"
+#include "qapi-errors.h"
#include <stdbool.h>
/**
@@ -70,6 +71,6 @@ void error_free(Error *err);
* Determine if an error is of a speific type (based on the qerror format).
* Non-QEMU users should get the `class' field to identify the error type.
*/
-bool error_is_type(Error *err, const char *fmt);
+bool error_is_type(Error *err, ErrClass err_class);
#endif
diff --git a/scripts/qapi-errors.py b/scripts/qapi-errors.py
index dc656f2..0f52f43 100644
--- a/scripts/qapi-errors.py
+++ b/scripts/qapi-errors.py
@@ -51,7 +51,7 @@ def gen_error_def_prologue(error_header, prefix=""):
prefix=prefix, error_header=error_header)
return ret
-def gen_error_macro(err_domain):
+def gen_error_enum_name(err_domain):
string = ''
cur = err_domain[0]
for nxt in err_domain[1:]:
@@ -82,13 +82,15 @@ static const char *error_object_table[] = {
''')
for err in exprs:
+ enum = gen_error_enum_name(err['error'])
data = gen_error_data_obj({})
if err.has_key('data'):
data = gen_error_data_obj(err['data'])
ret += mcgen('''
- "{ 'class': '%(error_class)s', 'data': { %(error_data)s } }",
+ [%(error_enum)s] =
+ "{ 'class': '%(error_class)s', 'data': { %(error_data)s } }",
''',
- error_class=err['error'], error_data=data)
+ error_enum=enum, error_class=err['error'], error_data=data)
ret += mcgen('''
NULL,
@@ -98,35 +100,22 @@ static const char *error_object_table[] = {
return ret;
-def gen_error_macro_data_str(data):
- colon = ''
- data_str = ''
- for k, v in data.items():
- data_str += colon + "'%s': " % k
- if v == 'str':
- data_str += "%s"
- elif v == 'int':
- data_str += '%"PRId64"'
- else:
- sys.exit("unknown data type '%s' for error '%s'" % (v, name))
- colon = ', '
- return data_str
+def gen_error_enum(exprs):
+ ret = mcgen('''
+typedef enum ErrClass {
+''')
-def gen_error_decl_macros(exprs):
- ret = ''
for err in exprs:
- data = gen_error_macro_data_str({})
- if err.has_key('data'):
- data = gen_error_macro_data_str(err['data'])
- macro = gen_error_macro(err['error'])
- name = err['error']
-
ret += mcgen('''
-#define %(error_macro)s \\
- "{ 'class': '%(error_class)s', 'data': { %(error_data)s } }"
-
+ %(enum)s,
''',
- error_macro=macro, error_class=name, error_data=data)
+ enum=gen_error_enum_name(err['error']))
+
+ ret += mcgen('''
+} ErrClass;
+
+''')
+
return ret
def maybe_open(really, name, opt):
@@ -172,7 +161,7 @@ if __name__ == '__main__':
ret = gen_error_decl_prologue(header=basename(h_file),
guard=guardname(h_file), prefix=prefix)
fdecl.write(ret)
- ret = gen_error_decl_macros(exprs)
+ ret = gen_error_enum(exprs)
fdecl.write(ret)
fdecl.write("#endif\n")
--
1.7.11.2.249.g31c7954.dirty
- [Qemu-devel] [PATCH 04/14] qerror: drop qerror_report_internal(), (continued)
- [Qemu-devel] [PATCH 04/14] qerror: drop qerror_report_internal(), Luiz Capitulino, 2012/07/25
- [Qemu-devel] [PATCH 05/14] qerror: qerror_format(): return an allocated string, Luiz Capitulino, 2012/07/25
- [Qemu-devel] [PATCH 06/14] qerror: don't delay error message construction, Luiz Capitulino, 2012/07/25
- [Qemu-devel] [PATCH 07/14] error: don't delay error message construction, Luiz Capitulino, 2012/07/25
- [Qemu-devel] [PATCH 08/14] qerror: add build_error_dict() and error_object_table[], Luiz Capitulino, 2012/07/25
- [Qemu-devel] [PATCH 09/14] qerror: qerror_report(): take an index and a human error message, Luiz Capitulino, 2012/07/25
- [Qemu-devel] [PATCH 11/14] qerror: drop qerror_table[] for good, Luiz Capitulino, 2012/07/25
- [Qemu-devel] [PATCH 12/14] error: turn QERR_ macros into an enumeration,
Luiz Capitulino <=
- [Qemu-devel] [PATCH 13/14] qerror: change all qerror_report() calls to use the ErrClass enum, Luiz Capitulino, 2012/07/25
- [Qemu-devel] [PATCH 10/14] error: error_set(): take an index and a human error message, Luiz Capitulino, 2012/07/25
- [Qemu-devel] [PATCH 14/14] error: change all error_set() calls to use the ErrClass enum, Luiz Capitulino, 2012/07/25
- Re: [Qemu-devel] [RFC 00/14]: add printf-like human msg to error_set(), Anthony Liguori, 2012/07/25
- Re: [Qemu-devel] [RFC 00/14]: add printf-like human msg to error_set(), Kevin Wolf, 2012/07/26