llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Vlad Serebrennikov (Endilll)

<details>
<summary>Changes</summary>

This should complete the list of things `Cursor` can be made from.

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


3 Files Affected:

- (modified) clang/bindings/python/clang/cindex.py (+4) 
- (modified) clang/bindings/python/tests/cindex/test_cursor.py (+6) 
- (modified) clang/docs/ReleaseNotes.rst (+1) 


``````````diff
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/140496
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to