class TranslationUnit
Represents a single translation unit (a compiled source file with its dependencies).
Nested
Definitions
def initialize(pointer, index)
Initialize a translation unit with a pointer and parent index.
Signature
-
parameter
pointerFFI::Pointer The translation unit pointer.
-
parameter
indexIndex The parent index that created this translation unit.
Implementation
def initialize(pointer, index)
super pointer
@index = index
end
def self.release(pointer)
Release the translation unit pointer.
Signature
-
parameter
pointerFFI::Pointer The translation unit pointer to release.
Implementation
def self.release(pointer)
Lib.dispose_translation_unit(pointer)
end
def self.default_editing_translation_unit_options
Get the default editing translation unit options.
Signature
-
returns
Array(Symbol) The default editing options.
Implementation
def self.default_editing_translation_unit_options
bitmask = Lib.default_editing_translation_unit_options
Lib.opts_from Lib::TranslationUnitFlags, bitmask
end
def default_save_options
Get the default save options for this translation unit.
Signature
-
returns
Array(Symbol) The default save options.
Implementation
def default_save_options
bitmask = Lib.default_save_options(self)
Lib.opts_from Lib::SaveTranslationUnitFlags, bitmask
end
def save(filename, opts = {})
Save the translation unit to a file.
Signature
-
parameter
filenameString The path where the translation unit should be saved.
-
parameter
optsHash Save options.
-
raises
Error If saving fails.
Implementation
def save(filename, opts = {})
ret = Lib.save_translation_unit(self, filename, 0)
sym = Lib::SaveError[ret]
raise Error, "unknown return values: #{ret} #{sym.inspect}" unless sym
raise Error, "save error: #{sym.inspect}, filename: #{filename}" if sym != :none
end
def default_reparse_options
Get the default reparse options for this translation unit.
Signature
-
returns
Array(Symbol) The default reparse options.
Implementation
def default_reparse_options
bitmask = Lib.default_save_options(self)
Lib.opts_from Lib::ReparseFlags, bitmask
end
def reparse(unsaved = [], opts = {})
Reparse the translation unit with updated file contents.
Signature
-
parameter
unsavedArray(UnsavedFile) Unsaved file buffers.
-
parameter
optsHash Reparse options.
-
raises
Error If reparsing fails.
Implementation
def reparse(unsaved = [], opts = {})
unsaved_files = UnsavedFile.unsaved_pointer_from(unsaved)
if Lib.reparse_translation_unit(self, unsaved.size, unsaved_files, 0) != 0
raise Error, "reparse error"
end
end
def diagnostics
Get all diagnostics for this translation unit.
Signature
-
returns
Array(Diagnostic) Array of diagnostics.
Implementation
def diagnostics
n = Lib.get_num_diagnostics(self)
n.times.map do |i|
Diagnostic.new(self, Lib.get_diagnostic(self, i))
end
end
def cursor(location = nil)
Get a cursor for the translation unit or at a specific location.
Signature
-
parameter
locationSourceLocation | Nil The location for the cursor, or
nilfor the root cursor.-
returns
Cursor The cursor at the specified location or the root cursor.
Implementation
def cursor(location = nil)
if location.nil?
Cursor.new Lib.get_translation_unit_cursor(self), self
else
Cursor.new Lib.get_cursor(self, location.location), self
end
end
def location(file, line, column)
Get a source location by file, line, and column.
Signature
-
parameter
fileFile The file object.
-
parameter
lineInteger The line number (1-indexed).
-
parameter
columnInteger The column number (1-indexed).
-
returns
ExpansionLocation The source location.
Implementation
def location(file, line, column)
ExpansionLocation.new Lib.get_location(self, file, line, column)
end
def location_offset(file, offset)
Get a source location by file and byte offset.
Signature
-
parameter
fileFile The file object.
-
parameter
offsetInteger The byte offset from the start of the file.
-
returns
ExpansionLocation The source location.
Implementation
def location_offset(file, offset)
ExpansionLocation.new Lib.get_location_offset(self, file, offset)
end
def file(file_name = nil)
Get a file object from this translation unit.
Signature
-
parameter
file_nameString | Nil The file name, or
nilto get the main file.-
returns
File The file object.
Implementation
def file(file_name = nil)
if file_name.nil?
File.new(Lib.get_file(self, spelling), self)
else
File.new(Lib.get_file(self, file_name), self)
end
end
def spelling
Get the spelling (filename) of this translation unit.
Signature
-
returns
String The filename of the translation unit.
Implementation
def spelling
Lib.extract_string Lib.get_translation_unit_spelling(self)
end
def resource_usage
Get resource usage information for this translation unit.
Signature
-
returns
ResourceUsage Resource usage statistics.
Implementation
def resource_usage
FFI::Clang::TranslationUnit::ResourceUsage.new Lib.resource_usage(self)
end
def tokenize(range)
Tokenize a source range.
Signature
-
parameter
rangeSourceRange The source range to tokenize.
-
returns
Tokens Collection of tokens in the range.
Implementation
def tokenize(range)
token_ptr = MemoryPointer.new :pointer
uint_ptr = MemoryPointer.new :uint
Lib.tokenize(self, range.range, token_ptr, uint_ptr)
Tokens.new(token_ptr.get_pointer(0), uint_ptr.get_uint(0), self)
end
def code_complete(source_file, line, column, unsaved = [], opts = nil)
Perform code completion at a specific location.
Signature
-
parameter
source_fileString The path to the source file.
-
parameter
lineInteger The line number for code completion.
-
parameter
columnInteger The column number for code completion.
-
parameter
unsavedArray(UnsavedFile) Unsaved file buffers.
-
parameter
optsArray(Symbol) | Nil Code completion options, or
nilfor defaults.-
returns
CodeCompletion::Results The code completion results.
Implementation
def code_complete(source_file, line, column, unsaved = [], opts = nil)
opts = CodeCompletion.default_code_completion_options if opts.nil?
unsaved_files = UnsavedFile.unsaved_pointer_from(unsaved)
option_bitmask = Lib.bitmask_from(Lib::CodeCompleteFlags, opts)
ptr = Lib.code_complete_at(self, source_file, line, column, unsaved_files, unsaved.length, option_bitmask)
CodeCompletion::Results.new ptr, self
end
def inclusions(&block)
Iterate over all file inclusions in this translation unit.
Signature
-
yields
{|file, locations| ...} Each inclusion with its file path and stack.
-
parameter
fileString The included file path.
-
parameter
locationsArray(SourceLocation) The inclusion stack.
-
parameter
Implementation
def inclusions(&block)
adapter = Proc.new do |included_file, inclusion_stack, include_len, unused|
file = Lib.extract_string Lib.get_file_name(included_file)
cur_ptr = inclusion_stack
inclusions = []
include_len.times {inclusions << SourceLocation.new(Lib::CXSourceLocation.new(cur_ptr))
cur_ptr += Lib::CXSourceLocation.size
}
block.call file, inclusions
end
Lib.get_inclusions(self, adapter, nil)
end