sort_includes.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. """Script to sort the top-most block of #include lines.
  3. Assumes the LLVM coding conventions.
  4. Currently, this script only bothers sorting the llvm/... headers. Patches
  5. welcome for more functionality, and sorting other header groups.
  6. """
  7. import argparse
  8. import os
  9. def sort_includes(f):
  10. """Sort the #include lines of a specific file."""
  11. # Skip files which are under INPUTS trees or test trees.
  12. if 'INPUTS/' in f.name or 'test/' in f.name:
  13. return
  14. ext = os.path.splitext(f.name)[1]
  15. if ext not in ['.cpp', '.c', '.h', '.inc', '.def']:
  16. return
  17. lines = f.readlines()
  18. look_for_api_header = ext in ['.cpp', '.c']
  19. found_headers = False
  20. headers_begin = 0
  21. headers_end = 0
  22. api_headers = []
  23. local_headers = []
  24. subproject_headers = []
  25. llvm_headers = []
  26. system_headers = []
  27. for (i, l) in enumerate(lines):
  28. if l.strip() == '':
  29. continue
  30. if l.startswith('#include'):
  31. if not found_headers:
  32. headers_begin = i
  33. found_headers = True
  34. headers_end = i
  35. header = l[len('#include'):].lstrip()
  36. if look_for_api_header and header.startswith('"'):
  37. api_headers.append(header)
  38. look_for_api_header = False
  39. continue
  40. if (header.startswith('<') or header.startswith('"gtest/') or
  41. header.startswith('"isl/') or header.startswith('"json/')):
  42. system_headers.append(header)
  43. continue
  44. if (header.startswith('"clang/') or header.startswith('"clang-c/') or
  45. header.startswith('"polly/')):
  46. subproject_headers.append(header)
  47. continue
  48. if (header.startswith('"llvm/') or header.startswith('"llvm-c/')):
  49. llvm_headers.append(header)
  50. continue
  51. local_headers.append(header)
  52. continue
  53. # Only allow comments and #defines prior to any includes. If either are
  54. # mixed with includes, the order might be sensitive.
  55. if found_headers:
  56. break
  57. if l.startswith('//') or l.startswith('#define') or l.startswith('#ifndef'):
  58. continue
  59. break
  60. if not found_headers:
  61. return
  62. local_headers = sorted(set(local_headers))
  63. subproject_headers = sorted(set(subproject_headers))
  64. llvm_headers = sorted(set(llvm_headers))
  65. system_headers = sorted(set(system_headers))
  66. headers = api_headers + local_headers + subproject_headers + llvm_headers + system_headers
  67. header_lines = ['#include ' + h for h in headers]
  68. lines = lines[:headers_begin] + header_lines + lines[headers_end + 1:]
  69. f.seek(0)
  70. f.truncate()
  71. f.writelines(lines)
  72. def main():
  73. parser = argparse.ArgumentParser(description=__doc__)
  74. parser.add_argument('files', nargs='+', type=argparse.FileType('r+'),
  75. help='the source files to sort includes within')
  76. args = parser.parse_args()
  77. for f in args.files:
  78. sort_includes(f)
  79. if __name__ == '__main__':
  80. main()