Skip to content

Commit

Permalink
Expose libclang's anonymous methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
cfis committed Apr 9, 2024
1 parent 66b97b2 commit c28cefe
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/ffi/clang/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ def completion
CodeCompletion::String.new Lib.get_cursor_completion_string(@cursor)
end

def anonymous?
Lib.cursor_is_anonymous(@cursor) != 0
end

def anonymous_record_decl?
Lib.cursor_is_anonymous_record_decl(@cursor) != 0
end

def declaration?
Lib.is_declaration(kind) != 0
end
Expand Down
2 changes: 2 additions & 0 deletions lib/ffi/clang/lib/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ class CXCursorAndRangeVisitor < FFI::Struct
attach_function :find_references_in_file, :clang_findReferencesInFile, [CXCursor.by_value, :CXFile, CXCursorAndRangeVisitor.by_value], :result

attach_function :get_cursor_type, :clang_getCursorType, [CXCursor.by_value], CXType.by_value
attach_function :cursor_is_anonymous, :clang_Cursor_isAnonymous, [CXCursor.by_value], :uint
attach_function :cursor_is_anonymous_record_decl, :clang_Cursor_isAnonymousRecordDecl, [CXCursor.by_value], :uint
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_decl_integer_type, :clang_getEnumDeclIntegerType, [CXCursor.by_value], CXType.by_value
Expand Down
10 changes: 10 additions & 0 deletions spec/ffi/clang/cursor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
let(:cursor_canon) { Index.new.parse_translation_unit(fixture_path("canonical.c")).cursor }
let(:cursor_pp) { Index.new.parse_translation_unit(fixture_path("docs.c"),[],[],[:detailed_preprocessing_record]).cursor }
let(:cursor_forward) { Index.new.parse_translation_unit(fixture_path("forward.h")).cursor }
let(:cursor_anonymous) { Index.new.parse_translation_unit(fixture_path("anonymous.h")).cursor }

it "can be obtained from a translation unit" do
expect(cursor).to be_kind_of(Cursor)
Expand Down Expand Up @@ -822,4 +823,13 @@
end
end
end

describe '#anonymous' do
let(:struct) { find_matching(cursor_anonymous) { |child, parent|
child.kind == :cursor_struct } }

it "is an an anonymous structure" do
expect(struct.anonymous?).to eq(true)
end
end
end
16 changes: 16 additions & 0 deletions spec/ffi/clang/fixtures/anonymous.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef ANONYMOUS_H
#define ANONYMOUS_H

// This variable has an anonymous structure type
struct
{
int field1;
} someVariable;

struct A
{
struct
{
int field2;
} anonymousField;
}

0 comments on commit c28cefe

Please sign in to comment.