3 """Script to sort the top-most block of #include lines.
5 Assumes the LLVM coding conventions.
7 Currently, this script only bothers sorting the llvm/... headers. Patches
8 welcome for more functionality, and sorting other header groups.
16 """Sort the #include lines of a specific file."""
18 # Skip files which are under INPUTS trees or test trees.
19 if "INPUTS/" in f
.name
or "test/" in f
.name
:
22 ext
= os
.path
.splitext(f
.name
)[1]
23 if ext
not in [".cpp", ".c", ".h", ".inc", ".def"]:
27 look_for_api_header
= ext
in [".cpp", ".c"]
33 subproject_headers
= []
36 for (i
, l
) in enumerate(lines
):
39 if l
.startswith("#include"):
44 header
= l
[len("#include") :].lstrip()
45 if look_for_api_header
and header
.startswith('"'):
46 api_headers
.append(header
)
47 look_for_api_header
= False
50 header
.startswith("<")
51 or header
.startswith('"gtest/')
52 or header
.startswith('"isl/')
53 or header
.startswith('"json/')
55 system_headers
.append(header
)
58 header
.startswith('"clang/')
59 or header
.startswith('"clang-c/')
60 or header
.startswith('"polly/')
62 subproject_headers
.append(header
)
64 if header
.startswith('"llvm/') or header
.startswith('"llvm-c/'):
65 llvm_headers
.append(header
)
67 local_headers
.append(header
)
70 # Only allow comments and #defines prior to any includes. If either are
71 # mixed with includes, the order might be sensitive.
74 if l
.startswith("//") or l
.startswith("#define") or l
.startswith("#ifndef"):
80 local_headers
= sorted(set(local_headers
))
81 subproject_headers
= sorted(set(subproject_headers
))
82 llvm_headers
= sorted(set(llvm_headers
))
83 system_headers
= sorted(set(system_headers
))
85 api_headers
+ local_headers
+ subproject_headers
+ llvm_headers
+ system_headers
87 header_lines
= ["#include " + h
for h
in headers
]
88 lines
= lines
[:headers_begin
] + header_lines
+ lines
[headers_end
+ 1 :]
96 parser
= argparse
.ArgumentParser(description
=__doc__
)
100 type=argparse
.FileType("r+"),
101 help="the source files to sort includes within",
103 args
= parser
.parse_args()
108 if __name__
== "__main__":