2012-12-03 14:23:44 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""Script to sort the top-most block of #include lines.
|
|
|
|
|
|
|
|
Assumes the LLVM coding conventions.
|
|
|
|
|
|
|
|
Currently, this script only bothers sorting the llvm/... headers. Patches
|
|
|
|
welcome for more functionality, and sorting other header groups.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
|
|
|
|
def sort_includes(f):
|
2012-12-04 07:04:58 +00:00
|
|
|
"""Sort the #include lines of a specific file."""
|
2012-12-04 09:59:54 +00:00
|
|
|
|
|
|
|
# Skip files which are under INPUTS trees or test trees.
|
|
|
|
if 'INPUTS/' in f.name or 'test/' in f.name:
|
|
|
|
return
|
|
|
|
|
2012-12-04 10:08:59 +00:00
|
|
|
ext = os.path.splitext(f.name)[1]
|
|
|
|
if ext not in ['.cpp', '.c', '.h', '.inc', '.def']:
|
|
|
|
return
|
|
|
|
|
2012-12-03 14:23:44 +00:00
|
|
|
lines = f.readlines()
|
2012-12-04 10:08:59 +00:00
|
|
|
look_for_api_header = ext in ['.cpp', '.c']
|
2012-12-04 07:04:58 +00:00
|
|
|
found_headers = False
|
2012-12-03 14:23:44 +00:00
|
|
|
headers_begin = 0
|
|
|
|
headers_end = 0
|
|
|
|
api_headers = []
|
|
|
|
local_headers = []
|
|
|
|
project_headers = []
|
|
|
|
system_headers = []
|
|
|
|
for (i, l) in enumerate(lines):
|
|
|
|
if l.strip() == '':
|
|
|
|
continue
|
|
|
|
if l.startswith('#include'):
|
2012-12-04 07:04:58 +00:00
|
|
|
if not found_headers:
|
2012-12-03 14:23:44 +00:00
|
|
|
headers_begin = i
|
2012-12-04 07:04:58 +00:00
|
|
|
found_headers = True
|
2012-12-03 14:23:44 +00:00
|
|
|
headers_end = i
|
|
|
|
header = l[len('#include'):].lstrip()
|
|
|
|
if look_for_api_header and header.startswith('"'):
|
|
|
|
api_headers.append(header)
|
|
|
|
look_for_api_header = False
|
|
|
|
continue
|
2012-12-04 09:44:38 +00:00
|
|
|
if header.startswith('<') or header.startswith('"gtest/'):
|
2012-12-03 14:23:44 +00:00
|
|
|
system_headers.append(header)
|
|
|
|
continue
|
2012-12-04 07:04:58 +00:00
|
|
|
if (header.startswith('"llvm/') or header.startswith('"llvm-c/') or
|
|
|
|
header.startswith('"clang/') or header.startswith('"clang-c/')):
|
2012-12-03 14:23:44 +00:00
|
|
|
project_headers.append(header)
|
|
|
|
continue
|
|
|
|
local_headers.append(header)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Only allow comments and #defines prior to any includes. If either are
|
|
|
|
# mixed with includes, the order might be sensitive.
|
2012-12-04 07:04:58 +00:00
|
|
|
if found_headers:
|
2012-12-03 14:23:44 +00:00
|
|
|
break
|
2012-12-03 17:01:46 +00:00
|
|
|
if l.startswith('//') or l.startswith('#define') or l.startswith('#ifndef'):
|
2012-12-03 14:23:44 +00:00
|
|
|
continue
|
|
|
|
break
|
2012-12-04 07:04:58 +00:00
|
|
|
if not found_headers:
|
2012-12-03 14:23:44 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
local_headers.sort()
|
|
|
|
project_headers.sort()
|
|
|
|
system_headers.sort()
|
|
|
|
headers = api_headers + local_headers + project_headers + system_headers
|
|
|
|
header_lines = ['#include ' + h for h in headers]
|
|
|
|
lines = lines[:headers_begin] + header_lines + lines[headers_end + 1:]
|
|
|
|
|
|
|
|
f.seek(0)
|
|
|
|
f.truncate()
|
|
|
|
f.writelines(lines)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
parser.add_argument('files', nargs='+', type=argparse.FileType('r+'),
|
|
|
|
help='the source files to sort includes within')
|
|
|
|
args = parser.parse_args()
|
|
|
|
for f in args.files:
|
|
|
|
sort_includes(f)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|