[clang] e20dd94 - [libclang/python] Expose `clang_getCursorLanguage` via `Cursor.language` (#152897)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 11 05:50:32 PDT 2025
Author: Jimmy Z
Date: 2025-08-11T14:50:29+02:00
New Revision: e20dd94bb39c8b0baf2f6024cb768cda09d24b06
URL: https://github.com/llvm/llvm-project/commit/e20dd94bb39c8b0baf2f6024cb768cda09d24b06
DIFF: https://github.com/llvm/llvm-project/commit/e20dd94bb39c8b0baf2f6024cb768cda09d24b06.diff
LOG: [libclang/python] Expose `clang_getCursorLanguage` via `Cursor.language` (#152897)
Added:
clang/bindings/python/tests/cindex/test_cursor_language.py
Modified:
clang/bindings/python/clang/cindex.py
clang/bindings/python/tests/cindex/test_enums.py
clang/docs/ReleaseNotes.rst
Removed:
################################################################################
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 824674309d262..812ad2cd2dc13 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -1907,6 +1907,15 @@ def linkage(self) -> LinkageKind:
return LinkageKind.from_id(self._linkage)
+ @property
+ @cursor_null_guard
+ def language(self) -> LanguageKind:
+ """Determine the "language" of the entity referred to by a given cursor."""
+ if not hasattr(self, "_language"):
+ self._language = conf.lib.clang_getCursorLanguage(self)
+
+ return LanguageKind.from_id(self._language)
+
@property
@cursor_null_guard
def tls_kind(self) -> TLSKind:
@@ -2584,6 +2593,17 @@ class LinkageKind(BaseEnumeration):
EXTERNAL = 4
+class LanguageKind(BaseEnumeration):
+ """
+ Describe the "language" of the entity referred to by a cursor.
+ """
+
+ INVALID = 0
+ C = 1
+ OBJ_C = 2
+ C_PLUS_PLUS = 3
+
+
class TLSKind(BaseEnumeration):
"""Describes the kind of thread-local storage (TLS) of a cursor."""
@@ -4084,6 +4104,7 @@ def set_property(self, property, value):
("clang_getCursorDisplayName", [Cursor], _CXString),
("clang_getCursorExceptionSpecificationType", [Cursor], c_int),
("clang_getCursorExtent", [Cursor], SourceRange),
+ ("clang_getCursorLanguage", [Cursor], c_int),
("clang_getCursorLexicalParent", [Cursor], Cursor),
("clang_getCursorLinkage", [Cursor], c_int),
("clang_getCursorLocation", [Cursor], SourceLocation),
diff --git a/clang/bindings/python/tests/cindex/test_cursor_language.py b/clang/bindings/python/tests/cindex/test_cursor_language.py
new file mode 100644
index 0000000000000..de07a7bdeef40
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/test_cursor_language.py
@@ -0,0 +1,27 @@
+import os
+
+from clang.cindex import Config, LanguageKind
+
+if "CLANG_LIBRARY_PATH" in os.environ:
+ Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
+
+import unittest
+
+from .util import get_cursor, get_tu
+
+
+class TestCursorLanguage(unittest.TestCase):
+ def test_c(self):
+ tu = get_tu("int a;", lang="c")
+ main_func = get_cursor(tu.cursor, "a")
+ self.assertEqual(main_func.language, LanguageKind.C)
+
+ def test_c(self):
+ tu = get_tu("class Cls {};", lang="cpp")
+ main_func = get_cursor(tu.cursor, "Cls")
+ self.assertEqual(main_func.language, LanguageKind.C_PLUS_PLUS)
+
+ def test_obj_c(self):
+ tu = get_tu("@interface If : NSObject", lang="objc")
+ main_func = get_cursor(tu.cursor, "If")
+ self.assertEqual(main_func.language, LanguageKind.OBJ_C)
diff --git a/clang/bindings/python/tests/cindex/test_enums.py b/clang/bindings/python/tests/cindex/test_enums.py
index 9e7f44fcf7867..48452fd4f82d0 100644
--- a/clang/bindings/python/tests/cindex/test_enums.py
+++ b/clang/bindings/python/tests/cindex/test_enums.py
@@ -6,6 +6,7 @@
BinaryOperator,
CursorKind,
ExceptionSpecificationKind,
+ LanguageKind,
LinkageKind,
RefQualifierKind,
StorageClass,
@@ -26,6 +27,7 @@ class TestEnums(unittest.TestCase):
AccessSpecifier,
TypeKind,
RefQualifierKind,
+ LanguageKind,
LinkageKind,
TLSKind,
StorageClass,
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a8b7a29933945..6b31238c279d8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -309,6 +309,7 @@ Sanitizers
Python Binding Changes
----------------------
+- Exposed `clang_getCursorLanguage` via `Cursor.language`.
OpenMP Support
--------------
More information about the cfe-commits
mailing list