Class: FFI::Clang::File

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

Overview

Represents a file in a translation unit.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer, translation_unit) ⇒ File

Initialize a file with a pointer and translation unit.



24
25
26
27
28
29
30
31
# File 'lib/ffi/clang/file.rb', line 24

def initialize(pointer, translation_unit)
	super pointer
	@translation_unit = translation_unit
	
	pointer = MemoryPointer.new(Lib::CXFileUniqueID)
	Lib.get_file_unique_id(self, pointer)
	@unique_id = Lib::CXFileUniqueID.new(pointer)
end

Instance Attribute Details

#The translation unit this file belongs to.(translationunitthisfilebelongsto.) ⇒ Object (readonly)



19
# File 'lib/ffi/clang/file.rb', line 19

attr_reader :translation_unit

#translation_unitObject (readonly)

Returns the value of attribute translation_unit.



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

def translation_unit
  @translation_unit
end

Instance Method Details

#==(other) ⇒ Object

Check if this file is equal to another file.



100
101
102
# File 'lib/ffi/clang/file.rb', line 100

def ==(other)
	Lib.file_is_equal(self, other) != 0
end

#contentsObject

Get the loaded contents of this file from libclang.



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

def contents
	size_pointer = MemoryPointer.new(:size_t)
	contents_pointer = Lib.get_file_contents(@translation_unit, self, size_pointer)
	return nil if contents_pointer.null?
	
	contents_pointer.read_string_length(size_pointer.read(:size_t))
end

#deviceObject

Get the device ID of the file.



69
70
71
# File 'lib/ffi/clang/file.rb', line 69

def device
	@unique_id[:device]
end

#find_includes(&block) ⇒ Object

Iterate over include directives in this file. The translation unit must have been parsed with ‘:detailed_preprocessing_record`.

Raises:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ffi/clang/file.rb', line 111

def find_includes(&block)
	return to_enum(__method__) unless block_given?
	
	visit_adapter = Proc.new do |unused, cxcursor, cxsource_range|
		cursor = Cursor.new(cxcursor, @translation_unit)
		result = block.call(cursor, SourceRange.new(cxsource_range))
		result == :break ? :break : :continue
	end
	
	visitor = FFI::Clang::Lib::CXCursorAndRangeVisitor.new
	visitor[:visit] = visit_adapter
	
	result = Lib.find_includes_in_file(@translation_unit, self, visitor)
	raise Error, "error finding includes in file: #{name.inspect}" if result == :invalid
	
	self
end

#include_guarded?Boolean

Check if the file has include guards.

Returns:

  • (Boolean)


63
64
65
# File 'lib/ffi/clang/file.rb', line 63

def include_guarded?
	Lib.is_file_multiple_include_guarded(@translation_unit, self) != 0
end

#inodeObject

Get the inode number of the file.



75
76
77
# File 'lib/ffi/clang/file.rb', line 75

def inode
	@unique_id[:inode]
end

#modificationObject

Get the modification time from the unique ID.



81
82
83
# File 'lib/ffi/clang/file.rb', line 81

def modification
	Time.at(@unique_id[:modification])
end

#nameObject

Get the file name.



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

def name
	Lib.extract_string Lib.get_file_name(self)
end

#real_path_nameObject

Get the real (resolved) path name of this file.



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

def real_path_name
	Lib.extract_string Lib.file_try_get_real_path_name(self)
end

#skipped_rangesObject

Get skipped preprocessor ranges for this file.



87
88
89
# File 'lib/ffi/clang/file.rb', line 87

def skipped_ranges
	@translation_unit.skipped_ranges(self)
end

#timeObject

Get the file modification time.



57
58
59
# File 'lib/ffi/clang/file.rb', line 57

def time
	Time.at(Lib.get_file_time(self))
end

#to_sObject

Get the file name as a string.



35
36
37
# File 'lib/ffi/clang/file.rb', line 35

def to_s
	name
end