File: c_struct_clean.py

package info (click to toggle)
blender 3.4.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 280,208 kB
  • sloc: ansic: 1,213,366; cpp: 1,148,738; python: 468,812; xml: 13,577; sh: 5,969; javascript: 304; lisp: 247; makefile: 67
file content (80 lines) | stat: -rwxr-xr-x 1,811 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later

"""
When a source file declares a struct which isn't used anywhere else in the file.
Remove it.

There may be times this is needed, however there can typically be removed.
"""

import os
import sys
import re

from typing import (
    Dict,
    Optional,
)

PWD = os.path.dirname(__file__)
sys.path.append(os.path.join(PWD, "modules"))

from batch_edit_text import run

SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.normpath(os.path.join(PWD, "..", "..", ".."))))

# TODO, move to config file
SOURCE_DIRS = (
    "source",
    "intern/ghost",
)

SOURCE_EXT = (
    # C/C++
    ".c", ".h", ".cpp", ".hpp", ".cc", ".hh", ".cxx", ".hxx", ".inl",
    # Objective C
    ".m", ".mm",
)

re_words = re.compile("[A-Za-z_][A-Za-z_0-9]*")
re_match_struct = re.compile(r"struct\s+([A-Za-z_][A-Za-z_0-9]*)\s*;")


def clean_structs(fn: str, data_src: str) -> Optional[str]:
    import re

    word_occurance: Dict[str, int] = {}
    for w_match in re_words.finditer(data_src):
        w = w_match.group(0)
        try:
            word_occurance[w] += 1
        except KeyError:
            word_occurance[w] = 1

    lines = data_src.splitlines(keepends=True)

    i = 0
    while i < len(lines):
        m = re_match_struct.match(lines[i])
        if m is not None:
            struct_name = m.group(1)
            if word_occurance[struct_name] == 1:
                print(struct_name, fn)
                del lines[i]
                i -= 1

        i += 1

    data_dst = "".join(lines)
    if data_src != data_dst:
        return data_dst
    return None


run(
    directories=[os.path.join(SOURCE_DIR, d) for d in SOURCE_DIRS],
    is_text=lambda fn: fn.endswith(SOURCE_EXT),
    text_operation=clean_structs,
    use_multiprocess=False,
)