[ovs-dev] [PATCH v3] ovsdb-idlc: Replace C/C++ keyword in column name

Yi-Hung Wei yihung.wei at gmail.com
Tue Dec 12 21:17:40 UTC 2017


With this patch, ovs-idlc appends an underscore to a column name
if it is a C or C++ keyword. Therefore, C and C++ compiler will
not get confused.

Signed-off-by: Yi-Hung Wei <yihung.wei at gmail.com>
---
 ovsdb/ovsdb-idlc.1  |  8 ++++++--
 ovsdb/ovsdb-idlc.in | 31 +++++++++++++++++++++++++++++--
 vswitchd/bridge.c   |  2 +-
 3 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/ovsdb/ovsdb-idlc.1 b/ovsdb/ovsdb-idlc.1
index b44757b820b8..af68e93e16f8 100644
--- a/ovsdb/ovsdb-idlc.1
+++ b/ovsdb/ovsdb-idlc.1
@@ -50,11 +50,15 @@ modified is re-serialized as JSON on standard output.
 .
 .IP "\fBc\-idl\-header\fI idl\fR"
 Reads \fIidl\fR and prints on standard output a C header file that
-defines a structure for each table defined by the schema.
+defines a structure for each table defined by the schema.  If a column
+name in \fIidl\fR is a C or C++ keyword, it will be appended with an
+underscore.
 .
 .IP "\fBc\-idl\-source\fI idl\fR"
 Reads \fIidl\fR and prints on standard output a C source file that
-implements C bindings for the database defined by the schema.
+implements C bindings for the database defined by the schema.  If a column
+name in \fIidl\fR is a C or C++ keyword, it will be appended with an
+underscore.
 .
 .SS "Options"
 .so lib/common.man
diff --git a/ovsdb/ovsdb-idlc.in b/ovsdb/ovsdb-idlc.in
index d07d527877a6..96420ca0bd34 100755
--- a/ovsdb/ovsdb-idlc.in
+++ b/ovsdb/ovsdb-idlc.in
@@ -122,8 +122,33 @@ def cMembers(prefix, tableName, columnName, column, const, refTable=True):
 def sorted_columns(table):
     return sorted(table.columns.items())
 
+# If a column name in the schema is a C or C++ keyword, append an underscore
+# to the column name.
+def replace_cplusplus_keyword(schema):
+    keywords = {'alignas', 'alignof', 'and', 'and_eq', 'asm', 'auto', 'bitand',
+                'bitor', 'bool', 'break', 'case', 'catch', 'char', 'char16_t',
+                'char32_t', 'class', 'compl', 'concept', 'const', 'const_cast',
+                'constexpr', 'continue', 'decltype', 'default', 'delete', 'do',
+                'double', 'dynamic_cast', 'else', 'enum', 'explicit', 'export',
+                'extern', 'false', 'float', 'for', 'friend', 'goto', 'if',
+                'inline', 'int', 'long', 'mutable', 'namespace', 'new',
+                'noexcept', 'not', 'not_eq', 'nullptr', 'operator', 'or',
+                'or_eq', 'private', 'protected', 'public', 'register',
+                'reinterpret_cast', 'requires', 'restrict', 'return', 'short',
+                'signed', 'sizeof', 'static', 'static_assert', 'static_cast',
+                'struct', 'switch', 'template', 'this', 'thread_local',
+                'throw', 'true', 'try', 'typedef', 'typeid', 'typename',
+                'union', 'unsigned', 'using', 'virtual', 'void', 'volatile',
+                'wchar_t', 'while', 'xor', 'xor_eq'}
+
+    for tableName, table in schema.tables.items():
+        for columnName in table.columns:
+            if columnName in keywords:
+                table.columns[columnName + '_'] = table.columns.pop(columnName)
+
 def printCIDLHeader(schemaFile):
     schema = parseSchema(schemaFile)
+    replace_cplusplus_keyword(schema)
     prefix = schema.idlPrefix
     print('''\
 /* Generated automatically -- do not modify!    -*- buffer-read-only: t -*- */
@@ -328,6 +353,7 @@ def printEnum(type, members):
 
 def printCIDLSource(schemaFile):
     schema = parseSchema(schemaFile)
+    replace_cplusplus_keyword(schema)
     prefix = schema.idlPrefix
     print('''\
 /* Generated automatically -- do not modify!    -*- buffer-read-only: t -*- */
@@ -1273,7 +1299,7 @@ void
                                   for x in column.type.cInitType("%s_col_%s" % (tableName, columnName), prereqs))
             print("""\
     [%(P)s%(T)s_COL_%(C)s] = {
-         .name = "%(c)s",
+         .name = "%(column_name_in_schema)s",
          .type = {
 %(type)s
          },
@@ -1286,7 +1312,8 @@ void
                'C': columnName.upper(),
                's': structName,
                'mutable': mutable,
-               'type': type_init})
+               'type': type_init,
+               'column_name_in_schema': column.name})
         print("};")
 
     # Table classes.
diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c
index 630c6fa535e2..27ee50646dd7 100644
--- a/vswitchd/bridge.c
+++ b/vswitchd/bridge.c
@@ -1046,7 +1046,7 @@ port_configure(struct port *port)
     }
 
     /* Protected port mode */
-    s.protected = cfg->protected;
+    s.protected = cfg->protected_;
 
     /* Register. */
     ofproto_bundle_register(port->bridge->ofproto, port, &s);
-- 
2.7.4



More information about the dev mailing list