Class: FFI::Clang::Cursor::OverriddenCursors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ffi/clang/overridden_cursors.rb

Overview

Represents the set of cursors overridden by a method. Calls clang_getOverriddenCursors, copies each CXCursor into Ruby-managed memory, then immediately disposes the buffer via clang_disposeOverriddenCursors. This avoids GC-order issues with AutoPointer where the buffer could be double-freed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cursor, translation_unit) ⇒ OverriddenCursors

Initialize overridden cursors for a given cursor.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ffi/clang/overridden_cursors.rb', line 26

def initialize(cursor, translation_unit)
	cursor_ptr = FFI::MemoryPointer.new :pointer
	num_ptr = FFI::MemoryPointer.new :uint
	Lib.get_overridden_cursors(cursor, cursor_ptr, num_ptr)
	
	@size = num_ptr.get_uint(0)
	
	if @size > 0
		buffer = cursor_ptr.get_pointer(0)
		
		# Dup each CXCursor into Ruby-managed memory before
		# disposing the buffer. Using an AutoPointer to defer
		# disposal causes double-free crashes on Linux and MacOS
		# (not windows) for unknown reasons.
		cur_ptr = buffer
		@cursors = @size.times.map do
			cursor = Lib::CXCursor.new(cur_ptr).dup
			cur_ptr += Lib::CXCursor.size
			Cursor.new(cursor, translation_unit)
		end
		
		Lib.dispose_overridden_cursors(buffer)
	else
		@cursors = []
	end
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



21
22
23
# File 'lib/ffi/clang/overridden_cursors.rb', line 21

def size
  @size
end

Instance Method Details

#each(&block) ⇒ Object

Iterate over each overridden cursor.



57
58
59
60
61
62
63
# File 'lib/ffi/clang/overridden_cursors.rb', line 57

def each(&block)
	return to_enum(__method__) unless block_given?
	
	@cursors.each(&block)
	
	self
end