File: verify_enum_integrity.py

package info (click to toggle)
duckdb 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 299,196 kB
  • sloc: cpp: 865,414; ansic: 57,292; python: 18,871; sql: 12,663; lisp: 11,751; yacc: 7,412; lex: 1,682; sh: 747; makefile: 558
file content (62 lines) | stat: -rw-r--r-- 2,256 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
from cxxheaderparser.parser import CxxParser, ParserOptions
from cxxheaderparser.visitor import CxxVisitor
from cxxheaderparser.preprocessor import make_pcpp_preprocessor
from cxxheaderparser.parserstate import NamespaceBlockState
from cxxheaderparser.types import EnumDecl
import textwrap
import os


class Visitor:
    def on_enum(self, state: NamespaceBlockState, cursor: EnumDecl) -> None:
        enum_name = cursor.typename.segments[0].format()
        if '<' in enum_name:
            raise Exception(
                "Enum '{}' is an anonymous enum, please name it\n".format(cursor.doxygen[3:] if cursor.doxygen else '')
            )

        enum_constants = dict()
        for enum_const in cursor.values:
            name = enum_const.name.format()
            if enum_const.value is None:
                raise Exception(f"Enum constant '{name}' in '{enum_name}' does not have an explicit value assignment.")
            value = enum_const.value.format()
            if value in enum_constants:
                other_constant = enum_constants[value]
                error = f"""
                        Enum '{enum_name}' contains a duplicate value:
                        Value {value} is defined for both '{other_constant}' and '{name}'
                    """
                error = textwrap.dedent(error)
                raise Exception(error)
            enum_constants[value] = name
        print(f"Successfully verified the integrity of enum {enum_name} ({len(enum_constants)} entries)")

    def __getattr__(self, name):
        return lambda *args, **kwargs: True


def parse_enum(file_path):
    # Create index
    parser = CxxParser(
        file_path,
        None,
        visitor=Visitor(),
        options=ParserOptions(preprocessor=make_pcpp_preprocessor()),
    )
    parser.parse()


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Parse a C header file and check enum integrity.")
    parser.add_argument("file_path", type=str, help="Path to the C header file")

    args = parser.parse_args()
    file_path = args.file_path

    if not os.path.exists(file_path):
        raise Exception(f"Error: file '{file_path}' does not exist")

    enum_dict = parse_enum(file_path)