123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- require "clangc/clangc"
- module Clangc
- ##
- # :call-seq:
- # Clangc.visit_children(Hash)
- #
- # This is a convenient method that call Clangc.visit_children_with_proc or
- # Clangc.visit_children_with_block.
- # the Hash arguments can accept two keys:
- # * cursor
- # * visitor
- #
- # Clangc.visit_children(cursor: acursor, visitor: aproc)
- #
- # Clangc.visit_children(cursor: acursor) do |cursor, parent|
- # #do your stuf
- # return Clangc::ChildVisitResult::Recurse
- # #return Clangc::ChildVisitResult::Break
- # end
- def self.visit_children(args)
- cursor = args[:cursor]
- callback = args[:visitor] || nil
- if(callback)
- visit_children_with_proc(cursor, callback)
- else
- visit_children_with_block(cursor) do |cursor, parent|
- yield(cursor, parent)
- end
- end
- end
- class TranslationUnit
- ##
- # :call-seq:
- # Clangc::TranslationUnit#diagnostics => Array
- #
- # Returns an array of Clangc::Diagnostic for the current Clangc::TranslationUnit.
- # The array is empty if no Clangc::Diagnostic can be found.
- def diagnostics
- ds = []
- for i in 0..(diagnostics_num - 1) do
- ds << diagnostic(i)
- end
- ds
- end
- end
- class Diagnostic
- ##
- # :call-seq:
- # Clangc::Diagnostic#source_ranges => Array
- #
- # Returns an array of Clangc::SourceRange for the current Clangc::Diagnostic.
- # The array is empty if there is no Clangc::SourceRange
- def source_ranges
- num = num_ranges
- sr = []
- return sr if num == 0
- for i in 0..(num - 1) do
- sr << source_range(i)
- end
- sr
- end
- end
- class Type
- ##
- # :call-seq:
- # Clangc::Type#arg_types -> Array
- #
- # Return an array that contains all the
- # types for the argument of the function that
- # is related to the current type.
- # If the current type is not a function, it returns
- # an empty array.
- def arg_types
- num = num_arg_types
- return [] if num == -1
- types = []
- for i in 0..(num - 1) do
- types << arg_type(i)
- end
- types
- end
- ##
- # :call-seq:
- # Clangc::Type#template_arguments_as_type -> Array
- #
- # Return an array that contains all the
- # types for the arguments of the current Class Type template that
- # is related to the current cursor.
- # If the Cursor is not a Class Declaration, it returns
- # an empty array.
- def template_arguments_as_type
- num = num_template_arguments
- types = []
- return types if num == -1
- for i in 0..(num - 1) do
- types << template_argument_as_type(i)
- end
- types
- end
- end
- class Cursor
- ##
- # :call-seq:
- # Clangc::Cursor#arguments -> Array
- #
- # Return an array that contains all the
- # cursors for the arguments of the function that
- # is related to the current cursor.
- # If the current cursor is not a function, it returns
- # an empty array.
- def arguments
- num = num_arguments
- cursors = []
- return cursors if num == -1
- for i in 0..(num - 1) do
- cursors << argument(i)
- end
- cursors
- end
- ##
- # :call-seq:
- # Clangc::Cursor#overloaded_decls -> Array
- #
- # Return an array that contains all the
- # cursors for the overloaded declarations that
- # are related to the current cursor.
- # If the current cursor is not an overloaded declaration, it returns
- # an empty array.
- def overloaded_decls
- num = num_overloaded_decls
- cursors = []
- return cursors if num == 0
- for i in 0..(num - 1) do
- cursors << overloaded_decl(i)
- end
- cursors
- end
- ##
- # :call-seq:
- # Clangc::Cursor#template_arguments_kinds -> Array
- #
- # Return an array that contains all the
- # kinds for the arguments of the function template that
- # is related to the current cursor.
- # If the current cursor is not a function declaration, it returns
- # an empty array.
- def template_arguments_kinds
- num = num_template_arguments
- kinds = []
- return kinds if num == -1
- for i in 0..(num - 1) do
- kinds << template_argument_kind(i)
- end
- kinds
- end
- ##
- # :call-seq:
- # Clangc::Cursor#template_arguments_types -> Array
- #
- # Return an array that contains all the
- # types for the arguments of the function template that
- # is related to the current cursor.
- # If the current cursor is not a function declaration, it returns
- # an empty array.
- def template_arguments_types
- num = num_template_arguments
- types = []
- return types if num == -1
- for i in 0..(num - 1) do
- types << template_argument_type(i)
- end
- types
- end
- ##
- # :call-seq:
- # Clangc::Cursor#template_arguments_values -> Array
- #
- # Return an array that contains all the
- # values for the arguments of the function template that
- # is related to the current cursor.
- # If the current cursor is not a function declaration, it returns
- # an empty array.
- def template_arguments_values
- num = num_template_arguments
- values = []
- return values if num == -1
- for i in 0..(num - 1) do
- values << template_argument_value(i)
- end
- values
- end
- ##
- # :call-seq:
- # Clangc::Cursor#template_arguments_unsigned_values -> Array
- #
- # Return an array that contains all the
- # values for the arguments of the function template that
- # is related to the current cursor.
- # If the current cursor is not a function declaration, it returns
- # an empty array.
- def template_arguments_unsigned_values
- num = num_template_arguments
- values = []
- return values if num == -1
- for i in 0..(num - 1) do
- values << template_argument_unsigned_value(i)
- end
- values
- end
- end
- class Module
- ##
- # :call-seq:
- # Clangc::Module#top_level_headers(Clangc::TranslationUnit) -> Array
- #
- # Return an array that contains all the
- # Clangc::File corresponding to the related
- # toplevel headers.
- # If the current cursor is not a module, it returns
- # an empty array.
- def top_level_headers(tu)
- num = num_top_level_headers(tu)
- headers = []
- return headers if num < 1
- for i in 0..(num - 1) do
- headers << top_level_header(tu, i)
- end
- headers
- end
- end
- class CompletionString
- ##
- # :call-seq:
- # Clangc::CompletionString#chunk_kinds -> Array
- #
- # Return an array that contains all the
- # kinds of the chunk completions for a
- # completion string.
- def chunk_kinds
- num = num_chunks
- return [] if num == -1
- kinds = []
- for i in 0..(num - 1) do
- kinds << chunk_kind(i)
- end
- kinds
- end
- ##
- # :call-seq:
- # Clangc::CompletionString#chunk_texts -> Array
- #
- # Return an array that contains all the
- # texts of the chunk completions for a
- # completion string.
- def chunk_texts
- num = num_chunks
- return [] if num == -1
- texts = []
- for i in 0..(num - 1) do
- texts << chunk_text(i)
- end
- texts
- end
- ##
- # :call-seq:
- # Clangc::CompletionString#num_annotations -> Array
- #
- # Return an array that contains all the
- # annotations for a completion string.
- def annotations
- num = num_annotations
- return [] if num == -1
- annotations = []
- for i in 0..(num - 1) do
- annotations << annotation(i)
- end
- annotations
- end
- ##
- # :call-seq:
- # Clangc::CompletionString#chunk_text_completion_strings -> Array
- #
- # Return an array that contains all the
- # completion strings for a
- # completion string.
- def chunk_completion_strings
- num = num_chunks
- return [] if num == -1
- completion_strings = []
- for i in 0..(num - 1) do
- completion_strings << chunk_completion_string(i)
- end
- completion_strings
- end
- end
- class CodeCompleteResults
- ##
- # :call-seq:
- # Clangc::CodeCompleteResults#results => Array
- #
- # Returns an Array of Clangc::CompletionResult
- def results
- num = num_results
- return [] if num < 0
- res = []
- for i in 0..(num - 1) do
- res << result(i)
- end
- res
- end
- ##
- # :call-seq:
- # Clangc::CodeCompleteResults#diagnostics => Array
- #
- # Returns an Array of Clangc::Diagnostics
- def diagnostics
- num = num_diagnostics
- return [] if num < 0
- diags = []
- for i in 0..(num - 1) do
- diags << diagnostic(i)
- end
- diags
- end
- end
- class Index
- ##
- # :call-seq:
- # Clangc::Index#translation_unit(options) => Clangc::TranslationUnit
- #
- # Convenient method that easily allow to create a translation unit
- # through different ways based on the options you use:
- # :source => source file
- # :args => command line arguments
- # :error => true or false or nil
- # :ast => String an ast file
- alias_method :create_translation_unit_raw, :create_translation_unit
- def create_translation_unit(options)
- source = options[:source] || ""
- args = options[:args] || []
- error = options[:error] || nil
- ast = options[:ast] || nil
- if ast
- error ? create_translation_unit2(ast) : create_translation_unit_raw(ast)
- else
- create_translation_unit_from_source_file(source, args)
- end
- end
- ##
- # :call-seq:
- # Clangc::Index#parse_translation_unit(options) => Clangc::TranslationUnit
- #
- # Convenient method that easily allow to parse a file to a translation
- # unit through different ways based on the options you use:
- # :source => source file
- # :args => command line arguments
- # :error => true or false or nil
- # :flags => bitwise OR of the TranslationUnit_Flags constants
- # :argv => true or false in order to use argv form
- alias_method :parse_translation_unit_raw, :parse_translation_unit
- def parse_translation_unit(options)
- source = options[:source] || ""
- args = options[:args] || []
- error = options[:error] || nil
- flags = options[:flags] || Clangc::TranslationUnit_Flags::NONE
- argv = options[:argv] || false
- if error && argv
- parse_translation_unit2_full_argv(source, args, flags)
- elsif error
- parse_translation_unit2(source, args, flags)
- else
- parse_translation_unit_raw(source, args, flags)
- end
- end
- end
- end
|