source_range.rb (2353B)
1 # -*- coding: utf-8 -*- 2 # Copyright, 2010-2012 by Jari Bakken. 3 # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com> 4 # Copyright, 2013, by Garry C. Marshall. <http://www.meaningfulname.net> 5 # Copyright, 2014, by Masahiro Sano. 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 # THE SOFTWARE. 24 25 require_relative 'lib/source_range' 26 27 module FFI 28 module Clang 29 class SourceRange 30 def self.null_range 31 SourceRange.new Lib.get_null_range 32 end 33 34 def initialize(range_or_begin_location, end_location = nil) 35 if end_location.nil? 36 @range = range_or_begin_location 37 else 38 @range = Lib.get_range(range_or_begin_location.location, end_location.location) 39 end 40 end 41 42 def start 43 @start ||= ExpansionLocation.new(Lib.get_range_start @range) 44 end 45 46 def end 47 @end ||= ExpansionLocation.new(Lib.get_range_end @range) 48 end 49 50 # The size, in bytes, of the source range. 51 def bytesize 52 self.end.offset - self.start.offset 53 end 54 55 # Read the part of the source file referred to by this source range. 56 def text 57 ::File.open(self.start.file, "r") do |file| 58 file.seek(self.start.offset) 59 return file.read(self.bytesize) 60 end 61 end 62 63 def null? 64 Lib.range_is_null(@range) != 0 65 end 66 67 attr_reader :range 68 69 def ==(other) 70 Lib.equal_range(@range, other.range) != 0 71 end 72 end 73 end 74 end