Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor Improvements, Type improvements, Printing Support #72

Merged
merged 15 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ jobs:
# - macos

ruby:
- "2.7"
- "3.0"
- "3.1"
- "3.2"

- "3.3"

clang:
- "15.0"

experimental: [false]

include:
Expand All @@ -38,14 +37,14 @@ jobs:
experimental: true

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{matrix.ruby}}
bundler-cache: true

- name: Install LLVM and Clang
uses: KyleMayes/install-llvm-action@v1
uses: KyleMayes/install-llvm-action@v2
with:
version: ${{matrix.clang}}

Expand Down
37 changes: 32 additions & 5 deletions lib/ffi/clang/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require_relative 'lib/cursor'
require_relative 'lib/code_completion'

require_relative 'printing_policy'
require_relative 'source_location'
require_relative 'source_range'
require_relative 'comment'
Expand Down Expand Up @@ -120,10 +121,21 @@ def display_name
Lib.extract_string Lib.get_cursor_display_name(@cursor)
end

def qualified_name
if self.kind != :cursor_translation_unit
result = self.semantic_parent.qualified_name
result ? "#{result}::#{self.spelling}" : self.spelling
end
end

def spelling
Lib.extract_string Lib.get_cursor_spelling(@cursor)
end

def printing_policy
PrintingPolicy.new(cursor)
end

def usr
Lib.extract_string Lib.get_cursor_usr(@cursor)
end
Expand All @@ -148,10 +160,6 @@ def underlying_type
Type.new Lib.get_typedef_decl_underlying_type(@cursor), @translation_unit
end

def typedef_type
Type.new Lib.get_typedef_decl_unerlying_type(@cursor), @translation_unit
end

def virtual_base?
Lib.is_virtual_base(@cursor) != 0
end
Expand Down Expand Up @@ -189,7 +197,7 @@ def enum_unsigned_value
end

def enum_type
Type.new Lib.get_enum_type(@cursor), @translation_unit
Type.new Lib.get_enum_decl_integer_type(@cursor), @translation_unit
end

def specialized_template
Expand All @@ -204,6 +212,21 @@ def definition
Cursor.new Lib.get_cursor_definition(@cursor), @translation_unit
end

def opaque_declaration?
# Is this a declaration that does not have a definition in the translation unit
self.declaration? && !self.definition? && self.definition.invalid?
end

def forward_declaration?
# Is this a forward declaration for a definition contained in the same translation_unit?
# https://joshpeterson.github.io/identifying-a-forward-declaration-with-libclang
#
# Possible alternate implementations?
# self.declaration? && !self.definition? && self.definition
# !self.definition? && self.definition
self.declaration? && !self.eql?(self.definition) && !self.definition.invalid?
end

def referenced
Cursor.new Lib.get_cursor_referenced(@cursor), @translation_unit
end
Expand Down Expand Up @@ -379,6 +402,10 @@ def select
end
end

def to_s
"Cursor <#{self.kind.to_s.gsub(/^cursor_/, '')}: #{self.spelling}>"
end

