File: generate_iwyu_mapping.py

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (116 lines) | stat: -rw-r--r-- 3,218 bytes parent folder | download | duplicates (3)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python

import os, pathlib, sys


def generate(private, public):
    return f'{{ include: [ "{private}", "private", "<{public}>", "public" ] }}'


def panic(file):
    print(f"========== {__file__} error ==========", file=sys.stderr)
    print(
        f"\tFile '{file}' is a top-level detail header without a mapping",
        file=sys.stderr,
    )
    sys.exit(1)


def generate_map(include):
    detail_files = []
    detail_directories = []
    c_headers = []

    for i in include.iterdir():
        if i.is_dir() and i.name.startswith("__"):
            detail_directories.append(f"{i.name}")
            continue

        if i.name.startswith("__"):
            detail_files.append(i.name)
            continue

        if i.name.endswith(".h"):
            c_headers.append(i.name)

    result = []
    temporary_mappings = {"__locale_dir": "locale"}
    for i in detail_directories:
        public_header = temporary_mappings.get(i, i.lstrip("_"))
        result.append(f'{generate(f"@<{i}/.*>", public_header)},')

    for i in detail_files:
        public = []
        if i == "__assert":
            continue
        elif i == "__availability":
            continue
        elif i == "__bit_reference":
            continue
        elif i == "__bits":
            public = ["bits"]
        elif i == "__config_site.in":
            continue
        elif i == "__config":
            continue
        elif i == "__errc":
            continue
        elif i == "__hash_table":
            public = ["unordered_map", "unordered_set"]
        elif i == "__locale":
            public = ["locale"]
        elif i == "__mbstate_t.h":
            continue
        elif i == "__mutex_base":
            continue
        elif i == "__node_handle":
            public = ["map", "set", "unordered_map", "unordered_set"]
        elif i == "__pstl_algorithm":
            continue
        elif i == "__pstl_config_site.in":
            continue
        elif i == "__pstl_execution":
            continue
        elif i == "__pstl_memory":
            continue
        elif i == "__pstl_numeric":
            continue
        elif i == "__split_buffer":
            public = ["deque", "vector"]
        elif i == "__std_clang_module":
            continue
        elif i == "__std_mbstate_t.h":
            continue
        elif i == "__threading_support":
            public = ["atomic", "mutex", "semaphore", "thread"]
        elif i == "__tree":
            public = ["map", "set"]
        elif i == "__undef_macros":
            continue
        elif i == "__verbose_abort":
            continue
        else:
            panic(i)

        for p in public:
            result.append(f'{generate(f"<{i}>", p)},')

    result.sort()
    return result


def main():
    monorepo_root = pathlib.Path(
        os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    )
    assert monorepo_root.exists()
    include = pathlib.Path(os.path.join(monorepo_root, "libcxx", "include"))

    mapping = generate_map(include)
    data = "[\n  " + "\n  ".join(mapping) + "\n]\n"
    with open(f"{include}/libcxx.imp", "w") as f:
        f.write(data)


if __name__ == "__main__":
    main()