[Orc] Reapply r234815, outputting via stdout instead.
[oota-llvm.git] / utils / sort_includes.py
index 9812cf1eff7a19b1c5e25df22dd0b52b76b43692..fef97550db8d8f84ec5e012246ecbf2ca5d88424 100755 (executable)
@@ -13,8 +13,17 @@ import os
 
 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 = os.path.splitext(f.name)[1] == '.cpp'
+  look_for_api_header = ext in ['.cpp', '.c']
   found_headers = False
   headers_begin = 0
   headers_end = 0
@@ -35,7 +44,7 @@ def sort_includes(f):
         api_headers.append(header)
         look_for_api_header = False
         continue
-      if header.startswith('<'):
+      if header.startswith('<') or header.startswith('"gtest/'):
         system_headers.append(header)
         continue
       if (header.startswith('"llvm/') or header.startswith('"llvm-c/') or
@@ -55,9 +64,9 @@ def sort_includes(f):
   if not found_headers:
     return
 
-  local_headers.sort()
-  project_headers.sort()
-  system_headers.sort()
+  local_headers = sorted(set(local_headers))
+  project_headers = sorted(set(project_headers))
+  system_headers = sorted(set(system_headers))
   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:]