-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[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
Conversation
@llvm/pr-subscribers-clang Author: Jimmy Z (jimmy-zx) ChangesFull diff: https://github.com/llvm/llvm-project/pull/152897.diff 3 Files Affected:
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
--------------
|
There was a problem hiding this 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks for the feedback! I’ve updated the enum in |
Thank you! Merged it in |
No description provided.