Patchwork [06/34] qerror: avoid passing qerr pointer

login
register
mail settings
Submitter Luiz Capitulino
Date Aug. 2, 2012, 1:02 a.m.
Message ID <1343869374-23417-7-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/174631/
State New
Headers show

Comments

Luiz Capitulino - Aug. 2, 2012, 1:02 a.m.
Helps dropping/modifying qerror functions.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 qerror.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
Markus Armbruster - Aug. 2, 2012, 11:23 a.m.
Luiz Capitulino <lcapitulino@redhat.com> writes:

> Helps dropping/modifying qerror functions.
>
> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> ---
>  qerror.c | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
>
> diff --git a/qerror.c b/qerror.c
> index 5efccec..59025ea 100644
> --- a/qerror.c
> +++ b/qerror.c
> @@ -346,10 +346,10 @@ static QError *qerror_new(void)
>      return qerr;
>  }
>  
> -static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
> -                                               const char *fmt, va_list *va)
> +static QDict *error_obj_from_fmt_no_fail(const char *fmt, va_list *va)

Elsewhere, we use FOO_nofail() for a variant of FOO() that must not
fail.

Call this function just error_obj_from_fmt()?

Or if you think the name must convey it can't fail:
error_obj_from_fmt_nofail()?

>  {
>      QObject *obj;
> +    QDict *ret;
>  
>      obj = qobject_from_jsonv(fmt, va);
>      if (!obj) {
> @@ -361,9 +361,8 @@ static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
>          abort();
>      }
>  
> -    qerr->error = qobject_to_qdict(obj);
> -
> -    obj = qdict_get(qerr->error, "class");
> +    ret = qobject_to_qdict(obj);
> +    obj = qdict_get(ret, "class");
>      if (!obj) {
>          fprintf(stderr, "missing 'class' key in '%s'\n", fmt);
>          abort();
> @@ -372,8 +371,8 @@ static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
>          fprintf(stderr, "'class' key value should be a string in '%s'\n", fmt);
>          abort();
>      }
> -    
> -    obj = qdict_get(qerr->error, "data");
> +
> +    obj = qdict_get(ret, "data");
>      if (!obj) {
>          fprintf(stderr, "missing 'data' key in '%s'\n", fmt);
>          abort();
> @@ -382,9 +381,11 @@ static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
>          fprintf(stderr, "'data' key value should be a dict in '%s'\n", fmt);
>          abort();
>      }
> +
> +    return ret;
>  }
>  
> -static void qerror_set_desc(QError *qerr, const char *fmt)
> +static const QErrorStringTable *get_desc_no_fail(const char *fmt)

Likewise.

>  {
>      int i;
>  
> @@ -392,8 +393,7 @@ static void qerror_set_desc(QError *qerr, const char *fmt)
>  
>      for (i = 0; qerror_table[i].error_fmt; i++) {
>          if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
> -            qerr->entry = &qerror_table[i];
> -            return;
> +            return &qerror_table[i];
>          }
>      }
>  
> @@ -426,8 +426,8 @@ static QError *qerror_from_info(const char *file, int linenr, const char *func,
>      qerr->file = file;
>      qerr->func = func;
>  
> -    qerror_set_data(qerr, fmt, va);
> -    qerror_set_desc(qerr, fmt);
> +    qerr->error = error_obj_from_fmt_no_fail(fmt, va);
> +    qerr->entry = get_desc_no_fail(fmt);
>  
>      return qerr;
>  }

Turns side effects on qerr into a nice clean return values.  Good!
Luiz Capitulino - Aug. 2, 2012, 1:44 p.m.
On Thu, 02 Aug 2012 13:23:58 +0200
Markus Armbruster <armbru@redhat.com> wrote:

> Luiz Capitulino <lcapitulino@redhat.com> writes:
> 
> > Helps dropping/modifying qerror functions.
> >
> > Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> > ---
> >  qerror.c | 24 ++++++++++++------------
> >  1 file changed, 12 insertions(+), 12 deletions(-)
> >
> > diff --git a/qerror.c b/qerror.c
> > index 5efccec..59025ea 100644
> > --- a/qerror.c
> > +++ b/qerror.c
> > @@ -346,10 +346,10 @@ static QError *qerror_new(void)
> >      return qerr;
> >  }
> >  
> > -static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
> > -                                               const char *fmt, va_list *va)
> > +static QDict *error_obj_from_fmt_no_fail(const char *fmt, va_list *va)
> 
> Elsewhere, we use FOO_nofail() for a variant of FOO() that must not
> fail.
> 
> Call this function just error_obj_from_fmt()?
> 
> Or if you think the name must convey it can't fail:
> error_obj_from_fmt_nofail()?

It's worth it, this function is dropped later on.

