[libc] Switch to using the generic `<gpuintrin.h>` implementations (#121810)
[llvm-project.git] / clang / docs / tools / dump_format_help.py
blobba41ed8c02c817c445d401e6d7e865e22f89ed8a
1 #!/usr/bin/env python3
2 # A tool to parse the output of `clang-format --help` and update the
3 # documentation in ../ClangFormat.rst automatically.
5 import argparse
6 import os
7 import re
8 import subprocess
9 import sys
11 PARENT_DIR = os.path.join(os.path.dirname(__file__), "..")
12 DOC_FILE = os.path.join(PARENT_DIR, "ClangFormat.rst")
15 def substitute(text, tag, contents):
16 replacement = "\n.. START_%s\n\n%s\n\n.. END_%s\n" % (tag, contents, tag)
17 pattern = r"\n\.\. START_%s\n.*\n\.\. END_%s\n" % (tag, tag)
18 return re.sub(pattern, replacement, text, flags=re.S)
21 def indent(text, columns, indent_first_line=True):
22 indent_str = " " * columns
23 s = re.sub(r"\n([^\n])", "\n" + indent_str + "\\1", text, flags=re.S)
24 if not indent_first_line or s.startswith("\n"):
25 return s
26 return indent_str + s
29 def get_help_output():
30 args = [binary, "--help"]
31 cmd = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
32 out, _ = cmd.communicate()
33 out = out.decode(sys.stdout.encoding)
34 return out
37 def get_help_text():
38 out = get_help_output()
39 out = re.sub(r" clang-format\.exe ", " clang-format ", out)
41 out = (
42 """.. code-block:: console
44 $ clang-format --help
45 """
46 + out
48 out = indent(out, 2, indent_first_line=False)
49 return out
52 def validate(text, columns):
53 for line in text.splitlines():
54 if len(line) > columns:
55 print("warning: line too long:\n", line, file=sys.stderr)
58 p = argparse.ArgumentParser()
59 p.add_argument("-d", "--directory", help="directory of clang-format")
60 p.add_argument("-o", "--output", help="path of output file")
61 opts = p.parse_args()
63 binary = "clang-format"
64 if opts.directory:
65 binary = opts.directory + "/" + binary
67 help_text = get_help_text()
68 validate(help_text, 100)
70 with open(DOC_FILE, encoding="utf-8") as f:
71 contents = f.read()
73 contents = substitute(contents, "FORMAT_HELP", help_text)
75 with open(
76 opts.output if opts.output else DOC_FILE, "w", newline="", encoding="utf-8"
77 ) as f:
78 f.write(contents)