263 module_path: str, module: str, check_name: str, description: str
265 wrapped_desc =
"\n".join(
267 description, width=80, initial_indent=
" ", subsequent_indent=
" "
270 check_name_dashes = module +
"-" + check_name
271 filename = os.path.normpath(
272 os.path.join(module_path,
"../../docs/ReleaseNotes.rst")
274 with io.open(filename,
"r", encoding=
"utf8")
as f:
275 lines = f.readlines()
277 lineMatcher = re.compile(
"New checks")
278 nextSectionMatcher = re.compile(
"New check aliases")
279 checkMatcher = re.compile(
"- New :doc:`(.*)")
281 print(
"Updating %s..." % filename)
282 with io.open(filename,
"w", encoding=
"utf8", newline=
"\n")
as f:
285 add_note_here =
False
289 match = lineMatcher.match(line)
290 match_next = nextSectionMatcher.match(line)
291 match_check = checkMatcher.match(line)
293 last_check = match_check.group(1)
294 if last_check > check_name_dashes:
305 if line.startswith(
"^^^^"):
309 if header_found
and add_note_here:
310 if not line.startswith(
"^^^^"):
313 <clang-tidy/checks/%s/%s>` check.
318 % (check_name_dashes, module, check_name, wrapped_desc)
384 docs_dir = os.path.join(clang_tidy_path,
"../docs/clang-tidy/checks")
385 filename = os.path.normpath(os.path.join(docs_dir,
"list.rst"))
387 with io.open(filename,
"r", encoding=
"utf8")
as f:
388 lines = f.readlines()
391 for subdir
in filter(
392 lambda s: os.path.isdir(os.path.join(docs_dir, s)), os.listdir(docs_dir)
395 lambda s: s.endswith(
".rst"), os.listdir(os.path.join(docs_dir, subdir))
397 doc_files.append((subdir, file))
402 def filename_from_module(module_name: str, check_name: str) -> str:
403 module_path = os.path.join(clang_tidy_path, module_name)
404 if not os.path.isdir(module_path):
407 if not os.path.isfile(module_file):
409 with io.open(module_file,
"r")
as f:
411 full_check_name = module_name +
"-" + check_name
412 name_pos = code.find(
'"' + full_check_name +
'"')
415 stmt_end_pos = code.find(
";", name_pos)
416 if stmt_end_pos == -1:
418 stmt_start_pos = code.rfind(
";", 0, name_pos)
419 if stmt_start_pos == -1:
420 stmt_start_pos = code.rfind(
"{", 0, name_pos)
421 if stmt_start_pos == -1:
423 stmt = code[stmt_start_pos + 1 : stmt_end_pos]
424 matches = re.search(
r'registerCheck<([^>:]*)>\(\s*"([^"]*)"\s*\)', stmt)
425 if matches
and matches[2] == full_check_name:
426 class_name = matches[1]
427 if "::" in class_name:
428 parts = class_name.split(
"::")
429 class_name = parts[-1]
430 class_path = os.path.join(
431 clang_tidy_path, module_name,
"..", *parts[0:-1]
434 class_path = os.path.join(clang_tidy_path, module_name)
440 def get_base_class(code: str, check_file: str) -> str:
441 check_class_name = os.path.splitext(os.path.basename(check_file))[0]
442 ctor_pattern = check_class_name +
r"\([^:]*\)\s*:\s*([A-Z][A-Za-z0-9]*Check)\("
443 matches = re.search(
r"\s+" + check_class_name +
"::" + ctor_pattern, code)
447 header_file = os.path.splitext(check_file)[0] +
".h"
448 if not os.path.isfile(header_file):
450 with io.open(header_file, encoding=
"utf8")
as f:
452 matches = re.search(
" " + ctor_pattern, code)
454 if matches
and matches[1] !=
"ClangTidyCheck":
459 def has_fixits(code: str) -> bool:
464 "TransformerClangTidyCheck",
471 def has_auto_fix(check_name: str) -> str:
472 dirname, _, check_name = check_name.partition(
"-")
475 os.path.join(clang_tidy_path, dirname),
478 if not os.path.isfile(check_file):
481 os.path.join(clang_tidy_path, dirname),
484 if not os.path.isfile(check_file):
486 check_file = filename_from_module(dirname, check_name)
487 if not check_file
or not os.path.isfile(check_file):
490 with io.open(check_file, encoding=
"utf8")
as f:
495 base_class = get_base_class(code, check_file)
497 base_file = os.path.join(clang_tidy_path, dirname, base_class +
".cpp")
498 if os.path.isfile(base_file):
499 with io.open(base_file, encoding=
"utf8")
as f:
506 def process_doc(doc_file: Tuple[str, str]) -> Tuple[str, Optional[Match[str]]]:
507 check_name = doc_file[0] +
"-" + doc_file[1].replace(
".rst",
"")
509 with io.open(os.path.join(docs_dir, *doc_file),
"r", encoding=
"utf8")
as doc:
511 match = re.search(
".*:orphan:.*", content)
517 match = re.search(
r".*:http-equiv=refresh: \d+;URL=(.*).html(.*)", content)
519 return check_name, match
521 def format_link(doc_file: Tuple[str, str]) -> str:
522 check_name, match = process_doc(doc_file)
523 if not match
and check_name
and not check_name.startswith(
"clang-analyzer-"):
524 return " :doc:`%(check_name)s <%(module)s/%(check)s>`,%(autofix)s\n" % {
525 "check_name": check_name,
526 "module": doc_file[0],
527 "check": doc_file[1].replace(
".rst",
""),
528 "autofix": has_auto_fix(check_name),
533 def format_link_alias(doc_file: Tuple[str, str]) -> str:
534 check_name, match = process_doc(doc_file)
535 if (match
or (check_name.startswith(
"clang-analyzer-")))
and check_name:
537 check_file = doc_file[1].replace(
".rst",
"")
540 or match.group(1) ==
"https://clang.llvm.org/docs/analyzer/checkers"
542 title =
"Clang Static Analyzer " + check_file
544 target =
"" if not match
else match.group(1) +
".html" + match.group(2)
549 redirect_parts = re.search(
r"^\.\./([^/]*)/([^/]*)$", match.group(1))
550 assert redirect_parts
551 title = redirect_parts[1] +
"-" + redirect_parts[2]
552 target = redirect_parts[1] +
"/" + redirect_parts[2]
553 autofix = has_auto_fix(title)
560 " :doc:`%(check_name)s <%(module)s/%(check_file)s>`, %(ref_begin)s`%(title)s <%(target)s>`%(ref_end)s,%(autofix)s\n"
562 "check_name": check_name,
564 "check_file": check_file,
568 "ref_begin": ref_begin,
575 " :doc:`%(check_name)s <%(module)s/%(check_file)s>`, %(title)s,%(autofix)s\n"
577 "check_name": check_name,
579 "check_file": check_file,
587 checks = map(format_link, doc_files)
588 checks_alias = map(format_link_alias, doc_files)
590 print(
"Updating %s..." % filename)
591 with io.open(filename,
"w", encoding=
"utf8", newline=
"\n")
as f:
594 if line.strip() ==
".. csv-table::":
596 f.write(
' :header: "Name", "Offers fixes"\n\n')
599 f.write(
"\nCheck aliases\n-------------\n\n")
600 f.write(
".. csv-table::\n")
601 f.write(
' :header: "Name", "Redirect", "Offers fixes"\n\n')
602 f.writelines(checks_alias)
640 language_to_extension = {
646 cpp_language_to_requirements = {
647 "c++98":
"CPlusPlus",
648 "c++11":
"CPlusPlus11",
649 "c++14":
"CPlusPlus14",
650 "c++17":
"CPlusPlus17",
651 "c++20":
"CPlusPlus20",
652 "c++23":
"CPlusPlus23",
653 "c++26":
"CPlusPlus26",
655 c_language_to_requirements = {
662 parser = argparse.ArgumentParser()
666 help=
"just update the list of documentation files, then exit",
670 help=
"language to use for new check (defaults to c++)",
671 choices=language_to_extension.keys(),
678 help=
"short description of what the check does",
679 default=
"FIXME: Write a short description",
684 help=
"Specify a specific version of the language",
687 cpp_language_to_requirements.keys(), c_language_to_requirements.keys()
695 help=
"module directory under which to place the new tidy check (e.g., misc)",
698 "check", nargs=
"?", help=
"name of new tidy check to add (e.g. foo-do-the-stuff)"
700 args = parser.parse_args()
706 if not args.module
or not args.check:
707 print(
"Module and check must be specified.")
712 check_name = args.check
714 if check_name.startswith(module):
716 'Check name "%s" must not start with the module "%s". Exiting.'
717 % (check_name, module)
720 clang_tidy_path = os.path.dirname(sys.argv[0])
721 module_path = os.path.join(clang_tidy_path, module)
728 namespace = module +
"_check"
732 description = args.description
733 if not description.endswith(
"."):
736 language = args.language
739 if args.standard
in cpp_language_to_requirements:
740 if language
and language !=
"c++":
741 raise ValueError(
"C++ standard chosen when language is not C++")
743 elif args.standard
in c_language_to_requirements:
744 if language
and language !=
"c":
745 raise ValueError(
"C standard chosen when language is not C")
751 language_restrict =
None
754 language_restrict =
"!%(lang)s.CPlusPlus"
755 extra = c_language_to_requirements.get(args.standard,
None)
757 language_restrict += f
" && %(lang)s.{extra}"
758 elif language ==
"c++":
759 language_restrict = (
760 f
"%(lang)s.{cpp_language_to_requirements.get(args.standard, 'CPlusPlus')}"
762 elif language
in [
"objc",
"objc++"]:
763 language_restrict =
"%(lang)s.ObjC"
765 raise ValueError(f
"Unsupported language '{language}' was specified")
777 adapt_module(module_path, module, check_name, check_name_camel)
779 test_extension = language_to_extension[language]
780 write_test(module_path, module, check_name, test_extension, args.standard)
783 print(
"Done. Now it's your turn!")