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

diagnostic.rb (3331B)


      1 # -*- coding: utf-8 -*-
      2 # Copyright, 2010-2012 by Jari Bakken.
      3 # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
      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/diagnostic'
     24 require_relative 'source_range'
     25 
     26 module FFI
     27 	module Clang
     28 		class Diagnostic < AutoPointer
     29 			def self.default_display_opts
     30 				Lib.opts_from Lib::DiagnosticDisplayOptions, Lib.default_diagnostic_display_options
     31 			end
     32 
     33 			def initialize(translation_unit, pointer)
     34 				super pointer
     35 
     36 				@translation_unit = translation_unit
     37 			end
     38 
     39 			def self.release(pointer)
     40 				Lib.dispose_diagnostic(pointer)
     41 			end
     42 
     43 			def format(opts = {})
     44 				cxstring = Lib.format_diagnostic(self, display_opts(opts))
     45 				Lib.extract_string cxstring
     46 			end
     47 
     48 			def severity
     49 				Lib.get_diagnostic_severity self
     50 			end
     51 
     52 			def spelling
     53 				Lib.get_string Lib.get_diagnostic_spelling(self)
     54 			end
     55 
     56 			def location
     57 				sl = Lib.get_diagnostic_location(self)
     58 				ExpansionLocation.new sl
     59 			end
     60 
     61 			def fixits
     62 				n = Lib.get_diagnostic_num_fix_its(self)
     63 				n.times.map { |i|
     64 				ptr = MemoryPointer.new Lib::CXSourceRange
     65 					replace_text = Lib.extract_string(Lib.get_diagnostic_fix_it(self, i, ptr))
     66 					{text: replace_text, range: SourceRange.new(ptr)}
     67 				}
     68 			end
     69 
     70 			def ranges
     71 				n = Lib.get_diagnostic_num_ranges(self)
     72 				
     73 				n.times.map {|i| SourceRange.new Lib.get_diagnostic_range(self, i)}
     74 			end
     75 
     76 			def children
     77 				diagnostic_set = Lib.get_child_diagnostics(self)
     78 				num_diagnostics = Lib.get_num_diagnostics_in_set(diagnostic_set)
     79 				num_diagnostics.times.map { |i|
     80 					Diagnostic.new(@translation_unit, Lib.get_diagnostic_in_set(diagnostic_set, i))
     81 				}
     82 			end
     83 
     84 			def enable_option
     85 				Lib.extract_string Lib.get_diagnostic_option(self, nil)
     86 			end
     87 
     88 			def disable_option
     89 				ptr = MemoryPointer.new Lib::CXString
     90 				Lib.get_diagnostic_option(self, ptr)
     91 				Lib.extract_string ptr
     92 			end
     93 
     94 			def category
     95 				Lib.extract_string Lib.get_diagnostic_category_text(self)
     96 			end
     97 
     98 			def category_id
     99 				Lib.get_diagnostic_category(self)
    100 			end
    101 
    102 			def inspect
    103 				"#{self.location}: #{self.format}"
    104 			end
    105 
    106 			private
    107 
    108 			def display_opts(opts)
    109 				if opts.empty?
    110 					Lib.default_diagnostic_display_options
    111 				else
    112 					Lib.bitmask_from Lib::DiagnosticDisplayOptions, opts
    113 				end
    114 			end
    115 		end
    116 	end
    117 end