https://github.com/Endilll created https://github.com/llvm/llvm-project/pull/140496
This should complete the list of things `Cursor` can be made from. >From 2fa8240df89d195c25da9d0ee52101830ae41677 Mon Sep 17 00:00:00 2001 From: Vlad Serebrennikov <serebrennikov.vladis...@gmail.com> Date: Mon, 19 May 2025 09:20:55 +0300 Subject: [PATCH] [libclang/python] Add `Cursor.from_translation_unit` --- clang/bindings/python/clang/cindex.py | 4 ++++ clang/bindings/python/tests/cindex/test_cursor.py | 6 ++++++ clang/docs/ReleaseNotes.rst | 1 + 3 files changed, 11 insertions(+) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index f65bcad780a70..615b70d5afb0f 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -1595,6 +1595,10 @@ class Cursor(Structure): def from_location(tu: TranslationUnit, location: SourceLocation) -> Cursor | None: return Cursor.from_result(conf.lib.clang_getCursor(tu, location), tu) + @staticmethod + def from_translation_unit(tu: TranslationUnit) -> Cursor | None: + return Cursor.from_result(conf.lib.clang_getTranslationUnitCursor(tu), tu) + # This function is not null-guarded because it is used in cursor_null_guard itself def __eq__(self, other: object) -> bool: if not isinstance(other, Cursor): diff --git a/clang/bindings/python/tests/cindex/test_cursor.py b/clang/bindings/python/tests/cindex/test_cursor.py index eb0d1d50601a6..aeb387f7a6f62 100644 --- a/clang/bindings/python/tests/cindex/test_cursor.py +++ b/clang/bindings/python/tests/cindex/test_cursor.py @@ -1064,3 +1064,9 @@ def test_null_cursor(self): nc.is_definition() with self.assertRaises(Exception): nc.spelling + + def test_cursor_from_tu(self): + tu = get_tu("int a = 0;") + cursor = Cursor.from_translation_unit(tu) + reference_cursor = tu.cursor + self.assertEqual(cursor, reference_cursor) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 4a3c1bee82831..8676c5a5e2cd5 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -959,6 +959,7 @@ Sanitizers Python Binding Changes ---------------------- - Made ``Cursor`` hashable. +- Added ``Cursor.from_translation_unit``, mirroring ``TranslationUnit.cursor``. - Added ``Cursor.has_attrs``, a binding for ``clang_Cursor_hasAttrs``, to check whether a cursor has any attributes. - Added ``Cursor.specialized_template``, a binding for _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits