trolldbois / ctypeslib

Generate python ctypes classes from C headers. Requires LLVM clang
http://trolldbois.blogspot.com/search?q=ctypeslib
MIT License
218 stars 61 forks source link

solve windows clang-17.0.6 get_version() access violation exception #136

Open huhutm opened 4 months ago

huhutm commented 4 months ago

It's because that clang_getClangVersion interface return type is not c_char_p but CXString. Below is my modify:

class CXString(ctypes.Structure):
    _fields_ = [
        ('data', ctypes.POINTER(None)),
        ('private_flags', ctypes.c_uint32),
    ]

def clang_version():
    """Pull the clang C library version from the API"""
    # avoid loading the cindex API (cindex.conf.lib) to avoid version conflicts
    get_version = cindex.conf.get_cindex_library().clang_getClangVersion
    get_version.restype = CXString
    version_string = get_version()
    version_string = ctypes.cast(version_string.data, ctypes.c_char_p).value
    version = 'Unknown'
    if version_string and len(version_string) > 0:
        version_groups = re.match(br'.+version ((\d+\.)?(\d+\.)?(\*|\d+))', version_string)
        if version_groups and len(version_groups.groups()) > 0:
            version = version_groups.group(1).decode()
    return version