Skip to content

Instantly share code, notes, and snippets.

@cat-in-136
Last active July 16, 2016 06:57
Show Gist options
  • Save cat-in-136/e8f1fd224d391873119f900c16842b0b to your computer and use it in GitHub Desktop.
Save cat-in-136/e8f1fd224d391873119f900c16842b0b to your computer and use it in GitHub Desktop.
.bundle
vendor/bundle
*~
*.swp

LLVM-ClangのASTを解析するまっとうな方法について を ruby で扱った場合どうなるか検討資料。

  • clangana.rb : 「AST を解析する方法」
  • clanganaxml.rb : 「ちょっとした XML を吐こうとすると」

使用方法

bundle install --path=vendor/bundle
bundle exec ruby clanganaxml.rb source.c

注: ast への変換 (emit-ast) は不要。

#!/bin/env ruby
require "ffi/clang"
if $0 == __FILE__
index = FFI::Clang::Index.new
tu = index.parse_translation_unit(ARGV[0])
tu.cursor.visit_children do |cursor, parent, client_data|
puts cursor.kind_spelling
:recurse
end
end
#!/bin/env ruby
require "ffi/clang"
require "builder"
if $0 == __FILE__
index = FFI::Clang::Index.new
builder = Builder::XmlMarkup.new(:target => $stdout, :indent => 1)
visit_children_callback = lambda do |cursor, parent|
entity_attr = {
:usr => cursor.usr,
:kind => cursor.kind_spelling,
:src => [cursor.location.file, cursor.location.line, cursor.location.column].join(":"),
}
builder.entity(entity_attr) do
cursor.visit_children(&visit_children_callback)
end
:continue
end
builder.ast do
tu = index.parse_translation_unit(ARGV[0])
tu.cursor.visit_children(&visit_children_callback)
end
end
source "https://rubygems.org"
gem "ffi-clang"
gem "builder"
GEM
remote: https://rubygems.org/
specs:
builder (3.2.2)
ffi (1.9.14)
ffi-clang (0.3.0)
ffi
PLATFORMS
ruby
DEPENDENCIES
builder
ffi-clang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment