def diagnostic_dict(self, diag): ranges = map(range_dict, diag.ranges) if len(ranges) == 0: ranges = None fixits = [] for fixit in diag.fixits: fixits.append({ 'range': range_dict(fixit.range), 'value': fixit.value, }) children = [] for child in child_diagnostics(self.custom_clang_lib, diag): children.append({ 'spelling': child.spelling, 'location': location_dict(child.location), 'ranges': map(range_dict, child.ranges), }) # Some fixits may be nested; add them to the root diagnostic. for fixit in child.fixits: fixits.append({ 'range': range_dict(fixit.range), 'value': fixit.value, }) return { 'spelling': diag.spelling, 'severity': diag.severity, 'location': location_dict(diag.location), 'ranges': ranges, 'fixits': fixits, 'children': children, }
def get_declaration_location_and_spelling(translation_unit, absolute_path, line, column): def log(s): logger.info('%s:%d:%d - %s', os.path.basename(absolute_path), line, column, s) source_location = translation_unit.get_location(absolute_path, (line, column)) cursor = Cursor.from_location(translation_unit, source_location) if cursor is None: log('No cursor') return None # Don't allow clicks/tooltips on most declarations, as their content is usually obvious. # Make an exception for variable declarations, as these can often have auto types. if cursor.kind != CursorKind.VAR_DECL and cursor.kind.is_declaration(): log('Ignoring declaration') return None referenced = cursor.referenced if referenced is None or referenced.location is None or referenced.location.file is None: log('No referenced information') return None loc = referenced.location log('Returning {0}:{1}:{2}'.format(os.path.basename(loc.file.name), loc.line, loc.column)) # An extent has a `start` and `end` property, each of which have a `line` # and `column` property. extent = cursor.extent type = None try: type = cursor.type and cursor.type.spelling except: logger.warn('Was not able to get cursor type') pass location = location_dict(loc) location['spelling'] = cursor.spelling location['type'] = type location['extent'] = range_dict(extent) return location
def get_declaration_location_and_spelling(translation_unit, absolute_path, line, column): def log(s): logger.info('%s:%d:%d - %s', os.path.basename(absolute_path), line, column, s) source_location = translation_unit.get_location(absolute_path, (line, column)) cursor = Cursor.from_location(translation_unit, source_location) if cursor is None: log('No cursor') return None # OBJC_INTERFACE_DECL extends all the way from @interface to @end; don't # allow clicking on it. if cursor.kind.is_declaration(): log('Ignoring declaration') return None referenced = cursor.referenced if referenced is None or referenced.location is None or referenced.location.file is None: log('No referenced information') return None loc = referenced.location log('Returning {0}:{1}:{2}'.format(os.path.basename(loc.file.name), loc.line, loc.column)) # An extent has a `start` and `end` property, each of which have a `line` # and `column` property. extent = cursor.extent type = None try: type = cursor.type and cursor.type.spelling except: logger.warn('Was not able to get cursor type') pass location = location_dict(loc) location['spelling'] = cursor.spelling location['type'] = type location['extent'] = range_dict(extent) return location
def get_declaration_location_and_spelling(translation_unit, absolute_path, line, column): def log(s): logger.info('%s:%d:%d - %s', os.path.basename(absolute_path), line, column, s) source_location = translation_unit.get_location( absolute_path, (line, column)) cursor = Cursor.from_location(translation_unit, source_location) if cursor is None: log('No cursor') return None # OBJC_INTERFACE_DECL extends all the way from @interface to @end; don't # allow clicking on it. if cursor.kind.is_declaration(): log('Ignoring declaration') return None referenced = cursor.referenced if referenced is None or referenced.location is None or referenced.location.file is None: log('No referenced information') return None loc = referenced.location log('Returning {0}:{1}:{2}'.format( os.path.basename(loc.file.name), loc.line, loc.column)) # An extent has a `start` and `end` property, each of which have a `line` # and `column` property. extent = cursor.extent type = None try: type = cursor.type and cursor.type.spelling except: logger.warn('Was not able to get cursor type') pass location = location_dict(loc) location['spelling'] = cursor.spelling location['type'] = type location['extent'] = range_dict(extent) return location
def get_declaration_location_and_spelling(translation_unit, contents, flags, absolute_path, line, column): def log(s): logger.info('%s:%d:%d - %s', os.path.basename(absolute_path), line, column, s) source_location = translation_unit.get_location( absolute_path, (line, column)) cursor = Cursor.from_location(translation_unit, source_location) if cursor is None: log('No cursor') return None # Don't allow clicks/tooltips on most declarations, as their content is usually obvious. # Make an exception for variable declarations, as these can often have auto types. if cursor.kind != CursorKind.VAR_DECL and cursor.kind.is_declaration(): log('Ignoring declaration') return None referenced = cursor.referenced if referenced is None or referenced.location is None or referenced.location.file is None: # If cursor is over an include statement, attempt to resolve the location # of the included file. line_text = get_line(contents, line) bounds = get_include_path_extent(line_text) if bounds is not None: start_col, end_col = bounds # Don't allow hyperclick if cursor is not over the include name, i.e.: # #include "header.h" # ^^^^^^^^ if column < start_col or column > end_col: return None filename = resolve_include(absolute_path, line_text, flags) if filename is None: return None # Point location to beginning of the found included file (line 0, column 0) location = { 'file': filename, 'line': 0, 'column': 0, # Show destination file of hyperclick in hover popover 'type': filename, 'spelling': None, 'extent': { 'start': {'line': line - 1, 'column': start_col}, 'end': {'line': line - 1, 'column': end_col} } } return location else: log('No referenced information') return None loc = referenced.location log('Returning {0}:{1}:{2}'.format( os.path.basename(loc.file.name), loc.line, loc.column)) # An extent has a `start` and `end` property, each of which have a `line` # and `column` property. extent = cursor.extent type = None try: type = cursor.type and cursor.type.spelling except: logger.warn('Was not able to get cursor type') pass location = location_dict(loc) location['spelling'] = cursor.spelling location['type'] = type location['extent'] = range_dict(extent) return location