Author: Vlad Serebrennikov Date: 2026-10-07T05:49:57Z New Revision: bb4b866582e9b44e7f2be679b9afdf932214391d
URL: https://github.com/llvm/llvm-project/commit/bb4b866582e9b44e7f2be679b9afdf932214391d DIFF: https://github.com/llvm/llvm-project/commit/bb4b866582e9b44e7f2be679b9afdf932214391d.diff LOG: [clang][python] Inherit Diagnostic from OpaqueClangObject (#229510) Despite the ordering of classes in cindex.py, `Diagnostic` is a wrapper around an opaque C API type, so this patch makes it a derived class of `OpaqueClangObject` and drops unnecessary `__init__` and `from_param` methods across all the derived classes of `OpaqueClangObject`. Added: Modified: clang/bindings/python/clang/cindex.py Removed: ################################################################################ diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index bbfb229b7dd9e..27216a5703d5f 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -424,7 +424,24 @@ def __repr__(self) -> str: return "<SourceRange start %r, end %r>" % (self.start, self.end) -class Diagnostic: +class OpaqueClangObject: + """ + A helper for Python objects that mirror opaque types of the C API. + It stores an opaque pointer returned by the C API, and implements a + `from_param` method, allowing Python objects to be implicitly converted + to the stored opaque pointer when it is passed as an argument to the + C API. + """ + + def __init__(self, obj): + assert isinstance(obj, c_object_p) and obj + self.obj = self._as_parameter_ = obj + + def from_param(self): + return self._as_parameter_ + + +class Diagnostic(OpaqueClangObject): """ A Diagnostic is a single instance of a Clang diagnostic. It includes the diagnostic severity, the message, the location the diagnostic occurred, as @@ -445,9 +462,6 @@ class Diagnostic: DisplayCategoryName = 0x20 _FormatOptionsMask = 0x3F - def __init__(self, ptr): - self.ptr = ptr - def __del__(self): conf.lib.clang_disposeDiagnostic(self) @@ -560,9 +574,6 @@ def __repr__(self): def __str__(self): return self.format() - def from_param(self): - return self.ptr - class FixIt: """ @@ -3052,23 +3063,6 @@ def __ne__(self, other: object) -> bool: ## Opaque Clang Objects ## -class OpaqueClangObject: - """ - A helper for Python objects that mirror opaque types of the C API. - It stores an opaque pointer returned by the C API, and implements a - `from_param` method, allowing Python objects to be implicitly converted - to the stored opaque pointer when it is passed as an argument to the - C API. - """ - - def __init__(self, obj): - assert isinstance(obj, c_object_p) and obj - self.obj = self._as_parameter_ = obj - - def from_param(self): - return self._as_parameter_ - - ### Completion Chunk Kinds ### class CompletionChunkKind(BaseEnumeration): """ @@ -4042,9 +4036,6 @@ def create(tu): """ return Rewriter(conf.lib.clang_CXRewriter_create(tu)) - def __init__(self, ptr): - OpaqueClangObject.__init__(self, ptr) - def __del__(self): conf.lib.clang_CXRewriter_dispose(self) @@ -4136,9 +4127,6 @@ def create(cursor): """ return PrintingPolicy(conf.lib.clang_getCursorPrintingPolicy(cursor)) - def __init__(self, ptr): - OpaqueClangObject.__init__(self, ptr) - def __del__(self): conf.lib.clang_PrintingPolicy_dispose(self) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
