Index: clang-tidy/rename_check.py =================================================================== --- /dev/null +++ clang-tidy/rename_check.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python +# +#===- add_new_check.py - clang-tidy check generator ----------*- 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 re +import sys +import glob + +def replaceInFile(fileName, sFrom, sTo): + 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) + +def fileRename(fileName, sFrom, sTo): + if sFrom not in fileName: + return + print("Rename %s -> %s" % (fileName, fileName.replace(sFrom, sTo))) + + os.rename(fileName, fileName.replace(sFrom, sTo)) + +def main(): + if len(sys.argv) != 4: + print('Usage: rename_check.py , e.g.\n') + print('rename_check.py misc awesome-functions new-awesome-function\n') + return + + module = sys.argv[1] + check_name = sys.argv[2] + check_name_camel = ''.join(map(lambda elem: elem.capitalize(), + check_name.split('-'))) + 'Check' + check_name_new = sys.argv[3] + check_name_new_camel = ''.join(map(lambda elem: elem.capitalize(), + check_name_new.split('-'))) + 'Check' + + clang_tidy_path = os.path.dirname(sys.argv[0]) + module_path = os.path.join(clang_tidy_path, module) + + header_guard_old = module.upper() + '_' + check_name.upper().replace('-', '_') + header_guard_new = module.upper() + '_' + check_name_new.upper().replace('-', '_') + + files = glob.glob(os.path.join(module_path, '*')) + 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', '*')) + for filename in files: + if not os.path.isfile(filename): + continue + replaceInFile(filename, header_guard_old, header_guard_new) + replaceInFile(filename, check_name, check_name_new) + replaceInFile(filename, check_name_camel, check_name_new_camel) + fileRename(filename, check_name, check_name_new) + fileRename(filename, check_name_camel, check_name_new_camel) + + +if __name__ == '__main__': + main()