File: generate_analyzer_options_docs.py

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,235,796 kB
  • sloc: cpp: 7,617,614; ansic: 1,433,901; asm: 1,058,726; python: 252,096; f90: 94,671; objc: 70,753; lisp: 42,813; pascal: 18,401; sh: 10,032; ml: 5,111; perl: 4,720; awk: 3,523; makefile: 3,401; javascript: 2,272; xml: 892; fortran: 770
file content (293 lines) | stat: -rw-r--r-- 9,376 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
# A tool to automatically generate documentation for the config options of the
# clang static analyzer by reading `AnalyzerOptions.def`.

import argparse
from collections import namedtuple
from enum import Enum, auto
import re
import sys
import textwrap


# The following code implements a trivial parser for the narrow subset of C++
# which is used in AnalyzerOptions.def. This supports the following features:
# - ignores preprocessor directives, even if they are continued with \ at EOL
# - ignores comments: both /* ... */ and // ...
# - parses string literals (even if they contain \" escapes)
# - concatenates adjacent string literals
# - parses numbers even if they contain ' as a thousands separator
# - recognizes MACRO(arg1, arg2, ..., argN) calls


class TT(Enum):
    "Token type enum."
    number = auto()
    ident = auto()
    string = auto()
    punct = auto()


TOKENS = [
    (re.compile(r"-?[0-9']+"), TT.number),
    (re.compile(r"\w+"), TT.ident),
    (re.compile(r'"([^\\"]|\\.)*"'), TT.string),
    (re.compile(r"[(),]"), TT.punct),
    (re.compile(r"/\*((?!\*/).)*\*/", re.S), None),  # C-style comment
    (re.compile(r"//.*\n"), None),  # C++ style oneline comment
    (re.compile(r"#.*(\\\n.*)*(?<!\\)\n"), None),  # preprocessor directive
    (re.compile(r"\s+"), None),  # whitespace
]

Token = namedtuple("Token", "kind code")


class ErrorHandler:
    def __init__(self):
        self.seen_errors = False

        # This script uses some heuristical tweaks to modify the documentation
        # of some analyzer options. As this code is fragile, we record the use
        # of these tweaks and report them if they become obsolete:
        self.unused_tweaks = [
            "escape star",
            "escape underline",
            "accepted values",
            "example file content",
        ]

    def record_use_of_tweak(self, tweak_name):
        try:
            self.unused_tweaks.remove(tweak_name)
        except ValueError:
            pass

    def replace_as_tweak(self, string, pattern, repl, tweak_name):
        res = string.replace(pattern, repl)
        if res != string:
            self.record_use_of_tweak(tweak_name)
        return res

    def report_error(self, msg):
        print("Error:", msg, file=sys.stderr)
        self.seen_errors = True

    def report_unexpected_char(self, s, pos):
        lines = (s[:pos] + "X").split("\n")
        lineno, col = (len(lines), len(lines[-1]))
        self.report_error(
            "unexpected character %r in AnalyzerOptions.def at line %d column %d"
            % (s[pos], lineno, col),
        )

    def report_unused_tweaks(self):
        if not self.unused_tweaks:
            return
        _is = " is" if len(self.unused_tweaks) == 1 else "s are"
        names = ", ".join(self.unused_tweaks)
        self.report_error(f"textual tweak{_is} unused in script: {names}")


err_handler = ErrorHandler()


def tokenize(s):
    result = []
    pos = 0
    while pos < len(s):
        for regex, kind in TOKENS:
            if m := regex.match(s, pos):
                if kind is not None:
                    result.append(Token(kind, m.group(0)))
                pos = m.end()
                break
        else:
            err_handler.report_unexpected_char(s, pos)
            pos += 1
    return result


def join_strings(tokens):
    result = []
    for tok in tokens:
        if tok.kind == TT.string and result and result[-1].kind == TT.string:
            # If this token is a string, and the previous non-ignored token is
            # also a string, then merge them into a single token. We need to
            # discard the closing " of the previous string and the opening " of
            # this string.
            prev = result.pop()
            result.append(Token(TT.string, prev.code[:-1] + tok.code[1:]))
        else:
            result.append(tok)
    return result


MacroCall = namedtuple("MacroCall", "name args")


class State(Enum):
    "States of the state machine used for parsing the macro calls."
    init = auto()
    after_ident = auto()
    before_arg = auto()
    after_arg = auto()


def get_calls(tokens, macro_names):
    state = State.init
    result = []
    current = None
    for tok in tokens:
        if state == State.init and tok.kind == TT.ident and tok.code in macro_names:
            current = MacroCall(tok.code, [])
            state = State.after_ident
        elif state == State.after_ident and tok == Token(TT.punct, "("):
            state = State.before_arg
        elif state == State.before_arg:
            if current is not None:
                current.args.append(tok)
                state = State.after_arg
        elif state == State.after_arg and tok.kind == TT.punct:
            if tok.code == ")":
                result.append(current)
                current = None
                state = State.init
            elif tok.code == ",":
                state = State.before_arg
        else:
            current = None
            state = State.init
    return result


