omtcyfz created this revision.
omtcyfz added reviewers: alexfh, ioeric, bkramer.
omtcyfz added subscribers: cfe-commits, aaron.ballman.

`add_new_check.py` didn't support Python 3.X for some reason, even though it 
used `print` in Python 3.X-compatible way. With just a few tweaks 
`add_new_check.py` supports both Python 2.7.X and Python 3.X.

Tested: Python 2.7.6, Python 2.7.12, Python 3.5.2.

https://reviews.llvm.org/D25019

Files:
  clang-tidy/add_new_check.py

Index: clang-tidy/add_new_check.py
===================================================================
--- clang-tidy/add_new_check.py
+++ clang-tidy/add_new_check.py
@@ -13,12 +13,14 @@
 import re
 import sys
 
+from six.moves import filter
+
 
 # Adapts the module's CMakelist file. Returns 'True' if it could add a new entry
 # and 'False' if the entry already existed.
 def adapt_cmake(module_path, check_name_camel):
   filename = os.path.join(module_path, 'CMakeLists.txt')
-  with open(filename, 'r') as f:
+  with open(filename, 'rb') as f:
     lines = f.readlines()
 
   cpp_file = check_name_camel + '.cpp'
@@ -52,12 +54,12 @@
   with open(filename, 'wb') as f:
     header_guard = ('LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_' + module.upper() + '_'
                     + check_name.upper().replace('-', '_') + '_H')
-    f.write('//===--- ')
-    f.write(os.path.basename(filename))
-    f.write(' - clang-tidy')
-    f.write('-' * max(0, 43 - len(os.path.basename(filename))))
-    f.write('*- C++ -*-===//')
-    f.write("""
+    f.write(b'//===--- ')
+    f.write((os.path.basename(filename)).encode())
+    f.write(b' - clang-tidy')
+    f.write(b'-' * max(0, 43 - len(os.path.basename(filename))))
+    f.write(b'*- C++ -*-===//')
+    f.write(b"""
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -92,23 +94,23 @@
 } // namespace clang
 
 #endif // %(header_guard)s
-""" % {'header_guard': header_guard,
-       'check_name': check_name_camel,
-       'check_name_dashes': check_name_dashes,
-       'module': module})
+""" % {b'header_guard': header_guard.encode(),
+       b'check_name': check_name_camel.encode(),
+       b'check_name_dashes': check_name_dashes.encode(),
+       b'module': module.encode()})
 
 
 # Adds the implementation of the new check.
 def write_implementation(module_path, module, check_name_camel):
   filename = os.path.join(module_path, check_name_camel) + '.cpp'
   print('Creating %s...' % filename)
   with open(filename, 'wb') as f:
-    f.write('//===--- ')
-    f.write(os.path.basename(filename))
-    f.write(' - clang-tidy')
-    f.write('-' * max(0, 52 - len(os.path.basename(filename))))
-    f.write('-===//')
-    f.write("""
+    f.write(b'//===--- ')
+    f.write((os.path.basename(filename)).encode())
+    f.write(b' - clang-tidy')
+    f.write(b'-' * max(0, 52 - len(os.path.basename(filename))))
+    f.write(b'-===//')
+    f.write(b"""
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -145,16 +147,16 @@
 } // namespace %(module)s
 } // namespace tidy
 } // namespace clang
-""" % {'check_name': check_name_camel,
-       'module': module})
+""" % {b'check_name': check_name_camel.encode(),
+       b'module': module.encode()})
 
 
 # Modifies the module to include the new check.
 def adapt_module(module_path, module, check_name, check_name_camel):
   modulecpp = filter(lambda p: p.lower() == module.lower() + 'tidymodule.cpp',
-                     os.listdir(module_path))[0]
-  filename = os.path.join(module_path, modulecpp)
-  with open(filename, 'r') as f:
+                     os.listdir(module_path))
+  filename = os.path.join(module_path, next(modulecpp))
+  with open(filename, 'rb') as f:
     lines = f.readlines()
 
   print('Updating %s...' % filename)
@@ -196,7 +198,7 @@
                                            check_name_dashes + '.cpp'))
   print('Creating %s...' % filename)
   with open(filename, 'wb') as f:
-    f.write("""// RUN: %%check_clang_tidy %%s %(check_name_dashes)s %%t
+    f.write(b"""// RUN: %%check_clang_tidy %%s %(check_name_dashes)s %%t
 
 // FIXME: Add something that triggers the check here.
 void f();
@@ -210,22 +212,22 @@
 
 // FIXME: Add something that doesn't trigger the check here.
 void awesome_f2();
-""" % {'check_name_dashes': check_name_dashes})
+""" % {b'check_name_dashes': check_name_dashes.encode()})
 
 
 # Recreates the list of checks in the docs/clang-tidy/checks directory.
 def update_checks_list(clang_tidy_path):
   docs_dir = os.path.join(clang_tidy_path, '../docs/clang-tidy/checks')
   filename = os.path.normpath(os.path.join(docs_dir, 'list.rst'))
-  with open(filename, 'r') as f:
+  with open(filename, 'rb') as f:
     lines = f.readlines()
   doc_files = filter(lambda s: s.endswith('.rst') and s != 'list.rst',
                      os.listdir(docs_dir))
-  doc_files.sort()
+  doc_files = sorted(doc_files)
 
   def format_link(doc_file):
     check_name = doc_file.replace('.rst', '')
-    with open(os.path.join(docs_dir, doc_file), 'r') as doc:
+    with open(os.path.join(docs_dir, doc_file), 'rb') as doc:
       match = re.search('.*:http-equiv=refresh: \d+;URL=(.*).html.*',
                         doc.read())
       if match:
@@ -241,7 +243,7 @@
   with open(filename, 'wb') as f:
     for line in lines:
       f.write(line)
-      if line.startswith('.. toctree::'):
+      if line.startswith(b'.. toctree::'):
         f.writelines(checks)
         break
 
@@ -253,37 +255,37 @@
       module_path, '../../docs/clang-tidy/checks/', check_name_dashes + '.rst'))
   print('Creating %s...' % filename)
   with open(filename, 'wb') as f:
-    f.write(""".. title:: clang-tidy - %(check_name_dashes)s
+    f.write(b""".. title:: clang-tidy - %(check_name_dashes)s
 
 %(check_name_dashes)s
 %(underline)s
 
 FIXME: Describe what patterns does the check detect and why. Give examples.
-""" % {'check_name_dashes': check_name_dashes,
-       'underline': '=' * len(check_name_dashes)})
+""" % {b'check_name_dashes': check_name_dashes.encode(),
+       b'underline': b'=' * len(check_name_dashes)})
 
 
 def main():
   if len(sys.argv) == 2 and sys.argv[1] == '--update-docs':
     update_checks_list(os.path.dirname(sys.argv[0]))
     return
 
   if len(sys.argv) != 3:
-    print """\
+    print("""\
 Usage: add_new_check.py <module> <check>, e.g.
   add_new_check.py misc awesome-functions
 
 Alternatively, run 'add_new_check.py --update-docs' to just update the list of
-documentation files."""
+documentation files.""")
 
     return
 
   module = sys.argv[1]
   check_name = sys.argv[2]
 
   if check_name.startswith(module):
-    print 'Check name "%s" must not start with the module "%s". Exiting.' % (
-        check_name, module)
+    print('Check name "%s" must not start with the module "%s". Exiting.' % (
+            check_name, module))
     return
   check_name_camel = ''.join(map(lambda elem: elem.capitalize(),
                                  check_name.split('-'))) + 'Check'
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to