Message ID | 1457502997-30904-2-git-send-email-peterx@redhat.com |
---|---|
State | New |
Headers | show |
On 03/08/2016 10:56 PM, Peter Xu wrote: > Fix two places to use literal printf format when possible. > > Signed-off-by: Peter Xu <peterx@redhat.com> > --- > block/qapi.c | 10 ++++------ > 1 file changed, 4 insertions(+), 6 deletions(-) Reviewed-by: Eric Blake <eblake@redhat.com> > > diff --git a/block/qapi.c b/block/qapi.c > index db2d3fb..c4c2115 100644 > --- a/block/qapi.c > +++ b/block/qapi.c > @@ -619,9 +619,8 @@ static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation, > for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) { > QType type = qobject_type(entry->value); > bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); > - const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: "; > - > - func_fprintf(f, format, indentation * 4, "", i); > + func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i, > + composite ? '\n' : ' '); [The nerd in me wants to point out that you could avoid the ternary by writing '"\n "[composite]', but that's too ugly to use outside of IOCCC submissions, and I wouldn't be surprised if it (rightfully) triggers clang warnings]
On Wed, Mar 09, 2016 at 03:14:03PM -0700, Eric Blake wrote: > > + func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i, > > + composite ? '\n' : ' '); > > [The nerd in me wants to point out that you could avoid the ternary by > writing '"\n "[composite]', but that's too ugly to use outside of IOCCC > submissions, and I wouldn't be surprised if it (rightfully) triggers > clang warnings] Do you mean something like: int i = 0; printf("%c", '"\n "[i]'); Is this a grammar btw? Peter
On 03/09/2016 06:46 PM, Peter Xu wrote: > On Wed, Mar 09, 2016 at 03:14:03PM -0700, Eric Blake wrote: >>> + func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i, >>> + composite ? '\n' : ' '); >> >> [The nerd in me wants to point out that you could avoid the ternary by >> writing '"\n "[composite]', but that's too ugly to use outside of IOCCC >> submissions, and I wouldn't be surprised if it (rightfully) triggers >> clang warnings] > > Do you mean something like: > > int i = 0; > printf("%c", '"\n "[i]'); You mean: printf("%c", "\n "[i]); (no ''). But with your declaration of 'i' as int, it is only defined if i <= 2 (whereas in my example above, "\n "[composite] is always defined because composite is bool rather than int). > > Is this a grammar btw? Yes, C has an ugly grammar, because [] is just syntactic sugar for deferencing pointer addition with nicer operator precedence. Quoting C99 6.5.2.1: "The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero)." And a string literal is just a fancy way of writing the address of an array of characters (where the address is chosen by the compiler). Thus, it IS valid to dereference the addition of an integer offset with the address implied by a string literal in order to obtain a character within the string. And since the [] operator is commutative (even though no one in their right mind commutes the operands), you can also write the even-uglier: composite["\n "] But now we've gone far astray from the original patch review :)
On Mon, Mar 21, 2016 at 03:14:48PM -0600, Eric Blake wrote: > On 03/09/2016 06:46 PM, Peter Xu wrote: > > > > Is this a grammar btw? > > Yes, C has an ugly grammar, because [] is just syntactic sugar for > deferencing pointer addition with nicer operator precedence. Quoting > C99 6.5.2.1: > > "The definition of the subscript operator [] is that E1[E2] is identical > to (*((E1)+(E2))). Because of the conversion rules that apply to the > binary + operator, if E1 is an array object (equivalently, a pointer to > the initial element of an array object) and E2 is an integer, E1[E2] > designates the E2-th element of E1 (counting from zero)." > > And a string literal is just a fancy way of writing the address of an > array of characters (where the address is chosen by the compiler). > > Thus, it IS valid to dereference the addition of an integer offset with > the address implied by a string literal in order to obtain a character > within the string. And since the [] operator is commutative (even > though no one in their right mind commutes the operands), you can also > write the even-uglier: > > composite["\n "] > > But now we've gone far astray from the original patch review :) Interesting thing to know. Thanks. :) -- peterx
Peter Xu <peterx@redhat.com> writes: > Fix two places to use literal printf format when possible. > > Signed-off-by: Peter Xu <peterx@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com>
diff --git a/block/qapi.c b/block/qapi.c index db2d3fb..c4c2115 100644 --- a/block/qapi.c +++ b/block/qapi.c @@ -619,9 +619,8 @@ static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation, for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) { QType type = qobject_type(entry->value); bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); - const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: "; - - func_fprintf(f, format, indentation * 4, "", i); + func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i, + composite ? '\n' : ' '); dump_qobject(func_fprintf, f, indentation + 1, entry->value); if (!composite) { func_fprintf(f, "\n"); @@ -637,7 +636,6 @@ static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) { QType type = qobject_type(entry->value); bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); - const char *format = composite ? "%*s%s:\n" : "%*s%s: "; char key[strlen(entry->key) + 1]; int i; @@ -646,8 +644,8 @@ static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; } key[i] = 0; - - func_fprintf(f, format, indentation * 4, "", key); + func_fprintf(f, "%*s%s:%c", indentation * 4, "", key, + composite ? '\n' : ' '); dump_qobject(func_fprintf, f, indentation + 1, entry->value); if (!composite) { func_fprintf(f, "\n");
Fix two places to use literal printf format when possible. Signed-off-by: Peter Xu <peterx@redhat.com> --- block/qapi.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)