Class: FFI::Clang::SourceRange

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

Overview

Represents a source range in a file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range_or_begin_location, end_location = nil) ⇒ SourceRange

Initialize a source range.



48
49
50
51
52
53
54
# File 'lib/ffi/clang/source_range.rb', line 48

def initialize(range_or_begin_location, end_location = nil)
	if end_location.nil?
		@range = range_or_begin_location
	else
		@range = Lib.get_range(range_or_begin_location.location, end_location.location)
	end
end

Instance Attribute Details

#rangeObject (readonly)

Returns the value of attribute range.



93
94
95
# File 'lib/ffi/clang/source_range.rb', line 93

def range
  @range
end

#The underlying range structure.(underlyingrangestructure.) ⇒ Object (readonly)



93
# File 'lib/ffi/clang/source_range.rb', line 93

attr_reader :range

Class Method Details

.from_source_range_list(range_list) ⇒ Object

Build Ruby source ranges from a libclang source range list. The list is always disposed before returning.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ffi/clang/source_range.rb', line 27

def self.from_source_range_list(range_list)
	return [] if range_list.nil?
	
	range_list = Lib::CXSourceRangeList.new(range_list) if range_list.is_a?(FFI::Pointer)
	return [] if range_list.pointer.null?
	
	begin
		range_pointer = range_list[:ranges]
		
		range_list[:count].times.map do |i|
			range = Lib::CXSourceRange.new(range_pointer + (i * Lib::CXSourceRange.size))
			SourceRange.new(Lib.get_range(Lib.get_range_start(range), Lib.get_range_end(range)))
		end
	ensure
		Lib.dispose_source_range_list(range_list)
	end
end

.null_rangeObject

Get a null source range.



19
20
21
# File 'lib/ffi/clang/source_range.rb', line 19

def self.null_range
	SourceRange.new Lib.get_null_range
end

Instance Method Details

#==(other) ⇒ Object

Check if this range equals another range.



98
99
100
# File 'lib/ffi/clang/source_range.rb', line 98

def ==(other)
	Lib.equal_range(@range, other.range) != 0
end

#bytesizeObject

Get the size in bytes of the source range.



70
71
72
# File 'lib/ffi/clang/source_range.rb', line 70

def bytesize
	self.end.offset - self.start.offset
end

#endObject

Get the end location of this range.



64
65
66
# File 'lib/ffi/clang/source_range.rb', line 64

def end
	@end ||= ExpansionLocation.new(Lib.get_range_end @range)
end

#null?Boolean

Check if this range is null.

Returns:

  • (Boolean)


88
89
90
# File 'lib/ffi/clang/source_range.rb', line 88

def null?
	Lib.range_is_null(@range) != 0
end

#startObject

Get the start location of this range.



58
59
60
# File 'lib/ffi/clang/source_range.rb', line 58

def start
	@start ||= ExpansionLocation.new(Lib.get_range_start @range)
end

#textObject

Read the text from the source file for this range.



76
77
78
79
80
81
82
83
84
# File 'lib/ffi/clang/source_range.rb', line 76

def text
	file_path = self.start.file
	return nil if file_path.nil?
	
	::File.open(file_path, "rb") do |file|
		file.seek(self.start.offset)
		return file.read(self.bytesize)
	end
end