File: parse-doxygen-xml.py

package info (click to toggle)
liborcus 0.20.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,872 kB
  • sloc: xml: 78,842; cpp: 75,171; sh: 4,626; makefile: 2,838; python: 2,614
file content (635 lines) | stat: -rwxr-xr-x 19,484 bytes parent folder | download | duplicates (3)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#!/usr/bin/env python
########################################################################
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
########################################################################

import sys
import xml.etree.ElementTree as ET
import argparse
import io
from dataclasses import dataclass, field
from pathlib import Path


def list_kinds(rootdir):

    compond_kinds = set()
    member_kinds = set()

    for p in rootdir.iterdir():
        tree = ET.parse(p)
        root = tree.getroot()

        for elem in root.findall(".//compounddef"):
            kind = elem.get("kind")
            if kind:
                compond_kinds.add(kind)

        for elem in root.findall(".//memberdef"):
            kind = elem.get("kind")
            if kind:
                member_kinds.add(kind)

    print("compound kinds:")
    for k in compond_kinds:
        print(f"  - {k}")

    print("member kinds:")
    for k in member_kinds:
        print(f"  - {k}")


@dataclass
class SymbolProps:
    """Extra properties of a symbol."""
    location: str = field(default=str)


@dataclass
class FuncProps(SymbolProps):
    """Extra properties of a function symbol."""
    argsstring: str = None


@dataclass
class EnumProps(SymbolProps):
    """Extra properties of a enum symbol."""

    members: list[str] = field(default_factory=list)
    """List of enum members."""


class SymbolTree:
    """Tree of namespaces."""

    def __init__(self):
        self._root = dict()

    def add(self, ns, type, name, props):
        ns_parts = ns.split("::")
        node = self._root
        for part in ns_parts:
            if part not in node:
                node[part] = dict()
            node = node[part]

        if "_store" not in node:
            node["_store"] = dict()

        store = node["_store"]
        if type not in store:
            store[type] = list()

        store[type].append((name, props))

    def _dump_recurse(self, node):
        for scope, child in node.items():
            if scope == "detail":
                continue
            if scope == "_store":
                ns = "::".join(self._scope)
                self._symbols[ns] = child
                continue
            self._scope.append(scope)
            self._dump_recurse(child)
            self._scope.pop()

    def dump(self):
        self._scope = []
        self._symbols = dict()
        self._dump_recurse(self._root)
        all_scopes = sorted(self._symbols.keys())
        for scope in all_scopes:
            print(f"namespace: {scope}")
            symbols = self._symbols[scope]
            if "enum" in symbols:
                print("  enum:")
                for symbol, props in symbols["enum"]:
                    print(f"    - name: {symbol}")
                    print(f"      location: {props.location}")
                    print(f"      members:")
                    for member in props.members:
                        print(f"        - {member}")

            if "typedef" in symbols:
                print("  typedef:")
                for symbol, props in symbols["typedef"]:
                    print(f"    - name: {symbol}")
                    print(f"      location: {props.location}")

            if "variable" in symbols:
                print("  variable:")
                for symbol, props in symbols["variable"]:
                    print(f"    - name: {symbol}")
                    print(f"      location: {props.location}")

            if "function" in symbols:
                print("  function:")
                for symbol, props in symbols["function"]:
                    print(f"    - name: {symbol}")
                    print(f"      argsstring: {props.argsstring}")
                    print(f"      location: {props.location}")

            if "class" in symbols:
                print("  class:")
                for symbol, props in symbols["class"]:
                    print(f"    - name: {symbol}")
                    print(f"      location: {props.location}")

            if "struct" in symbols:
                print("  struct:")
                for symbol, props in symbols["struct"]:
                    print(f"    - name: {symbol}")
                    print(f"      location: {props.location}")

    def walk(self, func):
        self._scope = list()
        self._walk(self._root, 0, func)

    def _walk(self, node, level, func):
        indent = " " * level * 2
        keys = sorted(node.keys())
        has_symbols = "_store" in keys
        if has_symbols:
            symbols = node["_store"]
            keys.remove("_store")
            func(self._scope, keys, symbols)
        else:
            func(self._scope, keys, {})

        for key in keys:
            child = node[key]
            self._scope.append(key)
            self._walk(child, level + 1, func)
            self._scope.pop()


