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.
19 look_for_api_header = f.name[-4:] == '.cpp'
26 for (i, l) in enumerate(lines):
29 if l.startswith('#include'):
30 if headers_begin == 0:
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('"clang/'):
42 project_headers.append(header)
44 local_headers.append(header)
47 # Only allow comments and #defines prior to any includes. If either are
48 # mixed with includes, the order might be sensitive.
49 if headers_begin != 0:
51 if l.startswith('//') or l.startswith('#define'):
54 if headers_begin == 0:
58 project_headers.sort()
60 headers = api_headers + local_headers + project_headers + system_headers
61 header_lines = ['#include ' + h for h in headers]
62 lines = lines[:headers_begin] + header_lines + lines[headers_end + 1:]
64 #for l in lines[headers_begin:headers_end]:
71 parser = argparse.ArgumentParser(description=__doc__)
72 parser.add_argument('files', nargs='+', type=argparse.FileType('r+'),
73 help='the source files to sort includes within')
74 args = parser.parse_args()
78 if __name__ == '__main__':