Skip to content

[libclang/python] Expose clang_getCursorLanguage via Cursor.language #152897

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 3 commits into from
Aug 11, 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
21 changes: 21 additions & 0 deletions clang/bindings/python/clang/cindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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."""

Expand Down Expand Up @@ -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),
Expand Down
27 changes: 27 additions & 0 deletions clang/bindings/python/tests/cindex/test_cursor_language.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions clang/bindings/python/tests/cindex/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
BinaryOperator,
CursorKind,
ExceptionSpecificationKind,
LanguageKind,
LinkageKind,
RefQualifierKind,
StorageClass,
Expand All @@ -26,6 +27,7 @@ class TestEnums(unittest.TestCase):
AccessSpecifier,
TypeKind,
RefQualifierKind,
LanguageKind,
LinkageKind,
TLSKind,
StorageClass,
Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ Sanitizers

Python Binding Changes
----------------------
- Exposed `clang_getCursorLanguage` via `Cursor.language`.

OpenMP Support
--------------
Expand Down