clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

rename_check.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 #===- rename_check.py - clang-tidy check renamer -------------*- python -*--===#
4 #
5 # The LLVM Compiler Infrastructure
6 #
7 # This file is distributed under the University of Illinois Open Source
8 # License. See LICENSE.TXT for details.
9 #
10 #===------------------------------------------------------------------------===#
11
12 import os
13 import re
14 import sys
15 import glob
16
17 def replaceInFile(fileName, sFrom, sTo):
18 if sFrom == sTo:
19 return
20 txt = None
21 with open(fileName, "r") as f:
22 txt = f.read()
23
24 if sFrom not in txt:
25 return
26
27 txt = txt.replace(sFrom, sTo)
28 print("Replace '%s' -> '%s' in '%s'" % (sFrom, sTo, fileName))
29 with open(fileName, "w") as f:
30 f.write(txt)
31
32 def generateCommentLineHeader(filename):
33 return ''.join(['//===--- ',
34 os.path.basename(filename),
35 ' - clang-tidy ',
36 '-' * max(0, 42 - len(os.path.basename(filename))),
37 '*- C++ -*-===//'])
38
39 def generateCommentLineSource(filename):
40 return ''.join(['//===--- ',
41 os.path.basename(filename),
42 ' - clang-tidy',
43 '-' * max(0, 52 - len(os.path.basename(filename))),
44 '-===//'])
45
46 def fileRename(fileName, sFrom, sTo):
47 if sFrom not in fileName:
48 return fileName
49 newFileName = fileName.replace(sFrom, sTo)
50 print("Rename '%s' -> '%s'" % (fileName, newFileName))
51 os.rename(fileName, newFileName)
52 return newFileName
53
54 def getListOfFiles(clang_tidy_path):
55 files = glob.glob(os.path.join(clang_tidy_path,'*'))
56 for dirname in files:
57 if os.path.isdir(dirname):
58 files += glob.glob(os.path.join(dirname,'*'))
59 files += glob.glob(os.path.join(clang_tidy_path,'..', 'test', 'clang-tidy', '*'))
60 files += glob.glob(os.path.join(clang_tidy_path,'..', 'docs', 'clang-tidy', 'checks', '*'))
61 return [filename for filename in files if os.path.isfile(filename)]
62
63 def main():
64 if len(sys.argv) != 4:
65 print('Usage: rename_check.py <module> <old-check-name> <new-check-name>\n')
66 print(' example: rename_check.py misc awesome-functions new-awesome-function')
67 return
68
69 module = sys.argv[1].lower()
70 check_name = sys.argv[2]
71 check_name_camel = ''.join(map(lambda elem: elem.capitalize(),
72 check_name.split('-'))) + 'Check'
73 check_name_new = sys.argv[3]
74 check_name_new_camel = ''.join(map(lambda elem: elem.capitalize(),
75 check_name_new.split('-'))) + 'Check'
76
77 clang_tidy_path = os.path.dirname(sys.argv[0])
78
79 header_guard_old = module.upper() + '_' + check_name.upper().replace('-', '_')
80 header_guard_new = module.upper() + '_' + check_name_new.upper().replace('-', '_')
81
82 for filename in getListOfFiles(clang_tidy_path):
83 originalName = filename
84 filename = fileRename(filename, check_name, check_name_new)
85 filename = fileRename(filename, check_name_camel, check_name_new_camel)
86 replaceInFile(filename, generateCommentLineHeader(originalName), generateCommentLineHeader(filename))
87 replaceInFile(filename, generateCommentLineSource(originalName), generateCommentLineSource(filename))
88 replaceInFile(filename, header_guard_old, header_guard_new)
89 replaceInFile(filename, check_name, check_name_new)
90 replaceInFile(filename, check_name_camel, check_name_new_camel)
91
92 if __name__ == '__main__':
93 main()
rename_check.main
def main
Definition: rename_check.py:63
rename_check.fileRename
def fileRename
Definition: rename_check.py:46
rename_check.replaceInFile
def replaceInFile
Definition: rename_check.py:17
rename_check.generateCommentLineSource
def generateCommentLineSource
Definition: rename_check.py:39
rename_check.generateCommentLineHeader
def generateCommentLineHeader
Definition: rename_check.py:32
rename_check.getListOfFiles
def getListOfFiles
Definition: rename_check.py:54