Skip to content

[libclang/python] Ensure all used library functions are registered #140015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions clang/bindings/python/clang/cindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3952,6 +3952,7 @@ def set_property(self, property, value):
("clang_equalRanges", [SourceRange, SourceRange], bool),
("clang_equalTypes", [Type, Type], bool),
("clang_formatDiagnostic", [Diagnostic, c_uint], _CXString),
("clang_getAddressSpace", [Type], c_uint),
("clang_getArgType", [Type, c_uint], Type),
("clang_getArrayElementType", [Type], Type),
("clang_getArraySize", [Type], c_longlong),
Expand All @@ -3970,8 +3971,10 @@ def set_property(self, property, value):
("clang_getCursorAvailability", [Cursor], c_int),
("clang_getCursorDefinition", [Cursor], Cursor),
("clang_getCursorDisplayName", [Cursor], _CXString),
("clang_getCursorExceptionSpecificationType", [Cursor], c_int),
("clang_getCursorExtent", [Cursor], SourceRange),
("clang_getCursorLexicalParent", [Cursor], Cursor),
("clang_getCursorLinkage", [Cursor], c_int),
("clang_getCursorLocation", [Cursor], SourceLocation),
("clang_getCursorPrettyPrinted", [Cursor, PrintingPolicy], _CXString),
("clang_getCursorPrintingPolicy", [Cursor], c_object_p),
Expand All @@ -3980,6 +3983,7 @@ def set_property(self, property, value):
("clang_getCursorResultType", [Cursor], Type),
("clang_getCursorSemanticParent", [Cursor], Cursor),
("clang_getCursorSpelling", [Cursor], _CXString),
("clang_getCursorTLSKind", [Cursor], c_int),
("clang_getCursorType", [Cursor], Type),
("clang_getCursorUSR", [Cursor], _CXString),
("clang_Cursor_getMangling", [Cursor], _CXString),
Expand All @@ -4005,6 +4009,7 @@ def set_property(self, property, value):
("clang_getEnumConstantDeclUnsignedValue", [Cursor], c_ulonglong),
("clang_getEnumConstantDeclValue", [Cursor], c_longlong),
("clang_getEnumDeclIntegerType", [Cursor], Type),
("clang_getExceptionSpecificationType", [Type], c_int),
("clang_getFile", [TranslationUnit, c_interop_string], c_object_p),
("clang_getFileName", [File], _CXString),
("clang_getFileTime", [File], c_uint),
Expand Down Expand Up @@ -4101,6 +4106,7 @@ def set_property(self, property, value):
("clang_Cursor_getBriefCommentText", [Cursor], _CXString),
("clang_Cursor_getRawCommentText", [Cursor], _CXString),
("clang_Cursor_getOffsetOfField", [Cursor], c_longlong),
("clang_Cursor_getStorageClass", [Cursor], c_int),
("clang_Cursor_isAnonymous", [Cursor], bool),
("clang_Cursor_isAnonymousRecordDecl", [Cursor], bool),
("clang_Cursor_isBitField", [Cursor], bool),
Expand Down
31 changes: 31 additions & 0 deletions clang/bindings/python/tests/cindex/test_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os

import clang.cindex

if "CLANG_LIBRARY_PATH" in os.environ:
clang.cindex.Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])

import unittest
import ast


class TestLib(unittest.TestCase):
def test_functions_registered(self):
def get_function_spelling(node):
# The call expressions we are interested in have
# their spelling in .attr, not .id
if hasattr(node, "attr"):
return node.attr
return ""

filename = clang.cindex.__file__
with open(filename) as file:
root = ast.parse(file.read())
functions = [
get_function_spelling(node.func)
for node in ast.walk(root)
if isinstance(node, ast.Call)
]
used_functions = set([func for func in functions if func.startswith("clang_")])
registered_functions = set([item[0] for item in clang.cindex.FUNCTION_LIST])
self.assertEqual(used_functions - registered_functions, set())