cleanup_computed_getters.py (2425B)
1 #!/usr/bin/env python3 2 # This Source Code Form is subject to the terms of the Mozilla Public 3 # License, v. 2.0. If a copy of the MPL was not distributed with this 4 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 6 7 """ 8 Script to remove unused getters in nsComputedDOMStyle. 9 10 It needs to be run from the topsrcdir, and it requires passing in the objdir 11 as first argument. It can only be run after nsComputedDOMStyleGenerated.inc 12 is generated in the objdir. 13 """ 14 15 import re 16 import sys 17 18 from pathlib import Path 19 20 if len(sys.argv) != 2: 21 print("Usage: {} objdir".format(sys.argv[0])) 22 exit(1) 23 24 generated = Path(sys.argv[1]) / "layout" / "style" 25 generated = generated / "nsComputedDOMStyleGenerated.inc" 26 RE_GENERATED = re.compile(r"DoGet\w+") 27 keeping = set() 28 with generated.open() as f: 29 for line in f: 30 m = RE_GENERATED.search(line) 31 if m is not None: 32 keeping.add(m.group(0)) 33 34 HEADER = "layout/style/nsComputedDOMStyle.h" 35 SOURCE = "layout/style/nsComputedDOMStyle.cpp" 36 37 # We need to keep functions invoked by others 38 RE_DEF = re.compile(r"nsComputedDOMStyle::(DoGet\w+)\(\)") 39 RE_SRC = re.compile(r"\b(DoGet\w+)\(\)") 40 with open(SOURCE, "r") as f: 41 for line in f: 42 m = RE_DEF.search(line) 43 if m is not None: 44 continue 45 m = RE_SRC.search(line) 46 if m is not None: 47 keeping.add(m.group(1)) 48 49 removing = set() 50 remaining_lines = [] 51 with open(HEADER, "r") as f: 52 for line in f: 53 m = RE_SRC.search(line) 54 if m is not None: 55 name = m.group(1) 56 if name not in keeping: 57 print("Removing " + name) 58 removing.add(name) 59 continue 60 remaining_lines.append(line) 61 62 with open(HEADER, "w", newline="") as f: 63 f.writelines(remaining_lines) 64 65 remaining_lines = [] 66 is_removing = False 67 with open(SOURCE, "r") as f: 68 for line in f: 69 if is_removing: 70 if line == "}\n": 71 is_removing = False 72 continue 73 m = RE_DEF.search(line) 74 if m is not None: 75 name = m.group(1) 76 if name in removing: 77 remaining_lines.pop() 78 if remaining_lines[-1] == "\n": 79 remaining_lines.pop() 80 is_removing = True 81 continue 82 remaining_lines.append(line) 83 84 with open(SOURCE, "w", newline="") as f: 85 f.writelines(remaining_lines)