-
Notifications
You must be signed in to change notification settings - Fork 2
/
rename_check.py
executable file
·93 lines (79 loc) · 3.31 KB
/
rename_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/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 re
import sys
import glob
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)
def generateCommentLineHeader(filename):
return ''.join(['//===--- ',
os.path.basename(filename),
' - clang-tidy ',
'-' * max(0, 42 - len(os.path.basename(filename))),
'*- C++ -*-===//'])
def generateCommentLineSource(filename):
return ''.join(['//===--- ',
os.path.basename(filename),
' - clang-tidy',
'-' * max(0, 52 - len(os.path.basename(filename))),
'-===//'])
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
def getListOfFiles(clang_tidy_path):
files = glob.glob(os.path.join(clang_tidy_path,'*'))
for dirname in files:
if os.path.isdir(dirname):
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', '*'))
return [filename for filename in files if os.path.isfile(filename)]
def main():
if len(sys.argv) != 4:
print('Usage: rename_check.py <module> <old-check-name> <new-check-name>\n')
print(' example: rename_check.py misc awesome-functions new-awesome-function')
return
module = sys.argv[1].lower()
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])
header_guard_old = module.upper() + '_' + check_name.upper().replace('-', '_')
header_guard_new = module.upper() + '_' + check_name_new.upper().replace('-', '_')
for filename in getListOfFiles(clang_tidy_path):
originalName = filename
filename = fileRename(filename, check_name, check_name_new)
filename = fileRename(filename, check_name_camel, check_name_new_camel)
replaceInFile(filename, generateCommentLineHeader(originalName), generateCommentLineHeader(filename))
replaceInFile(filename, generateCommentLineSource(originalName), generateCommentLineSource(filename))
replaceInFile(filename, header_guard_old, header_guard_new)
replaceInFile(filename, check_name, check_name_new)
replaceInFile(filename, check_name_camel, check_name_new_camel)
if __name__ == '__main__':
main()