Bump version to 19.1.0 (final)
[llvm-project.git] / libcxx / utils / generate_iwyu_mapping.py
blobb0ebe1b9e93d26f0decf6b93559f1b1684af302d
1 #!/usr/bin/env python
3 import argparse
4 import libcxx.header_information
5 import os
6 import pathlib
7 import re
8 import sys
9 import typing
11 def IWYU_mapping(header: str) -> typing.Optional[typing.List[str]]:
12 ignore = [
13 "__debug_utils/.+",
14 "__fwd/get[.]h",
15 "__pstl/.+",
16 "__support/.+",
17 "__utility/private_constructor_tag.h",
19 if any(re.match(pattern, header) for pattern in ignore):
20 return None
21 elif header == "__bits":
22 return ["bits"]
23 elif header in ("__bit_reference", "__fwd/bit_reference.h"):
24 return ["bitset", "vector"]
25 elif re.match("__configuration/.+", header) or header == "__config":
26 return ["version"]
27 elif header == "__hash_table":
28 return ["unordered_map", "unordered_set"]
29 elif header == "__locale":
30 return ["locale"]
31 elif re.match("__locale_dir/.+", header):
32 return ["locale"]
33 elif re.match("__math/.+", header):
34 return ["cmath"]
35 elif header == "__node_handle":
36 return ["map", "set", "unordered_map", "unordered_set"]
37 elif header == "__split_buffer":
38 return ["deque", "vector"]
39 elif re.match("(__thread/support[.]h)|(__thread/support/.+)", header):
40 return ["atomic", "mutex", "semaphore", "thread"]
41 elif header == "__tree":
42 return ["map", "set"]
43 elif header == "__fwd/pair.h":
44 return ["utility"]
45 elif header == "__fwd/subrange.h":
46 return ["ranges"]
47 elif re.match("__fwd/(fstream|ios|istream|ostream|sstream|streambuf)[.]h", header):
48 return ["iosfwd"]
49 # Handle remaining forward declaration headers
50 elif re.match("__fwd/(.+)[.]h", header):
51 return [re.match("__fwd/(.+)[.]h", header).group(1)]
52 # Handle detail headers for things like <__algorithm/foo.h>
53 elif re.match("__(.+?)/.+", header):
54 return [re.match("__(.+?)/.+", header).group(1)]
55 else:
56 return None
59 def main(argv: typing.List[str]):
60 parser = argparse.ArgumentParser()
61 parser.add_argument(
62 "-o",
63 help="File to output the IWYU mappings into",
64 type=argparse.FileType("w"),
65 required=True,
66 dest="output",
68 args = parser.parse_args(argv)
70 mappings = [] # Pairs of (header, public_header)
71 for header in libcxx.header_information.all_headers:
72 public_headers = IWYU_mapping(header)
73 if public_headers is not None:
74 mappings.extend((header, public) for public in public_headers)
76 # Validate that we only have valid public header names -- otherwise the mapping above
77 # needs to be updated.
78 for header, public in mappings:
79 if public not in libcxx.header_information.public_headers:
80 raise RuntimeError(f"{header}: Header {public} is not a valid header")
82 args.output.write("[\n")
83 for header, public in sorted(mappings):
84 args.output.write(
85 f' {{ include: [ "<{header}>", "private", "<{public}>", "public" ] }},\n'
87 args.output.write("]\n")
89 if __name__ == "__main__":
90 main(sys.argv[1:])