def to_a
filter.collect{|child, parent| child}
end
Expand Down
8 changes: 3 additions & 5 deletions lib/ffi/clang/lib/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ module Lib
enum :cursor_kind, [
:cursor_unexposed_decl, 1,
:cursor_struct, 2,
# :cusor_struct_decl, :cursor_struct
# :cursor_struct_decl, :cursor_struct
:cursor_union, 3,
# :cusor_union_decl, :cursor_union
# :cursor_union_decl, :cursor_union
:cursor_class_decl, 4,
:cursor_enum_decl, 5,
:cursor_field_decl, 6,
Expand Down Expand Up @@ -442,7 +442,7 @@ class CXCursorAndRangeVisitor < FFI::Struct
attach_function :get_cursor_type, :clang_getCursorType, [CXCursor.by_value], CXType.by_value
attach_function :get_cursor_result_type, :clang_getCursorResultType, [CXCursor.by_value], CXType.by_value
attach_function :get_typedef_decl_underlying_type, :clang_getTypedefDeclUnderlyingType, [CXCursor.by_value], CXType.by_value
attach_function :get_enum_type, :clang_getEnumDeclIntegerType, [CXCursor.by_value], CXType.by_value
attach_function :get_enum_decl_integer_type, :clang_getEnumDeclIntegerType, [CXCursor.by_value], CXType.by_value
attach_function :get_type_declaration, :clang_getTypeDeclaration, [CXType.by_value], FFI::Clang::Lib::CXCursor.by_value

attach_function :get_cursor_referenced, :clang_getCursorReferenced, [CXCursor.by_value], CXCursor.by_value
Expand Down Expand Up @@ -471,8 +471,6 @@ class CXCursorAndRangeVisitor < FFI::Struct
attach_function :get_overridden_cursors, :clang_getOverriddenCursors, [CXCursor.by_value, :pointer, :pointer], :void
attach_function :dispose_overridden_cursors, :clang_disposeOverriddenCursors, [:pointer], :void

attach_function :get_typedef_decl_unerlying_type, :clang_getTypedefDeclUnderlyingType, [CXCursor.by_value], CXType.by_value

attach_function :get_num_args, :clang_Cursor_getNumArguments, [CXCursor.by_value], :int

attach_function :is_converting_constructor, :clang_CXXConstructor_isConvertingConstructor, [CXCursor.by_value], :uint
Expand Down
47 changes: 47 additions & 0 deletions lib/ffi/clang/lib/printing_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2014, by Masahiro Sano.
# Copyright, 2014-2022, by Samuel Williams.

module FFI
module Clang
module Lib
typedef :pointer, :CXPrintingPolicy

PrintingPolicyProperty = enum [:printing_policy_indentation,
:printing_policy_suppress_specifiers,
:printing_policy_suppress_tag_keyword,
:printing_policy_include_tag_definition,
:printing_policy_suppress_scope,
:printing_policy_suppress_unwritten_scope,
:printing_policy_suppress_initializers,
:printing_policy_constant_array_aize_as_written,
:printing_policy_anonymous_tag_locations,
:printing_policy_suppress_strong_lifetime,
:printing_policy_suppress_lifetime_qualifiers,
:printing_policy_suppress_template_args_in_cxx_constructors,
:printing_policy_bool,
:printing_policy_restrict,
:printing_policy_alignof,
:printing_policy_underscore_alignof,
:printing_policy_use_void_for_zero_params,
:printing_policy_terse_output,
:printing_policy_polish_for_declaration,
:printing_policy_half,
:printing_policy_msw_char,
:printing_policy_include_new_lines,
:printing_policy_msvc_formatting,
:printing_policy_constants_as_written,
:printing_policy_suppress_implicit_base,
:printing_policy_fully_qualified_name,
:printing_policy_last_property]

attach_function :printing_policy_get_property, :clang_PrintingPolicy_getProperty, [:CXPrintingPolicy, PrintingPolicyProperty], :uint
attach_function :printing_policy_set_property, :clang_PrintingPolicy_setProperty, [:CXPrintingPolicy, PrintingPolicyProperty, :uint], :void
attach_function :get_printing_policy, :clang_getCursorPrintingPolicy, [CXCursor.by_value], :CXPrintingPolicy
attach_function :dispose_printing_policy, :clang_PrintingPolicy_dispose, [:CXPrintingPolicy], :void
attach_function :pretty_print, :clang_getCursorPrettyPrinted, [CXCursor.by_value, :CXPrintingPolicy], CXString.by_value
end
end
end
65 changes: 65 additions & 0 deletions lib/ffi/clang/lib/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,71 @@ module Lib
:type_variable_array, 115,
:type_dependent_sized_array, 116,
:type_member_pointer, 117,
:type_auto, 118,
:type_elaborated, 119,
:type_pipe, 120,
:type_ocl_image_1d_ro, 121,
:type_ocl_image_1d_array_ro, 122,
:type_ocl_image_1d_buffer_ro, 123,
:type_ocl_image_2d_ro, 124,
:type_ocl_image_2d_array_ro, 125,
:type_ocl_image_2d_depth_ro, 126,
:type_ocl_image_2d_array_depth_ro, 127,
:type_ocl_image_2d_msaa_ro, 128,
:type_ocl_image_2d_array_msaa_ro, 129,
:type_ocl_image_2d_msaa_depth_ro, 130,
:type_ocl_image_2d_array_msaa_depth_ro, 131,
:type_ocl_image_3d_ro, 132,
:type_ocl_image_1d_wo, 133,
:type_ocl_image_1d_array_wo, 134,
:type_ocl_image_1d_buffer_wo, 135,
:type_ocl_image_2d_wo, 136,
:type_ocl_image_2d_array_wo, 137,
:type_ocl_image_2d_depth_wo, 138,
:type_ocl_image_2d_arraydepth_wo, 139,
:type_ocl_image_2d_msaa_wo, 140,
:type_ocl_image_2d_array_msaa_wo, 141,
:type_ocl_image_2d_msaa_depth_wo, 142,
:type_ocl_image_2d_array_msaa_depth_wo, 143,
:type_ocl_image_3d_wo, 144,
:type_ocl_image_1d_rw, 145,
:type_ocl_image_1d_array_rw, 146,
:type_ocl_image_1d_buffer_rw, 147,
:type_ocl_image_2d_rw, 148,
:type_ocl_image_2d_array_rw, 149,
:type_ocl_image_2d_depth_rw, 150,
:type_ocl_image_2d_arraydepth_rw, 151,
:type_ocl_image_2d_msaa_rw, 152,
:type_ocl_image_2d_array_msaa_rw, 153,
:type_ocl_image_2d_msaa_depth_rw, 154,
:type_ocl_image_2d_array_msaa_depth_rw, 155,
:type_ocl_image_3d_rw, 156,
:type_ocl_sampler, 157,
:type_ocl_event, 158,
:type_ocl_queue, 159,
:type_ocl_reserve_id, 160,
:type_objc_object, 161,
:type_objc_type_param, 162,
:type_attributed, 163,
:type_ocl_intel_subgroup_avc_mce_payload, 164,
:type_ocl_intel_subgroup_avc_ime_payload, 165,
:type_ocl_intel_subgroup_avc_ref_payload, 166,
:type_ocl_intel_subgroup_avc_sic_payload, 167,
:type_ocl_intel_subgroup_avc_mce_result, 168,
:type_ocl_intel_subgroup_avc_ime_result, 169,
:type_ocl_intel_subgroup_avc_ref_result, 170,
:type_ocl_intel_subgroup_avc_sic_result, 171,
:type_ocl_intel_subgroup_avc_ime_result_single_reference_streamout, 172,
:type_ocl_intel_subgroup_avc_ime_result_dual_reference_streamout, 173,
:type_ocl_intel_subgroup_avc_ime_single_reference_streamin, 174,
:type_ocl_intel_subgroup_avc_ime_dual_reference_streamin, 175,
:type_ocl_intel_subgroup_avc_ime_result_single_ref_streamout, 172,
:type_ocl_intel_subgroup_avc_ime_result_dual_ref_streamout, 173,
:type_ocl_intel_subgroup_avc_ime_single_ref_streamin, 174,
:type_ocl_intel_subgroup_avc_ime_dual_ref_streamin, 175,
:type_ext_vector, 176,
:type_atomic, 177,
:type_btf_tag_attributed, 178
]

