2015-10-11 15:58:34 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
#===- rename_check.py - clang-tidy check renamer -------------*- python -*--===#
|
|
|
|
#
|
|
|
|
# The LLVM Compiler Infrastructure
|
|
|
|
#
|
|
|
|
# This file is distributed under the University of Illinois Open Source
|
|
|
|
# License. See LICENSE.TXT for details.
|
|
|
|
#
|
|
|
|
#===------------------------------------------------------------------------===#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import glob
|
2016-11-08 19:43:50 +08:00
|
|
|
import argparse
|
|
|
|
|
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)
|
|
|
|
print("Replace '%s' -> '%s' in '%s'" % (sFrom, sTo, fileName))
|
|
|
|
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):
|
|
|
|
if sFrom not in fileName:
|
|
|
|
return fileName
|
|
|
|
newFileName = fileName.replace(sFrom, sTo)
|
|
|
|
print("Rename '%s' -> '%s'" % (fileName, newFileName))
|
|
|
|
os.rename(fileName, newFileName)
|
|
|
|
return newFileName
|
|
|
|
|
2016-11-08 19:43:50 +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)]
|
|
|
|
|
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('module', type=str,
|
|
|
|
help='Module where the renamed check is defined')
|
|
|
|
parser.add_argument('old_check_name', type=str,
|
|
|
|
help='Old check name.')
|
|
|
|
parser.add_argument('new_check_name', type=str,
|
|
|
|
help='New check name.')
|
|
|
|
args = parser.parse_args()
|
2015-10-11 15:58:34 +08:00
|
|
|
|
2016-11-08 19:43:50 +08:00
|
|
|
args.module = args.module.lower()
|
2015-10-11 15:58:34 +08:00
|
|
|
check_name_camel = ''.join(map(lambda elem: elem.capitalize(),
|
2016-11-08 19:43:50 +08:00
|
|
|
args.old_check_name.split('-'))) + 'Check'
|
|
|
|
check_name_new_camel = (''.join(map(lambda elem: elem.capitalize(),
|
|
|
|
args.new_check_name.split('-'))) +
|
|
|
|
'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
|
|
|
|
2016-11-08 19:43:50 +08:00
|
|
|
header_guard_old = (args.module.upper() + '_' +
|
|
|
|
args.old_check_name.upper().replace('-', '_'))
|
|
|
|
header_guard_new = (args.module.upper() + '_' +
|
|
|
|
args.new_check_name.upper().replace('-', '_'))
|
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)
|
2015-10-11 15:58:34 +08:00
|
|
|
filename = fileRename(filename, check_name_camel, check_name_new_camel)
|
2016-11-08 19:43:50 +08:00
|
|
|
replaceInFile(filename, generateCommentLineHeader(originalName),
|
|
|
|
generateCommentLineHeader(filename))
|
|
|
|
replaceInFile(filename, generateCommentLineSource(originalName),
|
|
|
|
generateCommentLineSource(filename))
|
2015-10-11 15:58:34 +08:00
|
|
|
replaceInFile(filename, header_guard_old, header_guard_new)
|
2016-11-08 19:43:50 +08:00
|
|
|
replaceInFile(filename, args.old_check_name, args.new_check_name)
|
2015-10-11 15:58:34 +08:00
|
|
|
replaceInFile(filename, check_name_camel, check_name_new_camel)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|