diff options
author | Ben Pfaff <blp@nicira.com> | 2011-05-12 13:46:16 -0700 |
---|---|---|
committer | Ben Pfaff <blp@nicira.com> | 2011-05-23 12:14:12 -0700 |
commit | 7fae24e67c95b1dbe93497135ae533e39b61e110 (patch) | |
tree | 8ff59d8cb99acb5cf4b8a3e3bf58c38208bf4233 /ovsdb | |
parent | 7fb563b94c6b265f5dae9965fe33392716a7886e (diff) |
Avoid sparse error or warning for sizeof(_Bool).
The sparse checker does not like taking sizeof(_Bool). Older versions of
sparse output a hard error ("error: cannot size expression"). Newer
versions output a warning ("warning: expression using sizeof bool"). This
commit avoids the problem by not using sizeof(_Bool) anywhere.
The only place where OVS uses sizeof(_Bool) anyway is in code generated by
the OVSDB IDL. It generates it for populating "optional bool" columns in
the database, that is, columns that are allowed to contain 0 or 1 instances
of a bool. For these columns, it generates code that looks roughly like
this:
row->column = xmalloc(sizeof *row->column);
*row->column = value;
This commit changes these columns from type "bool *" to type "const bool *"
and changes the generated code to:
static const bool true_value = true;
static const bool false_value = false;
row->column = value ? &true_value : &false_value;
which avoids the problem and saves a malloc() call at the same time.
The idltest code had a column with a slightly different type ("0, 1, or
2 bools") that didn't fit the revised pattern and is a fairly stupid type
anyhow, so I just changed it to "0 or 1 bools".
Diffstat (limited to 'ovsdb')
-rwxr-xr-x | ovsdb/ovsdb-idlc.in | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/ovsdb/ovsdb-idlc.in b/ovsdb/ovsdb-idlc.in index 2a4c67ca..e5c81832 100755 --- a/ovsdb/ovsdb-idlc.in +++ b/ovsdb/ovsdb-idlc.in @@ -27,6 +27,8 @@ 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 = '' @@ -165,6 +167,10 @@ 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 @@ -215,7 +221,23 @@ static void keyVar = "row->%s" % columnName valueVar = None - if (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer(): + 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(): print print " assert(inited);" print " if (datum->n >= 1) {" @@ -283,7 +305,15 @@ static void # Unparse functions. for columnName, column in sorted(table.columns.iteritems()): type = column.type - if (type.n_min != 1 or type.n_max != 1) and not type.is_optional_pointer(): + if (type.key.type == ovs.db.types.BooleanType and not type.value + and type.n_min == 0 and type.n_max == 1): + print ''' +static void +%(s)s_unparse_%(c)s(struct ovsdb_idl_row *row OVS_UNUSED) +{ + /* Nothing to do. */ +}''' % {'s': structName, 'c': columnName} + elif (type.n_min != 1 or type.n_max != 1) and not type.is_optional_pointer(): print ''' static void %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_) |