https://github.com/jimmy-zx created 
https://github.com/llvm/llvm-project/pull/152897

None

>From 2ede2bff6c22ffdf7297a37da1423f586691a48e Mon Sep 17 00:00:00 2001
From: Jimmy Z <j5nx.z...@gmail.com>
Date: Sun, 10 Aug 2025 07:20:28 +0000
Subject: [PATCH] [libclang/python] Expose clang_getCursorLanguage

---
 clang/bindings/python/clang/cindex.py         | 21 +++++++++++++++
 .../tests/cindex/test_cursor_language.py      | 27 +++++++++++++++++++
 clang/docs/ReleaseNotes.rst                   |  1 +
 3 files changed, 49 insertions(+)
 create mode 100644 clang/bindings/python/tests/cindex/test_cursor_language.py

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/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
 --------------

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to