[lit] Fix handling of per test timeout when the installed psutil version
[oota-llvm.git] / utils / sort_includes.py
index 6f20cb76e4831c52e6b3e69858a779050a7dddb3..70bfdedfc6d32ad1572bae3c80a96e64955e1248 100755 (executable)
@@ -10,59 +10,73 @@ welcome for more functionality, and sorting other header groups.
 
 import argparse
 import os
-import re
-import sys
-import tempfile
 
 def sort_includes(f):
+  """Sort the #include lines of a specific file."""
+
+  # Skip files which are under INPUTS trees or test trees.
+  if 'INPUTS/' in f.name or 'test/' in f.name:
+    return
+
+  ext = os.path.splitext(f.name)[1]
+  if ext not in ['.cpp', '.c', '.h', '.inc', '.def']:
+    return
+
   lines = f.readlines()
-  look_for_api_header = f.name[-4:] == '.cpp'
+  look_for_api_header = ext in ['.cpp', '.c']
+  found_headers = False
   headers_begin = 0
   headers_end = 0
   api_headers = []
   local_headers = []
-  project_headers = []
+  subproject_headers = []
+  llvm_headers = []
   system_headers = []
   for (i, l) in enumerate(lines):
     if l.strip() == '':
       continue
     if l.startswith('#include'):
-      if headers_begin == 0:
+      if not found_headers:
         headers_begin = i
+        found_headers = True
       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
-      if header.startswith('<'):
+      if (header.startswith('<') or header.startswith('"gtest/') or
+          header.startswith('"isl/') or header.startswith('"json/')):
         system_headers.append(header)
         continue
-      if header.startswith('"llvm/') or header.startswith('"clang/'):
-        project_headers.append(header)
+      if (header.startswith('"clang/') or header.startswith('"clang-c/') or
+          header.startswith('"polly/')):
+        subproject_headers.append(header)
+        continue
+      if (header.startswith('"llvm/') or header.startswith('"llvm-c/')):
+        llvm_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.
-    if headers_begin != 0:
+    if found_headers:
       break
-    if l.startswith('//') or l.startswith('#define'):
+    if l.startswith('//') or l.startswith('#define') or l.startswith('#ifndef'):
       continue
     break
-  if headers_begin == 0:
+  if not found_headers:
     return
 
-  local_headers.sort()
-  project_headers.sort()
-  system_headers.sort()
-  headers = api_headers + local_headers + project_headers + system_headers
+  local_headers = sorted(set(local_headers))
+  subproject_headers = sorted(set(subproject_headers))
+  llvm_headers = sorted(set(llvm_headers))
+  system_headers = sorted(set(system_headers))
+  headers = api_headers + local_headers + subproject_headers + llvm_headers + system_headers
   header_lines = ['#include ' + h for h in headers]
   lines = lines[:headers_begin] + header_lines + lines[headers_end + 1:]
 
-  #for l in lines[headers_begin:headers_end]:
-  #  print l.rstrip()
   f.seek(0)
   f.truncate()
   f.writelines(lines)