ffi-clang 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWQwOTY2ZDA4ZWEzMjMwZDExZDY4ZDI3MDNmM2ZlYWJkYzFmZmI3Nw==
5
+ data.tar.gz: !binary |-
6
+ NzgyMmY3OTRjZGYxM2ZkNTQwNmZjYjU0YjE2ZDAwM2IwOThmMDIxZA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MTI0NGZlMzYyMGRmYTdhOGQwZDlmYzQzNTA0M2U4NmZlMmZjYjQ2ZDEzOGRk
10
+ ODE3NjIyMzliOGQyOTBhOWZiOGFhMmVkZjA5MDUwNmYxZjUxMWI3NDJhMTky
11
+ ZTc0ODc2OTJmNjQ4NmQxMDMwYjdlMjEzM2M0MTFjYjRjNDIxM2E=
12
+ data.tar.gz: !binary |-
13
+ NDI0MDJhYThkZGRiNGQ3MjMwYWYyNjU4NzI2NmY0NzMzOTIwZGI1OGQyY2Rj
14
+ YzM2MjEzMTJmMmRjMzkwYmJlZjIxNzZhZDI2OGI2YTY0ZjdlYjkyODkwNjQ0
15
+ OWQwNDE2ZGE4ZDE4YmRjOTZjN2YzZWQ2NDdkZWM1ZjhiZDIwNmQ=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0"
4
+ install:
5
+ # libclang-3.2-dev
6
+ - sudo add-apt-repository --yes ppa:h-rayflood/llvm
7
+ - sudo apt-get -qq update
8
+ - sudo apt-get -qq install libclang-3.2-dev
9
+ - bundle install
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ffi-clang.gemspec
4
+ gemspec
@@ -0,0 +1,54 @@
1
+ # FFI::Clang
2
+
3
+ Ruby FFI bindings for libclang. WIP.
4
+
5
+ http://llvm.org/devmtg/2010-11/Gregor-libclang.pdf
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'ffi-clang'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ffi-clang
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
32
+
33
+ ## License
34
+
35
+ Copyright, 2010-2012 by Jari Bakken.
36
+ Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
37
+
38
+ Permission is hereby granted, free of charge, to any person obtaining a copy
39
+ of this software and associated documentation files (the "Software"), to deal
40
+ in the Software without restriction, including without limitation the rights
41
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42
+ copies of the Software, and to permit persons to whom the Software is
43
+ furnished to do so, subject to the following conditions:
44
+
45
+ The above copyright notice and this permission notice shall be included in
46
+ all copies or substantial portions of the Software.
47
+
48
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
53
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
54
+ THE SOFTWARE.
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
11
+ # Put spec opts in a file named .rspec in root
12
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ffi/clang/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ffi-clang"
8
+ spec.version = FFI::Clang::VERSION
9
+ spec.authors = ["Jari Bakken", "Samuel Williams"]
10
+ spec.email = ["Jari Bakken", "samuel.williams@oriontransfer.co.nz"]
11
+ spec.description = %q{Ruby FFI bindings for libclang C interface.}
12
+ spec.summary = %q{Ruby FFI bindings for libclang C interface.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "ffi"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,51 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ require 'ffi'
23
+ require "rbconfig"
24
+
25
+ module FFI::Clang
26
+ class Error < StandardError
27
+ end
28
+
29
+ def self.platform
30
+ os = RbConfig::CONFIG["host_os"]
31
+
32
+ case os
33
+ when /darwin/
34
+ :osx
35
+ when /linux/
36
+ :linux
37
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
38
+ :windows
39
+ else
40
+ os
41
+ end
42
+ end
43
+ end
44
+
45
+ require 'ffi/clang/lib'
46
+ require 'ffi/clang/index'
47
+ require 'ffi/clang/translation_unit'
48
+ require 'ffi/clang/diagnostic'
49
+ #require 'ffi/clang/source_location'
50
+ #require 'ffi/clang/source_range'
51
+
@@ -0,0 +1,86 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ require 'ffi/clang/lib/diagnostic'
23
+
24
+ module FFI
25
+ module Clang
26
+ class SourceRange
27
+ def initialize(range_struct)
28
+ end
29
+ end
30
+
31
+ class Diagnostic < AutoPointer
32
+ def initialize(translation_unit, pointer)
33
+ super pointer
34
+
35
+ @translation_unit = translation_unit
36
+ end
37
+
38
+ def self.release(pointer)
39
+ Lib.dispose_diagnostic(pointer)
40
+ end
41
+
42
+ def format(opts = {})
43
+ cxstring = Lib.format_diagnostic(self, display_opts(opts))
44
+ Lib.extract_string cxstring
45
+ end
46
+
47
+ def severity
48
+ Lib.get_diagnostic_severity self
49
+ end
50
+
51
+ def spelling
52
+ Lib.get_string Lib.get_diagnostic_spelling(self)
53
+ end
54
+
55
+ def fixits
56
+ raise NotImplementedError
57
+ # unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag);
58
+ # – CXString clang_getDiagnosticFixIt(CXDiagnostic Diag,
59
+ # unsigned FixIt,
60
+ # CXSourceRange *ReplacementRange);
61
+
62
+ end
63
+
64
+ def ranges
65
+ n = Lib.get_diagnostic_num_ranges(self)
66
+
67
+ n.times.map {|i| SourceRange.new Lib.get_diagnostic_range(self, i)}
68
+ end
69
+
70
+ private
71
+
72
+ def range_count
73
+
74
+ end
75
+
76
+ def display_opts(opts)
77
+ if opts.empty?
78
+ Lib.default_diagnostic_display_options
79
+ else
80
+ Lib.bitmask_from Lib::DiagnosticDisplayOptions, opts
81
+ end
82
+ end
83
+
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,64 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ require 'ffi/clang/lib/index'
23
+
24
+ module FFI
25
+ module Clang
26
+ class Index < AutoPointer
27
+ def initialize(exclude_declarations = true, display_diagnostics = false)
28
+ super Lib.create_index(exclude_declarations ? 1 : 0, display_diagnostics ? 1 : 0)
29
+ end
30
+
31
+ def self.release(pointer)
32
+ Lib.dispose_index_debug(pointer)
33
+ end
34
+
35
+ def parse_translation_unit(source_file, command_line_args = nil, opts = {})
36
+ command_line_args = Array(command_line_args)
37
+
38
+ translation_unit_pointer = Lib.parse_translation_unit(self, source_file, args_pointer_from(command_line_args), command_line_args.size, nil, 0, options_bitmask_from(opts))
39
+
40
+ raise Error, "error parsing #{source_file.inspect}" unless translation_unit_pointer
41
+
42
+ TranslationUnit.new translation_unit_pointer
43
+ end
44
+
45
+ private
46
+
47
+ def args_pointer_from(command_line_args)
48
+ args_pointer = MemoryPointer.new(:pointer)
49
+
50
+ strings = command_line_args.map do |arg|
51
+ MemoryPointer.from_string(arg.to_s)
52
+ end
53
+
54
+ args_pointer.put_array_of_pointer(strings) unless strings.empty?
55
+ args_pointer
56
+ end
57
+
58
+ def options_bitmask_from(opts)
59
+ Lib.bitmask_from Lib::TranslationUnitFlags, opts
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,55 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ module FFI
23
+ module Clang
24
+ module Lib
25
+ extend FFI::Library
26
+
27
+ libs = ["clang"]
28
+
29
+ if Clang.platform == :linux
30
+ libs += [
31
+ "/usr/lib/llvm-3.2/lib/libclang.so",
32
+ "/usr/lib64/llvm-3.2/lib/libclang.so"
33
+ ]
34
+ end
35
+
36
+ ffi_lib libs
37
+
38
+ def self.bitmask_from(enum, opts)
39
+ bitmask = 0
40
+
41
+ opts.each do |key, val|
42
+ next unless val
43
+
44
+ if int = enum[key]
45
+ bitmask |= int
46
+ else
47
+ raise Error, "unknown option: #{key.inspect}, expected one of #{enum.symbols}"
48
+ end
49
+ end
50
+
51
+ bitmask
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,75 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ require 'ffi/clang/lib/translation_unit'
23
+ require 'ffi/clang/lib/string'
24
+
25
+ module FFI
26
+ module Clang
27
+ module Lib
28
+ class CXSourceLocation < FFI::Struct
29
+ layout(
30
+ :ptr_data1, :pointer,
31
+ :ptr_data2, :pointer,
32
+ :int_data, :uint
33
+ )
34
+ end
35
+
36
+ class CXSourceRange < FFI::Struct
37
+ layout(
38
+ :ptr_data1, :pointer,
39
+ :ptr_data2, :pointer,
40
+ :begin_int_data, :uint,
41
+ :end_int_data, :uint
42
+ )
43
+ end
44
+
45
+ typedef :pointer, :CXDiagnostic
46
+
47
+ # Source code diagnostics:
48
+ attach_function :get_num_diagnostics, :clang_getNumDiagnostics, [:CXTranslationUnit], :uint
49
+ attach_function :get_diagnostic, :clang_getDiagnostic, [:CXTranslationUnit, :uint], :CXDiagnostic
50
+ attach_function :dispose_diagnostic, :clang_disposeDiagnostic, [:CXDiagnostic], :void
51
+
52
+ attach_function :get_diagnostic_location, :clang_getDiagnosticLocation, [:CXDiagnostic], CXSourceLocation.by_value
53
+
54
+ # Diagnostic details and string representations:
55
+ DiagnosticDisplayOptions = enum [:source_location, 0x01, :column, 0x02, :source_ranges, 0x04]
56
+ attach_function :default_diagnostic_display_options, :clang_defaultDiagnosticDisplayOptions, [], :uint
57
+ attach_function :format_diagnostic, :clang_formatDiagnostic, [:CXDiagnostic, :uint], CXString.by_value
58
+
59
+ attach_function :get_diagnostic_spelling, :clang_getDiagnosticSpelling, [:CXDiagnostic], CXString.by_value
60
+
61
+ enum :diagnostic_severity, [:ignored, :note, :warning, :error, :fatal]
62
+ attach_function :get_diagnostic_severity, :clang_getDiagnosticSeverity, [:CXDiagnostic], :diagnostic_severity
63
+
64
+ # Diagnostic source ranges:
65
+ attach_function :get_diagnostic_num_ranges, :clang_getDiagnosticNumRanges, [:CXDiagnostic], :uint
66
+ attach_function :get_diagnostic_range, :clang_getDiagnosticRange, [:CXDiagnostic, :uint], CXSourceRange.by_value
67
+
68
+ # Range to source code location conversion:
69
+ attach_function :get_range_start, :clang_getRangeStart, [CXSourceRange.by_value], CXSourceLocation.by_value
70
+ attach_function :get_range_end, :clang_getRangeEnd, [CXSourceRange.by_value], CXSourceLocation.by_value
71
+
72
+ attach_function :equal_locations, :clang_equalLocations, [CXSourceLocation.by_value, CXSourceLocation.by_value], :uint
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ require 'ffi/clang/lib/string'
23
+ require 'ffi/clang/lib/translation_unit'
24
+
25
+ module FFI
26
+ module Clang
27
+ module Lib
28
+ typedef :pointer, :CXFile
29
+
30
+ # Retrieve a file handle within the given translation unit.
31
+ attach_function :get_file, :clang_getFile, [:CXTranslationUnit, :string], :CXFile
32
+
33
+ # Retrieve the complete file and path name of the given file.
34
+ attach_function :get_file_name, :clang_getFileName, [:CXFile], CXString.by_value
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,34 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ require 'ffi/clang/lib'
23
+
24
+ module FFI
25
+ module Clang
26
+ module Lib
27
+ typedef :pointer, :CXIndex
28
+
29
+ # Source code index:
30
+ attach_function :create_index, :clang_createIndex, [:int, :int], :CXIndex
31
+ attach_function :dispose_index, :clang_disposeIndex, [:CXIndex], :void
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,45 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ require 'ffi/clang/lib'
23
+
24
+ module FFI
25
+ module Clang
26
+ module Lib
27
+ class CXString < FFI::Struct
28
+ layout(
29
+ :data, :pointer,
30
+ :private_flags, :uint
31
+ )
32
+ end
33
+
34
+ attach_function :get_string, :clang_getCString, [CXString.by_value], :string
35
+ attach_function :dispose_string, :clang_disposeString, [CXString.by_value], :void
36
+
37
+ def self.extract_string(cxstring)
38
+ result = get_string(cxstring)
39
+ dispose_string cxstring
40
+
41
+ return result
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ require 'ffi/clang/lib/index'
23
+
24
+ module FFI
25
+ module Clang
26
+ module Lib
27
+ typedef :pointer, :CXTranslationUnit
28
+
29
+ TranslationUnitFlags = enum [:none, :detailed_preprocessing_record, :incomplete, :precompiled_preamble, :cache_completion_results]
30
+
31
+ # Source code translation units:
32
+ attach_function :parse_translation_unit, :clang_parseTranslationUnit, [:CXIndex, :string, :pointer, :int, :pointer, :uint, :uint], :CXTranslationUnit
33
+ attach_function :dispose_translation_unit, :clang_disposeTranslationUnit, [:CXTranslationUnit], :void
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,45 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ require 'ffi/clang/lib/translation_unit'
23
+
24
+ module FFI
25
+ module Clang
26
+ class TranslationUnit < AutoPointer
27
+ def initialize(pointer)
28
+ super pointer
29
+ end
30
+
31
+ def self.release(pointer)
32
+ Lib.dispose_translation_unit(pointer)
33
+ end
34
+
35
+ def diagnostics
36
+ n = Lib.get_num_diagnostics(self)
37
+
38
+ n.times.map do |i|
39
+ Diagnostic.new(self, Lib.get_diagnostic(self, i))
40
+ end
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,26 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+
22
+ module FFI
23
+ module Clang
24
+ VERSION = "0.1.0"
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Diagnostic do
4
+ let(:diagnostics) { Index.new.parse_translation_unit(fixture_path("list.c")).diagnostics }
5
+ let(:diagnostic) { diagnostics.first }
6
+
7
+ it "returns a string representation of the diagnostic" do
8
+ str = diagnostic.format
9
+ str.should be_kind_of(String)
10
+ str.should =~ /does not match previous/
11
+ end
12
+
13
+ it "returns a string representation according to the given opts" do
14
+ diagnostic.format(:source_location => true).should include("list.c:5")
15
+ end
16
+
17
+ it "returns the text of the diagnostic" do
18
+ diagnostic.spelling.should be_kind_of(String)
19
+ end
20
+
21
+ it "returns the severity of the diagnostic" do
22
+ diagnostic.severity.should == :error
23
+ end
24
+
25
+ it "returns the ranges of the diagnostic" do
26
+ rs = diagnostics[1].ranges
27
+ rs.should be_kind_of(Array)
28
+ rs.should_not be_empty
29
+ rs.first.should be_kind_of(SourceRange)
30
+ end
31
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Index do
4
+ let(:index) { Index.new }
5
+
6
+ it "can parse a source file" do
7
+ tu = index.parse_translation_unit fixture_path("a.c")
8
+ tu.should be_kind_of(TranslationUnit)
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe TranslationUnit do
4
+ let(:tu) { Index.new.parse_translation_unit fixture_path("a.c") }
5
+
6
+ it "returns a list of diagnostics" do
7
+ diags = tu.diagnostics
8
+ diags.should be_kind_of(Array)
9
+ diags.should_not be_empty
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ int main(int argc, char const *argv)
2
+ {
3
+ return 0;
4
+ }
@@ -0,0 +1,10 @@
1
+ struct List {
2
+ int Data;
3
+ struct List *Next;
4
+ };
5
+ int sum(union List *L) { return 1; };
6
+
7
+ main(int argc, char const *argv)
8
+ {
9
+ return 0;
10
+ }
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
+ require 'ffi/clang'
3
+
4
+ include FFI::Clang
5
+
6
+ module ClangSpecHelper
7
+ def fixture_path(path)
8
+ File.join File.expand_path("../fixtures", __FILE__), path
9
+ end
10
+ end
11
+
12
+ RSpec.configure do |c|
13
+ c.include ClangSpecHelper
14
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffi-clang
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jari Bakken
8
+ - Samuel Williams
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ffi
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '1.3'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '1.3'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: Ruby FFI bindings for libclang C interface.
71
+ email:
72
+ - Jari Bakken
73
+ - samuel.williams@oriontransfer.co.nz
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - .rspec
80
+ - .travis.yml
81
+ - Gemfile
82
+ - README.md
83
+ - Rakefile
84
+ - ffi-clang.gemspec
85
+ - lib/ffi/clang.rb
86
+ - lib/ffi/clang/diagnostic.rb
87
+ - lib/ffi/clang/index.rb
88
+ - lib/ffi/clang/lib.rb
89
+ - lib/ffi/clang/lib/diagnostic.rb
90
+ - lib/ffi/clang/lib/file.rb
91
+ - lib/ffi/clang/lib/index.rb
92
+ - lib/ffi/clang/lib/string.rb
93
+ - lib/ffi/clang/lib/translation_unit.rb
94
+ - lib/ffi/clang/translation_unit.rb
95
+ - lib/ffi/clang/version.rb
96
+ - spec/clang/diagnostic_spec.rb
97
+ - spec/clang/index_spec.rb
98
+ - spec/clang/translation_unit_spec.rb
99
+ - spec/fixtures/a.c
100
+ - spec/fixtures/list.c
101
+ - spec/spec_helper.rb
102
+ homepage: ''
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Ruby FFI bindings for libclang C interface.
126
+ test_files:
127
+ - spec/clang/diagnostic_spec.rb
128
+ - spec/clang/index_spec.rb
129
+ - spec/clang/translation_unit_spec.rb
130
+ - spec/fixtures/a.c
131
+ - spec/fixtures/list.c
132
+ - spec/spec_helper.rb