From 6fe5ad1ea5e55b429c6341f75beb2f5a2b31405d Mon Sep 17 00:00:00 2001 From: gen740 Date: Tue, 26 Sep 2023 17:08:28 +0900 Subject: [PATCH] Update --- cppygen/_clang/__init__.py | 7 +- cppygen/_clang/cindex.py | 1688 +++++++++++++------------------- cppygen/_clang/enumerations.py | 16 +- cppygen/logging.py | 2 +- 4 files changed, 691 insertions(+), 1022 deletions(-) diff --git a/cppygen/_clang/__init__.py b/cppygen/_clang/__init__.py index 14944b6..8d0d172 100644 --- a/cppygen/_clang/__init__.py +++ b/cppygen/_clang/__init__.py @@ -1,10 +1,10 @@ -#===- __init__.py - Clang Python Bindings --------------------*- python -*--===# +# ===- __init__.py - Clang Python Bindings --------------------*- python -*--===# # # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. # See https://llvm.org/LICENSE.txt for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception # -#===------------------------------------------------------------------------===# +# ===------------------------------------------------------------------------===# r""" Clang Library Bindings @@ -19,5 +19,4 @@ Bindings for the Clang indexing library. """ -__all__ = ['cindex'] - +__all__ = ["cindex"] diff --git a/cppygen/_clang/cindex.py b/cppygen/_clang/cindex.py index f83527c..217db4d 100644 --- a/cppygen/_clang/cindex.py +++ b/cppygen/_clang/cindex.py @@ -1,10 +1,10 @@ -#===- cindex.py - Python Indexing Library Bindings -----------*- python -*--===# +# ===- cindex.py - Python Indexing Library Bindings -----------*- python -*--===# # # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. # See https://llvm.org/LICENSE.txt for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception # -#===------------------------------------------------------------------------===# +# ===------------------------------------------------------------------------===# r""" Clang Indexing Library Bindings @@ -69,10 +69,10 @@ import os import sys + if sys.version_info[0] == 3: # Python 3 strings are unicode, translate them to/from utf8 for C-interop. class c_interop_string(c_char_p): - def __init__(self, p=None): if p is None: p = "" @@ -98,7 +98,9 @@ def from_param(cls, param): if param is None: # Support passing null to C functions expecting char arrays return None - raise TypeError("Cannot convert '{}' to '{}'".format(type(param).__name__, cls.__name__)) + raise TypeError( + "Cannot convert '{}' to '{}'".format(type(param).__name__, cls.__name__) + ) @staticmethod def to_python_string(x, *args): @@ -107,7 +109,7 @@ def to_python_string(x, *args): def b(x): if isinstance(x, bytes): return x - return x.encode('utf8') + return x.encode("utf8") elif sys.version_info[0] == 2: # Python 2 strings are utf8 byte strings, no translation is needed for @@ -122,6 +124,7 @@ def _to_python_string(x, *args): def b(x): return x + # Importing ABC-s directly from collections is deprecated since Python 3.7, # will stop working in Python 3.8. # See: https://docs.python.org/dev/whatsnew/3.7.html#id3 @@ -136,9 +139,11 @@ def b(x): try: fspath = os.fspath except AttributeError: + def fspath(x): return x + # ctypes doesn't implicitly convert c_void_p to the appropriate wrapper # object. This is a problem, because it means that from_parameter will see an # integer and pass the wrong value on platforms where int != void*. Work around @@ -149,6 +154,7 @@ def fspath(x): ### Exception Classes ### + class TranslationUnitLoadError(Exception): """Represents an error that occurred when loading a TranslationUnit. @@ -157,8 +163,10 @@ class TranslationUnitLoadError(Exception): FIXME: Make libclang expose additional error information in this scenario. """ + pass + class TranslationUnitSaveError(Exception): """Represents an error that occurred when saving a TranslationUnit. @@ -182,15 +190,19 @@ def __init__(self, enumeration, message): assert isinstance(enumeration, int) if enumeration < 1 or enumeration > 3: - raise Exception("Encountered undefined TranslationUnit save error " - "constant: %d. Please file a bug to have this " - "value supported." % enumeration) + raise Exception( + "Encountered undefined TranslationUnit save error " + "constant: %d. Please file a bug to have this " + "value supported." % enumeration + ) self.save_error = enumeration - Exception.__init__(self, 'Error %d: %s' % (enumeration, message)) + Exception.__init__(self, "Error %d: %s" % (enumeration, message)) + ### Structures and Utility Classes ### + class CachedProperty(object): """Decorator that lazy-loads the value of a property. @@ -234,14 +246,16 @@ class SourceLocation(Structure): """ A SourceLocation represents a particular location within a source file. """ + _fields_ = [("ptr_data", c_void_p * 2), ("int_data", c_uint)] _data = None def _get_instantiation(self): if self._data is None: f, l, c, o = c_object_p(), c_uint(), c_uint(), c_uint() - conf.lib.clang_getInstantiationLocation(self, byref(f), byref(l), - byref(c), byref(o)) + conf.lib.clang_getInstantiationLocation( + self, byref(f), byref(l), byref(c), byref(o) + ) if f: f = File(f) else: @@ -299,17 +313,23 @@ def __repr__(self): else: filename = None return "" % ( - filename, self.line, self.column) + filename, + self.line, + self.column, + ) + class SourceRange(Structure): """ A SourceRange describes a range of source locations within the source code. """ + _fields_ = [ ("ptr_data", c_void_p * 2), ("begin_int_data", c_uint), - ("end_int_data", c_uint)] + ("end_int_data", c_uint), + ] # FIXME: Eliminate this and make normal constructor? Requires hiding ctypes # object. @@ -345,8 +365,10 @@ def __contains__(self, other): return False if other.file is None and self.start.file is None: pass - elif ( self.start.file.name != other.file.name or - other.file.name != self.end.file.name): + elif ( + self.start.file.name != other.file.name + or other.file.name != self.end.file.name + ): # same file name return False # same file, in between lines @@ -365,6 +387,7 @@ def __contains__(self, other): def __repr__(self): return "" % (self.start, self.end) + class Diagnostic(object): """ A Diagnostic is a single instance of a Clang diagnostic. It includes the @@ -373,18 +396,18 @@ class Diagnostic(object): """ Ignored = 0 - Note = 1 + Note = 1 Warning = 2 - Error = 3 - Fatal = 4 + Error = 3 + Fatal = 4 DisplaySourceLocation = 0x01 - DisplayColumn = 0x02 - DisplaySourceRanges = 0x04 - DisplayOption = 0x08 - DisplayCategoryId = 0x10 - DisplayCategoryName = 0x20 - _FormatOptionsMask = 0x3f + DisplayColumn = 0x02 + DisplaySourceRanges = 0x04 + DisplayOption = 0x08 + DisplayCategoryId = 0x10 + DisplayCategoryName = 0x20 + _FormatOptionsMask = 0x3F def __init__(self, ptr): self.ptr = ptr @@ -414,7 +437,7 @@ def __len__(self): return int(conf.lib.clang_getDiagnosticNumRanges(self.diag)) def __getitem__(self, key): - if (key >= len(self)): + if key >= len(self): raise IndexError return conf.lib.clang_getDiagnosticRange(self.diag, key) @@ -431,8 +454,7 @@ def __len__(self): def __getitem__(self, key): range = SourceRange() - value = conf.lib.clang_getDiagnosticFixIt(self.diag, key, - byref(range)) + value = conf.lib.clang_getDiagnosticFixIt(self.diag, key, byref(range)) if len(value) == 0: raise IndexError @@ -489,18 +511,22 @@ def format(self, options=None): if options is None: options = conf.lib.clang_defaultDiagnosticDisplayOptions() if options & ~Diagnostic._FormatOptionsMask: - raise ValueError('Invalid format options') + raise ValueError("Invalid format options") return conf.lib.clang_formatDiagnostic(self, options) def __repr__(self): return "" % ( - self.severity, self.location, self.spelling) + self.severity, + self.location, + self.spelling, + ) def __str__(self): return self.format() def from_param(self): - return self.ptr + return self.ptr + class FixIt(object): """ @@ -516,6 +542,7 @@ def __init__(self, range, value): def __repr__(self): return "" % (self.range, self.value) + class TokenGroup(object): """Helper class to facilitate token management. @@ -530,6 +557,7 @@ class TokenGroup(object): You should not instantiate this class outside of this module. """ + def __init__(self, tu, memory, count): self._tu = tu self._memory = memory @@ -548,8 +576,7 @@ def get_tokens(tu, extent): tokens_memory = POINTER(Token)() tokens_count = c_uint() - conf.lib.clang_tokenize(tu, extent, byref(tokens_memory), - byref(tokens_count)) + conf.lib.clang_tokenize(tu, extent, byref(tokens_memory), byref(tokens_count)) count = int(tokens_count.value) @@ -571,10 +598,11 @@ def get_tokens(tu, extent): yield token + class TokenKind(object): """Describes a specific type of a Token.""" - _value_map = {} # int -> TokenKind + _value_map = {} # int -> TokenKind def __init__(self, value, name): """Create a new TokenKind instance from a numeric value and a name.""" @@ -582,7 +610,7 @@ def __init__(self, value, name): self.name = name def __repr__(self): - return 'TokenKind.%s' % (self.name,) + return "TokenKind.%s" % (self.name,) @staticmethod def from_value(value): @@ -590,7 +618,7 @@ def from_value(value): result = TokenKind._value_map.get(value, None) if result is None: - raise ValueError('Unknown TokenKind: %d' % value) + raise ValueError("Unknown TokenKind: %d" % value) return result @@ -602,12 +630,13 @@ def register(value, name): package. """ if value in TokenKind._value_map: - raise ValueError('TokenKind already registered: %d' % value) + raise ValueError("TokenKind already registered: %d" % value) kind = TokenKind(value, name) TokenKind._value_map[value] = kind setattr(TokenKind, name, kind) + ### Cursor Kinds ### class BaseEnumeration(object): """ @@ -625,13 +654,13 @@ def __init__(self, value): if value >= len(self.__class__._kinds): self.__class__._kinds += [None] * (value - len(self.__class__._kinds) + 1) if self.__class__._kinds[value] is not None: - raise ValueError('{0} value {1} already loaded'.format( - str(self.__class__), value)) + raise ValueError( + "{0} value {1} already loaded".format(str(self.__class__), value) + ) self.value = value self.__class__._kinds[value] = self self.__class__._name_map = None - def from_param(self): return self.value @@ -648,11 +677,14 @@ def name(self): @classmethod def from_id(cls, id): if id >= len(cls._kinds) or cls._kinds[id] is None: - raise ValueError('Unknown template argument kind %d' % id) + raise ValueError("Unknown template argument kind %d" % id) return cls._kinds[id] def __repr__(self): - return '%s.%s' % (self.__class__, self.name,) + return "%s.%s" % ( + self.__class__, + self.name, + ) class CursorKind(BaseEnumeration): @@ -706,7 +738,8 @@ def is_unexposed(self): return conf.lib.clang_isUnexposed(self) def __repr__(self): - return 'CursorKind.%s' % (self.name,) + return "CursorKind.%s" % (self.name,) + ### # Declaration Kinds @@ -1370,6 +1403,7 @@ def __repr__(self): # A code completion overload candidate. CursorKind.OVERLOAD_CANDIDATE = CursorKind(700) + ### Template Argument Kinds ### class TemplateArgumentKind(BaseEnumeration): """ @@ -1381,12 +1415,14 @@ class TemplateArgumentKind(BaseEnumeration): _kinds = [] _name_map = None + TemplateArgumentKind.NULL = TemplateArgumentKind(0) TemplateArgumentKind.TYPE = TemplateArgumentKind(1) TemplateArgumentKind.DECLARATION = TemplateArgumentKind(2) TemplateArgumentKind.NULLPTR = TemplateArgumentKind(3) TemplateArgumentKind.INTEGRAL = TemplateArgumentKind(4) + ### Exception Specification Kinds ### class ExceptionSpecificationKind(BaseEnumeration): """ @@ -1399,7 +1435,8 @@ class ExceptionSpecificationKind(BaseEnumeration): _name_map = None def __repr__(self): - return 'ExceptionSpecificationKind.{}'.format(self.name) + return "ExceptionSpecificationKind.{}".format(self.name) + ExceptionSpecificationKind.NONE = ExceptionSpecificationKind(0) ExceptionSpecificationKind.DYNAMIC_NONE = ExceptionSpecificationKind(1) @@ -1413,11 +1450,13 @@ def __repr__(self): ### Cursors ### + class Cursor(Structure): """ The Cursor class represents a reference to an element within the AST. It acts as a kind of iterator. """ + _fields_ = [("_kind_id", c_int), ("xdata", c_int), ("data", c_void_p * 3)] @staticmethod @@ -1449,23 +1488,19 @@ def is_const_method(self): return conf.lib.clang_CXXMethod_isConst(self) def is_converting_constructor(self): - """Returns True if the cursor refers to a C++ converting constructor. - """ + """Returns True if the cursor refers to a C++ converting constructor.""" return conf.lib.clang_CXXConstructor_isConvertingConstructor(self) def is_copy_constructor(self): - """Returns True if the cursor refers to a C++ copy constructor. - """ + """Returns True if the cursor refers to a C++ copy constructor.""" return conf.lib.clang_CXXConstructor_isCopyConstructor(self) def is_default_constructor(self): - """Returns True if the cursor refers to a C++ default constructor. - """ + """Returns True if the cursor refers to a C++ default constructor.""" return conf.lib.clang_CXXConstructor_isDefaultConstructor(self) def is_move_constructor(self): - """Returns True if the cursor refers to a C++ move constructor. - """ + """Returns True if the cursor refers to a C++ move constructor.""" return conf.lib.clang_CXXConstructor_isMoveConstructor(self) def is_default_method(self): @@ -1505,8 +1540,7 @@ def is_abstract_record(self): return conf.lib.clang_CXXRecord_isAbstract(self) def is_scoped_enum(self): - """Returns True if the cursor refers to a scoped enum declaration. - """ + """Returns True if the cursor refers to a scoped enum declaration.""" return conf.lib.clang_EnumDecl_isScoped(self) def get_definition(self): @@ -1544,7 +1578,7 @@ def kind(self): @property def spelling(self): """Return the spelling of the entity pointed at by the cursor.""" - if not hasattr(self, '_spelling'): + if not hasattr(self, "_spelling"): self._spelling = conf.lib.clang_getCursorSpelling(self) return self._spelling @@ -1558,7 +1592,7 @@ def displayname(self): cursor, such as the parameters of a function or template or the arguments of a class template specialization. """ - if not hasattr(self, '_displayname'): + if not hasattr(self, "_displayname"): self._displayname = conf.lib.clang_getCursorDisplayName(self) return self._displayname @@ -1566,7 +1600,7 @@ def displayname(self): @property def mangled_name(self): """Return the mangled name for the entity referenced by this cursor.""" - if not hasattr(self, '_mangled_name'): + if not hasattr(self, "_mangled_name"): self._mangled_name = conf.lib.clang_Cursor_getMangling(self) return self._mangled_name @@ -1577,7 +1611,7 @@ def location(self): Return the source location (the starting character) of the entity pointed at by the cursor. """ - if not hasattr(self, '_loc'): + if not hasattr(self, "_loc"): self._loc = conf.lib.clang_getCursorLocation(self) return self._loc @@ -1585,7 +1619,7 @@ def location(self): @property def linkage(self): """Return the linkage of this cursor.""" - if not hasattr(self, '_linkage'): + if not hasattr(self, "_linkage"): self._linkage = conf.lib.clang_getCursorLinkage(self) return LinkageKind.from_id(self._linkage) @@ -1593,7 +1627,7 @@ def linkage(self): @property def tls_kind(self): """Return the thread-local storage (TLS) kind of this cursor.""" - if not hasattr(self, '_tls_kind'): + if not hasattr(self, "_tls_kind"): self._tls_kind = conf.lib.clang_getCursorTLSKind(self) return TLSKind.from_id(self._tls_kind) @@ -1604,7 +1638,7 @@ def extent(self): Return the source range (the range of text) occupied by the entity pointed at by the cursor. """ - if not hasattr(self, '_extent'): + if not hasattr(self, "_extent"): self._extent = conf.lib.clang_getCursorExtent(self) return self._extent @@ -1615,7 +1649,7 @@ def storage_class(self): Retrieves the storage class (if any) of the entity pointed at by the cursor. """ - if not hasattr(self, '_storage_class'): + if not hasattr(self, "_storage_class"): self._storage_class = conf.lib.clang_Cursor_getStorageClass(self) return StorageClass.from_id(self._storage_class) @@ -1625,7 +1659,7 @@ def availability(self): """ Retrieves the availability of the entity pointed at by the cursor. """ - if not hasattr(self, '_availability'): + if not hasattr(self, "_availability"): self._availability = conf.lib.clang_getCursorAvailability(self) return AvailabilityKind.from_id(self._availability) @@ -1636,7 +1670,7 @@ def access_specifier(self): Retrieves the access specifier (if any) of the entity pointed at by the cursor. """ - if not hasattr(self, '_access_specifier'): + if not hasattr(self, "_access_specifier"): self._access_specifier = conf.lib.clang_getCXXAccessSpecifier(self) return AccessSpecifier.from_id(self._access_specifier) @@ -1646,7 +1680,7 @@ def type(self): """ Retrieve the Type (if any) of the entity pointed at by the cursor. """ - if not hasattr(self, '_type'): + if not hasattr(self, "_type"): self._type = conf.lib.clang_getCursorType(self) return self._type @@ -1660,7 +1694,7 @@ def canonical(self): declarations for the same class, the canonical cursor for the forward declarations will be identical. """ - if not hasattr(self, '_canonical'): + if not hasattr(self, "_canonical"): self._canonical = conf.lib.clang_getCanonicalCursor(self) return self._canonical @@ -1668,20 +1702,22 @@ def canonical(self): @property def result_type(self): """Retrieve the Type of the result for this Cursor.""" - if not hasattr(self, '_result_type'): + if not hasattr(self, "_result_type"): self._result_type = conf.lib.clang_getCursorResultType(self) return self._result_type @property def exception_specification_kind(self): - ''' + """ Retrieve the exception specification kind, which is one of the values from the ExceptionSpecificationKind enumeration. - ''' - if not hasattr(self, '_exception_specification_kind'): + """ + if not hasattr(self, "_exception_specification_kind"): exc_kind = conf.lib.clang_getCursorExceptionSpecificationType(self) - self._exception_specification_kind = ExceptionSpecificationKind.from_id(exc_kind) + self._exception_specification_kind = ExceptionSpecificationKind.from_id( + exc_kind + ) return self._exception_specification_kind @@ -1692,10 +1728,9 @@ def underlying_typedef_type(self): Returns a Type for the typedef this cursor is a declaration for. If the current cursor is not a typedef, this raises. """ - if not hasattr(self, '_underlying_type'): + if not hasattr(self, "_underlying_type"): assert self.kind.is_declaration() - self._underlying_type = \ - conf.lib.clang_getTypedefDeclUnderlyingType(self) + self._underlying_type = conf.lib.clang_getTypedefDeclUnderlyingType(self) return self._underlying_type @@ -1706,7 +1741,7 @@ def enum_type(self): Returns a Type corresponding to an integer. If the cursor is not for an enum, this raises. """ - if not hasattr(self, '_enum_type'): + if not hasattr(self, "_enum_type"): assert self.kind == CursorKind.ENUM_DECL self._enum_type = conf.lib.clang_getEnumDeclIntegerType(self) @@ -1715,24 +1750,25 @@ def enum_type(self): @property def enum_value(self): """Return the value of an enum constant.""" - if not hasattr(self, '_enum_value'): + if not hasattr(self, "_enum_value"): assert self.kind == CursorKind.ENUM_CONSTANT_DECL # Figure out the underlying type of the enum to know if it # is a signed or unsigned quantity. underlying_type = self.type if underlying_type.kind == TypeKind.ENUM: underlying_type = underlying_type.get_declaration().enum_type - if underlying_type.kind in (TypeKind.CHAR_U, - TypeKind.UCHAR, - TypeKind.CHAR16, - TypeKind.CHAR32, - TypeKind.USHORT, - TypeKind.UINT, - TypeKind.ULONG, - TypeKind.ULONGLONG, - TypeKind.UINT128): - self._enum_value = \ - conf.lib.clang_getEnumConstantDeclUnsignedValue(self) + if underlying_type.kind in ( + TypeKind.CHAR_U, + TypeKind.UCHAR, + TypeKind.CHAR16, + TypeKind.CHAR32, + TypeKind.USHORT, + TypeKind.UINT, + TypeKind.ULONG, + TypeKind.ULONGLONG, + TypeKind.UINT128, + ): + self._enum_value = conf.lib.clang_getEnumConstantDeclUnsignedValue(self) else: self._enum_value = conf.lib.clang_getEnumConstantDeclValue(self) return self._enum_value @@ -1740,16 +1776,15 @@ def enum_value(self): @property def objc_type_encoding(self): """Return the Objective-C type encoding as a str.""" - if not hasattr(self, '_objc_type_encoding'): - self._objc_type_encoding = \ - conf.lib.clang_getDeclObjCTypeEncoding(self) + if not hasattr(self, "_objc_type_encoding"): + self._objc_type_encoding = conf.lib.clang_getDeclObjCTypeEncoding(self) return self._objc_type_encoding @property def hash(self): """Returns a hash of the cursor as an int.""" - if not hasattr(self, '_hash'): + if not hasattr(self, "_hash"): self._hash = conf.lib.clang_hashCursor(self) return self._hash @@ -1757,7 +1792,7 @@ def hash(self): @property def semantic_parent(self): """Return the semantic parent for this cursor.""" - if not hasattr(self, '_semantic_parent'): + if not hasattr(self, "_semantic_parent"): self._semantic_parent = conf.lib.clang_getCursorSemanticParent(self) return self._semantic_parent @@ -1765,7 +1800,7 @@ def semantic_parent(self): @property def lexical_parent(self): """Return the lexical parent for this cursor.""" - if not hasattr(self, '_lexical_parent'): + if not hasattr(self, "_lexical_parent"): self._lexical_parent = conf.lib.clang_getCursorLexicalParent(self) return self._lexical_parent @@ -1783,7 +1818,7 @@ def referenced(self): For a cursor that is a reference, returns a cursor representing the entity that it references. """ - if not hasattr(self, '_referenced'): + if not hasattr(self, "_referenced"): self._referenced = conf.lib.clang_getCursorReferenced(self) return self._referenced @@ -1837,10 +1872,10 @@ def visitor(child, parent, children): # Create reference to TU so it isn't GC'd before Cursor. child._tu = self._tu children.append(child) - return 1 # continue + return 1 # continue + children = [] - conf.lib.clang_visitChildren(self, callbacks['cursor_visit'](visitor), - children) + conf.lib.clang_visitChildren(self, callbacks["cursor_visit"](visitor), children) return iter(children) def walk_preorder(self): @@ -1900,7 +1935,7 @@ def from_result(res, fn, args): tu = arg break - if hasattr(arg, 'translation_unit'): + if hasattr(arg, "translation_unit"): tu = arg.translation_unit break @@ -1918,6 +1953,7 @@ def from_cursor_result(res, fn, args): res._tu = args[0]._tu return res + class StorageClass(object): """ Describes the storage class of a declaration @@ -1931,7 +1967,7 @@ def __init__(self, value): if value >= len(StorageClass._kinds): StorageClass._kinds += [None] * (value - len(StorageClass._kinds) + 1) if StorageClass._kinds[value] is not None: - raise ValueError('StorageClass already loaded') + raise ValueError("StorageClass already loaded") self.value = value StorageClass._kinds[value] = self StorageClass._name_map = None @@ -1944,19 +1980,20 @@ def name(self): """Get the enumeration name of this storage class.""" if self._name_map is None: self._name_map = {} - for key,value in StorageClass.__dict__.items(): - if isinstance(value,StorageClass): + for key, value in StorageClass.__dict__.items(): + if isinstance(value, StorageClass): self._name_map[value] = key return self._name_map[self] @staticmethod def from_id(id): if id >= len(StorageClass._kinds) or not StorageClass._kinds[id]: - raise ValueError('Unknown storage class %d' % id) + raise ValueError("Unknown storage class %d" % id) return StorageClass._kinds[id] def __repr__(self): - return 'StorageClass.%s' % (self.name,) + return "StorageClass.%s" % (self.name,) + StorageClass.INVALID = StorageClass(0) StorageClass.NONE = StorageClass(1) @@ -1969,6 +2006,7 @@ def __repr__(self): ### Availability Kinds ### + class AvailabilityKind(BaseEnumeration): """ Describes the availability of an entity. @@ -1979,7 +2017,8 @@ class AvailabilityKind(BaseEnumeration): _name_map = None def __repr__(self): - return 'AvailabilityKind.%s' % (self.name,) + return "AvailabilityKind.%s" % (self.name,) + AvailabilityKind.AVAILABLE = AvailabilityKind(0) AvailabilityKind.DEPRECATED = AvailabilityKind(1) @@ -1988,6 +2027,7 @@ def __repr__(self): ### C++ access specifiers ### + class AccessSpecifier(BaseEnumeration): """ Describes the access of a C++ class member @@ -2001,7 +2041,8 @@ def from_param(self): return self.value def __repr__(self): - return 'AccessSpecifier.%s' % (self.name,) + return "AccessSpecifier.%s" % (self.name,) + AccessSpecifier.INVALID = AccessSpecifier(0) AccessSpecifier.PUBLIC = AccessSpecifier(1) @@ -2011,6 +2052,7 @@ def __repr__(self): ### Type Kinds ### + class TypeKind(BaseEnumeration): """ Describes the kind of type. @@ -2026,7 +2068,8 @@ def spelling(self): return conf.lib.clang_getTypeKindSpelling(self.value) def __repr__(self): - return 'TypeKind.%s' % (self.name,) + return "TypeKind.%s" % (self.name,) + TypeKind.INVALID = TypeKind(0) TypeKind.UNEXPOSED = TypeKind(1) @@ -2126,6 +2169,7 @@ def __repr__(self): TypeKind.EXTVECTOR = TypeKind(176) TypeKind.ATOMIC = TypeKind(177) + class RefQualifierKind(BaseEnumeration): """Describes a specific ref-qualifier of a type.""" @@ -2137,12 +2181,14 @@ def from_param(self): return self.value def __repr__(self): - return 'RefQualifierKind.%s' % (self.name,) + return "RefQualifierKind.%s" % (self.name,) + RefQualifierKind.NONE = RefQualifierKind(0) RefQualifierKind.LVALUE = RefQualifierKind(1) RefQualifierKind.RVALUE = RefQualifierKind(2) + class LinkageKind(BaseEnumeration): """Describes the kind of linkage of a cursor.""" @@ -2154,7 +2200,8 @@ def from_param(self): return self.value def __repr__(self): - return 'LinkageKind.%s' % (self.name,) + return "LinkageKind.%s" % (self.name,) + LinkageKind.INVALID = LinkageKind(0) LinkageKind.NO_LINKAGE = LinkageKind(1) @@ -2162,6 +2209,7 @@ def __repr__(self): LinkageKind.UNIQUE_EXTERNAL = LinkageKind(3) LinkageKind.EXTERNAL = LinkageKind(4) + class TLSKind(BaseEnumeration): """Describes the kind of thread-local storage (TLS) of a cursor.""" @@ -2173,16 +2221,19 @@ def from_param(self): return self.value def __repr__(self): - return 'TLSKind.%s' % (self.name,) + return "TLSKind.%s" % (self.name,) + TLSKind.NONE = TLSKind(0) TLSKind.DYNAMIC = TLSKind(1) TLSKind.STATIC = TLSKind(2) + class Type(Structure): """ The type of an element in the abstract syntax tree. """ + _fields_ = [("_kind_id", c_int), ("data", c_void_p * 2)] @property @@ -2196,6 +2247,7 @@ def argument_types(self): The returned object is iterable and indexable. Each item in the container is a Type instance. """ + class ArgumentsIterator(collections_abc.Sequence): def __init__(self, parent): self.parent = parent @@ -2216,8 +2268,10 @@ def __getitem__(self, key): raise IndexError("Only non-negative indexes are accepted.") if key >= len(self): - raise IndexError("Index greater than container length: " - "%d > %d" % ( key, len(self) )) + raise IndexError( + "Index greater than container length: " + "%d > %d" % (key, len(self)) + ) result = conf.lib.clang_getArgType(self.parent, key) if result.kind == TypeKind.INVALID: @@ -2237,7 +2291,7 @@ def element_type(self): """ result = conf.lib.clang_getElementType(self) if result.kind == TypeKind.INVALID: - raise Exception('Element type not available on this type.') + raise Exception("Element type not available on this type.") return result @@ -2251,7 +2305,7 @@ def element_count(self): """ result = conf.lib.clang_getNumElements(self) if result < 0: - raise Exception('Type does not have elements.') + raise Exception("Type does not have elements.") return result @@ -2268,7 +2322,7 @@ def from_result(res, fn, args): tu = None for arg in args: - if hasattr(arg, 'translation_unit'): + if hasattr(arg, "translation_unit"): tu = arg.translation_unit break @@ -2399,8 +2453,7 @@ def get_ref_qualifier(self): """ Retrieve the ref-qualifier of the type. """ - return RefQualifierKind.from_id( - conf.lib.clang_Type_getCXXRefQualifier(self)) + return RefQualifierKind.from_id(conf.lib.clang_Type_getCXXRefQualifier(self)) def get_fields(self): """Return an iterator for accessing the fields of this type.""" @@ -2411,10 +2464,12 @@ def visitor(field, children): # Create reference to TU so it isn't GC'd before Cursor. field._tu = self._tu fields.append(field) - return 1 # continue + return 1 # continue + fields = [] - conf.lib.clang_Type_visitFields(self, - callbacks['fields_visit'](visitor), fields) + conf.lib.clang_Type_visitFields( + self, callbacks["fields_visit"](visitor), fields + ) return iter(fields) def get_exception_specification_kind(self): @@ -2423,7 +2478,8 @@ def get_exception_specification_kind(self): the ExceptionSpecificationKind enumeration. """ return ExceptionSpecificationKind.from_id( - conf.lib.clang.getExceptionSpecificationType(self)) + conf.lib.clang.getExceptionSpecificationType(self) + ) @property def spelling(self): @@ -2439,17 +2495,20 @@ def __eq__(self, other): def __ne__(self, other): return not self.__eq__(other) + ## CIndex Objects ## # CIndex objects (derived from ClangObject) are essentially lightweight # wrappers attached to some underlying object, which is exposed via CIndex as # a void*. + class ClangObject(object): """ A helper for Clang objects. This class helps act as an intermediary for the ctypes library and the Clang CIndex library. """ + def __init__(self, obj): assert isinstance(obj, c_object_p) and obj self.obj = self._as_parameter_ = obj @@ -2460,35 +2519,38 @@ def from_param(self): class _CXUnsavedFile(Structure): """Helper for passing unsaved file arguments.""" - _fields_ = [("name", c_char_p), ("contents", c_char_p), ('length', c_ulong)] + + _fields_ = [("name", c_char_p), ("contents", c_char_p), ("length", c_ulong)] + # Functions calls through the python interface are rather slow. Fortunately, # for most symboles, we do not need to perform a function call. Their spelling # never changes and is consequently provided by this spelling cache. SpellingCache = { - # 0: CompletionChunk.Kind("Optional"), - # 1: CompletionChunk.Kind("TypedText"), - # 2: CompletionChunk.Kind("Text"), - # 3: CompletionChunk.Kind("Placeholder"), - # 4: CompletionChunk.Kind("Informative"), - # 5 : CompletionChunk.Kind("CurrentParameter"), - 6: '(', # CompletionChunk.Kind("LeftParen"), - 7: ')', # CompletionChunk.Kind("RightParen"), - 8: '[', # CompletionChunk.Kind("LeftBracket"), - 9: ']', # CompletionChunk.Kind("RightBracket"), - 10: '{', # CompletionChunk.Kind("LeftBrace"), - 11: '}', # CompletionChunk.Kind("RightBrace"), - 12: '<', # CompletionChunk.Kind("LeftAngle"), - 13: '>', # CompletionChunk.Kind("RightAngle"), - 14: ', ', # CompletionChunk.Kind("Comma"), - # 15: CompletionChunk.Kind("ResultType"), - 16: ':', # CompletionChunk.Kind("Colon"), - 17: ';', # CompletionChunk.Kind("SemiColon"), - 18: '=', # CompletionChunk.Kind("Equal"), - 19: ' ', # CompletionChunk.Kind("HorizontalSpace"), - # 20: CompletionChunk.Kind("VerticalSpace") + # 0: CompletionChunk.Kind("Optional"), + # 1: CompletionChunk.Kind("TypedText"), + # 2: CompletionChunk.Kind("Text"), + # 3: CompletionChunk.Kind("Placeholder"), + # 4: CompletionChunk.Kind("Informative"), + # 5 : CompletionChunk.Kind("CurrentParameter"), + 6: "(", # CompletionChunk.Kind("LeftParen"), + 7: ")", # CompletionChunk.Kind("RightParen"), + 8: "[", # CompletionChunk.Kind("LeftBracket"), + 9: "]", # CompletionChunk.Kind("RightBracket"), + 10: "{", # CompletionChunk.Kind("LeftBrace"), + 11: "}", # CompletionChunk.Kind("RightBrace"), + 12: "<", # CompletionChunk.Kind("LeftAngle"), + 13: ">", # CompletionChunk.Kind("RightAngle"), + 14: ", ", # CompletionChunk.Kind("Comma"), + # 15: CompletionChunk.Kind("ResultType"), + 16: ":", # CompletionChunk.Kind("Colon"), + 17: ";", # CompletionChunk.Kind("SemiColon"), + 18: "=", # CompletionChunk.Kind("Equal"), + 19: " ", # CompletionChunk.Kind("HorizontalSpace"), + # 20: CompletionChunk.Kind("VerticalSpace") } + class CompletionChunk(object): class Kind(object): def __init__(self, name): @@ -2511,7 +2573,7 @@ def __repr__(self): @CachedProperty def spelling(self): if self.__kindNumber in SpellingCache: - return SpellingCache[self.__kindNumber] + return SpellingCache[self.__kindNumber] return conf.lib.clang_getCompletionChunkText(self.cs, self.key) # We do not use @CachedProperty here, as the manual implementation is @@ -2520,8 +2582,9 @@ def spelling(self): @property def __kindNumber(self): if self.__kindNumberCache == -1: - self.__kindNumberCache = \ - conf.lib.clang_getCompletionChunkKind(self.cs, self.key) + self.__kindNumberCache = conf.lib.clang_getCompletionChunkKind( + self.cs, self.key + ) return self.__kindNumberCache @CachedProperty @@ -2530,51 +2593,53 @@ def kind(self): @CachedProperty def string(self): - res = conf.lib.clang_getCompletionChunkCompletionString(self.cs, - self.key) + res = conf.lib.clang_getCompletionChunkCompletionString(self.cs, self.key) - if (res): - return CompletionString(res) + if res: + return CompletionString(res) else: - None + None def isKindOptional(self): - return self.__kindNumber == 0 + return self.__kindNumber == 0 def isKindTypedText(self): - return self.__kindNumber == 1 + return self.__kindNumber == 1 def isKindPlaceHolder(self): - return self.__kindNumber == 3 + return self.__kindNumber == 3 def isKindInformative(self): - return self.__kindNumber == 4 + return self.__kindNumber == 4 def isKindResultType(self): - return self.__kindNumber == 15 + return self.__kindNumber == 15 + completionChunkKindMap = { - 0: CompletionChunk.Kind("Optional"), - 1: CompletionChunk.Kind("TypedText"), - 2: CompletionChunk.Kind("Text"), - 3: CompletionChunk.Kind("Placeholder"), - 4: CompletionChunk.Kind("Informative"), - 5: CompletionChunk.Kind("CurrentParameter"), - 6: CompletionChunk.Kind("LeftParen"), - 7: CompletionChunk.Kind("RightParen"), - 8: CompletionChunk.Kind("LeftBracket"), - 9: CompletionChunk.Kind("RightBracket"), - 10: CompletionChunk.Kind("LeftBrace"), - 11: CompletionChunk.Kind("RightBrace"), - 12: CompletionChunk.Kind("LeftAngle"), - 13: CompletionChunk.Kind("RightAngle"), - 14: CompletionChunk.Kind("Comma"), - 15: CompletionChunk.Kind("ResultType"), - 16: CompletionChunk.Kind("Colon"), - 17: CompletionChunk.Kind("SemiColon"), - 18: CompletionChunk.Kind("Equal"), - 19: CompletionChunk.Kind("HorizontalSpace"), - 20: CompletionChunk.Kind("VerticalSpace")} + 0: CompletionChunk.Kind("Optional"), + 1: CompletionChunk.Kind("TypedText"), + 2: CompletionChunk.Kind("Text"), + 3: CompletionChunk.Kind("Placeholder"), + 4: CompletionChunk.Kind("Informative"), + 5: CompletionChunk.Kind("CurrentParameter"), + 6: CompletionChunk.Kind("LeftParen"), + 7: CompletionChunk.Kind("RightParen"), + 8: CompletionChunk.Kind("LeftBracket"), + 9: CompletionChunk.Kind("RightBracket"), + 10: CompletionChunk.Kind("LeftBrace"), + 11: CompletionChunk.Kind("RightBrace"), + 12: CompletionChunk.Kind("LeftAngle"), + 13: CompletionChunk.Kind("RightAngle"), + 14: CompletionChunk.Kind("Comma"), + 15: CompletionChunk.Kind("ResultType"), + 16: CompletionChunk.Kind("Colon"), + 17: CompletionChunk.Kind("SemiColon"), + 18: CompletionChunk.Kind("Equal"), + 19: CompletionChunk.Kind("HorizontalSpace"), + 20: CompletionChunk.Kind("VerticalSpace"), +} + class CompletionString(ClangObject): class Availability(object): @@ -2615,19 +2680,27 @@ def briefComment(self): return _CXString() def __repr__(self): - return " | ".join([str(a) for a in self]) \ - + " || Priority: " + str(self.priority) \ - + " || Availability: " + str(self.availability) \ - + " || Brief comment: " + str(self.briefComment) + return ( + " | ".join([str(a) for a in self]) + + " || Priority: " + + str(self.priority) + + " || Availability: " + + str(self.availability) + + " || Brief comment: " + + str(self.briefComment) + ) + availabilityKinds = { - 0: CompletionChunk.Kind("Available"), - 1: CompletionChunk.Kind("Deprecated"), - 2: CompletionChunk.Kind("NotAvailable"), - 3: CompletionChunk.Kind("NotAccessible")} + 0: CompletionChunk.Kind("Available"), + 1: CompletionChunk.Kind("Deprecated"), + 2: CompletionChunk.Kind("NotAvailable"), + 3: CompletionChunk.Kind("NotAccessible"), +} + class CodeCompletionResult(Structure): - _fields_ = [('cursorKind', c_int), ('completionString', c_object_p)] + _fields_ = [("cursorKind", c_int), ("completionString", c_object_p)] def __repr__(self): return str(CompletionString(self.completionString)) @@ -2640,9 +2713,9 @@ def kind(self): def string(self): return CompletionString(self.completionString) + class CCRStructure(Structure): - _fields_ = [('results', POINTER(CodeCompletionResult)), - ('numResults', c_int)] + _fields_ = [("results", POINTER(CodeCompletionResult)), ("numResults", c_int)] def __len__(self): return self.numResults @@ -2653,6 +2726,7 @@ def __getitem__(self, key): return self.results[key] + class CodeCompletionResults(ClangObject): def __init__(self, ptr): assert isinstance(ptr, POINTER(CCRStructure)) and ptr @@ -2672,11 +2746,10 @@ def results(self): def diagnostics(self): class DiagnosticsItr(object): def __init__(self, ccr): - self.ccr= ccr + self.ccr = ccr def __len__(self): - return int(\ - conf.lib.clang_codeCompleteGetNumDiagnostics(self.ccr)) + return int(conf.lib.clang_codeCompleteGetNumDiagnostics(self.ccr)) def __getitem__(self, key): return conf.lib.clang_codeCompleteGetDiagnostic(self.ccr, key) @@ -2707,7 +2780,7 @@ def read(self, path): """Load a TranslationUnit from the given AST file.""" return TranslationUnit.from_ast_file(path, self) - def parse(self, path, args=None, unsaved_files=None, options = 0): + def parse(self, path, args=None, unsaved_files=None, options=0): """Load the translation unit from the given source code file by running clang and generating the AST before loading. Additional command line parameters can be passed to clang via the args parameter. @@ -2720,8 +2793,8 @@ def parse(self, path, args=None, unsaved_files=None, options = 0): If an error was encountered during parsing, a TranslationUnitLoadError will be raised. """ - return TranslationUnit.from_source(path, args, unsaved_files, options, - self) + return TranslationUnit.from_source(path, args, unsaved_files, options, self) + class TranslationUnit(ClangObject): """Represents a source code translation unit. @@ -2763,8 +2836,9 @@ class TranslationUnit(ClangObject): PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION = 128 @classmethod - def from_source(cls, filename, args=None, unsaved_files=None, options=0, - index=None): + def from_source( + cls, filename, args=None, unsaved_files=None, options=0, index=None + ): """Create a TranslationUnit by parsing source. This is capable of processing source code both from files on the @@ -2828,11 +2902,15 @@ def from_source(cls, filename, args=None, unsaved_files=None, options=0, unsaved_array[i].contents = contents unsaved_array[i].length = len(contents) - ptr = conf.lib.clang_parseTranslationUnit(index, - fspath(filename) if filename is not None else None, - args_array, - len(args), unsaved_array, - len(unsaved_files), options) + ptr = conf.lib.clang_parseTranslationUnit( + index, + fspath(filename) if filename is not None else None, + args_array, + len(args), + unsaved_array, + len(unsaved_files), + options, + ) if not ptr: raise TranslationUnitLoadError("Error parsing translation unit.") @@ -2894,6 +2972,7 @@ def get_includes(self): recursively iterate over header files included through precompiled headers. """ + def visitor(fobj, lptr, depth, includes): if depth > 0: loc = lptr.contents @@ -2901,8 +2980,9 @@ def visitor(fobj, lptr, depth, includes): # Automatically adapt CIndex/ctype pointers to python objects includes = [] - conf.lib.clang_getInclusions(self, - callbacks['translation_unit_includes'](visitor), includes) + conf.lib.clang_getInclusions( + self, callbacks["translation_unit_includes"](visitor), includes + ) return iter(includes) @@ -2945,20 +3025,21 @@ def get_extent(self, filename, locations): f = self.get_file(filename) if len(locations) < 2: - raise Exception('Must pass object with at least 2 elements') + raise Exception("Must pass object with at least 2 elements") start_location, end_location = locations - if hasattr(start_location, '__len__'): - start_location = SourceLocation.from_position(self, f, - start_location[0], start_location[1]) + if hasattr(start_location, "__len__"): + start_location = SourceLocation.from_position( + self, f, start_location[0], start_location[1] + ) elif isinstance(start_location, int): - start_location = SourceLocation.from_offset(self, f, - start_location) + start_location = SourceLocation.from_offset(self, f, start_location) - if hasattr(end_location, '__len__'): - end_location = SourceLocation.from_position(self, f, - end_location[0], end_location[1]) + if hasattr(end_location, "__len__"): + end_location = SourceLocation.from_position( + self, f, end_location[0], end_location[1] + ) elif isinstance(end_location, int): end_location = SourceLocation.from_offset(self, f, end_location) @@ -2972,6 +3053,7 @@ def diagnostics(self): """ Return an iterable (and indexable) object containing the diagnostics. """ + class DiagIterator(object): def __init__(self, tu): self.tu = tu @@ -3002,15 +3084,16 @@ def reparse(self, unsaved_files=None, options=0): unsaved_files_array = 0 if len(unsaved_files): unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))() - for i,(name,contents) in enumerate(unsaved_files): + for i, (name, contents) in enumerate(unsaved_files): if hasattr(contents, "read"): contents = contents.read() contents = b(contents) unsaved_files_array[i].name = b(fspath(name)) unsaved_files_array[i].contents = contents unsaved_files_array[i].length = len(contents) - ptr = conf.lib.clang_reparseTranslationUnit(self, len(unsaved_files), - unsaved_files_array, options) + ptr = conf.lib.clang_reparseTranslationUnit( + self, len(unsaved_files), unsaved_files_array, options + ) def save(self, filename): """Saves the TranslationUnit to a file. @@ -3028,15 +3111,22 @@ def save(self, filename): filename -- The path to save the translation unit to (str or PathLike). """ options = conf.lib.clang_defaultSaveOptions(self) - result = int(conf.lib.clang_saveTranslationUnit(self, fspath(filename), - options)) + result = int( + conf.lib.clang_saveTranslationUnit(self, fspath(filename), options) + ) if result != 0: - raise TranslationUnitSaveError(result, - 'Error saving TranslationUnit.') - - def codeComplete(self, path, line, column, unsaved_files=None, - include_macros=False, include_code_patterns=False, - include_brief_comments=False): + raise TranslationUnitSaveError(result, "Error saving TranslationUnit.") + + def codeComplete( + self, + path, + line, + column, + unsaved_files=None, + include_macros=False, + include_code_patterns=False, + include_brief_comments=False, + ): """ Code complete in this translation unit. @@ -3062,15 +3152,22 @@ def codeComplete(self, path, line, column, unsaved_files=None, unsaved_files_array = 0 if len(unsaved_files): unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))() - for i,(name,contents) in enumerate(unsaved_files): + for i, (name, contents) in enumerate(unsaved_files): if hasattr(contents, "read"): contents = contents.read() contents = b(contents) unsaved_files_array[i].name = b(fspath(name)) unsaved_files_array[i].contents = contents unsaved_files_array[i].length = len(contents) - ptr = conf.lib.clang_codeCompleteAt(self, fspath(path), line, column, - unsaved_files_array, len(unsaved_files), options) + ptr = conf.lib.clang_codeCompleteAt( + self, + fspath(path), + line, + column, + unsaved_files_array, + len(unsaved_files), + options, + ) if ptr: return CodeCompletionResults(ptr) return None @@ -3088,6 +3185,7 @@ def get_tokens(self, locations=None, extent=None): return TokenGroup.get_tokens(self, extent) + class File(ClangObject): """ The File class represents a particular source file that is part of a @@ -3124,6 +3222,7 @@ def from_result(res, fn, args): res._tu = args[0]._tu return res + class FileInclusion(object): """ The FileInclusion class represents the inclusion of one source file by @@ -3144,6 +3243,7 @@ def is_input_file(self): """True if the included file is the input file.""" return self.depth == 0 + class CompilationDatabaseError(Exception): """Represents an error that occurred when working with a CompilationDatabase @@ -3162,15 +3262,19 @@ def __init__(self, enumeration, message): assert isinstance(enumeration, int) if enumeration > 1: - raise Exception("Encountered undefined CompilationDatabase error " - "constant: %d. Please file a bug to have this " - "value supported." % enumeration) + raise Exception( + "Encountered undefined CompilationDatabase error " + "constant: %d. Please file a bug to have this " + "value supported." % enumeration + ) self.cdb_error = enumeration - Exception.__init__(self, 'Error %d: %s' % (enumeration, message)) + Exception.__init__(self, "Error %d: %s" % (enumeration, message)) + class CompileCommand(object): """Represents the compile command used to build a file""" + def __init__(self, cmd, ccmds): self.cmd = cmd # Keep a reference to the originating CompileCommands @@ -3199,11 +3303,13 @@ def arguments(self): for i in range(length): yield conf.lib.clang_CompileCommand_getArg(self.cmd, i) + class CompileCommands(object): """ CompileCommands is an iterable object containing all CompileCommand that can be used for building a specific file. """ + def __init__(self, ccmds): self.ccmds = ccmds @@ -3225,6 +3331,7 @@ def from_result(res, fn, args): return None return CompileCommands(res) + class CompilationDatabase(ClangObject): """ The CompilationDatabase is a wrapper class around @@ -3239,8 +3346,7 @@ def __del__(self): @staticmethod def from_result(res, fn, args): if not res: - raise CompilationDatabaseError(0, - "CompilationDatabase loading failed") + raise CompilationDatabaseError(0, "CompilationDatabase loading failed") return CompilationDatabase(res) @staticmethod @@ -3248,11 +3354,13 @@ def fromDirectory(buildDir): """Builds a CompilationDatabase from the database found in buildDir""" errorCode = c_uint() try: - cdb = conf.lib.clang_CompilationDatabase_fromDirectory(fspath(buildDir), - byref(errorCode)) + cdb = conf.lib.clang_CompilationDatabase_fromDirectory( + fspath(buildDir), byref(errorCode) + ) except CompilationDatabaseError as e: - raise CompilationDatabaseError(int(errorCode.value), - "CompilationDatabase loading failed") + raise CompilationDatabaseError( + int(errorCode.value), "CompilationDatabase loading failed" + ) return cdb def getCompileCommands(self, filename): @@ -3260,8 +3368,9 @@ def getCompileCommands(self, filename): Get an iterable object providing all the CompileCommands available to build filename. Returns None if filename is not found in the database. """ - return conf.lib.clang_CompilationDatabase_getCompileCommands(self, - fspath(filename)) + return conf.lib.clang_CompilationDatabase_getCompileCommands( + self, fspath(filename) + ) def getAllCompileCommands(self): """ @@ -3280,10 +3389,8 @@ class Token(Structure): Tokens are obtained from parsed TranslationUnit instances. You currently can't create tokens manually. """ - _fields_ = [ - ('int_data', c_uint * 4), - ('ptr_data', c_void_p) - ] + + _fields_ = [("int_data", c_uint * 4), ("ptr_data", c_void_p)] @property def spelling(self): @@ -3318,743 +3425,290 @@ def cursor(self): return cursor + # Now comes the plumbing to hook up the C library. # Register callback types in common container. -callbacks['translation_unit_includes'] = CFUNCTYPE(None, c_object_p, - POINTER(SourceLocation), c_uint, py_object) -callbacks['cursor_visit'] = CFUNCTYPE(c_int, Cursor, Cursor, py_object) -callbacks['fields_visit'] = CFUNCTYPE(c_int, Cursor, py_object) +callbacks["translation_unit_includes"] = CFUNCTYPE( + None, c_object_p, POINTER(SourceLocation), c_uint, py_object +) +callbacks["cursor_visit"] = CFUNCTYPE(c_int, Cursor, Cursor, py_object) +callbacks["fields_visit"] = CFUNCTYPE(c_int, Cursor, py_object) # Functions strictly alphabetical order. functionList = [ - ("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, - CompilationDatabase.from_result), - - ("clang_CompilationDatabase_getAllCompileCommands", - [c_object_p], - c_object_p, - CompileCommands.from_result), - - ("clang_CompilationDatabase_getCompileCommands", - [c_object_p, c_interop_string], - c_object_p, - CompileCommands.from_result), - - ("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, - _CXString.from_result), - - ("clang_CompileCommand_getDirectory", - [c_object_p], - _CXString, - _CXString.from_result), - - ("clang_CompileCommand_getFilename", - [c_object_p], - _CXString, - _CXString.from_result), - - ("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_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_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, - _CXString.from_result), - - ("clang_getArgType", - [Type, c_uint], - Type, - Type.from_result), - - ("clang_getArrayElementType", - [Type], - Type, - Type.from_result), - - ("clang_getArraySize", - [Type], - c_longlong), - - ("clang_getFieldDeclBitWidth", - [Cursor], - c_int), - - ("clang_getCanonicalCursor", - [Cursor], - Cursor, - Cursor.from_cursor_result), - - ("clang_getCanonicalType", - [Type], - Type, - Type.from_result), - - ("clang_getChildDiagnostics", - [Diagnostic], - c_object_p), - - ("clang_getCompletionAvailability", - [c_void_p], - c_int), - - ("clang_getCompletionBriefComment", - [c_void_p], - _CXString, - _CXString.from_result), - - ("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, - _CXString.from_result), - - ("clang_getCompletionPriority", - [c_void_p], - c_int), - - ("clang_getCString", - [_CXString], - c_interop_string, - c_interop_string.to_python_string), - - ("clang_getCursor", - [TranslationUnit, SourceLocation], - Cursor), - - ("clang_getCursorAvailability", - [Cursor], - c_int), - - ("clang_getCursorDefinition", - [Cursor], - Cursor, - Cursor.from_result), - - ("clang_getCursorDisplayName", - [Cursor], - _CXString, - _CXString.from_result), - - ("clang_getCursorExtent", - [Cursor], - SourceRange), - - ("clang_getCursorLexicalParent", - [Cursor], - Cursor, - Cursor.from_cursor_result), - - ("clang_getCursorLocation", - [Cursor], - SourceLocation), - - ("clang_getCursorReferenced", - [Cursor], - Cursor, - Cursor.from_result), - - ("clang_getCursorReferenceNameRange", - [Cursor, c_uint, c_uint], - SourceRange), - - ("clang_getCursorResultType", - [Cursor], - Type, - Type.from_result), - - ("clang_getCursorSemanticParent", - [Cursor], - Cursor, - Cursor.from_cursor_result), - - ("clang_getCursorSpelling", - [Cursor], - _CXString, - _CXString.from_result), - - ("clang_getCursorType", - [Cursor], - Type, - Type.from_result), - - ("clang_getCursorUSR", - [Cursor], - _CXString, - _CXString.from_result), - - ("clang_Cursor_getMangling", - [Cursor], - _CXString, - _CXString.from_result), - -# ("clang_getCXTUResourceUsage", -# [TranslationUnit], -# CXTUResourceUsage), - - ("clang_getCXXAccessSpecifier", - [Cursor], - c_uint), - - ("clang_getDeclObjCTypeEncoding", - [Cursor], - _CXString, - _CXString.from_result), - - ("clang_getDiagnostic", - [c_object_p, c_uint], - c_object_p), - - ("clang_getDiagnosticCategory", - [Diagnostic], - c_uint), - - ("clang_getDiagnosticCategoryText", - [Diagnostic], - _CXString, - _CXString.from_result), - - ("clang_getDiagnosticFixIt", - [Diagnostic, c_uint, POINTER(SourceRange)], - _CXString, - _CXString.from_result), - - ("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, - _CXString.from_result), - - ("clang_getDiagnosticRange", - [Diagnostic, c_uint], - SourceRange), - - ("clang_getDiagnosticSeverity", - [Diagnostic], - c_int), - - ("clang_getDiagnosticSpelling", - [Diagnostic], - _CXString, - _CXString.from_result), - - ("clang_getElementType", - [Type], - Type, - Type.from_result), - - ("clang_getEnumConstantDeclUnsignedValue", - [Cursor], - c_ulonglong), - - ("clang_getEnumConstantDeclValue", - [Cursor], - c_longlong), - - ("clang_getEnumDeclIntegerType", - [Cursor], - Type, - Type.from_result), - - ("clang_getFile", - [TranslationUnit, c_interop_string], - c_object_p), - - ("clang_getFileName", - [File], - _CXString, - _CXString.from_result), - - ("clang_getFileTime", - [File], - c_uint), - - ("clang_getIBOutletCollectionType", - [Cursor], - Type, - Type.from_result), - - ("clang_getIncludedFile", - [Cursor], - c_object_p, - File.from_result), - - ("clang_getInclusions", - [TranslationUnit, callbacks['translation_unit_includes'], 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_getOverloadedDecl", - [Cursor, c_uint], - Cursor, - Cursor.from_cursor_result), - - ("clang_getPointeeType", - [Type], - Type, - Type.from_result), - - ("clang_getRange", - [SourceLocation, SourceLocation], - SourceRange), - - ("clang_getRangeEnd", - [SourceRange], - SourceLocation), - - ("clang_getRangeStart", - [SourceRange], - SourceLocation), - - ("clang_getResultType", - [Type], - Type, - Type.from_result), - - ("clang_getSpecializedCursorTemplate", - [Cursor], - Cursor, - Cursor.from_cursor_result), - - ("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, - _CXString.from_result), - - ("clang_getTranslationUnitCursor", - [TranslationUnit], - Cursor, - Cursor.from_result), - - ("clang_getTranslationUnitSpelling", - [TranslationUnit], - _CXString, - _CXString.from_result), - - ("clang_getTUResourceUsageName", - [c_uint], - c_interop_string, - c_interop_string.to_python_string), - - ("clang_getTypeDeclaration", - [Type], - Cursor, - Cursor.from_result), - - ("clang_getTypedefDeclUnderlyingType", - [Cursor], - Type, - Type.from_result), - - ("clang_getTypedefName", - [Type], - _CXString, - _CXString.from_result), - - ("clang_getTypeKindSpelling", - [c_uint], - _CXString, - _CXString.from_result), - - ("clang_getTypeSpelling", - [Type], - _CXString, - _CXString.from_result), - - ("clang_hashCursor", - [Cursor], - c_uint), - - ("clang_isAttribute", - [CursorKind], - bool), - - ("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_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, callbacks['cursor_visit'], py_object], - c_uint), - - ("clang_Cursor_getNumArguments", - [Cursor], - c_int), - - ("clang_Cursor_getArgument", - [Cursor, c_uint], - Cursor, - Cursor.from_result), - - ("clang_Cursor_getNumTemplateArguments", - [Cursor], - c_int), - - ("clang_Cursor_getTemplateArgumentKind", - [Cursor, c_uint], - TemplateArgumentKind.from_id), - - ("clang_Cursor_getTemplateArgumentType", - [Cursor, c_uint], - Type, - Type.from_result), - - ("clang_Cursor_getTemplateArgumentValue", - [Cursor, c_uint], - c_longlong), - - ("clang_Cursor_getTemplateArgumentUnsignedValue", - [Cursor, c_uint], - c_ulonglong), - - ("clang_Cursor_isAnonymous", - [Cursor], - bool), - - ("clang_Cursor_isBitField", - [Cursor], - bool), - - ("clang_Cursor_getBriefCommentText", - [Cursor], - _CXString, - _CXString.from_result), - - ("clang_Cursor_getRawCommentText", - [Cursor], - _CXString, - _CXString.from_result), - - ("clang_Cursor_getOffsetOfField", - [Cursor], - c_longlong), - - ("clang_Type_getAlignOf", - [Type], - c_longlong), - - ("clang_Type_getClassType", - [Type], - Type, - Type.from_result), - - ("clang_Type_getNumTemplateArguments", - [Type], - c_int), - - ("clang_Type_getTemplateArgumentAsType", - [Type, c_uint], - Type, - Type.from_result), - - ("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, - Type.from_result), - - ("clang_Type_visitFields", - [Type, callbacks['fields_visit'], py_object], - c_uint), + ( + "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, + CompilationDatabase.from_result, + ), + ( + "clang_CompilationDatabase_getAllCompileCommands", + [c_object_p], + c_object_p, + CompileCommands.from_result, + ), + ( + "clang_CompilationDatabase_getCompileCommands", + [c_object_p, c_interop_string], + c_object_p, + CompileCommands.from_result, + ), + ("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, + _CXString.from_result, + ), + ( + "clang_CompileCommand_getDirectory", + [c_object_p], + _CXString, + _CXString.from_result, + ), + ( + "clang_CompileCommand_getFilename", + [c_object_p], + _CXString, + _CXString.from_result, + ), + ("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_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_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, _CXString.from_result), + ("clang_getArgType", [Type, c_uint], Type, Type.from_result), + ("clang_getArrayElementType", [Type], Type, Type.from_result), + ("clang_getArraySize", [Type], c_longlong), + ("clang_getFieldDeclBitWidth", [Cursor], c_int), + ("clang_getCanonicalCursor", [Cursor], Cursor, Cursor.from_cursor_result), + ("clang_getCanonicalType", [Type], Type, Type.from_result), + ("clang_getChildDiagnostics", [Diagnostic], c_object_p), + ("clang_getCompletionAvailability", [c_void_p], c_int), + ("clang_getCompletionBriefComment", [c_void_p], _CXString, _CXString.from_result), + ("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, + _CXString.from_result, + ), + ("clang_getCompletionPriority", [c_void_p], c_int), + ( + "clang_getCString", + [_CXString], + c_interop_string, + c_interop_string.to_python_string, + ), + ("clang_getCursor", [TranslationUnit, SourceLocation], Cursor), + ("clang_getCursorAvailability", [Cursor], c_int), + ("clang_getCursorDefinition", [Cursor], Cursor, Cursor.from_result), + ("clang_getCursorDisplayName", [Cursor], _CXString, _CXString.from_result), + ("clang_getCursorExtent", [Cursor], SourceRange), + ("clang_getCursorLexicalParent", [Cursor], Cursor, Cursor.from_cursor_result), + ("clang_getCursorLocation", [Cursor], SourceLocation), + ("clang_getCursorReferenced", [Cursor], Cursor, Cursor.from_result), + ("clang_getCursorReferenceNameRange", [Cursor, c_uint, c_uint], SourceRange), + ("clang_getCursorResultType", [Cursor], Type, Type.from_result), + ("clang_getCursorSemanticParent", [Cursor], Cursor, Cursor.from_cursor_result), + ("clang_getCursorSpelling", [Cursor], _CXString, _CXString.from_result), + ("clang_getCursorType", [Cursor], Type, Type.from_result), + ("clang_getCursorUSR", [Cursor], _CXString, _CXString.from_result), + ("clang_Cursor_getMangling", [Cursor], _CXString, _CXString.from_result), + # ("clang_getCXTUResourceUsage", + # [TranslationUnit], + # CXTUResourceUsage), + ("clang_getCXXAccessSpecifier", [Cursor], c_uint), + ("clang_getDeclObjCTypeEncoding", [Cursor], _CXString, _CXString.from_result), + ("clang_getDiagnostic", [c_object_p, c_uint], c_object_p), + ("clang_getDiagnosticCategory", [Diagnostic], c_uint), + ("clang_getDiagnosticCategoryText", [Diagnostic], _CXString, _CXString.from_result), + ( + "clang_getDiagnosticFixIt", + [Diagnostic, c_uint, POINTER(SourceRange)], + _CXString, + _CXString.from_result, + ), + ("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, + _CXString.from_result, + ), + ("clang_getDiagnosticRange", [Diagnostic, c_uint], SourceRange), + ("clang_getDiagnosticSeverity", [Diagnostic], c_int), + ("clang_getDiagnosticSpelling", [Diagnostic], _CXString, _CXString.from_result), + ("clang_getElementType", [Type], Type, Type.from_result), + ("clang_getEnumConstantDeclUnsignedValue", [Cursor], c_ulonglong), + ("clang_getEnumConstantDeclValue", [Cursor], c_longlong), + ("clang_getEnumDeclIntegerType", [Cursor], Type, Type.from_result), + ("clang_getFile", [TranslationUnit, c_interop_string], c_object_p), + ("clang_getFileName", [File], _CXString, _CXString.from_result), + ("clang_getFileTime", [File], c_uint), + ("clang_getIBOutletCollectionType", [Cursor], Type, Type.from_result), + ("clang_getIncludedFile", [Cursor], c_object_p, File.from_result), + ( + "clang_getInclusions", + [TranslationUnit, callbacks["translation_unit_includes"], 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_getOverloadedDecl", [Cursor, c_uint], Cursor, Cursor.from_cursor_result), + ("clang_getPointeeType", [Type], Type, Type.from_result), + ("clang_getRange", [SourceLocation, SourceLocation], SourceRange), + ("clang_getRangeEnd", [SourceRange], SourceLocation), + ("clang_getRangeStart", [SourceRange], SourceLocation), + ("clang_getResultType", [Type], Type, Type.from_result), + ("clang_getSpecializedCursorTemplate", [Cursor], Cursor, Cursor.from_cursor_result), + ("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, + _CXString.from_result, + ), + ("clang_getTranslationUnitCursor", [TranslationUnit], Cursor, Cursor.from_result), + ( + "clang_getTranslationUnitSpelling", + [TranslationUnit], + _CXString, + _CXString.from_result, + ), + ( + "clang_getTUResourceUsageName", + [c_uint], + c_interop_string, + c_interop_string.to_python_string, + ), + ("clang_getTypeDeclaration", [Type], Cursor, Cursor.from_result), + ("clang_getTypedefDeclUnderlyingType", [Cursor], Type, Type.from_result), + ("clang_getTypedefName", [Type], _CXString, _CXString.from_result), + ("clang_getTypeKindSpelling", [c_uint], _CXString, _CXString.from_result), + ("clang_getTypeSpelling", [Type], _CXString, _CXString.from_result), + ("clang_hashCursor", [Cursor], c_uint), + ("clang_isAttribute", [CursorKind], bool), + ("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_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, callbacks["cursor_visit"], py_object], c_uint), + ("clang_Cursor_getNumArguments", [Cursor], c_int), + ("clang_Cursor_getArgument", [Cursor, c_uint], Cursor, Cursor.from_result), + ("clang_Cursor_getNumTemplateArguments", [Cursor], c_int), + ( + "clang_Cursor_getTemplateArgumentKind", + [Cursor, c_uint], + TemplateArgumentKind.from_id, + ), + ("clang_Cursor_getTemplateArgumentType", [Cursor, c_uint], Type, Type.from_result), + ("clang_Cursor_getTemplateArgumentValue", [Cursor, c_uint], c_longlong), + ("clang_Cursor_getTemplateArgumentUnsignedValue", [Cursor, c_uint], c_ulonglong), + ("clang_Cursor_isAnonymous", [Cursor], bool), + ("clang_Cursor_isBitField", [Cursor], bool), + ("clang_Cursor_getBriefCommentText", [Cursor], _CXString, _CXString.from_result), + ("clang_Cursor_getRawCommentText", [Cursor], _CXString, _CXString.from_result), + ("clang_Cursor_getOffsetOfField", [Cursor], c_longlong), + ("clang_Type_getAlignOf", [Type], c_longlong), + ("clang_Type_getClassType", [Type], Type, Type.from_result), + ("clang_Type_getNumTemplateArguments", [Type], c_int), + ("clang_Type_getTemplateArgumentAsType", [Type, c_uint], Type, Type.from_result), + ("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, Type.from_result), + ("clang_Type_visitFields", [Type, callbacks["fields_visit"], py_object], c_uint), ] + class LibclangError(Exception): def __init__(self, message): self.m = message @@ -4062,14 +3716,17 @@ def __init__(self, message): def __str__(self): return self.m + def register_function(lib, item, ignore_errors): # A function may not exist, if these bindings are used with an older or # incompatible version of libclang.so. try: func = getattr(lib, item[0]) except AttributeError as e: - msg = str(e) + ". Please ensure that your python bindings are "\ - "compatible with your libclang.so version." + msg = ( + str(e) + ". Please ensure that your python bindings are " + "compatible with your libclang.so version." + ) if ignore_errors: return raise LibclangError(msg) @@ -4083,6 +3740,7 @@ def register_function(lib, item, ignore_errors): if len(item) == 4: func.errcheck = item[3] + def register_functions(lib, ignore_errors): """Register function prototypes with a libclang library instance. @@ -4096,6 +3754,7 @@ def register(item): for f in functionList: register(f) + class Config(object): library_path = None library_file = None @@ -4106,8 +3765,10 @@ class Config(object): def set_library_path(path): """Set the path in which to search for libclang""" if Config.loaded: - raise Exception("library path must be set before before using " \ - "any other functionalities in libclang.") + raise Exception( + "library path must be set before before using " + "any other functionalities in libclang." + ) Config.library_path = fspath(path) @@ -4115,14 +3776,16 @@ def set_library_path(path): def set_library_file(filename): """Set the exact location of libclang""" if Config.loaded: - raise Exception("library file must be set before before using " \ - "any other functionalities in libclang.") + raise Exception( + "library file must be set before before using " + "any other functionalities in libclang." + ) Config.library_file = fspath(filename) @staticmethod def set_compatibility_check(check_status): - """ Perform compatibility check when loading libclang + """Perform compatibility check when loading libclang The python bindings are only tested and evaluated with the version of libclang they are provided with. To ensure correct behavior a (limited) @@ -4139,8 +3802,10 @@ def set_compatibility_check(check_status): libclang versions. """ if Config.loaded: - raise Exception("compatibility_check must be set before before " \ - "using any other functionalities in libclang.") + raise Exception( + "compatibility_check must be set before before " + "using any other functionalities in libclang." + ) Config.compatibility_check = check_status @@ -4156,17 +3821,18 @@ def get_filename(self): return Config.library_file import platform + name = platform.system() - if name == 'Darwin': - file = 'libclang.dylib' - elif name == 'Windows': - file = 'libclang.dll' + if name == "Darwin": + file = "libclang.dylib" + elif name == "Windows": + file = "libclang.dll" else: - file = 'libclang.so' + file = "libclang.so" if Config.library_path: - file = Config.library_path + '/' + file + file = Config.library_path + "/" + file return file @@ -4174,9 +3840,11 @@ def get_cindex_library(self): try: library = cdll.LoadLibrary(self.get_filename()) except OSError as e: - msg = str(e) + ". To provide a path to libclang use " \ - "Config.set_library_path() or " \ - "Config.set_library_file()." + msg = ( + str(e) + ". To provide a path to libclang use " + "Config.set_library_path() or " + "Config.set_library_file()." + ) raise LibclangError(msg) return library @@ -4189,34 +3857,36 @@ def function_exists(self, name): return True + def register_enumerations(): for name, value in cppygen._clang.enumerations.TokenKinds: TokenKind.register(value, name) + conf = Config() register_enumerations() __all__ = [ - 'AvailabilityKind', - 'Config', - 'CodeCompletionResults', - 'CompilationDatabase', - 'CompileCommands', - 'CompileCommand', - 'CursorKind', - 'Cursor', - 'Diagnostic', - 'File', - 'FixIt', - 'Index', - 'LinkageKind', - 'SourceLocation', - 'SourceRange', - 'TLSKind', - 'TokenKind', - 'Token', - 'TranslationUnitLoadError', - 'TranslationUnit', - 'TypeKind', - 'Type', + "AvailabilityKind", + "Config", + "CodeCompletionResults", + "CompilationDatabase", + "CompileCommands", + "CompileCommand", + "CursorKind", + "Cursor", + "Diagnostic", + "File", + "FixIt", + "Index", + "LinkageKind", + "SourceLocation", + "SourceRange", + "TLSKind", + "TokenKind", + "Token", + "TranslationUnitLoadError", + "TranslationUnit", + "TypeKind", + "Type", ] diff --git a/cppygen/_clang/enumerations.py b/cppygen/_clang/enumerations.py index 520e134..b1013c7 100644 --- a/cppygen/_clang/enumerations.py +++ b/cppygen/_clang/enumerations.py @@ -1,10 +1,10 @@ -#===- enumerations.py - Python Enumerations ------------------*- python -*--===# +# ===- enumerations.py - Python Enumerations ------------------*- python -*--===# # # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. # See https://llvm.org/LICENSE.txt for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception # -#===------------------------------------------------------------------------===# +# ===------------------------------------------------------------------------===# """ Clang Enumerations @@ -23,11 +23,11 @@ # Maps to CXTokenKind. Note that libclang maintains a separate set of token # enumerations from the C++ API. TokenKinds = [ - ('PUNCTUATION', 0), - ('KEYWORD', 1), - ('IDENTIFIER', 2), - ('LITERAL', 3), - ('COMMENT', 4), + ("PUNCTUATION", 0), + ("KEYWORD", 1), + ("IDENTIFIER", 2), + ("LITERAL", 3), + ("COMMENT", 4), ] -__all__ = ['TokenKinds'] +__all__ = ["TokenKinds"] diff --git a/cppygen/logging.py b/cppygen/logging.py index 16b5bcc..6cc09ea 100644 --- a/cppygen/logging.py +++ b/cppygen/logging.py @@ -13,7 +13,7 @@ def create_default_formatter() -> colorlog.ColoredFormatter: return colorlog.ColoredFormatter( "%(log_color)s%(message)s", no_color=False if _color_supported() else True, - stream=sys.stdout + stream=sys.stdout, )