clang-tools 23.0.0git
check_update_docs.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# ===-----------------------------------------------------------------------===#
4#
5# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6# See https://llvm.org/LICENSE.txt for license information.
7# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8#
9# ===-----------------------------------------------------------------------===#
10
11"""
12
13Clang-Tidy Check List Checker
14=============================
15
16This wrapper script runs `add_new_check.py --update-docs` on
17a temporary copy of clang-tools-extra/{clang-tidy,docs} and
18writes the generated docs/clang-tidy/checks/list.rst to the
19requested output path.
20"""
21
22import argparse
23import io
24import os
25import shutil
26import subprocess
27import sys
28import tempfile
29from typing import Final, Sequence
30
31
32EXTRA_DIR: Final = os.path.join(os.path.dirname(__file__), "../..")
33CLANG_TIDY_DIR: Final = os.path.join(EXTRA_DIR, "clang-tidy")
34DOCS_DIR: Final = os.path.join(EXTRA_DIR, "docs")
35LIST_DOC: Final = os.path.join(DOCS_DIR, "clang-tidy", "checks", "list.rst")
36
37
38def read_text(path: str) -> str:
39 with io.open(path, "r", encoding="utf-8") as f:
40 return f.read()
41
42
43def write_text(path: str, content: str) -> None:
44 with io.open(path, "w", encoding="utf-8", newline="") as f:
45 f.write(content)
46
47
49 with tempfile.TemporaryDirectory() as td:
50 temp_root = os.path.join(td, "clang-tools-extra")
51 temp_clang_tidy_dir = os.path.join(temp_root, "clang-tidy")
52 temp_docs_dir = os.path.join(temp_root, "docs")
53
54 shutil.copytree(CLANG_TIDY_DIR, temp_clang_tidy_dir)
55 shutil.copytree(DOCS_DIR, temp_docs_dir)
56
57 subprocess.run(
58 [
59 sys.executable,
60 os.path.join(temp_clang_tidy_dir, "add_new_check.py"),
61 "--update-docs",
62 ],
63 cwd=temp_clang_tidy_dir,
64 check=True,
65 stdout=subprocess.PIPE,
66 stderr=subprocess.STDOUT,
67 text=True,
68 )
69
70 return read_text(
71 os.path.join(temp_docs_dir, "clang-tidy", "checks", "list.rst")
72 )
73
74
75def main(argv: Sequence[str]) -> int:
76 ap = argparse.ArgumentParser()
77 ap.add_argument("-o", "--output", dest="out", required=True)
78 args = ap.parse_args(argv)
79
80 generated = generate_updated_list()
81 write_text(args.out, generated)
82
83 if read_text(LIST_DOC) != generated:
84 sys.stderr.write(
85 "\n'clang-tools-extra/docs/clang-tidy/checks/list.rst' is out of date.\n"
86 "Fix it by running 'clang-tools-extra/clang-tidy/add_new_check.py --update-docs'.\n\n"
87 )
88
89 return 0
90
91
92if __name__ == "__main__":
93 sys.exit(main(sys.argv[1:]))
str read_text(str path)
None write_text(str path, str content)
int main(Sequence[str] argv)