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:
456 "TransformerClangTidyCheck",
463 def has_auto_fix(check_name: str) -> str:
464 dirname, _, check_name = check_name.partition(
"-")
467 os.path.join(clang_tidy_path, dirname),
470 if not os.path.isfile(check_file):
473 os.path.join(clang_tidy_path, dirname),
476 if not os.path.isfile(check_file):
478 check_file = filename_from_module(dirname, check_name)
479 if not (check_file
and os.path.isfile(check_file)):
482 with open(check_file, encoding=
"utf8")
as f:
487 if base_class := get_base_class(code, check_file):
488 base_file = os.path.join(clang_tidy_path, dirname, base_class +
".cpp")
489 if os.path.isfile(base_file):
490 with open(base_file, encoding=
"utf8")
as f:
497 def process_doc(doc_file: Tuple[str, str]) -> Tuple[str, Optional[Match[str]]]:
498 check_name = doc_file[0] +
"-" + doc_file[1].replace(
".rst",
"")
500 with open(os.path.join(docs_dir, *doc_file),
"r", encoding=
"utf8")
as doc:
503 if match := re.search(
".*:orphan:.*", content):
507 match = re.search(
r".*:http-equiv=refresh: \d+;URL=(.*).html(.*)", content)
509 return check_name, match
511 def format_link(doc_file: Tuple[str, str]) -> str:
512 check_name, match = process_doc(doc_file)
513 if not match
and check_name
and not check_name.startswith(
"clang-analyzer-"):
514 return " :doc:`%(check_name)s <%(module)s/%(check)s>`,%(autofix)s\n" % {
515 "check_name": check_name,
516 "module": doc_file[0],
517 "check": doc_file[1].replace(
".rst",
""),
518 "autofix": has_auto_fix(check_name),
523 def format_link_alias(doc_file: Tuple[str, str]) -> str:
524 check_name, match = process_doc(doc_file)
525 if (match
or (check_name.startswith(
"clang-analyzer-")))
and check_name:
527 check_file = doc_file[1].replace(
".rst",
"")
530 or match.group(1) ==
"https://clang.llvm.org/docs/analyzer/checkers"
532 title =
"Clang Static Analyzer " + check_file
534 target =
"" if not match
else match.group(1) +
".html" + match.group(2)
539 redirect_parts = re.search(
r"^\.\./([^/]*)/([^/]*)$", match.group(1))
540 assert redirect_parts
541 title = redirect_parts[1] +
"-" + redirect_parts[2]
542 target = redirect_parts[1] +
"/" + redirect_parts[2]
543 autofix = has_auto_fix(title)
550 " :doc:`%(check_name)s <%(module)s/%(check_file)s>`, %(ref_begin)s`%(title)s <%(target)s>`%(ref_end)s,%(autofix)s\n"
552 "check_name": check_name,
554 "check_file": check_file,
558 "ref_begin": ref_begin,
565 " :doc:`%(check_name)s <%(module)s/%(check_file)s>`, %(title)s,%(autofix)s\n"
567 "check_name": check_name,
569 "check_file": check_file,
576 print(
"Updating %s..." % filename)
577 with open(filename,
"w", encoding=
"utf8", newline=
"\n")
as f:
580 if line.strip() ==
".. csv-table::":
582 f.write(
' :header: "Name", "Offers fixes"\n\n')
583 f.writelines(map(format_link, doc_files))
585 f.write(
"\nCheck aliases\n-------------\n\n")
586 f.write(
".. csv-table::\n")
587 f.write(
' :header: "Name", "Redirect", "Offers fixes"\n\n')
588 f.writelines(map(format_link_alias, doc_files))
626 language_to_extension = {
632 cpp_language_to_requirements = {
633 "c++98":
"CPlusPlus",
634 "c++11":
"CPlusPlus11",
635 "c++14":
"CPlusPlus14",
636 "c++17":
"CPlusPlus17",
637 "c++20":
"CPlusPlus20",
638 "c++23":
"CPlusPlus23",
639 "c++26":
"CPlusPlus26",
641 c_language_to_requirements = {
648 parser = argparse.ArgumentParser()
652 help=
"just update the list of documentation files, then exit",
656 help=
"language to use for new check (defaults to c++)",
657 choices=language_to_extension.keys(),
664 help=
"short description of what the check does",
665 default=
"FIXME: Write a short description",
670 help=
"Specify a specific version of the language",
673 cpp_language_to_requirements.keys(), c_language_to_requirements.keys()
681 help=
"module directory under which to place the new tidy check (e.g., misc)",
684 "check", nargs=
"?", help=
"name of new tidy check to add (e.g. foo-do-the-stuff)"
686 args = parser.parse_args()
692 if not args.module
or not args.check:
693 print(
"Module and check must be specified.")
698 check_name = args.check
700 if check_name.startswith(module):
702 'Check name "%s" must not start with the module "%s". Exiting.'
703 % (check_name, module)
706 clang_tidy_path = os.path.dirname(sys.argv[0])
707 module_path = os.path.join(clang_tidy_path, module)
714 namespace = module +
"_check"
718 description = args.description
719 if not description.endswith(
"."):
722 language = args.language
725 if args.standard
in cpp_language_to_requirements:
726 if language
and language !=
"c++":
727 raise ValueError(
"C++ standard chosen when language is not C++")
729 elif args.standard
in c_language_to_requirements:
730 if language
and language !=
"c":
731 raise ValueError(
"C standard chosen when language is not C")
737 language_restrict =
None
740 language_restrict =
"!%(lang)s.CPlusPlus"
741 if extra := c_language_to_requirements.get(args.standard,
None):
742 language_restrict += f
" && %(lang)s.{extra}"
743 elif language ==
"c++":
744 language_restrict = (
745 f
"%(lang)s.{cpp_language_to_requirements.get(args.standard, 'CPlusPlus')}"
747 elif language
in [
"objc",
"objc++"]:
748 language_restrict =
"%(lang)s.ObjC"
750 raise ValueError(f
"Unsupported language '{language}' was specified")
762 adapt_module(module_path, module, check_name, check_name_camel)
764 test_extension = language_to_extension[language]
765 write_test(module_path, module, check_name, test_extension, args.standard)
768 print(
"Done. Now it's your turn!")