Skip to content

Commit 96d8191

Browse files
committed
[clang-tidy] Enable Python 3 support for add_new_check.py
Summary: In Python 3, filters are lazily evaluated and strings are not bytes. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: xazax.hun, cfe-commits Differential Revision: https://reviews.llvm.org/D44217 llvm-svn: 328418
1 parent f791c96 commit 96d8191

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

clang-tools-extra/clang-tidy/add_new_check.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
#
1010
#===------------------------------------------------------------------------===#
1111

12+
from __future__ import print_function
13+
1214
import argparse
1315
import os
1416
import re
1517
import sys
1618

17-
1819
# Adapts the module's CMakelist file. Returns 'True' if it could add a new entry
1920
# and 'False' if the entry already existed.
2021
def adapt_cmake(module_path, check_name_camel):
@@ -30,7 +31,7 @@ def adapt_cmake(module_path, check_name_camel):
3031
return False
3132

3233
print('Updating %s...' % filename)
33-
with open(filename, 'wb') as f:
34+
with open(filename, 'w') as f:
3435
cpp_found = False
3536
file_added = False
3637
for line in lines:
@@ -50,7 +51,7 @@ def write_header(module_path, module, check_name, check_name_camel):
5051
check_name_dashes = module + '-' + check_name
5152
filename = os.path.join(module_path, check_name_camel) + '.h'
5253
print('Creating %s...' % filename)
53-
with open(filename, 'wb') as f:
54+
with open(filename, 'w') as f:
5455
header_guard = ('LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_' + module.upper() + '_'
5556
+ check_name_camel.upper() + '_H')
5657
f.write('//===--- ')
@@ -103,7 +104,7 @@ class %(check_name)s : public ClangTidyCheck {
103104
def write_implementation(module_path, module, check_name_camel):
104105
filename = os.path.join(module_path, check_name_camel) + '.cpp'
105106
print('Creating %s...' % filename)
106-
with open(filename, 'wb') as f:
107+
with open(filename, 'w') as f:
107108
f.write('//===--- ')
108109
f.write(os.path.basename(filename))
109110
f.write(' - clang-tidy')
@@ -152,14 +153,15 @@ def write_implementation(module_path, module, check_name_camel):
152153

153154
# Modifies the module to include the new check.
154155
def adapt_module(module_path, module, check_name, check_name_camel):
155-
modulecpp = filter(lambda p: p.lower() == module.lower() + 'tidymodule.cpp',
156-
os.listdir(module_path))[0]
156+
modulecpp = list(filter(
157+
lambda p: p.lower() == module.lower() + 'tidymodule.cpp',
158+
os.listdir(module_path)))[0]
157159
filename = os.path.join(module_path, modulecpp)
158160
with open(filename, 'r') as f:
159161
lines = f.readlines()
160162

161163
print('Updating %s...' % filename)
162-
with open(filename, 'wb') as f:
164+
with open(filename, 'w') as f:
163165
header_added = False
164166
header_found = False
165167
check_added = False
@@ -199,7 +201,7 @@ def add_release_notes(module_path, module, check_name):
199201
lines = f.readlines()
200202

201203
print('Updating %s...' % filename)
202-
with open(filename, 'wb') as f:
204+
with open(filename, 'w') as f:
203205
note_added = False
204206
header_found = False
205207

@@ -227,7 +229,7 @@ def write_test(module_path, module, check_name, test_extension):
227229
filename = os.path.normpath(os.path.join(module_path, '../../test/clang-tidy',
228230
check_name_dashes + '.' + test_extension))
229231
print('Creating %s...' % filename)
230-
with open(filename, 'wb') as f:
232+
with open(filename, 'w') as f:
231233
f.write("""// RUN: %%check_clang_tidy %%s %(check_name_dashes)s %%t
232234
233235
// FIXME: Add something that triggers the check here.
@@ -251,8 +253,8 @@ def update_checks_list(clang_tidy_path):
251253
filename = os.path.normpath(os.path.join(docs_dir, 'list.rst'))
252254
with open(filename, 'r') as f:
253255
lines = f.readlines()
254-
doc_files = filter(lambda s: s.endswith('.rst') and s != 'list.rst',
255-
os.listdir(docs_dir))
256+
doc_files = list(filter(lambda s: s.endswith('.rst') and s != 'list.rst',
257+
os.listdir(docs_dir)))
256258
doc_files.sort()
257259

258260
def format_link(doc_file):
@@ -275,7 +277,7 @@ def format_link(doc_file):
275277
checks = map(format_link, doc_files)
276278

277279
print('Updating %s...' % filename)
278-
with open(filename, 'wb') as f:
280+
with open(filename, 'w') as f:
279281
for line in lines:
280282
f.write(line)
281283
if line.startswith('.. toctree::'):
@@ -289,7 +291,7 @@ def write_docs(module_path, module, check_name):
289291
filename = os.path.normpath(os.path.join(
290292
module_path, '../../docs/clang-tidy/checks/', check_name_dashes + '.rst'))
291293
print('Creating %s...' % filename)
292-
with open(filename, 'wb') as f:
294+
with open(filename, 'w') as f:
293295
f.write(""".. title:: clang-tidy - %(check_name_dashes)s
294296
295297
%(check_name_dashes)s
@@ -333,16 +335,16 @@ def main():
333335
return
334336

335337
if not args.module or not args.check:
336-
print 'Module and check must be specified.'
338+
print('Module and check must be specified.')
337339
parser.print_usage()
338340
return
339341

340342
module = args.module
341343
check_name = args.check
342344

343345
if check_name.startswith(module):
344-
print 'Check name "%s" must not start with the module "%s". Exiting.' % (
345-
check_name, module)
346+
print('Check name "%s" must not start with the module "%s". Exiting.' % (
347+
check_name, module))
346348
return
347349
check_name_camel = ''.join(map(lambda elem: elem.capitalize(),
348350
check_name.split('-'))) + 'Check'

0 commit comments

Comments
 (0)