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.
15 """Sort the #include lines of a specific file."""
17 look_for_api_header = os.path.splitext(f.name)[1] == '.cpp'
25 for (i, l) in enumerate(lines):
28 if l.startswith('#include'):
33 header = l[len('#include'):].lstrip()
34 if look_for_api_header and header.startswith('"'):
35 api_headers.append(header)
36 look_for_api_header = False
38 if header.startswith('<'):
39 system_headers.append(header)
41 if (header.startswith('"llvm/') or header.startswith('"llvm-c/') or
42 header.startswith('"clang/') or header.startswith('"clang-c/')):
43 project_headers.append(header)
45 local_headers.append(header)
48 # Only allow comments and #defines prior to any includes. If either are
49 # mixed with includes, the order might be sensitive.
52 if l.startswith('//') or l.startswith('#define') or l.startswith('#ifndef'):
59 project_headers.sort()
61 headers = api_headers + local_headers + project_headers + system_headers
62 header_lines = ['#include ' + h for h in headers]
63 lines = lines[:headers_begin] + header_lines + lines[headers_end + 1:]
70 parser = argparse.ArgumentParser(description=__doc__)
71 parser.add_argument('files', nargs='+', type=argparse.FileType('r+'),
72 help='the source files to sort includes within')
73 args = parser.parse_args()
77 if __name__ == '__main__':