2015-10-11 15:58:34 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2020-05-18 18:20:11 +08:00
|
|
|
#===- rename_check.py - clang-tidy check renamer ------------*- python -*--===#
|
2015-10-11 15:58:34 +08:00
|
|
|
#
|
2019-01-19 16:50:56 +08:00
|
|
|
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-10-11 15:58:34 +08:00
|
|
|
#
|
2020-05-18 18:20:11 +08:00
|
|
|
#===-----------------------------------------------------------------------===#
|
2015-10-11 15:58:34 +08:00
|
|
|
|
2016-11-08 19:43:50 +08:00
|
|
|
import argparse
|
2017-11-23 20:08:53 +08:00
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import re
|
2016-11-08 19:43:50 +08:00
|
|
|
|
2015-10-11 15:58:34 +08:00
|
|
|
|
2019-05-11 02:27:09 +08:00
|
|
|
def replaceInFileRegex(fileName, sFrom, sTo):
|
|
|
|
if sFrom == sTo:
|
|
|
|
return
|
|
|
|
txt = None
|
|
|
|
with open(fileName, "r") as f:
|
|
|
|
txt = f.read()
|
|
|
|
|
|
|
|
txt = re.sub(sFrom, sTo, txt)
|
|
|
|
print("Replacing '%s' -> '%s' in '%s'..." % (sFrom, sTo, fileName))
|
|
|
|
with open(fileName, "w") as f:
|
|
|
|
f.write(txt)
|
|
|
|
|
2020-05-18 18:20:11 +08:00
|
|
|
|
2015-10-11 15:58:34 +08:00
|
|
|
def replaceInFile(fileName, sFrom, sTo):
|
|
|
|
if sFrom == sTo:
|
|
|
|
return
|
|
|
|
txt = None
|
|
|
|
with open(fileName, "r") as f:
|
|
|
|
txt = f.read()
|
|
|
|
|
|
|
|
if sFrom not in txt:
|
|
|
|
return
|
|
|
|
|
|
|
|
txt = txt.replace(sFrom, sTo)
|
2017-11-23 20:08:53 +08:00
|
|
|
print("Replacing '%s' -> '%s' in '%s'..." % (sFrom, sTo, fileName))
|
2015-10-11 15:58:34 +08:00
|
|
|
with open(fileName, "w") as f:
|
|
|
|
f.write(txt)
|
|
|
|
|
2016-11-08 19:43:50 +08:00
|
|
|
|
2015-10-11 15:58:34 +08:00
|
|
|
def generateCommentLineHeader(filename):
|
|
|
|
return ''.join(['//===--- ',
|
|
|
|
os.path.basename(filename),
|
|
|
|
' - clang-tidy ',
|
|
|
|
'-' * max(0, 42 - len(os.path.basename(filename))),
|
|
|
|
'*- C++ -*-===//'])
|
|
|
|
|
2016-11-08 19:43:50 +08:00
|
|
|
|
2015-10-11 15:58:34 +08:00
|
|
|
def generateCommentLineSource(filename):
|
|
|
|
return ''.join(['//===--- ',
|
|
|
|
os.path.basename(filename),
|
|
|
|
' - clang-tidy',
|
|
|
|
'-' * max(0, 52 - len(os.path.basename(filename))),
|
|
|
|
'-===//'])
|
|
|
|
|
2016-11-08 19:43:50 +08:00
|
|
|
|
2015-10-11 15:58:34 +08:00
|
|
|
def fileRename(fileName, sFrom, sTo):
|
2017-11-23 20:08:53 +08:00
|
|
|
if sFrom not in fileName or sFrom == sTo:
|
2015-10-11 15:58:34 +08:00
|
|
|
return fileName
|
|
|
|
newFileName = fileName.replace(sFrom, sTo)
|
2017-11-23 20:08:53 +08:00
|
|
|
print("Renaming '%s' -> '%s'..." % (fileName, newFileName))
|
2015-10-11 15:58:34 +08:00
|
|
|
os.rename(fileName, newFileName)
|
|
|
|
return newFileName
|
|
|
|
|
2020-05-18 18:20:11 +08:00
|
|
|
|
2017-11-23 20:08:53 +08:00
|
|
|
def deleteMatchingLines(fileName, pattern):
|
|
|
|
lines = None
|
|
|
|
with open(fileName, "r") as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
not_matching_lines = [l for l in lines if not re.search(pattern, l)]
|
2017-11-23 22:59:19 +08:00
|
|
|
if len(not_matching_lines) == len(lines):
|
2017-11-23 20:08:53 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
print("Removing lines matching '%s' in '%s'..." % (pattern, fileName))
|
|
|
|
print(' ' + ' '.join([l for l in lines if re.search(pattern, l)]))
|
|
|
|
with open(fileName, "w") as f:
|
|
|
|
f.writelines(not_matching_lines)
|
|
|
|
|
|
|
|
return True
|
2016-11-08 19:43:50 +08:00
|
|
|
|
2020-05-18 18:20:11 +08:00
|
|
|
|
2015-10-11 15:58:34 +08:00
|
|
|
def getListOfFiles(clang_tidy_path):
|
2016-11-08 19:43:50 +08:00
|
|
|
files = glob.glob(os.path.join(clang_tidy_path, '*'))
|
2015-10-11 15:58:34 +08:00
|
|
|
for dirname in files:
|
|
|
|
if os.path.isdir(dirname):
|
2016-11-08 19:43:50 +08:00
|
|
|
files += glob.glob(os.path.join(dirname, '*'))
|
|
|
|
files += glob.glob(os.path.join(clang_tidy_path, '..', 'test',
|
|
|
|
'clang-tidy', '*'))
|
|
|
|
files += glob.glob(os.path.join(clang_tidy_path, '..', 'docs',
|
|
|
|
'clang-tidy', 'checks', '*'))
|
2015-10-11 15:58:34 +08:00
|
|
|
return [filename for filename in files if os.path.isfile(filename)]
|
|
|
|
|
2020-05-18 18:20:11 +08:00
|
|
|
|
|
|
|
# Adapts the module's CMakelist file. Returns 'True' if it could add a new
|
|
|
|
# entry and 'False' if the entry already existed.
|
2017-11-23 20:08:53 +08:00
|
|
|
def adapt_cmake(module_path, check_name_camel):
|
|
|
|
filename = os.path.join(module_path, 'CMakeLists.txt')
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
cpp_file = check_name_camel + '.cpp'
|
|
|
|
|
|
|
|
# Figure out whether this check already exists.
|
|
|
|
for line in lines:
|
|
|
|
if line.strip() == cpp_file:
|
|
|
|
return False
|
|
|
|
|
|
|
|
print('Updating %s...' % filename)
|
|
|
|
with open(filename, 'wb') as f:
|
|
|
|
cpp_found = False
|
|
|
|
file_added = False
|
|
|
|
for line in lines:
|
|
|
|
cpp_line = line.strip().endswith('.cpp')
|
|
|
|
if (not file_added) and (cpp_line or cpp_found):
|
|
|
|
cpp_found = True
|
|
|
|
if (line.strip() > cpp_file) or (not cpp_line):
|
2020-12-02 00:50:56 +08:00
|
|
|
f.write((' ' + cpp_file + '\n').encode())
|
2017-11-23 20:08:53 +08:00
|
|
|
file_added = True
|
2020-12-02 00:50:56 +08:00
|
|
|
f.write(line.encode())
|
2017-11-23 20:08:53 +08:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Modifies the module to include the new check.
|
|
|
|
def adapt_module(module_path, module, check_name, check_name_camel):
|
2020-12-02 00:50:56 +08:00
|
|
|
modulecpp = next(filter(lambda p: p.lower() == module.lower() + 'tidymodule.cpp', os.listdir(module_path)))
|
2017-11-23 20:08:53 +08:00
|
|
|
filename = os.path.join(module_path, modulecpp)
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
print('Updating %s...' % filename)
|
|
|
|
with open(filename, 'wb') as f:
|
|
|
|
header_added = False
|
|
|
|
header_found = False
|
|
|
|
check_added = False
|
|
|
|
check_decl = (' CheckFactories.registerCheck<' + check_name_camel +
|
|
|
|
'>(\n "' + check_name + '");\n')
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if not header_added:
|
|
|
|
match = re.search('#include "(.*)"', line)
|
|
|
|
if match:
|
|
|
|
header_found = True
|
|
|
|
if match.group(1) > check_name_camel:
|
|
|
|
header_added = True
|
2020-12-02 00:50:56 +08:00
|
|
|
f.write(('#include "' + check_name_camel + '.h"\n').encode())
|
2017-11-23 20:08:53 +08:00
|
|
|
elif header_found:
|
|
|
|
header_added = True
|
2020-12-02 00:50:56 +08:00
|
|
|
f.write(('#include "' + check_name_camel + '.h"\n').encode())
|
2017-11-23 20:08:53 +08:00
|
|
|
|
|
|
|
if not check_added:
|
|
|
|
if line.strip() == '}':
|
|
|
|
check_added = True
|
2020-12-02 00:50:56 +08:00
|
|
|
f.write(check_decl.encode())
|
2017-11-23 20:08:53 +08:00
|
|
|
else:
|
|
|
|
match = re.search('registerCheck<(.*)>', line)
|
|
|
|
if match and match.group(1) > check_name_camel:
|
|
|
|
check_added = True
|
2020-12-02 00:50:56 +08:00
|
|
|
f.write(check_decl.encode())
|
|
|
|
f.write(line.encode())
|
2017-11-23 20:08:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Adds a release notes entry.
|
|
|
|
def add_release_notes(clang_tidy_path, old_check_name, new_check_name):
|
|
|
|
filename = os.path.normpath(os.path.join(clang_tidy_path,
|
|
|
|
'../docs/ReleaseNotes.rst'))
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
2020-02-21 01:30:11 +08:00
|
|
|
lineMatcher = re.compile('Renamed checks')
|
|
|
|
nextSectionMatcher = re.compile('Improvements to include-fixer')
|
|
|
|
checkMatcher = re.compile('- The \'(.*)')
|
|
|
|
|
2017-11-23 20:08:53 +08:00
|
|
|
print('Updating %s...' % filename)
|
|
|
|
with open(filename, 'wb') as f:
|
|
|
|
note_added = False
|
|
|
|
header_found = False
|
2020-02-21 01:30:11 +08:00
|
|
|
add_note_here = False
|
2017-11-23 20:08:53 +08:00
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if not note_added:
|
2020-02-21 01:30:11 +08:00
|
|
|
match = lineMatcher.match(line)
|
|
|
|
match_next = nextSectionMatcher.match(line)
|
|
|
|
match_check = checkMatcher.match(line)
|
|
|
|
if match_check:
|
|
|
|
last_check = match_check.group(1)
|
|
|
|
if last_check > old_check_name:
|
|
|
|
add_note_here = True
|
|
|
|
|
|
|
|
if match_next:
|
|
|
|
add_note_here = True
|
|
|
|
|
2017-11-23 20:08:53 +08:00
|
|
|
if match:
|
|
|
|
header_found = True
|
2020-12-02 00:50:56 +08:00
|
|
|
f.write(line.encode())
|
2020-02-21 01:30:11 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
if line.startswith('^^^^'):
|
2020-12-02 00:50:56 +08:00
|
|
|
f.write(line.encode())
|
2020-02-21 01:30:11 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
if header_found and add_note_here:
|
2020-01-29 02:09:53 +08:00
|
|
|
if not line.startswith('^^^^'):
|
2020-12-02 00:50:56 +08:00
|
|
|
f.write(("""- The '%s' check was renamed to :doc:`%s
|
2018-03-22 01:06:13 +08:00
|
|
|
<clang-tidy/checks/%s>`
|
2020-02-21 01:30:11 +08:00
|
|
|
|
2020-12-02 00:50:56 +08:00
|
|
|
""" % (old_check_name, new_check_name, new_check_name)).encode())
|
2017-11-23 20:08:53 +08:00
|
|
|
note_added = True
|
|
|
|
|
2020-12-02 00:50:56 +08:00
|
|
|
f.write(line.encode())
|
2016-11-08 19:43:50 +08:00
|
|
|
|
2015-10-11 15:58:34 +08:00
|
|
|
def main():
|
2016-11-08 19:43:50 +08:00
|
|
|
parser = argparse.ArgumentParser(description='Rename clang-tidy check.')
|
|
|
|
parser.add_argument('old_check_name', type=str,
|
|
|
|
help='Old check name.')
|
|
|
|
parser.add_argument('new_check_name', type=str,
|
|
|
|
help='New check name.')
|
2017-11-28 01:59:26 +08:00
|
|
|
parser.add_argument('--check_class_name', type=str,
|
|
|
|
help='Old name of the class implementing the check.')
|
2016-11-08 19:43:50 +08:00
|
|
|
args = parser.parse_args()
|
2015-10-11 15:58:34 +08:00
|
|
|
|
2017-11-23 20:08:53 +08:00
|
|
|
old_module = args.old_check_name.split('-')[0]
|
|
|
|
new_module = args.new_check_name.split('-')[0]
|
2017-11-28 01:59:26 +08:00
|
|
|
if args.check_class_name:
|
|
|
|
check_name_camel = args.check_class_name
|
|
|
|
else:
|
|
|
|
check_name_camel = (''.join(map(lambda elem: elem.capitalize(),
|
|
|
|
args.old_check_name.split('-')[1:])) +
|
|
|
|
'Check')
|
|
|
|
|
2017-11-23 20:08:53 +08:00
|
|
|
new_check_name_camel = (''.join(map(lambda elem: elem.capitalize(),
|
|
|
|
args.new_check_name.split('-')[1:])) +
|
2016-11-08 19:43:50 +08:00
|
|
|
'Check')
|
2015-10-11 15:58:34 +08:00
|
|
|
|
2016-11-08 19:43:50 +08:00
|
|
|
clang_tidy_path = os.path.dirname(__file__)
|
2015-10-11 15:58:34 +08:00
|
|
|
|
2017-11-25 16:49:04 +08:00
|
|
|
header_guard_variants = [
|
2019-05-11 02:27:09 +08:00
|
|
|
(args.old_check_name.replace('-', '_')).upper() + '_CHECK',
|
|
|
|
(old_module + '_' + check_name_camel).upper(),
|
2017-11-25 16:49:04 +08:00
|
|
|
(old_module + '_' + new_check_name_camel).upper(),
|
|
|
|
args.old_check_name.replace('-', '_').upper()]
|
2017-11-24 22:33:06 +08:00
|
|
|
header_guard_new = (new_module + '_' + new_check_name_camel).upper()
|
2017-11-23 20:08:53 +08:00
|
|
|
|
|
|
|
old_module_path = os.path.join(clang_tidy_path, old_module)
|
|
|
|
new_module_path = os.path.join(clang_tidy_path, new_module)
|
|
|
|
|
2019-05-11 02:27:09 +08:00
|
|
|
if (args.old_check_name != args.new_check_name):
|
|
|
|
# Remove the check from the old module.
|
|
|
|
cmake_lists = os.path.join(old_module_path, 'CMakeLists.txt')
|
|
|
|
check_found = deleteMatchingLines(cmake_lists, '\\b' + check_name_camel)
|
|
|
|
if not check_found:
|
|
|
|
print("Check name '%s' not found in %s. Exiting." %
|
2017-11-23 20:08:53 +08:00
|
|
|
(check_name_camel, cmake_lists))
|
2019-05-11 02:27:09 +08:00
|
|
|
return 1
|
2017-11-23 20:08:53 +08:00
|
|
|
|
2020-12-02 00:50:56 +08:00
|
|
|
modulecpp = next(filter(
|
2019-05-11 02:27:09 +08:00
|
|
|
lambda p: p.lower() == old_module.lower() + 'tidymodule.cpp',
|
2020-12-02 00:50:56 +08:00
|
|
|
os.listdir(old_module_path)))
|
2019-05-11 02:27:09 +08:00
|
|
|
deleteMatchingLines(os.path.join(old_module_path, modulecpp),
|
2018-01-30 22:55:39 +08:00
|
|
|
'\\b' + check_name_camel + '|\\b' + args.old_check_name)
|
2015-10-11 15:58:34 +08:00
|
|
|
|
|
|
|
for filename in getListOfFiles(clang_tidy_path):
|
|
|
|
originalName = filename
|
2016-11-08 19:43:50 +08:00
|
|
|
filename = fileRename(filename, args.old_check_name,
|
|
|
|
args.new_check_name)
|
2017-11-23 20:08:53 +08:00
|
|
|
filename = fileRename(filename, check_name_camel, new_check_name_camel)
|
2016-11-08 19:43:50 +08:00
|
|
|
replaceInFile(filename, generateCommentLineHeader(originalName),
|
|
|
|
generateCommentLineHeader(filename))
|
|
|
|
replaceInFile(filename, generateCommentLineSource(originalName),
|
|
|
|
generateCommentLineSource(filename))
|
2017-11-25 16:49:04 +08:00
|
|
|
for header_guard in header_guard_variants:
|
|
|
|
replaceInFile(filename, header_guard, header_guard_new)
|
2017-11-23 22:05:32 +08:00
|
|
|
|
|
|
|
if args.new_check_name + '.rst' in filename:
|
|
|
|
replaceInFile(
|
|
|
|
filename,
|
|
|
|
args.old_check_name + '\n' + '=' * len(args.old_check_name) + '\n',
|
|
|
|
args.new_check_name + '\n' + '=' * len(args.new_check_name) + '\n')
|
|
|
|
|
2016-11-08 19:43:50 +08:00
|
|
|
replaceInFile(filename, args.old_check_name, args.new_check_name)
|
2017-11-28 01:59:26 +08:00
|
|
|
replaceInFile(filename, old_module + '::' + check_name_camel,
|
|
|
|
new_module + '::' + new_check_name_camel)
|
|
|
|
replaceInFile(filename, old_module + '/' + check_name_camel,
|
|
|
|
new_module + '/' + new_check_name_camel)
|
2017-11-23 20:08:53 +08:00
|
|
|
replaceInFile(filename, check_name_camel, new_check_name_camel)
|
|
|
|
|
2019-05-11 02:27:09 +08:00
|
|
|
if old_module != new_module or new_module == 'llvm':
|
|
|
|
if new_module == 'llvm':
|
|
|
|
new_namespace = new_module + '_check'
|
|
|
|
else:
|
|
|
|
new_namespace = new_module
|
2017-11-23 20:08:53 +08:00
|
|
|
check_implementation_files = glob.glob(
|
|
|
|
os.path.join(old_module_path, new_check_name_camel + '*'))
|
|
|
|
for filename in check_implementation_files:
|
|
|
|
# Move check implementation to the directory of the new module.
|
|
|
|
filename = fileRename(filename, old_module_path, new_module_path)
|
2019-05-11 02:27:09 +08:00
|
|
|
replaceInFileRegex(filename, 'namespace ' + old_module + '[^ \n]*',
|
|
|
|
'namespace ' + new_namespace)
|
|
|
|
|
|
|
|
if (args.old_check_name == args.new_check_name):
|
|
|
|
return
|
2017-11-23 20:08:53 +08:00
|
|
|
|
|
|
|
# Add check to the new module.
|
|
|
|
adapt_cmake(new_module_path, new_check_name_camel)
|
|
|
|
adapt_module(new_module_path, new_module, args.new_check_name,
|
|
|
|
new_check_name_camel)
|
|
|
|
|
|
|
|
os.system(os.path.join(clang_tidy_path, 'add_new_check.py')
|
|
|
|
+ ' --update-docs')
|
|
|
|
add_release_notes(clang_tidy_path, args.old_check_name, args.new_check_name)
|
2015-10-11 15:58:34 +08:00
|
|
|
|
2020-05-18 18:20:11 +08:00
|
|
|
|
2015-10-11 15:58:34 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|