Patchwork [12/14] error: turn QERR_ macros into an enumeration

login
register
mail settings
Submitter Luiz Capitulino
Date July 25, 2012, 8:50 p.m.
Message ID <1343249431-9245-13-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/173281/
State New
Headers show

Comments

Luiz Capitulino - July 25, 2012, 8:50 p.m.
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 <lcapitulino@redhat.com>
---
 error.c                | 24 ++----------------------
 error.h                |  3 ++-
 scripts/qapi-errors.py | 47 ++++++++++++++++++-----------------------------
 3 files changed, 22 insertions(+), 52 deletions(-)

Patch

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")