Bug 1454591 part 2 - Refactor GenerateCSSPropsGenerated.py. r?heycam draft
authorXidorn Quan <me@upsuper.org>
Fri, 04 May 2018 14:37:41 +1000
changeset 791364 3304d221aa8378873137f1500f982e45b9f643c0
parent 791363 3d002f7790b76aaa3cea373430206c7629825e47
child 791365 13b76ba90225edb1713834c43b6b9309fd74587e
push id108797
push userxquan@mozilla.com
push dateFri, 04 May 2018 07:07:28 +0000
reviewersheycam
bugs1454591
milestone61.0a1
Bug 1454591 part 2 - Refactor GenerateCSSPropsGenerated.py. r?heycam This removes the extra template file and uses the script to generate the whole nsCSSPropsGenerated.inc file directly, because it doesn't seem to really make much sense to have them separate. One behavior change to this refactor is that, the static assertions no longer include aliases. Other parts of the generated data all ignore aliases, so checking property id of aliases isn't really useful. It makes the code simpler everywhere to just strip aliases from the list at the very beginning. MozReview-Commit-ID: BYYvnCOqJwC
layout/style/GenerateCSSPropsGenerated.py
layout/style/moz.build
layout/style/nsCSSPropsGenerated.inc.in
--- a/layout/style/GenerateCSSPropsGenerated.py
+++ b/layout/style/GenerateCSSPropsGenerated.py
@@ -20,71 +20,47 @@ class PropertyWrapper(object):
             if not idl_name.startswith("Moz"):
                 idl_name = idl_name[0].lower() + idl_name[1:]
             self.idlname = idl_name
 
     def __getattr__(self, name):
         return getattr(self.prop, name)
 
 
-def get_properties(dataFile):
-    properties = runpy.run_path(dataFile)["data"]
-    return [PropertyWrapper(i, p) for i, p in enumerate(properties)]
+def generate(output, dataFile):
+    output.write("""/* THIS IS AN AUTOGENERATED FILE.  DO NOT EDIT */
 
-def generate_idl_names(properties):
-    names = []
-    for p in properties:
-        if p.type() == "alias":
-            continue
-        if p.idlname is None:
-            names.append("  nullptr,  // %s" % p.name)
-        else:
-            names.append('  "%s",' % p.idlname)
-    return "\n".join(names)
+/* processed file that defines CSS property tables that can't be generated
+   with the pre-processor, designed to be #included in nsCSSProps.cpp */
 
-def generate_assertions(properties):
-    def enum(p):
-        if p.type() == "alias":
-            return "eCSSPropertyAlias_%s" % p.alias_id
-        else:
-            return "eCSSProperty_%s" % p.id
-    msg = ('static_assert(%s == %d, "GenerateCSSPropsGenerated.py did not list '
-           'properties in nsCSSPropertyID order");')
-    return "\n".join(map(lambda p: msg % (enum(p), p.index), properties))
+""")
 
-def generate_idl_name_positions(properties):
-    # Skip aliases.
-    ps = filter(lambda p: p.type() != "alias", properties)
-
-    # Sort alphabetically by IDL name.
-    ps = sorted(ps, key=lambda p: p.idlname)
+    properties = runpy.run_path(dataFile)["data"]
+    properties = [PropertyWrapper(i, p)
+                  for i, p in enumerate(properties)
+                  if p.type() != "alias"]
 
-    # Annotate entries with the sorted position.
-    ps = [(p, position) for position, p in enumerate(ps)]
-
-    # Sort back to nsCSSPropertyID order.
-    ps = sorted(ps, key=lambda (p, position): p.index)
-
-    return ",\n".join(map(lambda (p, position): "  %d" % position, ps))
-
-def generate(output, cppTemplate, dataFile):
-    cppFile = open(cppTemplate, "r")
-    cppTemplate = cppFile.read()
-    cppFile.close()
+    # Generate kIDLNameTable
+    output.write("const char* const nsCSSProps::"
+                 "kIDLNameTable[eCSSProperty_COUNT] = {\n")
+    for p in properties:
+        if p.idlname is None:
+            output.write("  nullptr,  // {}\n".format(p.name))
+        else:
+            output.write('  "{}",\n'.format(p.idlname))
+    output.write("};\n\n")
 
-    properties = get_properties(dataFile)
-    substitutions = {
-        "idl_names": generate_idl_names(properties),
-        "assertions": generate_assertions(properties),
-        "idl_name_positions": generate_idl_name_positions(properties),
-    }
-    output.write("/* THIS IS AN AUTOGENERATED FILE.  DO NOT EDIT */\n\n" +
-                 string.Template(cppTemplate).substitute(substitutions) + "\n")
+    # Generate kIDLNameSortPositionTable
+    ps = sorted(properties, key=lambda p: p.idlname)
+    ps = [(p, position) for position, p in enumerate(ps)]
+    ps.sort(key=lambda (p, position): p.index)
+    output.write("const int32_t nsCSSProps::"
+                 "kIDLNameSortPositionTable[eCSSProperty_COUNT] = {\n")
+    for (p, position) in ps:
+        output.write("  {},\n".format(position))
+    output.write("};\n\n")
 
-def main():
-    parser = argparse.ArgumentParser()
-    parser.add_argument('cppTemplate', help='CSS property file template')
-    parser.add_argument('preprocessorHeader', help='Header file to pass through the preprocessor')
-    args = parser.parse_args()
-    generate(sys.stdout, args.cppTemplate, args.preprocessorHeader)
-
-if __name__ == '__main__':
-    main()
+    # Generate assertions
+    msg = ("GenerateCSSPropsGenerated.py did not list properties "
+           "in nsCSSPropertyID order")
+    for p in properties:
+        output.write('static_assert(eCSSProperty_{} == {}, "{}");\n'
+                     .format(p.id, p.index, msg))
--- a/layout/style/moz.build
+++ b/layout/style/moz.build
@@ -299,15 +299,14 @@ servo_props.inputs = [
 if CONFIG['COMPILE_ENVIRONMENT']:
     GENERATED_FILES += [
         'nsCSSPropsGenerated.inc',
     ]
 
     css_props = GENERATED_FILES['nsCSSPropsGenerated.inc']
     css_props.script = 'GenerateCSSPropsGenerated.py:generate'
     css_props.inputs = [
-        'nsCSSPropsGenerated.inc.in',
         '!ServoCSSPropList.py',
     ]
 
     CONFIGURE_SUBST_FILES += [
         'bindgen.toml',
     ]
deleted file mode 100644
--- a/layout/style/nsCSSPropsGenerated.inc.in
+++ /dev/null
@@ -1,17 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-/* processed file that defines CSS property tables that can't be generated
-   with the pre-processor, designed to be #included in nsCSSProps.cpp */
-
-const char* const nsCSSProps::kIDLNameTable[eCSSProperty_COUNT] = {
-${idl_names}
-};
-
-const int32_t nsCSSProps::kIDLNameSortPositionTable[eCSSProperty_COUNT] = {
-${idl_name_positions}
-};
-
-${assertions}