def build_symbol_tree(rootdir):
    all_symbols = list()
    type_scope = set()

    for p in rootdir.iterdir():
        tree = ET.parse(p)
        root = tree.getroot()

        for ns_elem in root.findall(".//compounddef[@kind='namespace']"):
            ns_name = ns_elem.findtext("compoundname")
            for elem in ns_elem.findall(".//memberdef[@kind='enum']"):
                name = elem.findtext("name")
                location = elem.find(".//location").attrib["file"]
                members = [elem_value.findtext("name") for elem_value in elem.findall(".//enumvalue")]
                props = EnumProps(members=members, location=location)
                all_symbols.append((ns_name, "enum", name, props))

            for elem in ns_elem.findall(".//memberdef[@kind='typedef']"):
                name = elem.findtext("name")
                location = elem.find(".//location").attrib["file"]
                props = SymbolProps(location=location)
                all_symbols.append((ns_name, "typedef", name, props))

            for elem in ns_elem.findall(".//memberdef[@kind='function']"):
                name = elem.findtext("name")
                if name.startswith("operator"):
                    continue
                location = elem.find(".//location").attrib["file"]
                props = FuncProps(location=location)
                props.argsstring = elem.findtext("argsstring")
                all_symbols.append((ns_name, "function", name, props))

            for elem in ns_elem.findall(".//memberdef[@kind='variable']"):
                name = elem.findtext("name")
                location = elem.find(".//location").attrib["file"]
                props = SymbolProps(location=location)
                all_symbols.append((ns_name, "variable", name, props))

        for elem in root.findall(".//compounddef[@kind='class']"):
            name = elem.findtext("compoundname")
            location = elem.find(".//location").attrib["file"]
            ns, name = name.rsplit('::', maxsplit=1)
            props = SymbolProps(location=location)
            all_symbols.append((ns, "class", name, props))

            type_scope.add(f"{ns}::{name}")

        for elem in root.findall(".//compounddef[@kind='struct']"):
            name = elem.findtext("compoundname")
            location = elem.find(".//location").attrib["file"]
            ns, name = name.rsplit('::', maxsplit=1)
            props = SymbolProps(location=location)
            all_symbols.append((ns, "struct", name, props))

            type_scope.add(f"{ns}::{name}")

    def _to_key(x):
        return f"{x[0]}:{x[1]}:{x[2]}"

    symbol_tree = SymbolTree()

    for scope, type, name, props in sorted(all_symbols, key=_to_key):
        if scope in type_scope:
            # this is a nested inner type - skip it
            continue

        symbol_tree.add(scope, type, name, props)

    return symbol_tree


def dump_symbols(rootdir):
    symbol_tree = build_symbol_tree(rootdir)
    symbol_tree.dump()


def find_parent_dir(path: Path, name: str):
    p = path
    while p.name != name:
        p = p.parent
    return p


