Skip to content

Commit 2ede2bf

Browse files
committed
[libclang/python] Expose clang_getCursorLanguage
1 parent b2cdd80 commit 2ede2bf

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,15 @@ def linkage(self) -> LinkageKind:
19071907

19081908
return LinkageKind.from_id(self._linkage)
19091909

1910+
@property
1911+
@cursor_null_guard
1912+
def language(self) -> LanguageKind:
1913+
"""Determine the "language" of the entity referred to by a given cursor."""
1914+
if not hasattr(self, "_language"):
1915+
self._language = conf.lib.clang_getCursorLanguage(self)
1916+
1917+
return LanguageKind.from_id(self._language)
1918+
19101919
@property
19111920
@cursor_null_guard
19121921
def tls_kind(self) -> TLSKind:
@@ -2584,6 +2593,17 @@ class LinkageKind(BaseEnumeration):
25842593
EXTERNAL = 4
25852594

25862595

2596+
class LanguageKind(BaseEnumeration):
2597+
"""
2598+
Describe the "language" of the entity referred to by a cursor.
2599+
"""
2600+
2601+
INVALID = 0
2602+
C = 1
2603+
OBJ_C = 2
2604+
C_PLUS_PLUS = 3
2605+
2606+
25872607
class TLSKind(BaseEnumeration):
25882608
"""Describes the kind of thread-local storage (TLS) of a cursor."""
25892609

@@ -4084,6 +4104,7 @@ def set_property(self, property, value):
40844104
("clang_getCursorDisplayName", [Cursor], _CXString),
40854105
("clang_getCursorExceptionSpecificationType", [Cursor], c_int),
40864106
("clang_getCursorExtent", [Cursor], SourceRange),
4107+
("clang_getCursorLanguage", [Cursor], c_int),
40874108
("clang_getCursorLexicalParent", [Cursor], Cursor),
40884109
("clang_getCursorLinkage", [Cursor], c_int),
40894110
("clang_getCursorLocation", [Cursor], SourceLocation),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
3+
from clang.cindex import Config, LanguageKind
4+
5+
if "CLANG_LIBRARY_PATH" in os.environ:
6+
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
7+
8+
import unittest
9+
10+
from .util import get_cursor, get_tu
11+
12+
13+
class TestCursorLanguage(unittest.TestCase):
14+
def test_c(self):
15+
tu = get_tu("int a;", lang="c")
16+
main_func = get_cursor(tu.cursor, "a")
17+
self.assertEqual(main_func.language, LanguageKind.C)
18+
19+
def test_c(self):
20+
tu = get_tu("class Cls {};", lang="cpp")
21+
main_func = get_cursor(tu.cursor, "Cls")
22+
self.assertEqual(main_func.language, LanguageKind.C_PLUS_PLUS)
23+
24+
def test_obj_c(self):
25+
tu = get_tu("@interface If : NSObject", lang="objc")
26+
main_func = get_cursor(tu.cursor, "If")
27+
self.assertEqual(main_func.language, LanguageKind.OBJ_C)

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ Sanitizers
309309

310310
Python Binding Changes
311311
----------------------
312+
- Exposed `clang_getCursorLanguage` via `Cursor.language`.
312313

313314
OpenMP Support
314315
--------------

0 commit comments

Comments
 (0)