Skip to content

Conversation

@jimmy-zx
Copy link
Contributor

No description provided.

@jimmy-zx jimmy-zx requested a review from DeinAlptraum as a code owner August 10, 2025 07:39
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:as-a-library libclang and C++ API labels Aug 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 10, 2025

@llvm/pr-subscribers-clang

Author: Jimmy Z (jimmy-zx)

Changes

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

3 Files Affected:

  • (modified) clang/bindings/python/clang/cindex.py (+21)
  • (added) clang/bindings/python/tests/cindex/test_cursor_language.py (+27)
  • (modified) clang/docs/ReleaseNotes.rst (+1)
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
 --------------

Copy link
Contributor

@DeinAlptraum DeinAlptraum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR!
LGTM, just one thing: please also add the new enum to the list of enums in test_enums.py.

@Endilll do you also want to take a look?

Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jimmy-zx
Copy link
Contributor Author

Thanks for the feedback! I’ve updated the enum in test_enums.py. As I don't have write permission on this repo, could you push the merge button when you have a chance?

@DeinAlptraum DeinAlptraum merged commit e20dd94 into llvm:main Aug 11, 2025
12 checks passed
@DeinAlptraum
Copy link
Contributor

Thank you! Merged it in

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.

4 participants