[ovs-dev] [PATCH 2/3] idl: Generalize special case boolean exception.

Ben Pfaff blp at nicira.com
Wed Feb 8 09:28:41 PST 2012


On Tue, Feb 07, 2012 at 07:28:53PM -0800, Ethan Jackson wrote:
> Sparse doesn't like taking sizeof boolean values.  The idl had
> worked around this limitation with a special case in the case of
> optional booleans.  A future patch needs an array with boolean
> values which the existing special case did not handle.  This patch
> generalizes the special case to handle this situation as well.
> 
> Signed-off-by: Ethan Jackson <ethan at nicira.com>

I had thought, when you brought it up earlier, that _Bool (and thus
bool) was guaranteed to have size 1 in C, but looking at C99 I don't
see anything that says that.  So I think that it's best to actually
use sizeof for bool, too.  We can use a fixed size 1 for sparse, since
it doesn't actually matter whether sparse gets the right size (it's
not generating code).

How about this alternative patch, then (based on yours):

diff --git a/ovsdb/ovsdb-idlc.in b/ovsdb/ovsdb-idlc.in
index d711541..d92173a 100755
--- a/ovsdb/ovsdb-idlc.in
+++ b/ovsdb/ovsdb-idlc.in
@@ -27,8 +27,6 @@ def constify(cType, const):
 
 def cMembers(prefix, columnName, column, const):
     type = column.type
-    if is_optional_bool(type):
-        const = True
     if type.n_min == 1 and type.n_max == 1:
         singleton = True
         pointer = ''
@@ -167,10 +165,6 @@ def printEnum(members):
     print "    %s" % members[-1]
     print "};"
 
-def is_optional_bool(type):
-    return (type.key.type == ovs.db.types.BooleanType and not type.value
-            and type.n_min == 0 and type.n_max == 1)
-
 def printCIDLSource(schemaFile):
     schema = parseSchema(schemaFile)
     prefix = schema.idlPrefix
@@ -184,6 +178,13 @@ def printCIDLSource(schemaFile):
 #include "ovsdb-data.h"
 #include "ovsdb-error.h"
 
+#ifdef __CHECKER__
+/* sparse dislikes sizeof(bool) ("warning: expression using sizeof bool"). */
+enum { sizeof_bool = 1 };
+#else
+enum { sizeof_bool = sizeof(bool) };
+#endif
+
 static bool inited;
 ''' % schema.idlHeader
 
@@ -221,23 +222,7 @@ static void
                 keyVar = "row->%s" % columnName
                 valueVar = None
 
-            if is_optional_bool(type):
-                # Special case for an optional bool.  This is only here because
-                # sparse does not like the "normal" case below ("warning:
-                # expression using sizeof bool").
-                print
-                print "    assert(inited);"
-                print "    if (datum->n >= 1) {"
-                print "        static const bool false_value = false;"
-                print "        static const bool true_value = true;"
-                print
-                print "        row->n_%s = 1;" % columnName
-                print "        %s = datum->keys[0].boolean ? &true_value : &false_value;" % keyVar
-                print "    } else {"
-                print "        row->n_%s = 0;" % columnName
-                print "        %s = NULL;" % keyVar
-                print "    }"
-            elif (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer():
+            if (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer():
                 print
                 print "    assert(inited);"
                 print "    if (datum->n >= 1) {"
@@ -289,9 +274,25 @@ static void
                 else:
                     indent = "        "
                 print "%sif (!row->n_%s) {" % (indent, columnName)
-                print "%s    %s = xmalloc(%s * sizeof *%s);" % (indent, keyVar, nMax, keyVar)
+
+                # Special case for an optional boolean types.  This is only
+                # here because sparse does not like the "normal" case
+                # ("warning: expression using sizeof bool").
+                if type.key.type != ovs.db.types.BooleanType:
+                    sizeMult = " * sizeof *%s" % keyVar
+                else:
+                    sizeMult = " * sizeof_bool"
+                print "%s    %s = xmalloc(%s%s);" % (indent, keyVar, nMax,
+                                                     sizeMult)
                 if valueVar:
-                    print "%s    %s = xmalloc(%s * sizeof *%s);" % (indent, valueVar, nMax, valueVar)
+                    # Special case for an optional boolean types (see above).
+                    sizeMult = ""
+                    if type.value.type != ovs.db.types.BooleanType:
+                        sizeMult = " * sizeof *%s" % valueVar
+                    else:
+                        sizeMult = " * sizeof_bool"
+                    print "%s    %s = xmalloc(%s%s);" % (indent, valueVar,
+                                                         nMax, sizeMult)
                 print "%s}" % indent
                 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
                 if valueVar:



More information about the dev mailing list