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

Better examples #51

Open
apfeltee opened this issue Sep 20, 2020 · 1 comment
Open

Better examples #51

apfeltee opened this issue Sep 20, 2020 · 1 comment

Comments

@apfeltee
Copy link

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
Copy link
Author

apfeltee commented Sep 20, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant