Skip to content

Commit

Permalink
Update bitmask options based on enums to always be an array of symbol…
Browse files Browse the repository at this point in the history
…s. (#69)
  • Loading branch information
cfis authored May 3, 2023
1 parent cda6eb6 commit c4c7df9
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/ffi/clang/code_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module FFI
module Clang
class CodeCompletion
def self.default_code_completion_options
Lib.opts_from Lib::CodeCompleteFlags, Lib.default_code_completion_options
Lib.opts_from(Lib::CodeCompleteFlags, Lib.default_code_completion_options)
end

class Results < FFI::AutoPointer
Expand Down
4 changes: 2 additions & 2 deletions lib/ffi/clang/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module FFI
module Clang
class Diagnostic < AutoPointer
def self.default_display_opts
Lib.opts_from Lib::DiagnosticDisplayOptions, Lib.default_diagnostic_display_options
Lib.opts_from(Lib::DiagnosticDisplayOptions, Lib.default_diagnostic_display_options)
end

def initialize(translation_unit, pointer)
Expand Down Expand Up @@ -96,7 +96,7 @@ def display_opts(opts)
if opts.empty?
Lib.default_diagnostic_display_options
else
Lib.bitmask_from Lib::DiagnosticDisplayOptions, opts
Lib.bitmask_from(Lib::DiagnosticDisplayOptions, opts)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ffi/clang/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.release(pointer)
Lib.dispose_index(pointer)
end

def parse_translation_unit(source_file, command_line_args = nil, unsaved = [], opts = {})
def parse_translation_unit(source_file, command_line_args = nil, unsaved = [], opts = [])
command_line_args = Array(command_line_args)
unsaved_files = UnsavedFile.unsaved_pointer_from(unsaved)

Expand Down Expand Up @@ -53,7 +53,7 @@ def args_pointer_from(command_line_args)
end

def options_bitmask_from(opts)
Lib.bitmask_from Lib::TranslationUnitFlags, opts
Lib.bitmask_from(Lib::TranslationUnitFlags, opts)
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/ffi/clang/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ module Lib
def self.bitmask_from(enum, opts)
bitmask = 0

opts.each do |key, value|
if int = enum[key]
opts.each do |symbol|
if int = enum[symbol]
bitmask |= int
else
raise Error, "unknown option: #{key.inspect}, expected one of #{enum.symbols}"
raise Error, "unknown option: #{symbol}, expected one of #{enum.symbols}"
end
end

Expand All @@ -82,19 +82,19 @@ def self.bitmask_from(enum, opts)

def self.opts_from(enum, bitmask)
bit = 1
opts = {}
result = []
while bitmask != 0
if bitmask & 1
if sym = enum[bit]
opts[sym] = true
if symbol = enum[bit]
result << symbol
else
raise Error, "unknown values: #{bit}, expected one of #{enum.symbols}"
raise(Error, "unknown values: #{bit}, expected one of #{enum.symbols}")
end
end
bitmask >>= 1
bit <<= 1
end
opts
result
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/ffi/clang/code_completion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
describe "self.default_code_completion_options" do
let(:options) { FFI::Clang::CodeCompletion.default_code_completion_options }
it "returns a default set of code-completion options" do
expect(options).to be_kind_of(Hash)
options.keys.each { |key|
expect(FFI::Clang::Lib::CodeCompleteFlags.symbols).to include(key)
expect(options).to be_kind_of(Array)
options.each {|symbol|
expect(FFI::Clang::Lib::CodeCompleteFlags.symbols).to include(symbol)
}
end
end
Expand Down Expand Up @@ -58,9 +58,9 @@
end

it "#contexts" do
expect(results.contexts).to be_kind_of(Hash)
results.contexts.keys.each { |key|
expect(FFI::Clang::Lib::CompletionContext.symbols).to include(key)
expect(results.contexts).to be_kind_of(Array)
results.contexts.each {|symbol|
expect(FFI::Clang::Lib::CompletionContext.symbols).to include(symbol)
}
end

Expand Down
2 changes: 1 addition & 1 deletion spec/ffi/clang/cursor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
let(:cursor) { Index.new.parse_translation_unit(fixture_path("list.c")).cursor }
let(:cursor_cxx) { Index.new.parse_translation_unit(fixture_path("test.cxx")).cursor }
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: true}).cursor }
let(:cursor_pp) { Index.new.parse_translation_unit(fixture_path("docs.c"),[],[],[:detailed_preprocessing_record]).cursor }

it "can be obtained from a translation unit" do
expect(cursor).to be_kind_of(Cursor)
Expand Down
8 changes: 4 additions & 4 deletions spec/ffi/clang/diagnostic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end

it "returns a string representation according to the given opts" do
expect(diagnostic.format(:source_location => true)).to include("list.c:5")
expect(diagnostic.format([:source_location])).to include("list.c:5")
end

it "returns the text of the diagnostic" do
Expand All @@ -43,9 +43,9 @@

context "#self.default_display_opts" do
it "returns the set of display options" do
expect(FFI::Clang::Diagnostic.default_display_opts).to be_kind_of(Hash)
expect(FFI::Clang::Diagnostic.default_display_opts.keys.map(&:class).uniq).to eq([Symbol])
expect(FFI::Clang::Diagnostic.default_display_opts.values.uniq).to eq([true])
expect(FFI::Clang::Diagnostic.default_display_opts).to be_kind_of(Array)
expect(FFI::Clang::Diagnostic.default_display_opts.map(&:class).uniq).to eq([Symbol])
expect(FFI::Clang::Diagnostic.default_display_opts.uniq).to eq([:source_location, :column, :source_ranges, :option])
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/ffi/clang/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
expect{index.parse_translation_unit(fixture_path("a.c"), [], [], [])}.not_to raise_error
end

it 'can handle translation options with random values' do
expect{index.parse_translation_unit(fixture_path("a.c"), [], [], {:incomplete => 654, :single_file_parse => 8, :cache_completion_results => 93})}.not_to raise_error
it 'throws error on options with random values' do
expect{index.parse_translation_unit(fixture_path("a.c"), [], [], [:not_valid])}.to raise_error
end

it "raises error when one of the translation options is invalid" do
Expand Down
18 changes: 9 additions & 9 deletions spec/ffi/clang/translation_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@
describe "#self.default_editing_translation_unit_options" do
let (:opts) { FFI::Clang::TranslationUnit.default_editing_translation_unit_options }
it "returns hash with symbols of TranslationUnitFlags" do
expect(opts).to be_kind_of(Hash)
opts.keys.each { |key|
expect(FFI::Clang::Lib::TranslationUnitFlags.symbols).to include(key)
expect(opts).to be_kind_of(Array)
opts.each {|symbol|
expect(FFI::Clang::Lib::TranslationUnitFlags.symbols).to include(symbol)
}
end
end

describe "#default_save_options" do
let (:opts) { translation_unit.default_save_options }
it "returns hash with symbols of SaveTranslationUnitFlags" do
expect(opts).to be_kind_of(Hash)
opts.keys.each { |key|
expect(FFI::Clang::Lib::SaveTranslationUnitFlags.symbols).to include(key)
expect(opts).to be_kind_of(Array)
opts.each {|symbol|
expect(FFI::Clang::Lib::SaveTranslationUnitFlags.symbols).to include(symbol)
}
end
end
Expand All @@ -142,9 +142,9 @@
describe "#default_reparse_options" do
let (:opts) { translation_unit.default_reparse_options }
it "returns hash with symbols of ReparseFlags" do
expect(opts).to be_kind_of(Hash)
opts.keys.each { |key|
expect(FFI::Clang::Lib::ReparseFlags.symbols).to include(key)
expect(opts).to be_kind_of(Array)
opts.each {|symbol|
expect(FFI::Clang::Lib::ReparseFlags.symbols).to include(symbol)
}
end
end
Expand Down

0 comments on commit c4c7df9

Please sign in to comment.