-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtranslation_unit.py
More file actions
executable file
·75 lines (59 loc) · 1.64 KB
/
translation_unit.py
File metadata and controls
executable file
·75 lines (59 loc) · 1.64 KB
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
import logging
import pybind11
from clang.cindex import TranslationUnit as TU
from .utils import get_index, get_includes
logger = logging.getLogger(__name__)
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:
logger.warning(path)
for d in diag:
logger.warning(d)
tr_unit.path = ("dummy.cxx", path.name)
return tr_unit