class File
Represents a file in a translation unit.
Definitions
attr_reader :translation_unit
Signature
-
attribute
TranslationUnit The translation unit this file belongs to.
def initialize(pointer, translation_unit)
Initialize a file with a pointer and translation unit.
Signature
-
parameter
pointerFFI::Pointer The file pointer.
-
parameter
translation_unitTranslationUnit The parent translation unit.
Implementation
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
def to_s
Get the file name as a string.
Signature
-
returns
String The file name.
Implementation
def to_s
name
end
def name
Get the file name.
Signature
-
returns
String The file name.
Implementation
def name
Lib.extract_string Lib.get_file_name(self)
end
def contents
Get the loaded contents of this file from libclang.
Signature
-
returns
String | Nil The file contents, or
nilif the file is not loaded.
Implementation
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
def time
Get the file modification time.
Signature
-
returns
Time The file modification time.
Implementation
def time
Time.at(Lib.get_file_time(self))
end
def include_guarded?
Check if the file has include guards.
Signature
-
returns
Boolean True if the file is include guarded.
Implementation
def include_guarded?
Lib.is_file_multiple_include_guarded(@translation_unit, self) != 0
end
def device
Get the device ID of the file.
Signature
-
returns
Integer The device ID.
Implementation
def device
@unique_id[:device]
end
def inode
Get the inode number of the file.
Signature
-
returns
Integer The inode number.
Implementation
def inode
@unique_id[:inode]
end
def modification
Get the modification time from the unique ID.
Signature
-
returns
Time The modification time.
Implementation
def modification
Time.at(@unique_id[:modification])
end
def skipped_ranges
Get skipped preprocessor ranges for this file.
Signature
-
returns
Array(SourceRange) The skipped preprocessor ranges in this file.
Implementation
def skipped_ranges
@translation_unit.skipped_ranges(self)
end
def real_path_name
Get the real (resolved) path name of this file.
Signature
-
returns
String The real path name.
Implementation
def real_path_name
Lib.extract_string Lib.file_try_get_real_path_name(self)
end
def ==(other)
Check if this file is equal to another file.
Signature
-
parameter
otherFile The other file to compare.
-
returns
Boolean True if the files are equal.
Implementation
def ==(other)
Lib.file_is_equal(self, other) != 0
end
def find_includes(&block)
Iterate over include directives in this file.
The translation unit must have been parsed with :detailed_preprocessing_record.
Signature
-
yields
{|cursor, range| ...} Each include directive cursor and its source range.
-
parameter
cursorCursor The include directive cursor.
-
parameter
rangeSourceRange The source range of the include directive.
-
parameter
-
returns
Enumerator If no block is given.
-
raises
Error If libclang cannot query includes for this file.
Implementation
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