File: translation_unit.py

package info (click to toggle)
python-opencascade-pywrap 0.0~git20251205135501.e4d7570-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,036 kB
  • sloc: python: 1,654; pascal: 32; makefile: 13; sh: 1
file content (74 lines) | stat: -rwxr-xr-x 1,655 bytes parent folder | download
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
import logzero
import pybind11

from clang.cindex import TranslationUnit as TU

from .utils import get_index, get_includes


def parse_tu(
    path,
    input_folder,
    prefix=None,
    platform_includes=[],
    args=[
        "-x",
        "c++",
        "-std=c++17",
        "-D__CODE_GENERATOR__",
        "-Wno-deprecated-declarations",
    ],
    parsing_header="",
    tu_parsing_header="",
    platform_parsing_header="",
    target_platform=None,
):
    """Run a translation unit thorugh clang
    """

    args.append(f"-I{pybind11.get_include()}")
    args.append(f"-I{input_folder}")

    if target_platform == "Windows":
        args.append("--target=x86_64-pc-windows-msvc")
        args.append("-fms-compatibility")
        args.append("-fms-extensions")

    if prefix:
        args.append(f"--sysroot={prefix}")

    for inc in get_includes():
        args.append(f"-I{inc}")

    for inc in platform_includes:
        args.append(f"-I{inc}")

    ix = get_index()

    with open(path) as f:
        src = f.read()

    # skip possible invisible BOM character which would lead to clang error later
    if src[0] == "\ufeff":
        src = src[1:]

    dummy_code = (
        f"{parsing_header}\n{platform_parsing_header}\n{
            tu_parsing_header}\n{src}"
    )
    tr_unit = ix.parse(
        "dummy.cxx",
        args,
        unsaved_files=[("dummy.cxx", dummy_code)],
        options=TU.PARSE_INCOMPLETE,
    )

    diag = list(tr_unit.diagnostics)
    if diag:
        logzero.logger.warning(path)
        for d in diag:
            logzero.logger.warning(d)

    tr_unit.path = ("dummy.cxx", path.name)

    return tr_unit