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
|
from clang.cindex import Config, Index, Cursor
from ctypes import c_uint
from path import Path
from os import getenv
from sys import platform, prefix
from .cymbal import monkeypatch_cursor
initialized = False
ix = None
def current_platform():
rv = None
if platform.startswith("win"):
rv = "Windows"
elif platform.startswith("linux"):
rv = "Linux"
elif platform.startswith("darwin"):
rv = "OSX"
elif platform.startswith("freebsd"):
rv = "FreeBSD"
else:
raise RuntimeError(f"Unsupported platform: {platform}")
return rv
def on_windows():
rv = False
if platform.startswith("win"):
rv = True
return rv
def get_includes(rv=[]):
if rv:
pass
elif on_windows():
rv.append(Path(prefix) / "Library/include/clang/")
else:
rv.append(Path(prefix) / "lib/clang/8.0.0/include/")
rv.append(Path(prefix) / "lib/clang/6.0.1/include/")
rv.append(Path(prefix) / "lib/clang/9.0.1/include/")
rv.append(Path(prefix) / "lib/clang/10.0.1/include/")
rv.append(Path(prefix) / "include/c++/v1/")
return rv
def init_clang(path=None):
global initialized, ix
if not initialized:
Config.set_library_file('/usr/lib/llvm-20/lib/libclang.so')
# Monkeypatch clang
monkeypatch_cursor("is_virtual", "clang_isVirtualBase", [Cursor], c_uint)
monkeypatch_cursor(
"is_inline", "clang_Cursor_isFunctionInlined", [Cursor], c_uint
)
monkeypatch_cursor(
"get_specialization", "clang_getSpecializedCursorTemplate", [Cursor], Cursor
)
monkeypatch_cursor(
"get_template_kind", "clang_getTemplateCursorKind", [Cursor], c_uint
)
monkeypatch_cursor(
"get_num_overloaded_decl", "clang_getNumOverloadedDecls", [Cursor], c_uint
)
initialized = True
ix = Index.create()
def get_index():
global initialized, ix
if not initialized:
init_clang()
return ix
|