259 module_path: str, module: str, check_name: str, description: str
261 wrapped_desc =
"\n".join(
263 description, width=80, initial_indent=
" ", subsequent_indent=
" "
266 check_name_dashes = module +
"-" + check_name
267 filename = os.path.normpath(
268 os.path.join(module_path,
"../../docs/ReleaseNotes.rst")
270 with open(filename,
"r", encoding=
"utf8")
as f:
271 lines = f.readlines()
273 lineMatcher = re.compile(
"New checks")
274 nextSectionMatcher = re.compile(
"New check aliases")
275 checkMatcher = re.compile(
"- New :doc:`(.*)")
277 print(
"Updating %s..." % filename)
278 with open(filename,
"w", encoding=
"utf8", newline=
"\n")
as f:
281 add_note_here =
False
285 if match_check := checkMatcher.match(line):
286 last_check = match_check.group(1)
287 if last_check > check_name_dashes:
290 if nextSectionMatcher.match(line):
293 if lineMatcher.match(line):
298 if line.startswith(
"^^^^"):
302 if header_found
and add_note_here:
303 if not line.startswith(
"^^^^"):
306 <clang-tidy/checks/%s/%s>` check.
311 % (check_name_dashes, module, check_name, wrapped_desc)
378 docs_dir = os.path.join(clang_tidy_path,
"../docs/clang-tidy/checks")
379 filename = os.path.normpath(os.path.join(docs_dir,
"list.rst"))
381 with open(filename,
"r", encoding=
"utf8")
as f:
382 lines = f.readlines()
385 for subdir
in filter(
386 lambda s: os.path.isdir(os.path.join(docs_dir, s)), os.listdir(docs_dir)
389 methodcaller(
"endswith",
".rst"), os.listdir(os.path.join(docs_dir, subdir))
391 doc_files.append((subdir, file))
396 def filename_from_module(module_name: str, check_name: str) -> str:
397 module_path = os.path.join(clang_tidy_path, module_name)
398 if not os.path.isdir(module_path):
401 if not os.path.isfile(module_file):
403 with open(module_file,
"r")
as f:
405 full_check_name = module_name +
"-" + check_name
406 if (name_pos := code.find(
'"' + full_check_name +
'"')) == -1:
408 if (stmt_end_pos := code.find(
";", name_pos)) == -1:
410 if (stmt_start_pos := code.rfind(
";", 0, name_pos)) == -1
and (
411 stmt_start_pos := code.rfind(
"{", 0, name_pos)
414 stmt = code[stmt_start_pos + 1 : stmt_end_pos]
415 matches = re.search(
r'registerCheck<([^>:]*)>\(\s*"([^"]*)"\s*\)', stmt)
416 if matches
and matches[2] == full_check_name:
417 class_name = matches[1]
418 if "::" in class_name:
419 parts = class_name.split(
"::")
420 class_name = parts[-1]
421 class_path = os.path.join(
422 clang_tidy_path, module_name,
"..", *parts[0:-1]
425 class_path = os.path.join(clang_tidy_path, module_name)
431 def get_base_class(code: str, check_file: str) -> str:
432 check_class_name = os.path.splitext(os.path.basename(check_file))[0]
433 ctor_pattern = check_class_name +
r"\([^:]*\)\s*:\s*([A-Z][A-Za-z0-9]*Check)\("
434 matches = re.search(
r"\s+" + check_class_name +
"::" + ctor_pattern, code)
438 header_file = os.path.splitext(check_file)[0] +
".h"
439 if not os.path.isfile(header_file):
441 with open(header_file, encoding=
"utf8")
as f:
443 matches = re.search(
" " + ctor_pattern, code)
445 if matches
and matches[1] !=
"ClangTidyCheck":
450 def has_fixits(code: str) -> bool:
455 "TransformerClangTidyCheck",
462 def has_auto_fix(check_name: str) -> str:
463 dirname, _, check_name = check_name.partition(
"-")
466 os.path.join(clang_tidy_path, dirname),
469 if not os.path.isfile(check_file):
472 os.path.join(clang_tidy_path, dirname),
475 if not os.path.isfile(check_file):
477 check_file = filename_from_module(dirname, check_name)
478 if not (check_file
and os.path.isfile(check_file)):
481 with open(check_file, encoding=
"utf8")
as f:
486 if base_class := get_base_class(code, check_file):
487 base_file = os.path.join(clang_tidy_path, dirname, base_class +
".cpp")
488 if os.path.isfile(base_file):
489 with open(base_file, encoding=
"utf8")
as f:
496 def process_doc(doc_file: Tuple[str, str]) -> Tuple[str, Optional[Match[str]]]:
497 check_name = doc_file[0] +
"-" + doc_file[1].replace(
".rst",
"")
499 with open(os.path.join(docs_dir, *doc_file),
"r", encoding=
"utf8")
as doc:
502 if match := re.search(
".*:orphan:.*", content):
506 match = re.search(
r".*:http-equiv=refresh: \d+;URL=(.*).html(.*)", content)
508 return check_name, match
510 def format_link(doc_file: Tuple[str, str]) -> str:
511 check_name, match = process_doc(doc_file)
512 if not match
and check_name
and not check_name.startswith(
"clang-analyzer-"):
513 return " :doc:`%(check_name)s <%(module)s/%(check)s>`,%(autofix)s\n" % {
514 "check_name": check_name,
515 "module": doc_file[0],
516 "check": doc_file[1].replace(
".rst",
""),
517 "autofix": has_auto_fix(check_name),
522 def format_link_alias(doc_file: Tuple[str, str]) -> str:
523 check_name, match = process_doc(doc_file)
524 if (match
or (check_name.startswith(
"clang-analyzer-")))
and check_name:
526 check_file = doc_file[1].replace(
".rst",
"")
529 or match.group(1) ==
"https://clang.llvm.org/docs/analyzer/checkers"
531 title =
"Clang Static Analyzer " + check_file
533 target =
"" if not match
else match.group(1) +
".html" + match.group(2)
538 redirect_parts = re.search(
r"^\.\./([^/]*)/([^/]*)$", match.group(1))
539 assert redirect_parts
540 title = redirect_parts[1] +
"-" + redirect_parts[2]
541 target = redirect_parts[1] +
"/" + redirect_parts[2]
542 autofix = has_auto_fix(title)
549 " :doc:`%(check_name)s <%(module)s/%(check_file)s>`, %(ref_begin)s`%(title)s <%(target)s>`%(ref_end)s,%(autofix)s\n"
551 "check_name": check_name,
553 "check_file": check_file,
557 "ref_begin": ref_begin,
564 " :doc:`%(check_name)s <%(module)s/%(check_file)s>`, %(title)s,%(autofix)s\n"
566 "check_name": check_name,
568 "check_file": check_file,
575 print(
"Updating %s..." % filename)
576 with open(filename,
"w", encoding=
"utf8", newline=
"\n")
as f:
579 if line.strip() ==
".. csv-table::":
581 f.write(
' :header: "Name", "Offers fixes"\n\n')
582 f.writelines(map(format_link, doc_files))
584 f.write(
"\nCheck aliases\n-------------\n\n")
585 f.write(
".. csv-table::\n")
586 f.write(
' :header: "Name", "Redirect", "Offers fixes"\n\n')
587 f.writelines(map(format_link_alias, doc_files))
625 language_to_extension = {
631 cpp_language_to_requirements = {
632 "c++98":
"CPlusPlus",
633 "c++11":
"CPlusPlus11",
634 "c++14":
"CPlusPlus14",
635 "c++17":
"CPlusPlus17",
636 "c++20":
"CPlusPlus20",
637 "c++23":
"CPlusPlus23",
638 "c++26":
"CPlusPlus26",
640 c_language_to_requirements = {
647 parser = argparse.ArgumentParser()
651 help=
"just update the list of documentation files, then exit",
655 help=
"language to use for new check (defaults to c++)",
656 choices=language_to_extension.keys(),
663 help=
"short description of what the check does",
664 default=
"FIXME: Write a short description",
669 help=
"Specify a specific version of the language",
672 cpp_language_to_requirements.keys(), c_language_to_requirements.keys()
680 help=
"module directory under which to place the new tidy check (e.g., misc)",
683 "check", nargs=
"?", help=
"name of new tidy check to add (e.g. foo-do-the-stuff)"
685 args = parser.parse_args()
691 if not args.module
or not args.check:
692 print(
"Module and check must be specified.")
697 check_name = args.check
699 if check_name.startswith(module):
701 'Check name "%s" must not start with the module "%s". Exiting.'
702 % (check_name, module)
705 clang_tidy_path = os.path.dirname(sys.argv[0])
706 module_path = os.path.join(clang_tidy_path, module)
713 namespace = module +
"_check"
717 description = args.description
718 if not description.endswith(
"."):
721 language = args.language
724 if args.standard
in cpp_language_to_requirements:
725 if language
and language !=
"c++":
726 raise ValueError(
"C++ standard chosen when language is not C++")
728 elif args.standard
in c_language_to_requirements:
729 if language
and language !=
"c":
730 raise ValueError(
"C standard chosen when language is not C")
736 language_restrict =
None
739 language_restrict =
"!%(lang)s.CPlusPlus"
740 if extra := c_language_to_requirements.get(args.standard,
None):
741 language_restrict += f
" && %(lang)s.{extra}"
742 elif language ==
"c++":
743 language_restrict = (
744 f
"%(lang)s.{cpp_language_to_requirements.get(args.standard, 'CPlusPlus')}"
746 elif language
in [
"objc",
"objc++"]:
747 language_restrict =
"%(lang)s.ObjC"
749 raise ValueError(f
"Unsupported language '{language}' was specified")
761 adapt_module(module_path, module, check_name, check_name_camel)
763 test_extension = language_to_extension[language]
764 write_test(module_path, module, check_name, test_extension, args.standard)
767 print(
"Done. Now it's your turn!")