source_location.rb (4178B)
1 # Copyright, 2010-2012 by Jari Bakken. 2 # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com> 3 # Copyright, 2014, by Masahiro Sano. 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 23 require_relative 'lib/source_location' 24 require_relative 'lib/file' 25 26 module FFI 27 module Clang 28 class SourceLocation 29 def self.null_location 30 ExpansionLocation.new Lib.get_null_location 31 end 32 33 attr_reader :location 34 def initialize(location) 35 @location = location 36 end 37 38 def in_system_header? 39 Lib.location_in_system_header(@location) != 0 40 end 41 42 def from_main_file? 43 Lib.location_is_from_main_file(@location) != 0 44 end 45 46 def expansion_location 47 ExpansionLocation.new(@location) 48 end 49 50 def presumed_location 51 PresumedLocation.new(@location) 52 end 53 54 def spelling_location 55 SpellingLocation.new(@location) 56 end 57 58 def file_location 59 FileLocation.new(@location) 60 end 61 62 def null? 63 Lib.equal_locations(@location, Lib.get_null_location) != 0 64 end 65 66 def ==(other) 67 Lib.equal_locations(@location, other.location) != 0 68 end 69 end 70 71 class ExpansionLocation < SourceLocation 72 attr_reader :file, :line, :column, :offset 73 74 def initialize(location) 75 super(location) 76 77 cxfile = MemoryPointer.new :pointer 78 line = MemoryPointer.new :uint 79 column = MemoryPointer.new :uint 80 offset = MemoryPointer.new :uint 81 82 Lib::get_expansion_location(@location, cxfile, line, column, offset) 83 84 @file = Lib.extract_string Lib.get_file_name(cxfile.read_pointer) 85 @line = line.get_uint(0) 86 @column = column.get_uint(0) 87 @offset = offset.get_uint(0) 88 end 89 end 90 91 class PresumedLocation < SourceLocation 92 attr_reader :filename, :line, :column, :offset 93 94 def initialize(location) 95 super(location) 96 97 cxstring = MemoryPointer.new Lib::CXString 98 line = MemoryPointer.new :uint 99 column = MemoryPointer.new :uint 100 101 Lib::get_presumed_location(@location, cxstring, line, column) 102 103 @filename = Lib.extract_string cxstring 104 @line = line.get_uint(0) 105 @column = column.get_uint(0) 106 end 107 end 108 109 class SpellingLocation < SourceLocation 110 attr_reader :file, :line, :column, :offset 111 112 def initialize(location) 113 super(location) 114 115 cxfile = MemoryPointer.new :pointer 116 line = MemoryPointer.new :uint 117 column = MemoryPointer.new :uint 118 offset = MemoryPointer.new :uint 119 120 Lib::get_spelling_location(@location, cxfile, line, column, offset) 121 122 @file = Lib.extract_string Lib.get_file_name(cxfile.read_pointer) 123 @line = line.get_uint(0) 124 @column = column.get_uint(0) 125 @offset = offset.get_uint(0) 126 end 127 end 128 129 class FileLocation < SourceLocation 130 attr_reader :file, :line, :column, :offset 131 132 def initialize(location) 133 super(location) 134 135 cxfile = MemoryPointer.new :pointer 136 line = MemoryPointer.new :uint 137 column = MemoryPointer.new :uint 138 offset = MemoryPointer.new :uint 139 140 Lib::get_file_location(@location, cxfile, line, column, offset) 141 142 @file = Lib.extract_string Lib.get_file_name(cxfile.read_pointer) 143 @line = line.get_uint(0) 144 @column = column.get_uint(0) 145 @offset = offset.get_uint(0) 146 end 147 end 148 end 149 end