ffi-clang icon indicating copy to clipboard operation
ffi-clang copied to clipboard

Better examples

Open apfeltee opened this issue 5 years ago • 1 comments

The current solitary example shows well how to extract data/tokens, but it's very spartan, too spartan, to be honest.

Is there a chance to add some more extensive examples? Perhaps one showing AST rewriting (assuming that's possible via the C-api)?

apfeltee avatar Sep 20 '20 08:09 apfeltee

On that note, I'd contribute the following, which, given sourcefile(s), prints out prototypes of functions therein:

# this snippet is public domain
class MkProto
  def initialize
    @index = FFI::Clang::Index.new
    @seenkinds = []
  end

  def do_file(file)
    tu = @index.parse_translation_unit(file)
    tu.cursor.select{|d| d.kind == :cursor_function }.each do |decl|
      proto = decl.display_name
      rtyp = decl.result_type.spelling
        fullprot = sprintf("%s %s;", rtyp, proto)
        yield fullprot
    end
  end
end

begin
  if ARGV.empty? then
    $stderr.printf("usage: %s <file> [<another-file>...]\n", $0)
    exit(1)
  else
    mkp = MkProto.new
    ARGV.each do |file|
      $stdout.printf("/* %s */\n", file)
      mkp.do_file(file) do |prot|
        $stdout.printf("%s\n", prot)
      end
    end
  end
end

The reason I'm not making this a pull request: I have no idea if this is how it's supposed to be done. Hence, better examples would be great.

apfeltee avatar Sep 20 '20 08:09 apfeltee