[clang] [libclang/python] Add a few things to the python api (PR #120590)

via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 19 07:24:59 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Mathias Stearn (RedBeard0531)

<details>
<summary>Changes</summary>

I modified a local copy of cindex.py to add these when working on something and wanted to upstream the improvements.

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


1 Files Affected:

- (modified) clang/bindings/python/clang/cindex.py (+23-1) 


``````````diff
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index f8a20a1e224724..2d0c2214ec9260 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -1552,6 +1552,9 @@ def from_location(tu, location):
 
         return cursor
 
+    def __hash__(self):
+        return self.hash
+
     def __eq__(self, other):
         return conf.lib.clang_equalCursors(self, other)  # type: ignore [no-any-return]
 
@@ -1797,7 +1800,7 @@ def mangled_name(self):
         return self._mangled_name
 
     @property
-    def location(self):
+    def location(self) -> SourceLocation:
         """
         Return the source location (the starting character) of the entity
         pointed at by the cursor.
@@ -2022,6 +2025,14 @@ def lexical_parent(self):
 
         return self._lexical_parent
 
+    @property
+    def specialized_template(self):
+        """Return the base template that this cursor is a specialization of, if any."""
+        return Cursor.from_cursor_result(
+            conf.lib.clang_getSpecializedCursorTemplate(self),
+            self
+        )
+
     @property
     def translation_unit(self):
         """Returns the TranslationUnit to which this Cursor belongs."""
@@ -2143,6 +2154,9 @@ def get_bitfield_width(self):
         """
         return conf.lib.clang_getFieldDeclBitWidth(self)  # type: ignore [no-any-return]
 
+    def has_attrs(self):
+        return bool(conf.lib.clang_Cursor_hasAttrs(self))
+
     @staticmethod
     def from_result(res, arg):
         assert isinstance(res, Cursor)
@@ -3401,6 +3415,12 @@ def __str__(self):
     def __repr__(self):
         return "<File: %s>" % (self.name)
 
+    def __eq__(self, other):
+        return isinstance(other, File) and bool(conf.lib.clang_File_isEqual(self, other))
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
     @staticmethod
     def from_result(res, arg):
         assert isinstance(res, c_object_p)
@@ -3795,6 +3815,7 @@ def write_main_file_to_stdout(self):
     ("clang_getCursorType", [Cursor], Type),
     ("clang_getCursorUSR", [Cursor], _CXString),
     ("clang_Cursor_getMangling", [Cursor], _CXString),
+    ("clang_Cursor_hasAttrs", [Cursor], c_uint),
     # ("clang_getCXTUResourceUsage",
     #  [TranslationUnit],
     #  CXTUResourceUsage),
@@ -3819,6 +3840,7 @@ def write_main_file_to_stdout(self):
     ("clang_getFile", [TranslationUnit, c_interop_string], c_object_p),
     ("clang_getFileName", [File], _CXString),
     ("clang_getFileTime", [File], c_uint),
+    ("clang_File_isEqual", [File, File], c_int),
     ("clang_getIBOutletCollectionType", [Cursor], Type),
     ("clang_getIncludedFile", [Cursor], c_object_p),
     (

``````````

</details>


https://github.com/llvm/llvm-project/pull/120590


More information about the cfe-commits mailing list