# The information will be extracted from calls to these two macros:
# #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL)
# #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC,
#                                              SHALLOW_VAL, DEEP_VAL)

MACRO_NAMES_PARAMCOUNTS = {
    "ANALYZER_OPTION": 5,
    "ANALYZER_OPTION_DEPENDS_ON_USER_MODE": 6,
}


def string_value(tok):
    if tok.kind != TT.string:
        raise ValueError(f"expected a string token, got {tok.kind.name}")
    text = tok.code[1:-1]  # Remove quotes
    text = re.sub(r"\\(.)", r"\1", text)  # Resolve backslash escapes
    return text


def cmdflag_to_rst_title(cmdflag_tok):
    text = string_value(cmdflag_tok)
    underline = "-" * len(text)
    ref = f".. _analyzer-option-{text}:"

    return f"{ref}\n\n{text}\n{underline}\n\n"


def desc_to_rst_paragraphs(tok):
    desc = string_value(tok)

    # Escape some characters that have special meaning in RST:
    desc = err_handler.replace_as_tweak(desc, "*", r"\*", "escape star")
    desc = err_handler.replace_as_tweak(desc, "_", r"\_", "escape underline")

    # Many descriptions end with "Value: <list of accepted values>", which is
    # OK for a terse command line printout, but should be prettified for web
    # documentation.
    # Moreover, the option ctu-invocation-list shows some example file content
    # which is formatted as a preformatted block.
    paragraphs = [desc]
    extra = ""
    if m := re.search(r"(^|\s)Value:", desc):
        err_handler.record_use_of_tweak("accepted values")
        paragraphs = [desc[: m.start()], "Accepted values:" + desc[m.end() :]]
    elif m := re.search(r"\s*Example file.content:", desc):
        err_handler.record_use_of_tweak("example file content")
        paragraphs = [desc[: m.start()]]
        extra = "Example file content::\n\n  " + desc[m.end() :] + "\n\n"

    wrapped = [textwrap.fill(p, width=80) for p in paragraphs if p.strip()]

    return "\n\n".join(wrapped + [""]) + extra


def default_to_rst(tok):
    if tok.kind == TT.string:
        if tok.code == '""':
            return "(empty string)"
        return tok.code
    if tok.kind == TT.ident:
        return tok.code
    if tok.kind == TT.number:
        return tok.code.replace("'", "")
    raise ValueError(f"unexpected token as default value: {tok.kind.name}")


def defaults_to_rst_paragraph(defaults):
    strs = [default_to_rst(d) for d in defaults]

    if len(strs) == 1:
        return f"Default value: {strs[0]}\n\n"
    if len(strs) == 2:
        return (
            f"Default value: {strs[0]} (in shallow mode) / {strs[1]} (in deep mode)\n\n"
        )
    raise ValueError("unexpected count of default values: %d" % len(defaults))


def macro_call_to_rst_paragraphs(macro_call):
    try:
        arg_count = len(macro_call.args)
        param_count = MACRO_NAMES_PARAMCOUNTS[macro_call.name]
        if arg_count != param_count:
            raise ValueError(
                f"expected {param_count} arguments for {macro_call.name}, found {arg_count}"
            )

        _, _, cmdflag, desc, *defaults = macro_call.args

        return (
            cmdflag_to_rst_title(cmdflag)
            + desc_to_rst_paragraphs(desc)
            + defaults_to_rst_paragraph(defaults)
        )
    except ValueError as ve:
        err_handler.report_error(ve.args[0])
        return ""


def get_option_list(input_file):
    with open(input_file, encoding="utf-8") as f:
        contents = f.read()
    tokens = join_strings(tokenize(contents))
    macro_calls = get_calls(tokens, MACRO_NAMES_PARAMCOUNTS)

    result = ""
    for mc in macro_calls:
        result += macro_call_to_rst_paragraphs(mc)
    return result


p = argparse.ArgumentParser()
p.add_argument("--options-def", help="path to AnalyzerOptions.def")
p.add_argument("--template", help="template file")
p.add_argument("--out", help="output file")
opts = p.parse_args()

with open(opts.template, encoding="utf-8") as f:
    doc_template = f.read()

PLACEHOLDER = ".. OPTIONS_LIST_PLACEHOLDER\n"

rst_output = doc_template.replace(PLACEHOLDER, get_option_list(opts.options_def))

err_handler.report_unused_tweaks()

with open(opts.out, "w", newline="", encoding="utf-8") as f:
    f.write(rst_output)

if err_handler.seen_errors:
    sys.exit(1)