[llvm-commits] [llvm] r169125 - /llvm/trunk/utils/sort_includes.py
Matt Beaumont-Gay
matthewbg at google.com
Mon Dec 3 08:54:38 PST 2012
On Mon, Dec 3, 2012 at 6:23 AM, Chandler Carruth <chandlerc at gmail.com> wrote:
> Author: chandlerc
> Date: Mon Dec 3 08:23:44 2012
> New Revision: 169125
>
> URL: http://llvm.org/viewvc/llvm-project?rev=169125&view=rev
> Log:
> Add a completely hack-ish tool to sort includes according to the coding
> standards.
>
> I am a terrible Python programmer. Patches more the welcome. Please tell
> me how this should look if it should look differently. It's just a tiny
> little script so it didn't make sense to go through pre-commit review,
> especially as someone who actually knows python may want to just rip it
> apart and do it The Right Way.
>
> I will be preparing a commit shortly that uses this script to
> canonicalize *all* of the #include lines in LLVM. Really, all of them.
>
> Added:
> llvm/trunk/utils/sort_includes.py (with props)
>
> Added: llvm/trunk/utils/sort_includes.py
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/sort_includes.py?rev=169125&view=auto
> ==============================================================================
> --- llvm/trunk/utils/sort_includes.py (added)
> +++ llvm/trunk/utils/sort_includes.py Mon Dec 3 08:23:44 2012
> @@ -0,0 +1,79 @@
> +#!/usr/bin/env python
> +
> +"""Script to sort the top-most block of #include lines.
> +
> +Assumes the LLVM coding conventions.
> +
> +Currently, this script only bothers sorting the llvm/... headers. Patches
> +welcome for more functionality, and sorting other header groups.
> +"""
> +
> +import argparse
> +import os
> +import re
> +import sys
> +import tempfile
Some of these are unused.
> +
> +def sort_includes(f):
Docstring?
> + lines = f.readlines()
> + look_for_api_header = f.name[-4:] == '.cpp'
os.path.splitext(f.name)[1]
> + headers_begin = 0
> + headers_end = 0
> + api_headers = []
> + local_headers = []
> + project_headers = []
> + system_headers = []
> + for (i, l) in enumerate(lines):
> + if l.strip() == '':
> + continue
> + if l.startswith('#include'):
> + if headers_begin == 0:
> + headers_begin = i
This only works if there isn't a #include on the first line.
(Admittedly, since every file should have a comment block at the top,
it should be fine in practice.)
> + headers_end = i
> + header = l[len('#include'):].lstrip()
Strip trailing whitespace too?
> + if look_for_api_header and header.startswith('"'):
> + api_headers.append(header)
> + look_for_api_header = False
> + continue
> + if header.startswith('<'):
> + system_headers.append(header)
> + continue
> + if header.startswith('"llvm/') or header.startswith('"clang/'):
Do we care about '#include "clang-c/..."'?
> + project_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:
> + break
> + if l.startswith('//') or l.startswith('#define'):
> + continue
> + break
> + if headers_begin == 0:
> + return
> +
> + local_headers.sort()
> + project_headers.sort()
> + system_headers.sort()
> + 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:]
> +
> + #for l in lines[headers_begin:headers_end]:
> + # print l.rstrip()
Debugging leftovers.
> + f.seek(0)
> + f.truncate()
> + f.writelines(lines)
> +
> +def main():
> + parser = argparse.ArgumentParser(description=__doc__)
> + parser.add_argument('files', nargs='+', type=argparse.FileType('r+'),
> + help='the source files to sort includes within')
> + args = parser.parse_args()
> + for f in args.files:
> + sort_includes(f)
> +
> +if __name__ == '__main__':
> + main()
>
> Propchange: llvm/trunk/utils/sort_includes.py
> ------------------------------------------------------------------------------
> svn:executable = *
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list