summaryrefslogtreecommitdiffstats
path: root/bindings/python/clang/cindex.py
diff options
context:
space:
mode:
Diffstat (limited to 'bindings/python/clang/cindex.py')
-rw-r--r--bindings/python/clang/cindex.py375
1 files changed, 350 insertions, 25 deletions
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index 7cc9dda..b4563eb 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -63,6 +63,7 @@ call is efficient.
# o implement additional SourceLocation, SourceRange, and File methods.
from ctypes import *
+import collections
def get_cindex_library():
# FIXME: It's probably not the case that the library is actually found in
@@ -111,10 +112,21 @@ class SourceLocation(Structure):
if self._data is None:
f, l, c, o = c_object_p(), c_uint(), c_uint(), c_uint()
SourceLocation_loc(self, byref(f), byref(l), byref(c), byref(o))
- f = File(f) if f else None
+ if f:
+ f = File(f)
+ else:
+ f = None
self._data = (f, int(l.value), int(c.value), int(o.value))
return self._data
+ @staticmethod
+ def from_position(tu, file, line, column):
+ """
+ Retrieve the source location associated with a given file/line/column in
+ a particular translation unit.
+ """
+ return SourceLocation_getLocation(tu, file, line, column)
+
@property
def file(self):
"""Get the file represented by this source location."""
@@ -135,9 +147,19 @@ class SourceLocation(Structure):
"""Get the file offset represented by this source location."""
return self._get_instantiation()[3]
+ def __eq__(self, other):
+ return SourceLocation_equalLocations(self, other)
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
def __repr__(self):
+ if self.file:
+ filename = self.file.name
+ else:
+ filename = None
return "<SourceLocation file %r, line %r, column %r>" % (
- self.file.name if self.file else None, self.line, self.column)
+ filename, self.line, self.column)
class SourceRange(Structure):
"""
@@ -171,6 +193,12 @@ class SourceRange(Structure):
"""
return SourceRange_end(self)
+ def __eq__(self, other):
+ return SourceRange_equalRanges(self, other)
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
def __repr__(self):
return "<SourceRange start %r, end %r>" % (self.start, self.end)
@@ -215,8 +243,8 @@ class Diagnostic(object):
return int(_clang_getDiagnosticNumRanges(self.diag))
def __getitem__(self, key):
- if (key >= len(self)):
- raise IndexError
+ if (key >= len(self)):
+ raise IndexError
return _clang_getDiagnosticRange(self.diag, key)
return RangeIterator(self)
@@ -240,6 +268,29 @@ class Diagnostic(object):
return FixItIterator(self)
+ @property
+ def category_number(self):
+ """The category number for this diagnostic."""
+ return _clang_getDiagnosticCategory(self)
+
+ @property
+ def category_name(self):
+ """The string name of the category for this diagnostic."""
+ return _clang_getDiagnosticCategoryName(self.category_number)
+
+ @property
+ def option(self):
+ """The command-line option that enables this diagnostic."""
+ return _clang_getDiagnosticOption(self, None)
+
+ @property
+ def disable_option(self):
+ """The command-line option that disables this diagnostic."""
+ disable = _CXString()
+ _clang_getDiagnosticOption(self, byref(disable))
+
+ return _CXString_getCString(disable)
+
def __repr__(self):
return "<Diagnostic severity %r, location %r, spelling %r>" % (
self.severity, self.location, self.spelling)
@@ -329,6 +380,18 @@ class CursorKind(object):
"""Test if this is an invalid kind."""
return CursorKind_is_inv(self)
+ def is_translation_unit(self):
+ """Test if this is a translation unit kind."""
+ return CursorKind_is_translation_unit(self)
+
+ def is_preprocessing(self):
+ """Test if this is a preprocessing kind."""
+ return CursorKind_is_preprocessing(self)
+
+ def is_unexposed(self):
+ """Test if this is an unexposed kind."""
+ return CursorKind_is_unexposed(self)
+
def __repr__(self):
return 'CursorKind.%s' % (self.name,)
@@ -592,7 +655,7 @@ CursorKind.ADDR_LABEL_EXPR = CursorKind(120)
# This is the GNU Statement Expression extension: ({int X=4; X;})
CursorKind.StmtExpr = CursorKind(121)
-# Represents a C1X generic selection.
+# Represents a C11 generic selection.
CursorKind.GENERIC_SELECTION_EXPR = CursorKind(122)
# Implements the GNU __null extension, which is a name for a null
@@ -801,6 +864,11 @@ CursorKind.IB_ACTION_ATTR = CursorKind(401)
CursorKind.IB_OUTLET_ATTR = CursorKind(402)
CursorKind.IB_OUTLET_COLLECTION_ATTR = CursorKind(403)
+CursorKind.CXX_FINAL_ATTR = CursorKind(404)
+CursorKind.CXX_OVERRIDE_ATTR = CursorKind(405)
+CursorKind.ANNOTATE_ATTR = CursorKind(406)
+CursorKind.ASM_LABEL_ATTR = CursorKind(407)
+
###
# Preprocessing
CursorKind.PREPROCESSING_DIRECTIVE = CursorKind(500)
@@ -817,11 +885,15 @@ class Cursor(Structure):
"""
_fields_ = [("_kind_id", c_int), ("xdata", c_int), ("data", c_void_p * 3)]
+ @staticmethod
+ def from_location(tu, location):
+ return Cursor_get(tu, location)
+
def __eq__(self, other):
return Cursor_eq(self, other)
def __ne__(self, other):
- return not Cursor_eq(self, other)
+ return not self.__eq__(other)
def is_definition(self):
"""
@@ -903,13 +975,54 @@ class Cursor(Structure):
@property
def type(self):
"""
- Retrieve the type (if any) of of the entity pointed at by the
- cursor.
+ Retrieve the Type (if any) of the entity pointed at by the cursor.
"""
if not hasattr(self, '_type'):
self._type = Cursor_type(self)
return self._type
+ @property
+ def underlying_typedef_type(self):
+ """Return the underlying type of a typedef declaration.
+
+ 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'):
+ assert self.kind.is_declaration()
+ self._underlying_type = Cursor_underlying_type(self)
+
+ return self._underlying_type
+
+ @property
+ def enum_type(self):
+ """Return the integer type of an enum declaration.
+
+ Returns a Type corresponding to an integer. If the cursor is not for an
+ enum, this raises.
+ """
+ if not hasattr(self, '_enum_type'):
+ assert self.kind == CursorKind.ENUM_DECL
+ self._enum_type = Cursor_enum_type(self)
+
+ return self._enum_type
+
+ @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 = Cursor_objc_type_encoding(self)
+
+ return self._objc_type_encoding
+
+ @property
+ def hash(self):
+ """Returns a hash of the cursor as an int."""
+ if not hasattr(self, '_hash'):
+ self._hash = Cursor_hash(self)
+
+ return self._hash
+
def get_children(self):
"""Return an iterator for accessing the children of this cursor."""
@@ -969,7 +1082,7 @@ class TypeKind(object):
@staticmethod
def from_id(id):
if id >= len(TypeKind._kinds) or TypeKind._kinds[id] is None:
- raise ValueError,'Unknown cursor kind'
+ raise ValueError,'Unknown type kind %d' % id
return TypeKind._kinds[id]
def __repr__(self):
@@ -1020,6 +1133,7 @@ TypeKind.OBJCOBJECTPOINTER = TypeKind(109)
TypeKind.FUNCTIONNOPROTO = TypeKind(110)
TypeKind.FUNCTIONPROTO = TypeKind(111)
TypeKind.CONSTANTARRAY = TypeKind(112)
+TypeKind.VECTOR = TypeKind(113)
class Type(Structure):
"""
@@ -1032,6 +1146,71 @@ class Type(Structure):
"""Return the kind of this type."""
return TypeKind.from_id(self._kind_id)
+ def argument_types(self):
+ """Retrieve a container for the non-variadic arguments for this type.
+
+ The returned object is iterable and indexable. Each item in the
+ container is a Type instance.
+ """
+ class ArgumentsIterator(collections.Sequence):
+ def __init__(self, parent):
+ self.parent = parent
+ self.length = None
+
+ def __len__(self):
+ if self.length is None:
+ self.length = Type_get_num_arg_types(self.parent)
+
+ return self.length
+
+ def __getitem__(self, key):
+ # FIXME Support slice objects.
+ if not isinstance(key, int):
+ raise TypeError("Must supply a non-negative int.")
+
+ if key < 0:
+ raise IndexError("Only non-negative indexes are accepted.")
+
+ if key >= len(self):
+ raise IndexError("Index greater than container length: "
+ "%d > %d" % ( key, len(self) ))
+
+ result = Type_get_arg_type(self.parent, key)
+ if result.kind == TypeKind.INVALID:
+ raise IndexError("Argument could not be retrieved.")
+
+ return result
+
+ assert self.kind == TypeKind.FUNCTIONPROTO
+ return ArgumentsIterator(self)
+
+ @property
+ def element_type(self):
+ """Retrieve the Type of elements within this Type.
+
+ If accessed on a type that is not an array, complex, or vector type, an
+ exception will be raised.
+ """
+ result = Type_get_element_type(self)
+ if result.kind == TypeKind.INVALID:
+ raise Exception('Element type not available on this type.')
+
+ return result
+
+ @property
+ def element_count(self):
+ """Retrieve the number of elements in this type.
+
+ Returns an int.
+
+ If the Type is not an array or vector, this raises.
+ """
+ result = Type_get_num_elements(self)
+ if result < 0:
+ raise Exception('Type does not have elements.')
+
+ return result
+
@staticmethod
def from_result(res, fn, args):
assert isinstance(res, Type)
@@ -1050,29 +1229,39 @@ class Type(Structure):
return Type_get_canonical(self)
def is_const_qualified(self):
- """
- Determine whether a Type has the "const" qualifier set,
- without looking through typedefs that may have added "const"
+ """Determine whether a Type has the "const" qualifier set.
+
+ This does not look through typedefs that may have added "const"
at a different level.
"""
return Type_is_const_qualified(self)
def is_volatile_qualified(self):
- """
- Determine whether a Type has the "volatile" qualifier set,
- without looking through typedefs that may have added
- "volatile" at a different level.
+ """Determine whether a Type has the "volatile" qualifier set.
+
+ This does not look through typedefs that may have added "volatile"
+ at a different level.
"""
return Type_is_volatile_qualified(self)
def is_restrict_qualified(self):
- """
- Determine whether a Type has the "restrict" qualifier set,
- without looking through typedefs that may have added
- "restrict" at a different level.
+ """Determine whether a Type has the "restrict" qualifier set.
+
+ This does not look through typedefs that may have added "restrict" at
+ a different level.
"""
return Type_is_restrict_qualified(self)
+ def is_function_variadic(self):
+ """Determine whether this function Type is a variadic function type."""
+ assert self.kind == TypeKind.FUNCTIONPROTO
+
+ return Type_is_variadic(self)
+
+ def is_pod(self):
+ """Determine whether this Type represents plain old data (POD)."""
+ return Type_is_pod(self)
+
def get_pointee(self):
"""
For pointer types, returns the type of the pointee.
@@ -1091,6 +1280,27 @@ class Type(Structure):
"""
return Type_get_result(self)
+ def get_array_element_type(self):
+ """
+ Retrieve the type of the elements of the array type.
+ """
+ return Type_get_array_element(self)
+
+ def get_array_size(self):
+ """
+ Retrieve the size of the constant array.
+ """
+ return Type_get_array_size(self)
+
+ def __eq__(self, other):
+ if type(other) != type(self):
+ return False
+
+ return Type_equal(self, other)
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
## CIndex Objects ##
# CIndex objects (derived from ClangObject) are essentially lightweight
@@ -1157,6 +1367,20 @@ _clang_getDiagnosticFixIt.argtypes = [Diagnostic, c_uint, POINTER(SourceRange)]
_clang_getDiagnosticFixIt.restype = _CXString
_clang_getDiagnosticFixIt.errcheck = _CXString.from_result
+_clang_getDiagnosticCategory = lib.clang_getDiagnosticCategory
+_clang_getDiagnosticCategory.argtypes = [Diagnostic]
+_clang_getDiagnosticCategory.restype = c_uint
+
+_clang_getDiagnosticCategoryName = lib.clang_getDiagnosticCategoryName
+_clang_getDiagnosticCategoryName.argtypes = [c_uint]
+_clang_getDiagnosticCategoryName.restype = _CXString
+_clang_getDiagnosticCategoryName.errcheck = _CXString.from_result
+
+_clang_getDiagnosticOption = lib.clang_getDiagnosticOption
+_clang_getDiagnosticOption.argtypes = [Diagnostic, POINTER(_CXString)]
+_clang_getDiagnosticOption.restype = _CXString
+_clang_getDiagnosticOption.errcheck = _CXString.from_result
+
###
class CompletionChunk:
@@ -1350,7 +1574,9 @@ class Index(ClangObject):
def read(self, path):
"""Load the translation unit from the given AST file."""
ptr = TranslationUnit_read(self, path)
- return TranslationUnit(ptr) if ptr else None
+ if ptr:
+ return TranslationUnit(ptr)
+ return None
def parse(self, path, args = [], unsaved_files = [], options = 0):
"""
@@ -1383,7 +1609,9 @@ class Index(ClangObject):
ptr = TranslationUnit_parse(self, path, arg_array, len(args),
unsaved_files_array, len(unsaved_files),
options)
- return TranslationUnit(ptr) if ptr else None
+ if ptr:
+ return TranslationUnit(ptr)
+ return None
class TranslationUnit(ClangObject):
@@ -1502,8 +1730,9 @@ class TranslationUnit(ClangObject):
unsaved_files_array,
len(unsaved_files),
options)
- return CodeCompletionResults(ptr) if ptr else None
-
+ if ptr:
+ return CodeCompletionResults(ptr)
+ return None
class File(ClangObject):
"""
@@ -1511,6 +1740,11 @@ class File(ClangObject):
translation unit.
"""
+ @staticmethod
+ def from_name(translation_unit, file_name):
+ """Retrieve a file handle within the given translation unit."""
+ return File(File_getFile(translation_unit, file_name))
+
@property
def name(self):
"""Return the complete file and path name of the file."""
@@ -1521,6 +1755,12 @@ class File(ClangObject):
"""Return the last modification time of the file."""
return File_time(self)
+ def __str__(self):
+ return self.name
+
+ def __repr__(self):
+ return "<File: %s>" % (self.name)
+
class FileInclusion(object):
"""
The FileInclusion class represents the inclusion of one source file by
@@ -1557,6 +1797,14 @@ SourceLocation_loc.argtypes = [SourceLocation, POINTER(c_object_p),
POINTER(c_uint), POINTER(c_uint),
POINTER(c_uint)]
+SourceLocation_getLocation = lib.clang_getLocation
+SourceLocation_getLocation.argtypes = [TranslationUnit, File, c_uint, c_uint]
+SourceLocation_getLocation.restype = SourceLocation
+
+SourceLocation_equalLocations = lib.clang_equalLocations
+SourceLocation_equalLocations.argtypes = [SourceLocation, SourceLocation]
+SourceLocation_equalLocations.restype = bool
+
# Source Range Functions
SourceRange_getRange = lib.clang_getRange
SourceRange_getRange.argtypes = [SourceLocation, SourceLocation]
@@ -1570,6 +1818,10 @@ SourceRange_end = lib.clang_getRangeEnd
SourceRange_end.argtypes = [SourceRange]
SourceRange_end.restype = SourceLocation
+SourceRange_equalRanges = lib.clang_equalRanges
+SourceRange_equalRanges.argtypes = [SourceRange, SourceRange]
+SourceRange_equalRanges.restype = bool
+
# CursorKind Functions
CursorKind_is_decl = lib.clang_isDeclaration
CursorKind_is_decl.argtypes = [CursorKind]
@@ -1595,6 +1847,18 @@ CursorKind_is_inv = lib.clang_isInvalid
CursorKind_is_inv.argtypes = [CursorKind]
CursorKind_is_inv.restype = bool
+CursorKind_is_translation_unit = lib.clang_isTranslationUnit
+CursorKind_is_translation_unit.argtypes = [CursorKind]
+CursorKind_is_translation_unit.restype = bool
+
+CursorKind_is_preprocessing = lib.clang_isPreprocessing
+CursorKind_is_preprocessing.argtypes = [CursorKind]
+CursorKind_is_preprocessing.restype = bool
+
+CursorKind_is_unexposed = lib.clang_isUnexposed
+CursorKind_is_unexposed.argtypes = [CursorKind]
+CursorKind_is_unexposed.restype = bool
+
# Cursor Functions
# TODO: Implement this function
Cursor_get = lib.clang_getCursor
@@ -1620,7 +1884,11 @@ Cursor_def.errcheck = Cursor.from_result
Cursor_eq = lib.clang_equalCursors
Cursor_eq.argtypes = [Cursor, Cursor]
-Cursor_eq.restype = c_uint
+Cursor_eq.restype = bool
+
+Cursor_hash = lib.clang_hashCursor
+Cursor_hash.argtypes = [Cursor]
+Cursor_hash.restype = c_uint
Cursor_spelling = lib.clang_getCursorSpelling
Cursor_spelling.argtypes = [Cursor]
@@ -1650,6 +1918,21 @@ Cursor_type.argtypes = [Cursor]
Cursor_type.restype = Type
Cursor_type.errcheck = Type.from_result
+Cursor_underlying_type = lib.clang_getTypedefDeclUnderlyingType
+Cursor_underlying_type.argtypes = [Cursor]
+Cursor_underlying_type.restype = Type
+Cursor_underlying_type.errcheck = Type.from_result
+
+Cursor_enum_type = lib.clang_getEnumDeclIntegerType
+Cursor_enum_type.argtypes = [Cursor]
+Cursor_enum_type.restype = Type
+Cursor_enum_type.errcheck = Type.from_result
+
+Cursor_objc_type_encoding = lib.clang_getDeclObjCTypeEncoding
+Cursor_objc_type_encoding.argtypes = [Cursor]
+Cursor_objc_type_encoding.restype = _CXString
+Cursor_objc_type_encoding.errcheck = _CXString.from_result
+
Cursor_visit_callback = CFUNCTYPE(c_int, Cursor, Cursor, py_object)
Cursor_visit = lib.clang_visitChildren
Cursor_visit.argtypes = [Cursor, Cursor_visit_callback, py_object]
@@ -1673,6 +1956,14 @@ Type_is_restrict_qualified = lib.clang_isRestrictQualifiedType
Type_is_restrict_qualified.argtypes = [Type]
Type_is_restrict_qualified.restype = bool
+Type_is_pod = lib.clang_isPODType
+Type_is_pod.argtypes = [Type]
+Type_is_pod.restype = bool
+
+Type_is_variadic = lib.clang_isFunctionTypeVariadic
+Type_is_variadic.argtypes = [Type]
+Type_is_variadic.restype = bool
+
Type_get_pointee = lib.clang_getPointeeType
Type_get_pointee.argtypes = [Type]
Type_get_pointee.restype = Type
@@ -1688,6 +1979,36 @@ Type_get_result.argtypes = [Type]
Type_get_result.restype = Type
Type_get_result.errcheck = Type.from_result
+Type_get_num_arg_types = lib.clang_getNumArgTypes
+Type_get_num_arg_types.argtypes = [Type]
+Type_get_num_arg_types.restype = c_uint
+
+Type_get_arg_type = lib.clang_getArgType
+Type_get_arg_type.argtypes = [Type, c_uint]
+Type_get_arg_type.restype = Type
+Type_get_arg_type.errcheck = Type.from_result
+Type_get_element_type = lib.clang_getElementType
+
+Type_get_element_type.argtypes = [Type]
+Type_get_element_type.restype = Type
+Type_get_element_type.errcheck = Type.from_result
+
+Type_get_num_elements = lib.clang_getNumElements
+Type_get_num_elements.argtypes = [Type]
+Type_get_num_elements.restype = c_longlong
+
+Type_get_array_element = lib.clang_getArrayElementType
+Type_get_array_element.argtypes = [Type]
+Type_get_array_element.restype = Type
+Type_get_array_element.errcheck = Type.from_result
+
+Type_get_array_size = lib.clang_getArraySize
+Type_get_array_size.argtype = [Type]
+Type_get_array_size.restype = c_longlong
+
+Type_equal = lib.clang_equalTypes
+Type_equal.argtypes = [Type, Type]
+Type_equal.restype = bool
# Index Functions
Index_create = lib.clang_createIndex
@@ -1739,6 +2060,10 @@ TranslationUnit_includes.argtypes = [TranslationUnit,
py_object]
# File Functions
+File_getFile = lib.clang_getFile
+File_getFile.argtypes = [TranslationUnit, c_char_p]
+File_getFile.restype = c_object_p
+
File_name = lib.clang_getFileName
File_name.argtypes = [File]
File_name.restype = _CXString
OpenPOWER on IntegriCloud