[llvm-commits] [llvm] r169222 - /llvm/trunk/utils/sort_includes.py

Sean Silva silvas at purdue.edu
Tue Dec 4 00:09:26 PST 2012


Not that it's a big deal, but the "official" coding convention for
Python <http://www.python.org/dev/peps/pep-0008/> uses 4-spaces. With
significant whitespace I find that something a bit stronger than 2
spaces is needed for keeping track of the start/end of blocks.

Also you should be able to simplify the for-loop to

    for i, l in enumerate(f):

since iterating over a file automatically iterates over its lines.

btw +1 for using argparse.

-- Sean Silva

On Tue, Dec 4, 2012 at 2:04 AM, Chandler Carruth <chandlerc at gmail.com> wrote:
> Author: chandlerc
> Date: Tue Dec  4 01:04:58 2012
> New Revision: 169222
>
> URL: http://llvm.org/viewvc/llvm-project?rev=169222&view=rev
> Log:
> Address review comments from Matt on the sort_includes.py script.
>
> 1) Teach it to handle files with #include on the first line -- these do
>    actually exist in LLVM.
> 2) Support llvm-c and clang-c include projects.
> 3) Nuke some stail imports.
> 4) Switch to using os.path to split the file extension off.
> 5) Remove debugging leftovers.
> 6) Add docstring (a really puny one) for the sort function.
>
> I'm continuing te avoid stripping the whitespace on the RHS to preserve
> whatever newline characters happen to be in the original file.
>
> Modified:
>     llvm/trunk/utils/sort_includes.py
>
> Modified: llvm/trunk/utils/sort_includes.py
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/sort_includes.py?rev=169222&r1=169221&r2=169222&view=diff
> ==============================================================================
> --- llvm/trunk/utils/sort_includes.py (original)
> +++ llvm/trunk/utils/sort_includes.py Tue Dec  4 01:04:58 2012
> @@ -10,13 +10,12 @@
>
>  import argparse
>  import os
> -import re
> -import sys
> -import tempfile
>
>  def sort_includes(f):
> +  """Sort the #include lines of a specific file."""
>    lines = f.readlines()
> -  look_for_api_header = f.name[-4:] == '.cpp'
> +  look_for_api_header = os.path.splitext(f.name)[1] == '.cpp'
> +  found_headers = False
>    headers_begin = 0
>    headers_end = 0
>    api_headers = []
> @@ -27,8 +26,9 @@
>      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('"'):
> @@ -38,7 +38,8 @@
>        if header.startswith('<'):
>          system_headers.append(header)
>          continue
> -      if header.startswith('"llvm/') or header.startswith('"clang/'):
> +      if (header.startswith('"llvm/') or header.startswith('"llvm-c/') or
> +          header.startswith('"clang/') or header.startswith('"clang-c/')):
>          project_headers.append(header)
>          continue
>        local_headers.append(header)
> @@ -46,12 +47,12 @@
>
>      # 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') or l.startswith('#ifndef'):
>        continue
>      break
> -  if headers_begin == 0:
> +  if not found_headers:
>      return
>
>    local_headers.sort()
> @@ -61,8 +62,6 @@
>    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)
>
>
> _______________________________________________
> 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