Chromium Code Reviews| Index: tools/sort-headers.py |
| diff --git a/tools/sort-headers.py b/tools/sort-headers.py |
| index 5e8fc2641f261e502fcb30c64ed80ecc9a4944c9..7d0f90f5e2b9f6c5a328f9c019cb8555416a0993 100755 |
| --- a/tools/sort-headers.py |
| +++ b/tools/sort-headers.py |
| @@ -16,44 +16,52 @@ import sys |
| from yes_no import YesNo |
| -def IncludeCompareKey(line): |
| - """Sorting comparator key used for comparing two #include lines. |
| - Returns the filename without the #include/#import/import prefix. |
| - """ |
| - for prefix in ('#include ', '#import ', 'import '): |
| - if line.startswith(prefix): |
| - line = line[len(prefix):] |
| - break |
| - |
| - # The win32 api has all sorts of implicit include order dependencies :-/ |
| - # Give a few headers special sort keys that make sure they appear before all |
| - # other headers. |
| - if line.startswith('<windows.h>'): # Must be before e.g. shellapi.h |
| - return '0' |
| - if line.startswith('<atlbase.h>'): # Must be before atlapp.h. |
| - return '1' + line |
| - if line.startswith('<ole2.h>'): # Must be before e.g. intshcut.h |
| - return '1' + line |
| - if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h |
| - return '1' + line |
| - |
| - # C++ system headers should come after C system headers. |
| - if line.startswith('<'): |
| - if line.find('.h>') != -1: |
| - return '2' + line.lower() |
| - else: |
| - return '3' + line.lower() |
| - |
| - return '4' + line |
| - |
| - |
| def IsInclude(line): |
| """Returns True if the line is an #include/#import/import line.""" |
| return any([line.startswith('#include '), line.startswith('#import '), |
| line.startswith('import ')]) |
| -def SortHeader(infile, outfile): |
| +def SortHeader(infile, outfile, for_blink): |
| + def IncludeCompareKey(line): |
|
Matt Giuca
2015/07/07 05:20:53
Hmm, not really a fan of nesting this whole functi
benwells
2015/07/13 05:46:31
Done the latter way as it seems more obvious.
|
| + """Sorting comparator key used for comparing two #include lines. |
| + Returns the filename without the #include/#import/import prefix. |
| + """ |
| + for prefix in ('#include ', '#import ', 'import '): |
| + if line.startswith(prefix): |
| + line = line[len(prefix):] |
| + break |
| + |
| + # The win32 api has all sorts of implicit include order dependencies :-/ |
| + # Give a few headers special sort keys that make sure they appear before all |
| + # other headers. |
| + if line.startswith('<windows.h>'): # Must be before e.g. shellapi.h |
| + return '0' |
| + if line.startswith('<atlbase.h>'): # Must be before atlapp.h. |
| + return '1' + line |
| + if line.startswith('<ole2.h>'): # Must be before e.g. intshcut.h |
| + return '1' + line |
| + if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h |
| + return '1' + line |
| + |
| + if for_blink: |
| + # Blink likes to have its "config.h" include first. |
| + if line.startswith('"config.h"'): |
| + return '0' |
|
Matt Giuca
2015/07/07 05:20:53
Won't this conflict with <windows.h>?
benwells
2015/07/13 05:46:31
Yes. From looking at blink code they don't seem to
|
| + |
| + # Blink sorts system headers after others. This is handled by sorting |
| + # alphabetically so no need to do anything tricky. |
| + return '1' + line |
| + |
| + # C++ system headers should come after C system headers. |
| + if line.startswith('<'): |
| + if line.find('.h>') != -1: |
| + return '2' + line.lower() |
| + else: |
| + return '3' + line.lower() |
| + |
| + return '4' + line |
| + |
| """Sorts the headers in infile, writing the sorted file to outfile.""" |
| for line in infile: |
| if IsInclude(line): |
| @@ -79,7 +87,7 @@ def SortHeader(infile, outfile): |
| def FixFileWithConfirmFunction(filename, confirm_function, |
| - perform_safety_checks): |
| + perform_safety_checks, for_blink = False): |
|
Matt Giuca
2015/07/07 05:20:52
No spaces around =.
"for_blink=False"
benwells
2015/07/13 05:46:31
Done.
|
| """Creates a fixed version of the file, invokes |confirm_function| |
| to decide whether to use the new file, and cleans up. |
| @@ -99,7 +107,7 @@ def FixFileWithConfirmFunction(filename, confirm_function, |
| fixfilename = filename + '.new' |
| infile = open(filename, 'rb') |
| outfile = open(fixfilename, 'wb') |
| - SortHeader(infile, outfile) |
| + SortHeader(infile, outfile, for_blink) |
| infile.close() |
| outfile.close() # Important so the below diff gets the updated contents. |
| @@ -116,7 +124,7 @@ def FixFileWithConfirmFunction(filename, confirm_function, |
| pass |
| -def DiffAndConfirm(filename, should_confirm, perform_safety_checks): |
| +def DiffAndConfirm(filename, should_confirm, perform_safety_checks, for_blink): |
| """Shows a diff of what the tool would change the file named |
| filename to. Shows a confirmation prompt if should_confirm is true. |
| Saves the resulting file if should_confirm is false or the user |
| @@ -132,7 +140,8 @@ def DiffAndConfirm(filename, should_confirm, perform_safety_checks): |
| return (not should_confirm or YesNo('Use new file (y/N)?')) |
| - FixFileWithConfirmFunction(filename, ConfirmFunction, perform_safety_checks) |
| + FixFileWithConfirmFunction(filename, ConfirmFunction, perform_safety_checks, |
| + for_blink) |
| def IsUnsafeToReorderHeaders(filename): |
| # *_message_generator.cc is almost certainly a file that generates IPC |
| @@ -153,6 +162,9 @@ def main(): |
| help='Do not perform the safety checks via which this ' |
| 'script refuses to operate on files for which it thinks ' |
| 'the include ordering is semantically significant.') |
| + parser.add_option('--for_blink', action='store_true', default=False, |
| + dest='for_blink', help='Whether the blink header sorting ' |
| + 'rules should be applied.') |
| opts, filenames = parser.parse_args() |
| if len(filenames) < 1: |
| @@ -160,7 +172,8 @@ def main(): |
| return 1 |
| for filename in filenames: |
| - DiffAndConfirm(filename, opts.should_confirm, opts.perform_safety_checks) |
| + DiffAndConfirm(filename, opts.should_confirm, opts.perform_safety_checks, |
| + opts.for_blink) |
| if __name__ == '__main__': |