broadcom/xml: Fix up safe name confusion with prefixing.
authorEric Anholt <eric@anholt.net>
Thu, 28 Dec 2017 20:59:33 +0000 (12:59 -0800)
committerEric Anholt <eric@anholt.net>
Wed, 3 Jan 2018 22:25:23 +0000 (14:25 -0800)
For enums we were doubling the underscore if the value had a numeric first
character of its name (which safe_name() adds an underscore to).  A little
helper function cleans up the other instance of prefixing while also
fixing this.

src/broadcom/cle/gen_pack_header.py

index 78ad9ad31a46d9fd7fdd71d1fefc253b7a0ca2e9..a23b69b67586d8bd1eee4eb499ff03f36e1f5c2e 100644 (file)
@@ -82,6 +82,11 @@ def safe_name(name):
 
     return name
 
+def prefixed_upper_name(prefix, name):
+    if prefix:
+        name = prefix + "_" + name
+    return safe_name(name).upper()
+
 def num_from_str(num_str):
     if num_str.lower().startswith('0x'):
         return int(num_str, base=16)
@@ -164,15 +169,9 @@ class Field(object):
 
         print("   %-36s %s%s;" % (type, self.name, dim))
 
-        if len(self.values) > 0 and self.default == None:
-            if self.prefix:
-                prefix = self.prefix + "_"
-            else:
-                prefix = ""
-
         for value in self.values:
-            print("#define %-40s %d" % ((prefix + value.name).replace("__", "_"),
-                                        value.value))
+            name = prefixed_upper_name(self.prefix, value.name)
+            print("#define %-40s %d" % (name, value.value))
 
     def overlaps(self, field):
         return self != field and max(self.start, field.start) <= min(self.end, field.end)
@@ -371,7 +370,7 @@ class Group(object):
 
 class Value(object):
     def __init__(self, attrs):
-        self.name = safe_name(attrs["name"]).upper()
+        self.name = attrs["name"]
         self.value = int(attrs["value"])
 
 class Parser(object):
@@ -440,7 +439,7 @@ class Parser(object):
             self.enum = safe_name(attrs["name"])
             self.enums.add(attrs["name"])
             if "prefix" in attrs:
-                self.prefix = safe_name(attrs["prefix"])
+                self.prefix = attrs["prefix"]
             else:
                 self.prefix= None
         elif name == "value":
@@ -544,11 +543,11 @@ class Parser(object):
     def emit_enum(self):
         print('enum %s {' % self.gen_prefix(self.enum))
         for value in self.values:
+            name = value.name
             if self.prefix:
-                name = self.prefix + "_" + value.name
-            else:
-                name = value.name
-            print('        % -36s = %6d,' % (name.upper(), value.value))
+                name = self.prefix + "_" + name
+            name = safe_name(name).upper()
+            print('        % -36s = %6d,' % (name, value.value))
         print('};\n')
 
     def parse(self, filename):