enum :calling_conv, [
Expand Down
36 changes: 36 additions & 0 deletions lib/ffi/clang/printing_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2014, by Masahiro Sano.
# Copyright, 2014-2022, by Samuel Williams.

require_relative 'lib/printing_policy'

module FFI
module Clang
class PrintingPolicy < AutoPointer
def initialize(cursor)
policy = Lib.get_printing_policy(cursor)
super(policy)
@cursor = cursor
end

def self.release(pointer)
Lib.dispose_printing_policy(pointer)
end

def get_property(property)
result = Lib.printing_policy_get_property(self, property)
result == 0 ? false : true
end

def set_property(property, value)
Lib.printing_policy_set_property(self, property, value ? 1 : 0)
end

def pretty_print
Lib.extract_string Lib.pretty_print(@cursor, self)
end
end
end
end
50 changes: 44 additions & 6 deletions lib/ffi/clang/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,29 @@ def num_arg_types
Lib.get_num_arg_types(@type)
end

def pointer?
[:type_pointer, :type_block_pointer, :type_obj_c_object_pointer, :type_member_pointer].
include?(self.kind)
end

def pointee
Type.new Lib.get_pointee_type(@type), @translation_unit
if self.pointer?
Type.new Lib.get_pointee_type(@type), @translation_unit
else
nil
end
end

def canonical
Type.new Lib.get_canonical_type(@type), @translation_unit
end

def class_type
Type.new Lib.type_get_class_type(@type), @translation_unit
if self.kind == :type_member_pointer
Type.new Lib.type_get_class_type(@type), @translation_unit
else
nil
end
end

def const_qualified?
Expand All @@ -65,24 +78,49 @@ def restrict_qualified?
Lib.is_restrict_qualified_type(@type) != 0
end

def function?
[:type_function_no_proto, :type_function_proto].include?(self.kind)
end

def arg_type(i)
Type.new Lib.get_arg_type(@type, i), @translation_unit
if self.function?
Type.new Lib.get_arg_type(@type, i), @translation_unit
else
nil
end
end

def result_type
Type.new Lib.get_result_type(@type), @translation_unit
if self.function?
Type.new Lib.get_result_type(@type), @translation_unit
else
nil
end
end

def element_type
Type.new Lib.get_element_type(@type), @translation_unit
if self.array? || [:type_vector, :type_complex].include?(self.kind)
Type.new Lib.get_element_type(@type), @translation_unit
else
nil
end
end

def num_elements
Lib.get_num_elements(@type)
end

def array?
[:type_constant_array, :type_incomplete_array, :type_variable_array, :type_dependent_sized_array].
include?(self.kind)
end

def array_element_type
Type.new Lib.get_array_element_type(@type), @translation_unit
if self.array?
Type.new Lib.get_array_element_type(@type), @translation_unit
else
nil
end
end

def array_size
Expand Down
Loading