Bug 1471114 part 7 - Scripts used to generated the previous patches. r?emilio draft
authorXidorn Quan <me@upsuper.org>
Wed, 27 Jun 2018 08:49:10 +1000
changeset 811184 960b212b998e728f90bbf564f5899a92f65c9e0e
parent 811145 65a744682fe99d8f0de4fa4b7a478e10aba0349e
push id114219
push userxquan@mozilla.com
push dateWed, 27 Jun 2018 05:01:47 +0000
reviewersemilio
bugs1471114
milestone63.0a1
Bug 1471114 part 7 - Scripts used to generated the previous patches. r?emilio MozReview-Commit-ID: E9dB5l9AmeS
layout/style/tools/cleanup_computed_getters.py
layout/style/tools/cleanup_keywords.py
layout/style/tools/cleanup_ktables.py
new file mode 100644
--- /dev/null
+++ b/layout/style/tools/cleanup_computed_getters.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+
+"""
+Script to remove unused getters in nsComputedDOMStyle.
+
+It needs to be run from the topsrcdir, and it requires passing in the objdir
+as first argument. It can only be run after nsComputedDOMStyleGenerated.cpp
+is generated in the objdir.
+"""
+
+import re
+import sys
+
+from pathlib import Path
+
+if len(sys.argv) != 2:
+    print("Usage: {} objdir".format(sys.argv[0]))
+    exit(1)
+
+generated = Path(sys.argv[1]) / "layout" / "style"
+generated = generated / "nsComputedDOMStyleGenerated.cpp"
+RE_GENERATED = re.compile(r"DoGet\w+")
+keeping = set()
+with generated.open() as f:
+    for line in f:
+        m = RE_GENERATED.search(line)
+        if m is not None:
+            keeping.add(m.group(0))
+
+HEADER = "layout/style/nsComputedDOMStyle.h"
+SOURCE = "layout/style/nsComputedDOMStyle.cpp"
+
+# We need to keep functions invoked by others
+RE_DEF = re.compile(r"nsComputedDOMStyle::(DoGet\w+)\(\)")
+RE_SRC = re.compile(r"\b(DoGet\w+)\(\)")
+with open(SOURCE, "r") as f:
+    for line in f:
+        m = RE_DEF.search(line)
+        if m is not None:
+            continue
+        m = RE_SRC.search(line)
+        if m is not None:
+            keeping.add(m.group(1))
+
+removing = set()
+remaining_lines = []
+with open(HEADER, "r") as f:
+    for line in f:
+        m = RE_SRC.search(line)
+        if m is not None:
+            name = m.group(1)
+            if name not in keeping:
+                print("Removing " + name)
+                removing.add(name)
+                continue
+        remaining_lines.append(line)
+
+with open(HEADER, "w", newline="") as f:
+    f.writelines(remaining_lines)
+
+remaining_lines = []
+is_removing = False
+with open(SOURCE, "r") as f:
+    for line in f:
+        if is_removing:
+            if line == "}\n":
+                is_removing = False
+            continue
+        m = RE_DEF.search(line)
+        if m is not None:
+            name = m.group(1)
+            if name in removing:
+                remaining_lines.pop()
+                if remaining_lines[-1] == "\n":
+                    remaining_lines.pop()
+                is_removing = True
+                continue
+        remaining_lines.append(line)
+
+with open(SOURCE, "w", newline="") as f:
+    f.writelines(remaining_lines)
new file mode 100644
--- /dev/null
+++ b/layout/style/tools/cleanup_keywords.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+"""
+Script to remove unused keywords from nsCSSKeywordList.
+
+It needs to be run from the topsrcdir.
+"""
+
+import re
+import subprocess
+
+LIST_FILE = "layout/style/nsCSSKeywordList.h"
+
+RE_KEYWORD = re.compile(r"\beCSSKeyword_(\w+)")
+rg_result = subprocess.check_output(["rg", r"eCSSKeyword_\w+"], encoding="UTF-8")
+to_keep = set()
+for item in rg_result.splitlines():
+    file, line = item.split(':', 1)
+    for m in RE_KEYWORD.finditer(line):
+        to_keep.add(m.group(1))
+
+remaining_lines = []
+RE_ITEM = re.compile(r"CSS_KEY\(.+, (\w+)\)")
+with open(LIST_FILE, "r") as f:
+    for line in f:
+        m = RE_ITEM.search(line)
+        if m is not None and m.group(1) not in to_keep:
+            print("Removing " + m.group(1))
+            continue
+        remaining_lines.append(line)
+with open(LIST_FILE, "w", newline="") as f:
+    f.writelines(remaining_lines)
new file mode 100644
--- /dev/null
+++ b/layout/style/tools/cleanup_ktables.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+
+"""
+Script to remove unused keyword tables in nsCSSProps.
+
+It needs to be run from the topsrcdir.
+"""
+
+import re
+import subprocess
+
+from pathlib import Path
+
+HEADER = Path("layout/style/nsCSSProps.h")
+SOURCE = Path("layout/style/nsCSSProps.cpp")
+
+RE_TABLE = re.compile(r"\b(k\w+KTable)")
+rg_result = subprocess.check_output(["rg", r"\bk\w+KTable"], encoding="UTF-8")
+to_keep = set()
+all = set()
+for item in rg_result.splitlines():
+    file, line = item.split(':', 1)
+    name = RE_TABLE.search(line).group(1)
+    path = Path(file)
+    if path != HEADER and path != SOURCE:
+        to_keep.add(name)
+    else:
+        all.add(name)
+
+to_remove = all - to_keep
+remaining_lines = []
+with HEADER.open() as f:
+    for line in f:
+        m = RE_TABLE.search(line)
+        if m is not None and m.group(1) in to_remove:
+            print("Removing " + m.group(1))
+            continue
+        remaining_lines.append(line)
+with HEADER.open("w", newline="") as f:
+    f.writelines(remaining_lines)
+
+remaining_lines = []
+removing = False
+RE_DEF = re.compile(r"KTableEntry nsCSSProps::(k\w+KTable)\[\]")
+with SOURCE.open() as f:
+    for line in f:
+        if removing:
+            if line == "};\n":
+                removing = False
+            continue
+        m = RE_DEF.search(line)
+        if m is not None and m.group(1) in to_remove:
+            if remaining_lines[-1] == "\n":
+                remaining_lines.pop()
+            removing = True
+            continue
+        remaining_lines.append(line)
+with SOURCE.open("w", newline="") as f:
+    f.writelines(remaining_lines)