Class: FFI::Clang::TranslationUnit

Inherits:
AutoPointer
  • Object
show all
Defined in:
lib/ffi/clang/translation_unit.rb

Overview

Represents a single translation unit (a compiled source file with its dependencies).

Defined Under Namespace

Classes: ResourceUsage

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer, index) ⇒ TranslationUnit

Initialize a translation unit with a pointer and parent index.



27
28
29
30
# File 'lib/ffi/clang/translation_unit.rb', line 27

def initialize(pointer, index)
  super pointer
  @index = index
end

Class Method Details

.default_editing_translation_unit_optionsObject

Get the default editing translation unit options.



40
41
42
43
# File 'lib/ffi/clang/translation_unit.rb', line 40

def self.default_editing_translation_unit_options
  bitmask = Lib.default_editing_translation_unit_options
  Lib.opts_from Lib::TranslationUnitFlags, bitmask
end

.release(pointer) ⇒ Object

Release the translation unit pointer.



34
35
36
# File 'lib/ffi/clang/translation_unit.rb', line 34

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

Instance Method Details

#code_complete(source_file, line, column, unsaved = [], opts = nil) ⇒ Object

Perform code completion at a specific location.



159
160
161
162
163
164
165
# File 'lib/ffi/clang/translation_unit.rb', line 159

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

#cursor(location = nil) ⇒ Object

Get a cursor for the translation unit or at a specific location.



94
95
96
97
98
99
100
# File 'lib/ffi/clang/translation_unit.rb', line 94

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

#default_reparse_optionsObject

Get the default reparse options for this translation unit.



65
66
67
68
# File 'lib/ffi/clang/translation_unit.rb', line 65

def default_reparse_options
  bitmask = Lib.default_save_options(self)
  Lib.opts_from Lib::ReparseFlags, bitmask
end

#default_save_optionsObject

Get the default save options for this translation unit.



47
48
49
50
# File 'lib/ffi/clang/translation_unit.rb', line 47

def default_save_options
  bitmask = Lib.default_save_options(self)
  Lib.opts_from Lib::SaveTranslationUnitFlags, bitmask
end

#diagnosticsObject

Get all diagnostics for this translation unit.



83
84
85
86
87
88
89
# File 'lib/ffi/clang/translation_unit.rb', line 83

def diagnostics
  n = Lib.get_num_diagnostics(self)
  
  n.times.map do |i|
    Diagnostic.new(self, Lib.get_diagnostic(self, i))
  end
end

#file(file_name = nil) ⇒ Object

Get a file object from this translation unit.



122
123
124
125
126
127
128
# File 'lib/ffi/clang/translation_unit.rb', line 122

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

#inclusions(&block) ⇒ Object

Iterate over all file inclusions in this translation unit.



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ffi/clang/translation_unit.rb', line 171

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

#location(file, line, column) ⇒ Object

Get a source location by file, line, and column.



107
108
109
# File 'lib/ffi/clang/translation_unit.rb', line 107

def location(file, line, column)
  ExpansionLocation.new Lib.get_location(self, file, line, column)
end

#location_offset(file, offset) ⇒ Object

Get a source location by file and byte offset.



115
116
117
# File 'lib/ffi/clang/translation_unit.rb', line 115

def location_offset(file, offset)
  ExpansionLocation.new Lib.get_location_offset(self, file, offset)
end

#reparse(unsaved = [], opts = {}) ⇒ Object

Reparse the translation unit with updated file contents.



74
75
76
77
78
79
# File 'lib/ffi/clang/translation_unit.rb', line 74

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

#resource_usageObject

Get resource usage information for this translation unit.



138
139
140
# File 'lib/ffi/clang/translation_unit.rb', line 138

def resource_usage
  FFI::Clang::TranslationUnit::ResourceUsage.new Lib.resource_usage(self)
end

#save(filename, opts = {}) ⇒ Object

Save the translation unit to a file.

Raises:



56
57
58
59
60
61
# File 'lib/ffi/clang/translation_unit.rb', line 56

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

#spellingObject

Get the spelling (filename) of this translation unit.



132
133
134
# File 'lib/ffi/clang/translation_unit.rb', line 132

def spelling
  Lib.extract_string Lib.get_translation_unit_spelling(self)
end

#tokenize(range) ⇒ Object

Tokenize a source range.



145
146
147
148
149
150
# File 'lib/ffi/clang/translation_unit.rb', line 145

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