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
|
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="",
):
"""Run a translation unit thorugh clang
"""
args.append(f"-I{pybind11.get_include()}")
args.append(f"-I{input_folder}")
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
|