def create_enum_stream_test(rootdir, output_dir):
    symbol_tree = build_symbol_tree(rootdir)

    output_dir.mkdir(parents=True, exist_ok=True)

    headers = set()
    func_bodies = dict()

    # grow this list
    included_symbols = set([
        "orcus::format_t",
        "orcus::spreadsheet::auto_filter_node_op_t",
        "orcus::spreadsheet::auto_filter_op_t",
        "orcus::spreadsheet::border_style_t",
        "orcus::spreadsheet::border_style_t",
        "orcus::spreadsheet::formula_grammar_t",
        "orcus::spreadsheet::hor_alignment_t",
        "orcus::spreadsheet::pivot_axis_t",
        "orcus::spreadsheet::pivot_cache_group_by_t",
        "orcus::spreadsheet::pivot_data_subtotal_t",
        "orcus::spreadsheet::pivot_data_show_data_as_t",
        "orcus::spreadsheet::pivot_field_item_t",
        "orcus::spreadsheet::strikethrough_style_t",
        "orcus::spreadsheet::strikethrough_text_t",
        "orcus::spreadsheet::strikethrough_type_t",
        "orcus::spreadsheet::strikethrough_width_t",
        "orcus::spreadsheet::underline_count_t",
        "orcus::spreadsheet::underline_spacing_t",
        "orcus::spreadsheet::underline_style_t",
        "orcus::spreadsheet::underline_thickness_t",
        "orcus::spreadsheet::ver_alignment_t",
    ])

    # either not worth testing or cannot be tested for reasons
    skipped_symbols = set([
        "orcus::string_escape_char_t",
        "orcus::spreadsheet::error_value_t", # non-standard labels
        "orcus::yaml::detail::keyword_t",
        "orcus::yaml::detail::parse_token_t",
        "orcus::yaml::detail::scope_t",
        "orcus::yaml::node_t",
    ])

    def _func(scope, b, symbols):
        ns = "::".join(scope)
        enums = symbols.get("enum", [])
        for enum, props in enums:
            type_symbol = f"{ns}::{enum}"
            if type_symbol in skipped_symbols:
                continue

            disabled = type_symbol not in included_symbols
            print(f"\u274c {type_symbol}" if disabled else f"\u2705 {type_symbol}")
            header_path = Path(props.location)
            include_dir = find_parent_dir(header_path, "include")
            header_path = header_path.relative_to(include_dir)
            headers.add(header_path)

            strm = io.StringIO()
            # generate unit test code
            def _print(*args):
                print(*args, file=strm)

            func_name = "test_" + type_symbol.replace("::", "_")
            _print(f"void {func_name}()")
            _print("{")
            if disabled:
                _print("#if 0")
            _print("    ORCUS_TEST_FUNC_SCOPE;")
            _print()
            for m in props.members:
                mem_type = f"{type_symbol}::{m}"
                mem_value = m.replace('_', '-')
                _print(f'    {{ bool result = verify_stream_value({mem_type}, "{mem_value}"); assert(result); }}')

            if disabled:
                _print("#endif")
            _print("}")

            strm.seek(0)
            func_bodies[func_name] = strm.getvalue()

    symbol_tree.walk(_func)

    test_file = output_dir / "enum_stream_test.cpp"
    with test_file.open("w") as f:
        def _print(*args):
            print(*args, file=f)
        _print(f"// auto-generated by {Path(__file__).name}")
        _print('#include "test_global.hpp"')
        for header in sorted(headers):
            _print(f"#include <{header}>")

        _print()
        _print("using orcus::test::verify_stream_value;")
        _print()
        for func_name in sorted(func_bodies.keys()):
            func_body = func_bodies[func_name]
            _print(func_body)

        _print()
        _print("int main()")
        _print("{")
        for func_name in sorted(func_bodies.keys()):
            _print(f"    {func_name}();")
        _print()
        _print("    return EXIT_SUCCESS;")
        _print("}")


