ffi-clang

FORK: Ruby FFI bindings for my custom patched clang 8.0.
git clone https://git.neptards.moe/neptards/ffi-clang.git
Log | Files | Refs | README

index.rb (2797B)


      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/index'
     24 
     25 module FFI
     26 	module Clang
     27 		class Index < AutoPointer
     28 			def initialize(exclude_declarations = true, display_diagnostics = false)
     29 				super Lib.create_index(exclude_declarations ? 1 : 0, display_diagnostics ? 1 : 0)
     30 			end
     31 
     32 			def self.release(pointer)
     33 				Lib.dispose_index(pointer)
     34 			end
     35 
     36 			def parse_translation_unit(source_file, command_line_args = nil, unsaved = [], opts = {})
     37 				command_line_args = Array(command_line_args)
     38 				unsaved_files = UnsavedFile.unsaved_pointer_from(unsaved)
     39 
     40 				translation_unit_pointer = Lib.parse_translation_unit(self, source_file, args_pointer_from(command_line_args), command_line_args.size, unsaved_files, unsaved.length, options_bitmask_from(opts))
     41 
     42 				raise Error, "error parsing #{source_file.inspect}" if translation_unit_pointer.null?
     43 
     44 				TranslationUnit.new translation_unit_pointer, self
     45 			end
     46 
     47 			def create_translation_unit(ast_filename)
     48 				translation_unit_pointer = Lib.create_translation_unit(self, ast_filename)
     49 				raise Error, "error parsing #{ast_filename.inspect}" if translation_unit_pointer.null?
     50 				TranslationUnit.new translation_unit_pointer, self
     51 			end
     52 
     53 			private
     54 
     55 			def args_pointer_from(command_line_args)
     56 				args_pointer = MemoryPointer.new(:pointer, command_line_args.length)
     57 
     58 				strings = command_line_args.map do |arg|
     59 					MemoryPointer.from_string(arg.to_s)
     60 				end
     61 
     62 				args_pointer.put_array_of_pointer(0, strings) unless strings.empty?
     63 				args_pointer
     64 			end
     65 
     66 			def options_bitmask_from(opts)
     67 				Lib.bitmask_from Lib::TranslationUnitFlags, opts
     68 			end
     69 		end
     70 	end
     71 end