> 
> >  {
> >      QObject *obj;
> > +    QDict *ret;
> >  
> >      obj = qobject_from_jsonv(fmt, va);
> >      if (!obj) {
> > @@ -361,9 +361,8 @@ static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
> >          abort();
> >      }
> >  
> > -    qerr->error = qobject_to_qdict(obj);
> > -
> > -    obj = qdict_get(qerr->error, "class");
> > +    ret = qobject_to_qdict(obj);
> > +    obj = qdict_get(ret, "class");
> >      if (!obj) {
> >          fprintf(stderr, "missing 'class' key in '%s'\n", fmt);
> >          abort();
> > @@ -372,8 +371,8 @@ static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
> >          fprintf(stderr, "'class' key value should be a string in '%s'\n", fmt);
> >          abort();
> >      }
> > -    
> > -    obj = qdict_get(qerr->error, "data");
> > +
> > +    obj = qdict_get(ret, "data");
> >      if (!obj) {
> >          fprintf(stderr, "missing 'data' key in '%s'\n", fmt);
> >          abort();
> > @@ -382,9 +381,11 @@ static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
> >          fprintf(stderr, "'data' key value should be a dict in '%s'\n", fmt);
> >          abort();
> >      }
> > +
> > +    return ret;
> >  }
> >  
> > -static void qerror_set_desc(QError *qerr, const char *fmt)
> > +static const QErrorStringTable *get_desc_no_fail(const char *fmt)
> 
> Likewise.
> 
> >  {
> >      int i;
> >  
> > @@ -392,8 +393,7 @@ static void qerror_set_desc(QError *qerr, const char *fmt)
> >  
> >      for (i = 0; qerror_table[i].error_fmt; i++) {
> >          if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
> > -            qerr->entry = &qerror_table[i];
> > -            return;
> > +            return &qerror_table[i];
> >          }
> >      }
> >  
> > @@ -426,8 +426,8 @@ static QError *qerror_from_info(const char *file, int linenr, const char *func,
> >      qerr->file = file;
> >      qerr->func = func;
> >  
> > -    qerror_set_data(qerr, fmt, va);
> > -    qerror_set_desc(qerr, fmt);
> > +    qerr->error = error_obj_from_fmt_no_fail(fmt, va);
> > +    qerr->entry = get_desc_no_fail(fmt);
> >  
> >      return qerr;
> >  }
> 
> Turns side effects on qerr into a nice clean return values.  Good!
>

Patch

diff --git a/qerror.c b/qerror.c
index 5efccec..59025ea 100644
--- a/qerror.c
+++ b/qerror.c
@@ -346,10 +346,10 @@  static QError *qerror_new(void)
     return qerr;
 }
 
-static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
-                                               const char *fmt, va_list *va)
+static QDict *error_obj_from_fmt_no_fail(const char *fmt, va_list *va)
 {
     QObject *obj;
+    QDict *ret;
 
     obj = qobject_from_jsonv(fmt, va);
     if (!obj) {
@@ -361,9 +361,8 @@  static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
         abort();
     }
 
-    qerr->error = qobject_to_qdict(obj);
-
-    obj = qdict_get(qerr->error, "class");
+    ret = qobject_to_qdict(obj);
+    obj = qdict_get(ret, "class");
     if (!obj) {
         fprintf(stderr, "missing 'class' key in '%s'\n", fmt);
         abort();
@@ -372,8 +371,8 @@  static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
         fprintf(stderr, "'class' key value should be a string in '%s'\n", fmt);
         abort();
     }
-    
-    obj = qdict_get(qerr->error, "data");
+
+    obj = qdict_get(ret, "data");
     if (!obj) {
         fprintf(stderr, "missing 'data' key in '%s'\n", fmt);
         abort();
@@ -382,9 +381,11 @@  static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
         fprintf(stderr, "'data' key value should be a dict in '%s'\n", fmt);
         abort();
     }
+
+    return ret;
 }
 
-static void qerror_set_desc(QError *qerr, const char *fmt)
+static const QErrorStringTable *get_desc_no_fail(const char *fmt)
 {
     int i;
 
@@ -392,8 +393,7 @@  static void qerror_set_desc(QError *qerr, const char *fmt)
 
     for (i = 0; qerror_table[i].error_fmt; i++) {
         if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
-            qerr->entry = &qerror_table[i];
-            return;
+            return &qerror_table[i];
         }
     }
 
@@ -426,8 +426,8 @@  static QError *qerror_from_info(const char *file, int linenr, const char *func,
     qerr->file = file;
     qerr->func = func;
 
-    qerror_set_data(qerr, fmt, va);
-    qerror_set_desc(qerr, fmt);
+    qerr->error = error_obj_from_fmt_no_fail(fmt, va);
+    qerr->entry = get_desc_no_fail(fmt);
 
     return qerr;
 }