def generate_rst(thisdir, scope, child_scopes, symbols):
    include_dir = Path(__file__).parent.parent / "include"

    if scope:
        ns = "::".join(scope)
        title = "namespace " + ns
        ref_anchor = "ns-" + '-'.join(scope)
    else:
        ref_anchor = "cpp-api"
        title = "C++ API Reference"

    buf = [
        f".. _{ref_anchor}:",
        "",
        title,
        '=' * len(title),
        "",
    ]

    if "enum" in symbols:
        this_buf = [
            "Enum",
            "----",
            "",
            ".. toctree::",
            "   :maxdepth: 1",
            "",
        ]

        for name, props in symbols["enum"]:
            child_file = f"enum-{name}.rst"
            this_buf.append(f"   {child_file}")

            header = Path(props.location).relative_to(include_dir)

            full_name = f"{ns}::{name}"
            block = [
                name,
                "=" * len(name),
                "",
                f"Defined in header: <{header}>",
                "",
                f".. doxygenenum:: {full_name}",
            ]

            thisdir.joinpath(child_file).write_text("\n".join(block))

        this_buf.append("")
        buf.extend(this_buf)

    if "typedef" in symbols:
        this_buf = [
            "Type aliases",
            "------------",
            "",
            ".. toctree::",
            "   :maxdepth: 1",
            "",
        ]

        for name, props in symbols["typedef"]:
            child_file = f"typedef-{name}.rst"
            this_buf.append(f"   {child_file}")

            header = Path(props.location).relative_to(include_dir)

            full_name = f"{ns}::{name}"
            block = [
                name,
                "=" * len(name),
                "",
                f"Defined in header: <{header}>",
                "",
                f".. doxygentypedef:: {full_name}",
            ]

            thisdir.joinpath(child_file).write_text("\n".join(block))

        this_buf.append("")
        buf.extend(this_buf)

    if "variable" in symbols:
        this_buf = [
            "Constants",
            "---------",
            "",
            ".. toctree::",
            "   :maxdepth: 1",
            "",
        ]

        for name, props in symbols["variable"]:
            child_file = f"variable-{name}.rst"
            this_buf.append(f"   {child_file}")

            header = Path(props.location).relative_to(include_dir)

            full_name = f"{ns}::{name}"
            block = [
                name,
                "=" * len(name),
                "",
                f"Defined in header: <{header}>",
                "",
                f".. doxygenvariable:: {full_name}",
            ]

            thisdir.joinpath(child_file).write_text("\n".join(block))

        this_buf.append("")
        buf.extend(this_buf)

    if "function" in symbols:
        this_buf = [
            "Functions",
            "---------",
            "",
            ".. toctree::",
            "   :maxdepth: 1",
            "",
        ]

        for name, props in symbols["function"]:
            child_file = f"function-{name}.rst"
            this_buf.append(f"   {child_file}")

            header = Path(props.location).relative_to(include_dir)

            full_name = f"{ns}::{name}{props.argsstring}"
            block = [
                f"{name}",
                "=" * len(name),
                "",
                f"Defined in header: <{header}>",
                "",
                f".. doxygenfunction:: {full_name}",
            ]

            thisdir.joinpath(child_file).write_text("\n".join(block))

        this_buf.append("")
        buf.extend(this_buf)

    if "struct" in symbols:
        this_buf = [
            "Struct",
            "------",
            "",
            ".. toctree::",
            "   :maxdepth: 1",
            "",
        ]

        for name, props in symbols["struct"]:
            child_file = f"struct-{name}.rst"
            this_buf.append(f"   {child_file}")

            header = Path(props.location).relative_to(include_dir)

            full_name = f"{ns}::{name}"
            block = [
                name,
                "=" * len(name),
                "",
                f"Defined in header: <{header}>",
                "",
                f".. doxygenstruct:: {full_name}",
                f"   :members:",
            ]

            thisdir.joinpath(child_file).write_text("\n".join(block))

        this_buf.append("")
        buf.extend(this_buf)

    if "class" in symbols:
        this_buf = [
            "Classes",
            "-------",
            "",
            ".. toctree::",
            "   :maxdepth: 1",
            "",
        ]

        for name, props in symbols["class"]:
            child_file = f"class-{name}.rst"
            this_buf.append(f"   {child_file}")

            header = Path(props.location).relative_to(include_dir)

            full_name = f"{ns}::{name}"
            block = [
                name,
                "=" * len(name),
                "",
                f"Defined in header: <{header}>",
                "",
                f".. doxygenclass:: {full_name}",
                f"   :members:",
                "",
            ]

            thisdir.joinpath(child_file).write_text("\n".join(block))

        this_buf.append("")
        buf.extend(this_buf)

    if child_scopes:
        toctree = [
            "Child namespaces",
            "----------------",
            ""
            ".. toctree::",
            "   :maxdepth: 1",
            "",
        ]

        for cs in child_scopes:
            p = f"{cs}/index.rst"
            toctree.append(f"   {p}")

        buf.extend(toctree)

    return '\n'.join(buf)


def generate_doctree(rootdir, outdir):
    symbol_tree = build_symbol_tree(rootdir)

    def _func(scope, child_scopes, symbols):
        thisdir = outdir.joinpath(*scope)
        index_file = thisdir / "index.rst"
        print(index_file)
        thisdir.mkdir(parents=True, exist_ok=True)
        index_file.write_text(generate_rst(thisdir, scope, child_scopes, symbols))

    symbol_tree.walk(_func)


def main():
    parser = argparse.ArgumentParser(
        description="Parse doxygen XML outputs and perform actions.",
        epilog="""To re-generate the C++ API Reference, run this script with
        --mode doctree and specify the output directory to ./doc/cpp.  To re-generate
        the enum-stream-test code, run this script with --mode enum-test and specify
        the output directory to ./src.
        """
    )
    parser.add_argument("--mode", type=str, required=True, help="Type of action to perform.")
    parser.add_argument("--output", "-o", type=Path, help="Output directory path.")
    parser.add_argument("rootdir", type=Path, help="Directory where the doxygen XML files are found.")
    args = parser.parse_args()

    actions = {
        "kinds": (list_kinds, (args.rootdir,)),
        "symbols": (dump_symbols, (args.rootdir,)),
        "enum-test": (create_enum_stream_test, (args.rootdir, args.output)),
        "doctree": (generate_doctree, (args.rootdir, args.output))
    }

    action = actions.get(args.mode)
    if not action:
        print(f"unknown mode: {args.mode}", file=sys.stderr)
        sys.exit(1)

    func, args = action
    func(*args)


if __name__ == "__main__":
    main()