blob: 70bfdedfc6d32ad1572bae3c80a96e64955e1248 [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 Carruth2aa2a9b2012-12-04 09:59:54 +000016
17 # Skip files which are under INPUTS trees or test trees.
18 if 'INPUTS/' in f.name or 'test/' in f.name:
19 return
20
Chandler Carruthc5c675d2012-12-04 10:08:59 +000021 ext = os.path.splitext(f.name)[1]
22 if ext not in ['.cpp', '.c', '.h', '.inc', '.def']:
23 return
24
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000025 lines = f.readlines()
Chandler Carruthc5c675d2012-12-04 10:08:59 +000026 look_for_api_header = ext in ['.cpp', '.c']
Chandler Carruth43342d52012-12-04 07:04:58 +000027 found_headers = False
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000028 headers_begin = 0
29 headers_end = 0
30 api_headers = []
31 local_headers = []
Tobias Grossere7f512a2015-05-09 09:08:56 +000032 subproject_headers = []
33 llvm_headers = []
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000034 system_headers = []
35 for (i, l) in enumerate(lines):
36 if l.strip() == '':
37 continue
38 if l.startswith('#include'):
Chandler Carruth43342d52012-12-04 07:04:58 +000039 if not found_headers:
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000040 headers_begin = i
Chandler Carruth43342d52012-12-04 07:04:58 +000041 found_headers = True
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000042 headers_end = i
43 header = l[len('#include'):].lstrip()
44 if look_for_api_header and header.startswith('"'):
45 api_headers.append(header)
46 look_for_api_header = False
47 continue
Tobias Grossere7f512a2015-05-09 09:08:56 +000048 if (header.startswith('<') or header.startswith('"gtest/') or
49 header.startswith('"isl/') or header.startswith('"json/')):
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000050 system_headers.append(header)
51 continue
Tobias Grossere7f512a2015-05-09 09:08:56 +000052 if (header.startswith('"clang/') or header.startswith('"clang-c/') or
53 header.startswith('"polly/')):
54 subproject_headers.append(header)
55 continue
56 if (header.startswith('"llvm/') or header.startswith('"llvm-c/')):
57 llvm_headers.append(header)
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000058 continue
59 local_headers.append(header)
60 continue
61
62 # Only allow comments and #defines prior to any includes. If either are
63 # mixed with includes, the order might be sensitive.
Chandler Carruth43342d52012-12-04 07:04:58 +000064 if found_headers:
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000065 break
Chandler Carruth6a451d02012-12-03 17:01:46 +000066 if l.startswith('//') or l.startswith('#define') or l.startswith('#ifndef'):
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000067 continue
68 break
Chandler Carruth43342d52012-12-04 07:04:58 +000069 if not found_headers:
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000070 return
71
Benjamin Kramerd0eb3922012-12-21 18:00:08 +000072 local_headers = sorted(set(local_headers))
Tobias Grossere7f512a2015-05-09 09:08:56 +000073 subproject_headers = sorted(set(subproject_headers))
74 llvm_headers = sorted(set(llvm_headers))
Benjamin Kramerd0eb3922012-12-21 18:00:08 +000075 system_headers = sorted(set(system_headers))
Tobias Grossere7f512a2015-05-09 09:08:56 +000076 headers = api_headers + local_headers + subproject_headers + llvm_headers + system_headers
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000077 header_lines = ['#include ' + h for h in headers]
78 lines = lines[:headers_begin] + header_lines + lines[headers_end + 1:]
79
Chandler Carruthcba0f3d2012-12-03 14:23:44 +000080 f.seek(0)
81 f.truncate()
82 f.writelines(lines)
83
84def main():
85 parser = argparse.ArgumentParser(description=__doc__)
86 parser.add_argument('files', nargs='+', type=argparse.FileType('r+'),
87 help='the source files to sort includes within')
88 args = parser.parse_args()
89 for f in args.files:
90 sort_includes(f)
91
92if __name__ == '__main__':
93 main()