OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Given a filename as an argument, sort the #include/#imports in that file. | 6 """Given a filename as an argument, sort the #include/#imports in that file. |
7 | 7 |
8 Shows a diff and prompts for confirmation before doing the deed. | 8 Shows a diff and prompts for confirmation before doing the deed. |
9 Works great with tools/git/for-all-touched-files.py. | 9 Works great with tools/git/for-all-touched-files.py. |
10 """ | 10 """ |
(...skipping 19 matching lines...) Expand all Loading... |
30 finally: | 30 finally: |
31 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | 31 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) |
32 print ch | 32 print ch |
33 return ch in ('Y', 'y') | 33 return ch in ('Y', 'y') |
34 | 34 |
35 | 35 |
36 def IncludeCompareKey(line): | 36 def IncludeCompareKey(line): |
37 """Sorting comparator key used for comparing two #include lines. | 37 """Sorting comparator key used for comparing two #include lines. |
38 Returns the filename without the #include/#import prefix. | 38 Returns the filename without the #include/#import prefix. |
39 """ | 39 """ |
40 line = line.lower() | |
41 for prefix in ('#include ', '#import '): | 40 for prefix in ('#include ', '#import '): |
42 if line.startswith(prefix): | 41 if line.startswith(prefix): |
43 line = line[len(prefix):] | 42 line = line[len(prefix):] |
44 break | 43 break |
45 | 44 |
46 # The win32 api has all sorts of implicit include order dependencies :-/ | 45 # The win32 api has all sorts of implicit include order dependencies :-/ |
47 # Give a few headers special sort keys that make sure they appear before all | 46 # Give a few headers special sort keys that make sure they appear before all |
48 # other headers. | 47 # other headers. |
49 if line.startswith('<windows.h>'): # Must be before e.g. shellapi.h | 48 if line.startswith('<windows.h>'): # Must be before e.g. shellapi.h |
50 return '0' | 49 return '0' |
51 if line.startswith('<atlbase.h>'): # Must be before atlapp.h. | 50 if line.startswith('<atlbase.h>'): # Must be before atlapp.h. |
52 return '1' + line | 51 return '1' + line |
53 if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h | 52 if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h |
54 return '1' + line | 53 return '1' + line |
55 | 54 |
56 # C++ system headers should come after C system headers. | 55 # C++ system headers should come after C system headers. |
57 if line.startswith('<'): | 56 if line.startswith('<'): |
58 if line.find('.h>') != -1: | 57 if line.find('.h>') != -1: |
59 return '2' + line | 58 return '2' + line.lower() |
60 else: | 59 else: |
61 return '3' + line | 60 return '3' + line.lower() |
62 | 61 |
63 return '4' + line | 62 return '4' + line |
64 | 63 |
65 | 64 |
66 def IsInclude(line): | 65 def IsInclude(line): |
67 """Returns True if the line is an #include/#import line.""" | 66 """Returns True if the line is an #include/#import line.""" |
68 return line.startswith('#include ') or line.startswith('#import ') | 67 return line.startswith('#include ') or line.startswith('#import ') |
69 | 68 |
70 | 69 |
71 def SortHeader(infile, outfile): | 70 def SortHeader(infile, outfile): |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 if len(filenames) < 1: | 135 if len(filenames) < 1: |
137 parser.print_help() | 136 parser.print_help() |
138 return 1 | 137 return 1 |
139 | 138 |
140 for filename in filenames: | 139 for filename in filenames: |
141 DiffAndConfirm(filename, opts.should_confirm) | 140 DiffAndConfirm(filename, opts.should_confirm) |
142 | 141 |
143 | 142 |
144 if __name__ == '__main__': | 143 if __name__ == '__main__': |
145 sys.exit(main()) | 144 sys.exit(main()) |
OLD | NEW |