FFI::ClangSourceFFIClangIndex

class Index

Represents a libclang index that manages translation units and provides a top-level context for parsing.

Definitions

def initialize(exclude_declarations = true, display_diagnostics = false)

Initialize a new index for managing translation units.

Signature

parameter exclude_declarations Boolean

Whether to exclude declarations from PCH.

parameter display_diagnostics Boolean

Whether to display diagnostics during parsing.

Implementation

def initialize(exclude_declarations = true, display_diagnostics = false)
	super Lib.create_index(exclude_declarations ? 1 : 0, display_diagnostics ? 1 : 0)
end

def self.release(pointer)

Release the index pointer.

Signature

parameter pointer FFI::Pointer

The index pointer to release.

Implementation

def self.release(pointer)
	Lib.dispose_index(pointer)
end

def parse_translation_unit(source_file, command_line_args = nil, unsaved = [], opts = {})

Parse a source file and create a translation unit.

Signature

parameter source_file String

The path to the source file to parse.

parameter command_line_args Array(String) | String | Nil

Compiler arguments for parsing.

parameter unsaved Array(UnsavedFile)

Unsaved file buffers.

parameter opts Hash

Parsing options as a hash of flags.

returns TranslationUnit

The parsed translation unit.

raises Error

If parsing fails.

Implementation

def parse_translation_unit(source_file, command_line_args = nil, unsaved = [], opts = {})
	command_line_args = Array(command_line_args)
	unsaved_files = UnsavedFile.unsaved_pointer_from(unsaved)
	
	translation_unit_pointer_out = FFI::MemoryPointer.new(:pointer)
	
	error_code = Lib.parse_translation_unit2(self, source_file, args_pointer_from(command_line_args), command_line_args.size, unsaved_files, unsaved.length, options_bitmask_from(opts), translation_unit_pointer_out)
	if error_code != :cx_error_success
		error_name = Lib::ErrorCodes.from_native(error_code, nil)
		message = "Error parsing file. Code: #{error_name}. File: #{source_file.inspect}"
		raise(Error, message)
	end
	
	translation_unit_pointer = translation_unit_pointer_out.read_pointer
	TranslationUnit.new translation_unit_pointer, self
end

def create_translation_unit(ast_filename)

Create a translation unit from a precompiled AST file.

Signature

parameter ast_filename String

The path to the AST file.

returns TranslationUnit

The loaded translation unit.

raises Error

If loading the AST file fails.

Implementation

def create_translation_unit(ast_filename)
	translation_unit_pointer = Lib.create_translation_unit(self, ast_filename)
	raise Error, "error parsing #{ast_filename.inspect}" if translation_unit_pointer.null?
	TranslationUnit.new translation_unit_pointer, self
end

def args_pointer_from(command_line_args)

Convert command line arguments to a pointer array for libclang.

Signature

parameter command_line_args Array(String)

The command line arguments.

returns FFI::MemoryPointer

A pointer to the arguments array.

Implementation

def args_pointer_from(command_line_args)
	args_pointer = MemoryPointer.new(:pointer, command_line_args.length)
	
	strings = command_line_args.map do |arg|
		MemoryPointer.from_string(arg.to_s)
	end
	
	args_pointer.put_array_of_pointer(0, strings) unless strings.empty?
	args_pointer
end

def options_bitmask_from(opts)

Convert options hash to a bitmask for libclang.

Signature

parameter opts Hash

The options hash.

returns Integer

The bitmask representing the options.

Implementation

def options_bitmask_from(opts)
	Lib.bitmask_from(Lib::TranslationUnitFlags, opts)
end