17from typing
import List
29 with io.open(fileName,
"r", encoding=
"utf8")
as f:
32 txt = re.sub(sFrom, sTo, txt)
33 print(
"Replacing '%s' -> '%s' in '%s'..." % (sFrom, sTo, fileName))
34 with io.open(fileName,
"w", encoding=
"utf8")
as f:
42 with io.open(fileName,
"r", encoding=
"utf8")
as f:
48 txt = txt.replace(sFrom, sTo)
49 print(
"Replacing '%s' -> '%s' in '%s'..." % (sFrom, sTo, fileName))
50 with io.open(fileName,
"w", encoding=
"utf8")
as f:
58 os.path.basename(filename),
60 "-" * max(0, 42 - len(os.path.basename(filename))),
70 os.path.basename(filename),
72 "-" * max(0, 52 - len(os.path.basename(filename))),
78def fileRename(fileName: str, sFrom: str, sTo: str) -> str:
79 if sFrom
not in fileName
or sFrom == sTo:
81 newFileName = fileName.replace(sFrom, sTo)
82 print(
"Renaming '%s' -> '%s'..." % (fileName, newFileName))
83 os.rename(fileName, newFileName)
89 with io.open(fileName,
"r", encoding=
"utf8")
as f:
92 not_matching_lines = [l
for l
in lines
if not re.search(pattern, l)]
93 if len(not_matching_lines) == len(lines):
96 print(
"Removing lines matching '%s' in '%s'..." % (pattern, fileName))
97 print(
" " +
" ".join([l
for l
in lines
if re.search(pattern, l)]))
98 with io.open(fileName,
"w", encoding=
"utf8")
as f:
99 f.writelines(not_matching_lines)
105 files = glob.glob(os.path.join(clang_tidy_path,
"**"), recursive=
True)
107 os.path.normpath(os.path.join(clang_tidy_path,
"../docs/ReleaseNotes.rst"))
110 os.path.join(clang_tidy_path,
"..",
"test",
"clang-tidy",
"checkers",
"**"),
114 os.path.join(clang_tidy_path,
"..",
"docs",
"clang-tidy",
"checks",
"*.rst")
118 clang_tidy_path,
"..",
"docs",
"clang-tidy",
"checks",
"*",
"*.rst"
122 return [filename
for filename
in files
if os.path.isfile(filename)]
128 filename = os.path.join(module_path,
"CMakeLists.txt")
129 with io.open(filename,
"r", encoding=
"utf8")
as f:
130 lines = f.readlines()
132 cpp_file = check_name_camel +
".cpp"
136 if line.strip() == cpp_file:
139 print(
"Updating %s..." % filename)
140 with io.open(filename,
"w", encoding=
"utf8")
as f:
144 cpp_line = line.strip().endswith(
".cpp")
145 if (
not file_added)
and (cpp_line
or cpp_found):
147 if (line.strip() > cpp_file)
or (
not cpp_line):
148 f.write(
" " + cpp_file +
"\n")
157 module_path: str, module: str, check_name: str, check_name_camel: str
162 lambda p: p.lower() == module.lower() +
"tidymodule.cpp",
163 os.listdir(module_path),
167 filename = os.path.join(module_path, modulecpp)
168 with io.open(filename,
"r", encoding=
"utf8")
as f:
169 lines = f.readlines()
171 print(
"Updating %s..." % filename)
172 with io.open(filename,
"w", encoding=
"utf8")
as f:
177 " CheckFactories.registerCheck<"
186 match = re.search(
'#include "(.*)"', line)
189 if match.group(1) > check_name_camel:
191 f.write(
'#include "' + check_name_camel +
'.h"\n')
194 f.write(
'#include "' + check_name_camel +
'.h"\n')
197 if line.strip() ==
"}":
201 match = re.search(
"registerCheck<(.*)>", line)
202 if match
and match.group(1) > check_name_camel:
210 clang_tidy_path: str, old_check_name: str, new_check_name: str
212 filename = os.path.normpath(
213 os.path.join(clang_tidy_path,
"../docs/ReleaseNotes.rst")
215 with io.open(filename,
"r", encoding=
"utf8")
as f:
216 lines = f.readlines()
218 lineMatcher = re.compile(
"Renamed checks")
219 nextSectionMatcher = re.compile(
"Improvements to include-fixer")
220 checkMatcher = re.compile(
"- The '(.*)")
222 print(
"Updating %s..." % filename)
223 with io.open(filename,
"w", encoding=
"utf8")
as f:
226 add_note_here =
False
230 match = lineMatcher.match(line)
231 match_next = nextSectionMatcher.match(line)
232 match_check = checkMatcher.match(line)
234 last_check = match_check.group(1)
235 if last_check > old_check_name:
246 if line.startswith(
"^^^^"):
250 if header_found
and add_note_here:
251 if not line.startswith(
"^^^^"):
253 """- The '%s' check was renamed to :doc:`%s
254 <clang-tidy/checks/%s/%s>`
260 new_check_name.split("-", 1)[0],
261 "-".join(new_check_name.split(
"-")[1:]),
270 parser = argparse.ArgumentParser(description=
"Rename clang-tidy check.")
271 parser.add_argument(
"old_check_name", type=str, help=
"Old check name.")
272 parser.add_argument(
"new_check_name", type=str, help=
"New check name.")
274 "--check_class_name",
276 help=
"Old name of the class implementing the check.",
278 args = parser.parse_args()
280 old_module = args.old_check_name.split(
"-")[0]
281 new_module = args.new_check_name.split(
"-")[0]
282 old_name =
"-".join(args.old_check_name.split(
"-")[1:])
283 new_name =
"-".join(args.new_check_name.split(
"-")[1:])
285 if args.check_class_name:
286 check_name_camel = args.check_class_name
289 "".join(map(
lambda elem: elem.capitalize(), old_name.split(
"-"))) +
"Check"
292 new_check_name_camel = (
293 "".join(map(
lambda elem: elem.capitalize(), new_name.split(
"-"))) +
"Check"
296 clang_tidy_path = os.path.dirname(__file__)
298 header_guard_variants = [
299 (args.old_check_name.replace(
"-",
"_")).upper() +
"_CHECK",
300 (old_module +
"_" + check_name_camel).upper(),
301 (old_module +
"_" + new_check_name_camel).upper(),
302 args.old_check_name.replace(
"-",
"_").upper(),
304 header_guard_new = (new_module +
"_" + new_check_name_camel).upper()
306 old_module_path = os.path.join(clang_tidy_path, old_module)
307 new_module_path = os.path.join(clang_tidy_path, new_module)
309 if old_module != new_module:
311 cmake_lists = os.path.join(old_module_path,
"CMakeLists.txt")
315 "Check name '%s' not found in %s. Exiting."
316 % (check_name_camel, cmake_lists)
323 lambda p: p.lower() == old_module.lower() +
"tidymodule.cpp",
324 os.listdir(old_module_path),
329 os.path.join(old_module_path, modulecpp),
330 "\\b" + check_name_camel +
"|\\b" + args.old_check_name,
334 originalName = filename
336 filename, old_module +
"/" + old_name, new_module +
"/" + new_name
338 filename =
fileRename(filename, args.old_check_name, args.new_check_name)
339 filename =
fileRename(filename, check_name_camel, new_check_name_camel)
350 for header_guard
in header_guard_variants:
353 if new_module +
"/" + new_name +
".rst" in filename:
356 args.old_check_name +
"\n" +
"=" * len(args.old_check_name) +
"\n",
357 args.new_check_name +
"\n" +
"=" * len(args.new_check_name) +
"\n",
360 replaceInFile(filename, args.old_check_name, args.new_check_name)
363 old_module +
"::" + check_name_camel,
364 new_module +
"::" + new_check_name_camel,
368 old_module +
"/" + check_name_camel,
369 new_module +
"/" + new_check_name_camel,
372 filename, old_module +
"/" + old_name, new_module +
"/" + new_name
374 replaceInFile(filename, check_name_camel, new_check_name_camel)
376 if old_module != new_module
or new_module ==
"llvm":
377 if new_module ==
"llvm":
378 new_namespace = new_module +
"_check"
380 new_namespace = new_module
381 check_implementation_files = glob.glob(
382 os.path.join(old_module_path, new_check_name_camel +
"*")
384 for filename
in check_implementation_files:
386 filename =
fileRename(filename, old_module_path, new_module_path)
389 "namespace clang::tidy::" + old_module +
"[^ \n]*",
390 "namespace clang::tidy::" + new_namespace,
393 if old_module != new_module:
398 new_module_path, new_module, args.new_check_name, new_check_name_camel
401 os.system(os.path.join(clang_tidy_path,
"add_new_check.py") +
" --update-docs")
405if __name__ ==
"__main__":
List[str] getListOfFiles(str clang_tidy_path)
None adapt_module(str module_path, str module, str check_name, str check_name_camel)
str fileRename(str fileName, str sFrom, str sTo)
str generateCommentLineSource(str filename)
None replaceInFileRegex(str fileName, str sFrom, str sTo)
None add_release_notes(str clang_tidy_path, str old_check_name, str new_check_name)
bool adapt_cmake(str module_path, str check_name_camel)
str generateCommentLineHeader(str filename)
bool deleteMatchingLines(str fileName, str pattern)
None replaceInFile(str fileName, str sFrom, str sTo)