[clang] [libclang/python] Add a few things to the python api (PR #120590)
Mathias Stearn via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 19 07:24:06 PST 2024
https://github.com/RedBeard0531 created https://github.com/llvm/llvm-project/pull/120590
I modified a local copy of cindex.py to add these when working on something and wanted to upstream the improvements.
>From 1c68440616b555c376a3c227338f23ca80a2c777 Mon Sep 17 00:00:00 2001
From: Mathias Stearn <redbeard0531 at gmail.com>
Date: Thu, 19 Dec 2024 16:22:04 +0100
Subject: [PATCH] [libclang/python] Add a few things to the python api
---
clang/bindings/python/clang/cindex.py | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
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),
(
More information about the cfe-commits
mailing list