llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Jannick Kremer (DeinAlptraum) <details> <summary>Changes</summary> This is a PoC for now --- Patch is 34.24 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/142120.diff 1 Files Affected: - (modified) clang/bindings/python/clang/cindex.py (+583-250) ``````````diff diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 6f7243cdf80ac..889df2e2f0a87 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -78,6 +78,7 @@ c_void_p, cast, cdll, + _Pointer, py_object, ) @@ -93,6 +94,7 @@ Iterator, Literal, Optional, + Protocol, Sequence, Type as TType, TypeVar, @@ -101,8 +103,7 @@ ) if TYPE_CHECKING: - from ctypes import _Pointer - from typing_extensions import Protocol, TypeAlias + from typing_extensions import TypeAlias StrPath: TypeAlias = TUnion[str, os.PathLike[str]] LibFunc: TypeAlias = TUnion[ @@ -3986,256 +3987,588 @@ def set_property(self, property, value): # Now comes the plumbing to hook up the C library. # Register callback types -translation_unit_includes_callback = CFUNCTYPE( +translation_unit_includes_callback: TypeAlias = CFUNCTYPE( # type: ignore [valid-type] None, c_object_p, POINTER(SourceLocation), c_uint, py_object ) -cursor_visit_callback = CFUNCTYPE(c_int, Cursor, Cursor, py_object) -fields_visit_callback = CFUNCTYPE(c_int, Cursor, py_object) +cursor_visit_callback: TypeAlias = CFUNCTYPE(c_int, Cursor, Cursor, py_object) # type: ignore [valid-type] +fields_visit_callback: TypeAlias = CFUNCTYPE(c_int, Cursor, py_object) # type: ignore [valid-type] + + +def _get_annotations() -> list[LibFunc]: + def str_to_type(typename: str) -> type: + # The _Pointer types are instantiated dynamically at runtime + # so convert manually accordingly + if typename.startswith("_Pointer[") and typename.endswith("]"): + inner_type = typename[9:-1] + inner_mapped_type = str_to_type(inner_type) + return POINTER(inner_mapped_type) + return globals_flat[typename] + + # Get all globally available names and flatten the builtins onto the + # top level of the dict. In the case of duplicates the builtin is + # overwritten with the non-builtin + gl = globals() + builtins = gl.pop("__builtins__") + globals_flat: dict[str, type] = {**builtins, **gl} + + func_names = {attr for attr in vars(ClangLib) if attr.startswith("clang_")} + annotation_types: list[LibFunc] = [] + for func_name in func_names: + annotation = getattr(ClangLib, func_name).__annotations__ + ret_type = str_to_type(annotation.pop("return")) + arg_types = [str_to_type(annotation[key]) for key in annotation.keys()] + annotation_types.append((func_name, arg_types, ret_type)) + return annotation_types + # Functions strictly alphabetical order. -FUNCTION_LIST: list[LibFunc] = [ - ( - "clang_annotateTokens", - [TranslationUnit, POINTER(Token), c_uint, POINTER(Cursor)], - ), - ("clang_CompilationDatabase_dispose", [c_object_p]), - ( - "clang_CompilationDatabase_fromDirectory", - [c_interop_string, POINTER(c_uint)], - c_object_p, - ), - ("clang_CompilationDatabase_getAllCompileCommands", [c_object_p], c_object_p), - ( - "clang_CompilationDatabase_getCompileCommands", - [c_object_p, c_interop_string], - c_object_p, - ), - ("clang_CompileCommands_dispose", [c_object_p]), - ("clang_CompileCommands_getCommand", [c_object_p, c_uint], c_object_p), - ("clang_CompileCommands_getSize", [c_object_p], c_uint), - ("clang_CompileCommand_getArg", [c_object_p, c_uint], _CXString), - ("clang_CompileCommand_getDirectory", [c_object_p], _CXString), - ("clang_CompileCommand_getFilename", [c_object_p], _CXString), - ("clang_CompileCommand_getNumArgs", [c_object_p], c_uint), - ( - "clang_codeCompleteAt", - [TranslationUnit, c_interop_string, c_int, c_int, c_void_p, c_int, c_int], - POINTER(CCRStructure), - ), - ("clang_codeCompleteGetDiagnostic", [CodeCompletionResults, c_int], Diagnostic), - ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), - ("clang_createIndex", [c_int, c_int], c_object_p), - ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), - ("clang_CXRewriter_create", [TranslationUnit], c_object_p), - ("clang_CXRewriter_dispose", [Rewriter]), - ("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), - ("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), - ("clang_CXRewriter_removeText", [Rewriter, SourceRange]), - ("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), - ("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), - ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), - ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), - ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), - ("clang_CXXConstructor_isMoveConstructor", [Cursor], bool), - ("clang_CXXField_isMutable", [Cursor], bool), - ("clang_CXXMethod_isConst", [Cursor], bool), - ("clang_CXXMethod_isDefaulted", [Cursor], bool), - ("clang_CXXMethod_isDeleted", [Cursor], bool), - ("clang_CXXMethod_isCopyAssignmentOperator", [Cursor], bool), - ("clang_CXXMethod_isMoveAssignmentOperator", [Cursor], bool), - ("clang_CXXMethod_isExplicit", [Cursor], bool), - ("clang_CXXMethod_isPureVirtual", [Cursor], bool), - ("clang_CXXMethod_isStatic", [Cursor], bool), - ("clang_CXXMethod_isVirtual", [Cursor], bool), - ("clang_CXXRecord_isAbstract", [Cursor], bool), - ("clang_EnumDecl_isScoped", [Cursor], bool), - ("clang_defaultDiagnosticDisplayOptions", [], c_uint), - ("clang_defaultSaveOptions", [TranslationUnit], c_uint), - ("clang_disposeCodeCompleteResults", [CodeCompletionResults]), - # ("clang_disposeCXTUResourceUsage", - # [CXTUResourceUsage]), - ("clang_disposeDiagnostic", [Diagnostic]), - ("clang_disposeIndex", [Index]), - ("clang_disposeString", [_CXString]), - ("clang_disposeTokens", [TranslationUnit, POINTER(Token), c_uint]), - ("clang_disposeTranslationUnit", [TranslationUnit]), - ("clang_equalCursors", [Cursor, Cursor], bool), - ("clang_equalLocations", [SourceLocation, SourceLocation], bool), - ("clang_equalRanges", [SourceRange, SourceRange], bool), - ("clang_equalTypes", [Type, Type], bool), - ("clang_formatDiagnostic", [Diagnostic, c_uint], _CXString), - ("clang_getAddressSpace", [Type], c_uint), - ("clang_getArgType", [Type, c_uint], Type), - ("clang_getArrayElementType", [Type], Type), - ("clang_getArraySize", [Type], c_longlong), - ("clang_getFieldDeclBitWidth", [Cursor], c_int), - ("clang_getCanonicalCursor", [Cursor], Cursor), - ("clang_getCanonicalType", [Type], Type), - ("clang_getChildDiagnostics", [Diagnostic], c_object_p), - ("clang_getCompletionAvailability", [c_void_p], c_int), - ("clang_getCompletionBriefComment", [c_void_p], _CXString), - ("clang_getCompletionChunkCompletionString", [c_void_p, c_int], c_object_p), - ("clang_getCompletionChunkKind", [c_void_p, c_int], c_int), - ("clang_getCompletionChunkText", [c_void_p, c_int], _CXString), - ("clang_getCompletionPriority", [c_void_p], c_int), - ("clang_getCString", [_CXString], c_interop_string), - ("clang_getCursor", [TranslationUnit, SourceLocation], Cursor), - ("clang_getCursorAvailability", [Cursor], c_int), - ("clang_getCursorDefinition", [Cursor], Cursor), - ("clang_getCursorDisplayName", [Cursor], _CXString), - ("clang_getCursorExceptionSpecificationType", [Cursor], c_int), - ("clang_getCursorExtent", [Cursor], SourceRange), - ("clang_getCursorLexicalParent", [Cursor], Cursor), - ("clang_getCursorLinkage", [Cursor], c_int), - ("clang_getCursorLocation", [Cursor], SourceLocation), - ("clang_getCursorPrettyPrinted", [Cursor, PrintingPolicy], _CXString), - ("clang_getCursorPrintingPolicy", [Cursor], c_object_p), - ("clang_getCursorReferenced", [Cursor], Cursor), - ("clang_getCursorReferenceNameRange", [Cursor, c_uint, c_uint], SourceRange), - ("clang_getCursorResultType", [Cursor], Type), - ("clang_getCursorSemanticParent", [Cursor], Cursor), - ("clang_getCursorSpelling", [Cursor], _CXString), - ("clang_getCursorTLSKind", [Cursor], c_int), - ("clang_getCursorType", [Cursor], Type), - ("clang_getCursorUSR", [Cursor], _CXString), - ("clang_Cursor_getMangling", [Cursor], _CXString), - ("clang_Cursor_hasAttrs", [Cursor], c_uint), - # ("clang_getCXTUResourceUsage", - # [TranslationUnit], - # CXTUResourceUsage), - ("clang_getCXXAccessSpecifier", [Cursor], c_uint), - ("clang_getDeclObjCTypeEncoding", [Cursor], _CXString), - ("clang_getDiagnostic", [c_object_p, c_uint], c_object_p), - ("clang_getDiagnosticCategory", [Diagnostic], c_uint), - ("clang_getDiagnosticCategoryText", [Diagnostic], _CXString), - ("clang_getDiagnosticFixIt", [Diagnostic, c_uint, POINTER(SourceRange)], _CXString), - ("clang_getDiagnosticInSet", [c_object_p, c_uint], c_object_p), - ("clang_getDiagnosticLocation", [Diagnostic], SourceLocation), - ("clang_getDiagnosticNumFixIts", [Diagnostic], c_uint), - ("clang_getDiagnosticNumRanges", [Diagnostic], c_uint), - ("clang_getDiagnosticOption", [Diagnostic, POINTER(_CXString)], _CXString), - ("clang_getDiagnosticRange", [Diagnostic, c_uint], SourceRange), - ("clang_getDiagnosticSeverity", [Diagnostic], c_int), - ("clang_getDiagnosticSpelling", [Diagnostic], _CXString), - ("clang_getElementType", [Type], Type), - ("clang_getEnumConstantDeclUnsignedValue", [Cursor], c_ulonglong), - ("clang_getEnumConstantDeclValue", [Cursor], c_longlong), - ("clang_getEnumDeclIntegerType", [Cursor], Type), - ("clang_getExceptionSpecificationType", [Type], c_int), - ("clang_getFile", [TranslationUnit, c_interop_string], c_object_p), - ("clang_getFileName", [File], _CXString), - ("clang_getFileTime", [File], c_uint), - ("clang_File_isEqual", [File, File], bool), - ("clang_getIBOutletCollectionType", [Cursor], Type), - ("clang_getIncludedFile", [Cursor], c_object_p), - ( - "clang_getInclusions", - [TranslationUnit, translation_unit_includes_callback, py_object], - ), - ( - "clang_getInstantiationLocation", - [ - SourceLocation, - POINTER(c_object_p), - POINTER(c_uint), - POINTER(c_uint), - POINTER(c_uint), - ], - ), - ("clang_getLocation", [TranslationUnit, File, c_uint, c_uint], SourceLocation), - ("clang_getLocationForOffset", [TranslationUnit, File, c_uint], SourceLocation), - ("clang_getNullCursor", None, Cursor), - ("clang_getNumArgTypes", [Type], c_uint), - ("clang_getNumCompletionChunks", [c_void_p], c_int), - ("clang_getNumDiagnostics", [c_object_p], c_uint), - ("clang_getNumDiagnosticsInSet", [c_object_p], c_uint), - ("clang_getNumElements", [Type], c_longlong), - ("clang_getNumOverloadedDecls", [Cursor], c_uint), - ("clang_getOffsetOfBase", [Cursor, Cursor], c_longlong), - ("clang_getOverloadedDecl", [Cursor, c_uint], Cursor), - ("clang_getPointeeType", [Type], Type), - ("clang_getRange", [SourceLocation, SourceLocation], SourceRange), - ("clang_getRangeEnd", [SourceRange], SourceLocation), - ("clang_getRangeStart", [SourceRange], SourceLocation), - ("clang_getResultType", [Type], Type), - ("clang_getSpecializedCursorTemplate", [Cursor], Cursor), - ("clang_getTemplateCursorKind", [Cursor], c_uint), - ("clang_getTokenExtent", [TranslationUnit, Token], SourceRange), - ("clang_getTokenKind", [Token], c_uint), - ("clang_getTokenLocation", [TranslationUnit, Token], SourceLocation), - ("clang_getTokenSpelling", [TranslationUnit, Token], _CXString), - ("clang_getTranslationUnitCursor", [TranslationUnit], Cursor), - ("clang_getTranslationUnitSpelling", [TranslationUnit], _CXString), - ("clang_getTUResourceUsageName", [c_uint], c_interop_string), - ("clang_getTypeDeclaration", [Type], Cursor), - ("clang_getTypedefDeclUnderlyingType", [Cursor], Type), - ("clang_getTypedefName", [Type], _CXString), - ("clang_getTypeKindSpelling", [c_uint], _CXString), - ("clang_getTypePrettyPrinted", [Type, PrintingPolicy], _CXString), - ("clang_getTypeSpelling", [Type], _CXString), - ("clang_hashCursor", [Cursor], c_uint), - ("clang_isAttribute", [CursorKind], bool), - ("clang_getFullyQualifiedName", [Type, PrintingPolicy, c_uint], _CXString), - ("clang_isConstQualifiedType", [Type], bool), - ("clang_isCursorDefinition", [Cursor], bool), - ("clang_isDeclaration", [CursorKind], bool), - ("clang_isExpression", [CursorKind], bool), - ("clang_isFileMultipleIncludeGuarded", [TranslationUnit, File], bool), - ("clang_isFunctionTypeVariadic", [Type], bool), - ("clang_isInvalid", [CursorKind], bool), - ("clang_isPODType", [Type], bool), - ("clang_isPreprocessing", [CursorKind], bool), - ("clang_isReference", [CursorKind], bool), - ("clang_isRestrictQualifiedType", [Type], bool), - ("clang_isStatement", [CursorKind], bool), - ("clang_isTranslationUnit", [CursorKind], bool), - ("clang_isUnexposed", [CursorKind], bool), - ("clang_isVirtualBase", [Cursor], bool), - ("clang_isVolatileQualifiedType", [Type], bool), - ("clang_isBeforeInTranslationUnit", [SourceLocation, SourceLocation], bool), - ( - "clang_parseTranslationUnit", - [Index, c_interop_string, c_void_p, c_int, c_void_p, c_int, c_int], - c_object_p, - ), - ("clang_reparseTranslationUnit", [TranslationUnit, c_int, c_void_p, c_int], c_int), - ("clang_saveTranslationUnit", [TranslationUnit, c_interop_string, c_uint], c_int), - ( - "clang_tokenize", - [TranslationUnit, SourceRange, POINTER(POINTER(Token)), POINTER(c_uint)], - ), - ("clang_visitChildren", [Cursor, cursor_visit_callback, py_object], c_uint), - ("clang_visitCXXBaseClasses", [Type, fields_visit_callback, py_object], c_uint), - ("clang_visitCXXMethods", [Type, fields_visit_callback, py_object], c_uint), - ("clang_Cursor_getNumArguments", [Cursor], c_int), - ("clang_Cursor_getArgument", [Cursor, c_uint], Cursor), - ("clang_Cursor_getNumTemplateArguments", [Cursor], c_int), - ("clang_Cursor_getTemplateArgumentKind", [Cursor, c_uint], c_uint), - ("clang_Cursor_getTemplateArgumentType", [Cursor, c_uint], Type), - ("clang_Cursor_getTemplateArgumentValue", [Cursor, c_uint], c_longlong), - ("clang_Cursor_getTemplateArgumentUnsignedValue", [Cursor, c_uint], c_ulonglong), - ("clang_getCursorBinaryOperatorKind", [Cursor], c_int), - ("clang_Cursor_getBriefCommentText", [Cursor], _CXString), - ("clang_Cursor_getRawCommentText", [Cursor], _CXString), - ("clang_Cursor_getOffsetOfField", [Cursor], c_longlong), - ("clang_Cursor_getStorageClass", [Cursor], c_int), - ("clang_Cursor_isAnonymous", [Cursor], bool), - ("clang_Cursor_isAnonymousRecordDecl", [Cursor], bool), - ("clang_Cursor_isBitField", [Cursor], bool), - ("clang_Location_isInSystemHeader", [SourceLocation], bool), - ("clang_PrintingPolicy_dispose", [PrintingPolicy]), - ("clang_PrintingPolicy_getProperty", [PrintingPolicy, c_int], c_uint), - ("clang_PrintingPolicy_setProperty", [PrintingPolicy, c_int, c_uint]), - ("clang_Type_getAlignOf", [Type], c_longlong), - ("clang_Type_getClassType", [Type], Type), - ("clang_Type_getNumTemplateArguments", [Type], c_int), - ("clang_Type_getTemplateArgumentAsType", [Type, c_uint], Type), - ("clang_Type_getOffsetOf", [Type, c_interop_string], c_longlong), - ("clang_Type_getSizeOf", [Type], c_longlong), - ("clang_Type_getCXXRefQualifier", [Type], c_uint), - ("clang_Type_getNamedType", [Type], Type), - ("clang_Type_visitFields", [Type, fields_visit_callback, py_object], c_uint), -] +class ClangLib(Protocol): + + def clang_annotateTokens( + self, + arg1: TranslationUnit, + arg2: _Pointer[Token], + arg3: c_uint, + arg4: _Pointer[Cursor], + ) -> None: ... + + def clang_CompilationDatabase_dispose(self, arg1: _Pointer[c_void_p]) -> None: ... + + def clang_CompilationDatabase_fromDirectory( + self, arg1: c_interop_string, arg2: _Pointer[c_uint] + ) -> _Pointer[c_void_p]: ... + + def clang_CompilationDatabase_getAllCompileCommands( + self, arg1: _Pointer[c_void_p] + ) -> _Pointer[c_void_p]: ... + + def clang_CompilationDatabase_getCompileCommands( + self, arg1: _Pointer[c_void_p], arg2: c_interop_string + ) -> _Pointer[c_void_p]: ... + + def clang_CompileCommands_dispose(self, arg1: _Pointer[c_void_p]) -> None: ... + + def clang_CompileCommands_getCommand( + self, arg1: _Pointer[c_void_p], arg2: c_uint + ) -> _Pointer[c_void_p]: ... + + def clang_CompileCommands_getSize(self, arg1: _Pointer[c_void_p]) -> c_uint: ... + + def clang_CompileCommand_getArg( + self, arg1: _Pointer[c_void_p], arg2: c_uint + ) -> _CXString: ... + + def clang_CompileCommand_getDirectory( + self, arg1: _Pointer[c_void_p] + ) -> _CXString: ... + + def clang_CompileCommand_getFilename( + self, arg1: _Pointer[c_void_p] + ) -> _CXString: ... + + def clang_CompileCommand_getNumArgs(self, arg1: _Pointer[c_void_p]) -> c_uint: ... + + def clang_codeCompleteAt( + self, + arg1: TranslationUnit, + arg2: c_interop_string, + arg3: c_int, + arg4: c_int, + arg5: c_void_p, + arg6: c_int, + arg7: c_int, + ) -> _Pointer[CCRStructure]: ... + + def clang_codeCompleteGetDiagnostic( + self, arg1: CodeCompletionResults, arg2: c_int + ) -> Diagnostic: ... + + def clang_codeCompleteGetNumDiagnostics( + self, arg1: CodeCompletionResults + ) -> c_int: ... + + def clang_createIndex(self, arg1: c_int, arg2: c_int) -> _Pointer[c_void_p]: ... + + def clang_createTranslationUnit( + self, arg1: Index, arg2: c_interop_string + ) -> _Pointer[c_void_p]: ... + + def clang_CXRewriter_create(self, arg1: TranslationUnit) -> _Pointer[c_void_p]: ... + + def clang_CXRewriter_dispose(self, arg1: Rewriter) -> None: ... + + def clang_CXRewriter_insertTextBefore( + self, arg1: Rewriter, arg2: SourceLocation, arg3: c_interop_string + ) -> None: ... + + def clang_CXRewriter_overwriteChangedFiles(self, arg1: Rewriter) -> c_int: ... + + def clang_CXRewriter_removeText( + self, arg1: Rewriter, arg2: SourceRange + ) -> None: ... + + def clang_CXRewriter_replaceText( + self, arg1: Rewriter, arg2: SourceRange, arg3: c_interop_string + ) -> None: ... + + def clang_CXRewriter_writeMainFileToStdOut(self, arg1: Rewriter) -> None: ... + + def clang_CXXConstructor_isConvertingConstructor(self, arg1: Cursor) -> bool: ... + + def clang_CXXConstructor_isCopyConstructor(self, arg1: Cursor) -> bool: ... + + def clang_CXXConstructor_isDefaultConstructor(self, arg1: Cursor) -> bool: ... + + def clang_CXXConstructor_isMoveConstructor(self, arg1: Cursor) -> bool: ... + + def clang_CXXField_isMutable(self, arg1: Cursor) -> bool: ... + + def clang_CXXMethod_isConst(self, arg1: Cursor) -> bool: ... + + def clang_CXXMethod_isDefaulted(self, arg1: Cursor) -> bool: ... + + def clang_CXXMethod_isDeleted(self, arg1: Cursor) -> bool: ... + + def clang_CXXMethod_isCopyAssignmentOperator(self, arg1: Cursor) -> bool: ... + + def clang_CXXMethod_isMoveAssignmentOperator(self, arg1: Cursor) -> bool: ... + + def clang_CXXMethod_isExplicit(self, arg1: Cursor) -> bool: ... + + def clang_CXXMethod_isPureVirtual(self, arg1: Cursor) -> bool: ... + + def clang_CXXMethod_isStatic(self, arg1: Cursor) -> bool: ... + + def clang_CXXMethod_isVirtual(self, arg1: Cursor) -> bool: ... + + def clang_CXXRecord_isAbstract(self, arg1: Cursor) -> bool: ... + + def clang_EnumDecl_isScoped(self, arg1: Cursor) -> bool: ... + + def clang_defaultDiagnosticDisplayOptions(self) -> c_uint: ... + + def clang_defaultSaveOptions(self, arg1: TranslationUnit) -> c_uint: ... + + def clang_disposeCodeCompleteResults(self, arg1: CodeCompletionResults) -> None: ... + + def clang_disposeDiagnostic(self, arg1: Diagnostic) -> None: ... + + def clang_disposeIndex(self, arg1: Index) -> None: ... + + def clang_disposeString(self, arg1: _CXString) -> None: ... + + def cl... [truncated] `````````` </details> https://github.com/llvm/llvm-project/pull/142120 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits