Skip to content

Conversation

@DeinAlptraum
Copy link
Contributor

@DeinAlptraum DeinAlptraum commented May 15, 2025

Add a few library functions that were not previously registered to the CDLL object. The current behavior relies on the default restype to work.

Add a test to check that all used library functions are properly registered.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:as-a-library libclang and C++ API labels May 15, 2025
@llvmbot
Copy link
Member

llvmbot commented May 15, 2025

@llvm/pr-subscribers-clang

Author: Jannick Kremer (DeinAlptraum)

Changes

Add a few library functions that were not previously registered to the CDLL object. The current behavior relies on the default restype to work.

Add a test to check that all library functions are properly registered. This is not 100% reliable: for a yet unkown reason, clang_getCursorTLSKind is not in the list of attributes of the library object, despite working as intended even when not registered.


Full diff: https://github.com/llvm/llvm-project/pull/140015.diff

1 Files Affected:

  • (added) clang/bindings/python/tests/cindex/test_lib.py (+17)
diff --git a/clang/bindings/python/tests/cindex/test_lib.py b/clang/bindings/python/tests/cindex/test_lib.py
new file mode 100644
index 0000000000000..f8f892731500b
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/test_lib.py
@@ -0,0 +1,17 @@
+import os
+
+from clang.cindex import Config, conf, FUNCTION_LIST
+
+if "CLANG_LIBRARY_PATH" in os.environ:
+    Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
+
+import unittest
+
+
+class TestIndex(unittest.TestCase):
+    def test_functions_registered(self):
+        IGNORED = set(["_FuncPtr", "_name", "_handle"])
+        lib_functions = set(vars(conf.lib).keys())
+        registered_functions = set([item[0] for item in FUNCTION_LIST])
+        unregistered_functions = lib_functions - registered_functions - IGNORED
+        self.assertEqual(unregistered_functions, set())

@DeinAlptraum
Copy link
Contributor Author

@Endilll This PR adds the missing functions to FUNCTION_LIST, as well as a test that checks for completeness, as discussed here: #138103 (comment)
The test is unfortunately not 100% foolproof, since as noted clang_getCursorTLSKind is not among the "visible" attributes of the library object, despite being callable. I've tried various approaches, including inspect.getmembers, but none of them was able to find clang_getCursorTLSKind

@DeinAlptraum DeinAlptraum requested a review from Endilll May 15, 2025 08:39
@DeinAlptraum
Copy link
Contributor Author

You can see what this looks like in case of failure (missing library function) on the first CI run: https://github.com/llvm/llvm-project/actions/runs/15038723157/job/42265314399

@github-actions
Copy link

github-actions bot commented May 16, 2025

✅ With the latest revision this PR passed the Python code formatter.

@Endilll
Copy link
Contributor

Endilll commented May 16, 2025

PR title should mention somewhere that only used functions are registered.

@DeinAlptraum DeinAlptraum changed the title [libclang/python] Ensure all library functions are registered [libclang/python] Ensure all used library functions are registered May 16, 2025
@DeinAlptraum
Copy link
Contributor Author

Adapted the title and description.

@DeinAlptraum DeinAlptraum merged commit 82a9cb3 into llvm:main May 16, 2025
13 checks passed
@DeinAlptraum DeinAlptraum deleted the functionlist branch May 16, 2025 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:as-a-library libclang and C++ API clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants