blob: 855cb38ad594c4f5829a6f2512c8c3037d966931 [file] [log] [blame]
Chandler Carruthcba0f3d2012-12-03 14:23:44 +00001#!/usr/bin/env python
2
3"""Script to sort the top-most block of #include lines.
4
5Assumes the LLVM coding conventions.
6
7Currently, this script only bothers sorting the llvm/... headers. Patches
8welcome for more functionality, and sorting other header groups.
9"""
10
11import argparse
12import os
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000013
14def sort_includes(f):
Chandler Carruth43342d52012-12-04 07:04:58 +000015 """Sort the #include lines of a specific file."""
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000016 lines = f.readlines()
Chandler Carruth43342d52012-12-04 07:04:58 +000017 look_for_api_header = os.path.splitext(f.name)[1] == '.cpp'
18 found_headers = False
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000019 headers_begin = 0
20 headers_end = 0
21 api_headers = []
22 local_headers = []
23 project_headers = []
24 system_headers = []
25 for (i, l) in enumerate(lines):
26 if l.strip() == '':
27 continue
28 if l.startswith('#include'):
Chandler Carruth43342d52012-12-04 07:04:58 +000029 if not found_headers:
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000030 headers_begin = i
Chandler Carruth43342d52012-12-04 07:04:58 +000031 found_headers = True
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000032 headers_end = i
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
37 continue
Chandler Carruthed09f452012-12-04 09:44:38 +000038 if header.startswith('<') or header.startswith('"gtest/'):
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000039 system_headers.append(header)
40 continue
Chandler Carruth43342d52012-12-04 07:04:58 +000041 if (header.startswith('"llvm/') or header.startswith('"llvm-c/') or
42 header.startswith('"clang/') or header.startswith('"clang-c/')):
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000043 project_headers.append(header)
44 continue
45 local_headers.append(header)
46 continue
47
48 # Only allow comments and #defines prior to any includes. If either are
49 # mixed with includes, the order might be sensitive.
Chandler Carruth43342d52012-12-04 07:04:58 +000050 if found_headers:
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000051 break
Chandler Carruth6a451d02012-12-03 17:01:46 +000052 if l.startswith('//') or l.startswith('#define') or l.startswith('#ifndef'):
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000053 continue
54 break
Chandler Carruth43342d52012-12-04 07:04:58 +000055 if not found_headers:
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000056 return
57
58 local_headers.sort()
59 project_headers.sort()
60 system_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:]
64
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000065 f.seek(0)
66 f.truncate()
67 f.writelines(lines)
68
69def main():
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()
74 for f in args.files:
75 sort_includes(f)
76
77if __name__ == '__main__':
78 main()