ffi-clang 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWQwOTY2ZDA4ZWEzMjMwZDExZDY4ZDI3MDNmM2ZlYWJkYzFmZmI3Nw==
4
+ Njk3ODJkZDU2YzlmODI2YTQ2ZmJiYzI5ZjE2OWY5MWEyZDAwNWM5Ng==
5
5
  data.tar.gz: !binary |-
6
- NzgyMmY3OTRjZGYxM2ZkNTQwNmZjYjU0YjE2ZDAwM2IwOThmMDIxZA==
6
+ NDEzNjk5MzM0NmJjY2M4NDVjMjBlNjc3Y2M5Y2QwOTdiZGRiMGRiOA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MTI0NGZlMzYyMGRmYTdhOGQwZDlmYzQzNTA0M2U4NmZlMmZjYjQ2ZDEzOGRk
10
- ODE3NjIyMzliOGQyOTBhOWZiOGFhMmVkZjA5MDUwNmYxZjUxMWI3NDJhMTky
11
- ZTc0ODc2OTJmNjQ4NmQxMDMwYjdlMjEzM2M0MTFjYjRjNDIxM2E=
9
+ NmViOGUxYWIwNDJmMWEyMTk1ZDkzODA3NmQwNTEyMmRkODI5YmM0ZmNhYWM2
10
+ OWI2NjdkMGMwMTU0MGNkOWJiYTA4YzVhZGEzNjZhMjExMjU3ZGE4Yjg0ZTI3
11
+ MWJiOWFiZWY4NWQ3NTE4NjQ3NTAyMThmOWFjNzFiZjI0NDk2OGQ=
12
12
  data.tar.gz: !binary |-
13
- NDI0MDJhYThkZGRiNGQ3MjMwYWYyNjU4NzI2NmY0NzMzOTIwZGI1OGQyY2Rj
14
- YzM2MjEzMTJmMmRjMzkwYmJlZjIxNzZhZDI2OGI2YTY0ZjdlYjkyODkwNjQ0
15
- OWQwNDE2ZGE4ZDE4YmRjOTZjN2YzZWQ2NDdkZWM1ZjhiZDIwNmQ=
13
+ ZDQyZWRiYzhiYjcxYmYyYjc2OGNiNDM1MTMyNWVkMmRjMjgwMDgyNDNiNmNj
14
+ OTU2YWU5NWVkNzgzMjBmM2YzMDQ4ZjU4ZDkxOWIwMTI2ODNiNmI2MjY3ZTIw
15
+ YTI4MDM2MzZlNzUxNmQ4MTEyNmZlMzMxYzIzNTI0ZmNiOWE1YWE=
data/README.md CHANGED
@@ -4,6 +4,9 @@ Ruby FFI bindings for libclang. WIP.
4
4
 
5
5
  http://llvm.org/devmtg/2010-11/Gregor-libclang.pdf
6
6
 
7
+ [![Build Status](https://secure.travis-ci.org/ioquatix/ffi-clang.png)](http://travis-ci.org/ioquatix/ffi-clang)
8
+ [![Code Climate](https://codeclimate.com/github/ioquatix/ffi-clang.png)](https://codeclimate.com/github/ioquatix/ffi-clang)
9
+
7
10
  ## Installation
8
11
 
9
12
  Add this line to your application's Gemfile:
@@ -34,6 +37,7 @@ TODO: Write usage instructions here
34
37
 
35
38
  Copyright, 2010-2012 by Jari Bakken.
36
39
  Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
40
+ Copyright, 2013, by Garry C. Marshall. <http://www.meaningfulname.net>
37
41
 
38
42
  Permission is hereby granted, free of charge, to any person obtaining a copy
39
43
  of this software and associated documentation files (the "Software"), to deal
@@ -46,6 +46,8 @@ require 'ffi/clang/lib'
46
46
  require 'ffi/clang/index'
47
47
  require 'ffi/clang/translation_unit'
48
48
  require 'ffi/clang/diagnostic'
49
- #require 'ffi/clang/source_location'
50
- #require 'ffi/clang/source_range'
49
+ require 'ffi/clang/cursor'
50
+ require 'ffi/clang/source_location'
51
+ require 'ffi/clang/source_range'
52
+ require 'ffi/clang/unsaved_file'
51
53
 
@@ -0,0 +1,110 @@
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/cursor'
23
+ require 'ffi/clang/source_location'
24
+
25
+ module FFI
26
+ module Clang
27
+ class Cursor
28
+ def self.null_cursor
29
+ Cursor.new Lib.get_null_cursor
30
+ end
31
+
32
+ def initialize( cxcursor )
33
+ @cursor = cxcursor
34
+ end
35
+
36
+ def null?
37
+ Lib.cursor_is_null(@cursor) != 0
38
+ end
39
+
40
+ def declaration?
41
+ Lib.is_declaration(kind) != 0
42
+ end
43
+
44
+ def reference?
45
+ Lib.is_reference(kind) != 0
46
+ end
47
+
48
+ def expression?
49
+ Lib.is_expression(kind) != 0
50
+ end
51
+
52
+ def statement?
53
+ Lib.is_statement(kind) != 0
54
+ end
55
+
56
+ def attribute?
57
+ Lib.is_attribute(kind) != 0
58
+ end
59
+
60
+ def invalid?
61
+ Lib.is_invalid(kind) != 0
62
+ end
63
+
64
+ def translation_unit?
65
+ Lib.is_translation_unit(kind) != 0
66
+ end
67
+
68
+ def preprocessing?
69
+ Lib.is_preprocessing(kind) != 0
70
+ end
71
+
72
+ def unexposed?
73
+ Lib.is_unexposed(kind) != 0
74
+ end
75
+
76
+ def location
77
+ SourceLocation.new(Lib.get_cursor_location(@cursor))
78
+ end
79
+
80
+ def extent
81
+ SourceRange.new(Lib.get_cursor_extent(@cursor))
82
+ end
83
+
84
+ def displayName
85
+ Lib.extract_string Lib.get_cursor_display_name(@cursor)
86
+ end
87
+
88
+ def spelling
89
+ Lib.extract_string Lib.get_cursor_spelling(@cursor)
90
+ end
91
+
92
+ def kind
93
+ @cursor[:kind]
94
+ end
95
+
96
+ def visit_children(&block)
97
+ adapter = Proc.new do | cxcursor, parent_cursor, unused |
98
+ block.call Cursor.new(cxcursor), Cursor.new(parent_cursor)
99
+ end
100
+ Lib.visit_children(@cursor, adapter, nil)
101
+ end
102
+
103
+ attr_reader :cursor
104
+
105
+ def ==(other)
106
+ Lib.are_equal(@cursor, other.cursor) != 0
107
+ end
108
+ end
109
+ end
110
+ end
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  # Copyright, 2010-2012 by Jari Bakken.
2
3
  # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
4
  #
@@ -20,21 +21,17 @@
20
21
  # THE SOFTWARE.
21
22
 
22
23
  require 'ffi/clang/lib/diagnostic'
24
+ require 'ffi/clang/source_range'
23
25
 
24
26
  module FFI
25
27
  module Clang
26
- class SourceRange
27
- def initialize(range_struct)
28
- end
29
- end
30
-
31
28
  class Diagnostic < AutoPointer
32
29
  def initialize(translation_unit, pointer)
33
30
  super pointer
34
-
31
+
35
32
  @translation_unit = translation_unit
36
33
  end
37
-
34
+
38
35
  def self.release(pointer)
39
36
  Lib.dispose_diagnostic(pointer)
40
37
  end
@@ -52,13 +49,17 @@ module FFI
52
49
  Lib.get_string Lib.get_diagnostic_spelling(self)
53
50
  end
54
51
 
52
+ def location
53
+ sl = Lib.get_diagnostic_location(self)
54
+ SourceLocation.new sl
55
+ end
56
+
55
57
  def fixits
56
58
  raise NotImplementedError
57
59
  # unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag);
58
60
  # – CXString clang_getDiagnosticFixIt(CXDiagnostic Diag,
59
61
  # unsigned FixIt,
60
62
  # CXSourceRange *ReplacementRange);
61
-
62
63
  end
63
64
 
64
65
  def ranges
@@ -32,10 +32,11 @@ module FFI
32
32
  Lib.dispose_index_debug(pointer)
33
33
  end
34
34
 
35
- def parse_translation_unit(source_file, command_line_args = nil, opts = {})
35
+ def parse_translation_unit(source_file, command_line_args = nil, unsaved = [], opts = {})
36
36
  command_line_args = Array(command_line_args)
37
+ unsaved_files = unsaved_pointer_from(unsaved)
37
38
 
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
+ translation_unit_pointer = Lib.parse_translation_unit(self, source_file, args_pointer_from(command_line_args), command_line_args.size, unsaved_files, unsaved.length, options_bitmask_from(opts))
39
40
 
40
41
  raise Error, "error parsing #{source_file.inspect}" unless translation_unit_pointer
41
42
 
@@ -59,6 +60,20 @@ module FFI
59
60
  Lib.bitmask_from Lib::TranslationUnitFlags, opts
60
61
  end
61
62
 
63
+ def unsaved_pointer_from(unsaved)
64
+ return nil if unsaved.length == 0
65
+
66
+ vec = MemoryPointer.new(Lib::CXUnsavedFile, unsaved.length)
67
+
68
+ unsaved.each_with_index do |file, i|
69
+ uf = Lib::CXUnsavedFile.new(vec + i * Lib::CXUnsavedFile.size)
70
+ uf[:filename] = MemoryPointer.from_string(file.filename)
71
+ uf[:contents] = MemoryPointer.from_string(file.contents)
72
+ uf[:length] = file.contents.length
73
+ end
74
+
75
+ vec
76
+ end
62
77
  end
63
78
  end
64
79
  end
@@ -34,7 +34,7 @@ module FFI
34
34
  end
35
35
 
36
36
  ffi_lib libs
37
-
37
+
38
38
  def self.bitmask_from(enum, opts)
39
39
  bitmask = 0
40
40
 
@@ -0,0 +1,72 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ # Copyright, 2013, by Garry C. Marshall. <http://www.meaningfulname.net>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'ffi/clang/lib/translation_unit'
24
+ require 'ffi/clang/lib/diagnostic'
25
+
26
+ module FFI
27
+ module Clang
28
+ module Lib
29
+ enum :kind, [:cursor_struct, 2,
30
+ :cursor_function, 8,
31
+ :cursor_invalid_file, 70,
32
+ :cursor_translation_unit, 300]
33
+
34
+ class CXCursor < FFI::Struct
35
+ layout(
36
+ :kind, :kind,
37
+ :xdata, :int,
38
+ :data, [:pointer, 3]
39
+ )
40
+ end
41
+
42
+ attach_function :get_translation_unit_cursor, :clang_getTranslationUnitCursor, [:CXTranslationUnit], CXCursor.by_value
43
+
44
+ attach_function :get_null_cursor, :clang_getNullCursor, [], CXCursor.by_value
45
+
46
+ attach_function :cursor_is_null, :clang_Cursor_isNull, [CXCursor.by_value], :int
47
+
48
+ attach_function :get_cursor_location, :clang_getCursorLocation, [CXCursor.by_value], CXSourceLocation.by_value
49
+ attach_function :get_cursor_extent, :clang_getCursorExtent, [CXCursor.by_value], CXSourceRange.by_value
50
+ attach_function :get_cursor_display_name, :clang_getCursorDisplayName, [CXCursor.by_value], CXString.by_value
51
+ attach_function :get_cursor_spelling, :clang_getCursorSpelling, [CXCursor.by_value], CXString.by_value
52
+
53
+ attach_function :are_equal, :clang_equalCursors, [CXCursor.by_value, CXCursor.by_value], :uint
54
+
55
+ attach_function :is_declaration, :clang_isDeclaration, [:kind], :uint
56
+ attach_function :is_reference, :clang_isReference, [:kind], :uint
57
+ attach_function :is_expression, :clang_isExpression, [:kind], :uint
58
+ attach_function :is_statement, :clang_isStatement, [:kind], :uint
59
+ attach_function :is_attribute, :clang_isAttribute, [:kind], :uint
60
+ attach_function :is_invalid, :clang_isInvalid, [:kind], :uint
61
+ attach_function :is_translation_unit, :clang_isTranslationUnit, [:kind], :uint
62
+ attach_function :is_preprocessing, :clang_isPreprocessing, [:kind], :uint
63
+ attach_function :is_unexposed, :clang_isUnexposed, [:kind], :uint
64
+
65
+ enum :child_visit_result, [:break, :continue, :recurse]
66
+
67
+ callback :visit_children_function, [CXCursor.by_value, CXCursor.by_value, :pointer], :child_visit_result
68
+ attach_function :visit_children, :clang_visitChildren, [CXCursor.by_value, :visit_children_function, :pointer], :uint
69
+ end
70
+ end
71
+ end
72
+
@@ -1,16 +1,16 @@
1
1
  # Copyright, 2010-2012 by Jari Bakken.
2
2
  # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
- #
3
+ #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  # of this software and associated documentation files (the "Software"), to deal
6
6
  # in the Software without restriction, including without limitation the rights
7
7
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
8
  # copies of the Software, and to permit persons to whom the Software is
9
9
  # furnished to do so, subject to the following conditions:
10
- #
10
+ #
11
11
  # The above copyright notice and this permission notice shall be included in
12
12
  # all copies or substantial portions of the Software.
13
- #
13
+ #
14
14
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
15
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
16
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -20,55 +20,47 @@
20
20
  # THE SOFTWARE.
21
21
 
22
22
  require 'ffi/clang/lib/translation_unit'
23
+ require 'ffi/clang/lib/source_location'
23
24
  require 'ffi/clang/lib/string'
24
25
 
25
26
  module FFI
26
27
  module Clang
27
28
  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
29
  class CXSourceRange < FFI::Struct
37
30
  layout(
38
- :ptr_data1, :pointer,
39
- :ptr_data2, :pointer,
31
+ :ptr_data, [:pointer, 2],
40
32
  :begin_int_data, :uint,
41
33
  :end_int_data, :uint
42
34
  )
43
35
  end
44
-
36
+
45
37
  typedef :pointer, :CXDiagnostic
46
38
 
47
39
  # Source code diagnostics:
48
40
  attach_function :get_num_diagnostics, :clang_getNumDiagnostics, [:CXTranslationUnit], :uint
49
41
  attach_function :get_diagnostic, :clang_getDiagnostic, [:CXTranslationUnit, :uint], :CXDiagnostic
50
42
  attach_function :dispose_diagnostic, :clang_disposeDiagnostic, [:CXDiagnostic], :void
51
-
43
+
52
44
  attach_function :get_diagnostic_location, :clang_getDiagnosticLocation, [:CXDiagnostic], CXSourceLocation.by_value
53
-
45
+
54
46
  # Diagnostic details and string representations:
55
47
  DiagnosticDisplayOptions = enum [:source_location, 0x01, :column, 0x02, :source_ranges, 0x04]
56
48
  attach_function :default_diagnostic_display_options, :clang_defaultDiagnosticDisplayOptions, [], :uint
57
49
  attach_function :format_diagnostic, :clang_formatDiagnostic, [:CXDiagnostic, :uint], CXString.by_value
58
-
50
+
59
51
  attach_function :get_diagnostic_spelling, :clang_getDiagnosticSpelling, [:CXDiagnostic], CXString.by_value
60
-
52
+
61
53
  enum :diagnostic_severity, [:ignored, :note, :warning, :error, :fatal]
62
54
  attach_function :get_diagnostic_severity, :clang_getDiagnosticSeverity, [:CXDiagnostic], :diagnostic_severity
63
-
55
+
64
56
  # Diagnostic source ranges:
65
57
  attach_function :get_diagnostic_num_ranges, :clang_getDiagnosticNumRanges, [:CXDiagnostic], :uint
66
58
  attach_function :get_diagnostic_range, :clang_getDiagnosticRange, [:CXDiagnostic, :uint], CXSourceRange.by_value
67
-
59
+
68
60
  # Range to source code location conversion:
69
61
  attach_function :get_range_start, :clang_getRangeStart, [CXSourceRange.by_value], CXSourceLocation.by_value
70
62
  attach_function :get_range_end, :clang_getRangeEnd, [CXSourceRange.by_value], CXSourceLocation.by_value
71
-
63
+
72
64
  attach_function :equal_locations, :clang_equalLocations, [CXSourceLocation.by_value, CXSourceLocation.by_value], :uint
73
65
  end
74
66
  end
@@ -25,11 +25,19 @@ require 'ffi/clang/lib/translation_unit'
25
25
  module FFI
26
26
  module Clang
27
27
  module Lib
28
+ class CXUnsavedFile < FFI::Struct
29
+ layout(
30
+ :filename, :pointer,
31
+ :contents, :pointer,
32
+ :length, :ulong
33
+ )
34
+ end
35
+
28
36
  typedef :pointer, :CXFile
29
-
37
+
30
38
  # Retrieve a file handle within the given translation unit.
31
39
  attach_function :get_file, :clang_getFile, [:CXTranslationUnit, :string], :CXFile
32
-
40
+
33
41
  # Retrieve the complete file and path name of the given file.
34
42
  attach_function :get_file_name, :clang_getFileName, [:CXFile], CXString.by_value
35
43
  end
@@ -25,7 +25,7 @@ module FFI
25
25
  module Clang
26
26
  module Lib
27
27
  typedef :pointer, :CXIndex
28
-
28
+
29
29
  # Source code index:
30
30
  attach_function :create_index, :clang_createIndex, [:int, :int], :CXIndex
31
31
  attach_function :dispose_index, :clang_disposeIndex, [:CXIndex], :void
@@ -0,0 +1,35 @@
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
+ class CXSourceLocation < FFI::Struct
26
+ layout(
27
+ :ptr_data, [:pointer, 2],
28
+ :int_data, :uint
29
+ )
30
+ end
31
+
32
+ attach_function :get_expansion_location, :clang_getExpansionLocation, [CXSourceLocation.by_value, :pointer, :pointer, :pointer, :pointer], :void
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,41 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright, 2010-2012 by Jari Bakken.
3
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ # Copyright, 2013, by Garry C. Marshall. <http://www.meaningfulname.net>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+
24
+ require 'ffi/clang/lib/source_location'
25
+
26
+ module FFI
27
+ module Clang
28
+ module Lib
29
+ class CXSourceRange < FFI::Struct
30
+ layout(
31
+ :ptr_data, [:pointer, 2],
32
+ :begin_int_data, :uint,
33
+ :end_int_data, :uint
34
+ )
35
+ end
36
+
37
+ attach_function :get_range_start, :clang_getRangeStart, [CXSourceRange.by_value], CXSourceLocation.by_value
38
+ attach_function :get_range_end, :clang_getRangeEnd, [CXSourceRange.by_value], CXSourceLocation.by_value
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,47 @@
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/source_location'
23
+ require 'ffi/clang/lib/file'
24
+
25
+ module FFI
26
+ module Clang
27
+ class SourceLocation
28
+ def initialize(location)
29
+ @location = location
30
+
31
+ cxfile = MemoryPointer.new :pointer
32
+ line = MemoryPointer.new :uint
33
+ column = MemoryPointer.new :uint
34
+ offset = MemoryPointer.new :uint
35
+
36
+ Lib::get_expansion_location(@location, cxfile, line, column, offset)
37
+
38
+ @file = Lib.extract_string Lib.get_file_name(cxfile.read_pointer)
39
+ @line = line.get_uint(0)
40
+ @column = column.get_uint(0)
41
+ @offset = offset.get_uint(0)
42
+ end
43
+
44
+ attr_reader :file, :line, :column, :offset
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,40 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright, 2010-2012 by Jari Bakken.
3
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ # Copyright, 2013, by Garry C. Marshall. <http://www.meaningfulname.net>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+
24
+ module FFI
25
+ module Clang
26
+ class SourceRange
27
+ def initialize(range)
28
+ @range = range
29
+ end
30
+
31
+ def start
32
+ SourceLocation.new(Lib.get_range_start @range)
33
+ end
34
+
35
+ def end
36
+ SourceLocation.new(Lib.get_range_end @range)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -20,6 +20,7 @@
20
20
  # THE SOFTWARE.
21
21
 
22
22
  require 'ffi/clang/lib/translation_unit'
23
+ require 'ffi/clang/cursor'
23
24
 
24
25
  module FFI
25
26
  module Clang
@@ -40,6 +41,9 @@ module FFI
40
41
  end
41
42
  end
42
43
 
44
+ def cursor
45
+ Cursor.new(Lib.get_translation_unit_cursor(self))
46
+ end
43
47
  end
44
48
  end
45
49
  end
@@ -0,0 +1,33 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright, 2013, by Carlos Martín Nieto <cmn@dwim.me.
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
+ class UnsavedFile
25
+ def initialize(filename, contents)
26
+ @filename = filename
27
+ @contents = contents
28
+ end
29
+
30
+ attr_accessor :filename, :contents
31
+ end
32
+ end
33
+ end
@@ -21,6 +21,6 @@
21
21
 
22
22
  module FFI
23
23
  module Clang
24
- VERSION = "0.1.0"
24
+ VERSION = "0.1.1"
25
25
  end
26
26
  end
@@ -0,0 +1,147 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ # Copyright, 2013, by Garry C. Marshall. <http://www.meaningfulname.net>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ def find_first(cursor, kind)
26
+ first = nil
27
+
28
+ cursor.visit_children do |cursor, parent|
29
+ if (cursor.kind == kind)
30
+ first = cursor
31
+ :break
32
+ else
33
+ :recurse
34
+ end
35
+ end
36
+
37
+ first
38
+ end
39
+
40
+ describe Cursor do
41
+ let(:cursor) { Index.new.parse_translation_unit(fixture_path("list.c")).cursor }
42
+
43
+ it "can be obtained from a translation unit" do
44
+ cursor.should be_kind_of(Cursor)
45
+ cursor.kind.should equal(:cursor_translation_unit)
46
+ cursor.null?.should equal(false)
47
+ cursor.translation_unit?.should equal(true)
48
+ end
49
+
50
+ it "returns the source location of the cursor" do
51
+ location = cursor.location
52
+ location.should be_kind_of(SourceLocation)
53
+ end
54
+
55
+ it "has an extent which is a SourceRange" do
56
+ extent = cursor.extent
57
+ extent.should be_kind_of(SourceRange)
58
+
59
+ extent.start.file.should eq(fixture_path("list.c"))
60
+ extent.start.line.should equal(1)
61
+
62
+ extent.end.file.should eq(fixture_path("list.c"))
63
+ extent.end.line.should equal(10)
64
+ end
65
+
66
+ it "returns the path of the translation unit for the translation unit cursor" do
67
+ cursor.displayName.should eq(fixture_path("list.c"))
68
+ cursor.spelling.should eq(fixture_path("list.c"))
69
+ end
70
+
71
+ it "allows us to visit its children" do
72
+ counter = 0
73
+ cursor.visit_children do |cursor, parent|
74
+ counter += 1
75
+ :recurse
76
+ end
77
+ counter.should_not equal(0)
78
+ end
79
+
80
+ describe "Null Cursor" do
81
+ it "can be a null cursor" do
82
+ Cursor.null_cursor.should be_kind_of(Cursor)
83
+ Cursor.null_cursor.kind.should equal(:cursor_invalid_file)
84
+ end
85
+
86
+ it "is null?" do
87
+ Cursor.null_cursor.null?.should equal(true)
88
+ end
89
+
90
+ it "is invalid?" do
91
+ Cursor.null_cursor.invalid?.should equal(true)
92
+ end
93
+
94
+ it "compares as equal to another null cursor instance" do
95
+ Cursor.null_cursor.should eq(Cursor.null_cursor)
96
+ end
97
+
98
+ it "should not equal a Translation Unit cursor" do
99
+ Cursor.null_cursor.should_not eq(cursor)
100
+ end
101
+ end
102
+
103
+ describe "Function Cursors" do
104
+ let (:func) { find_first(cursor, :cursor_function) }
105
+
106
+ it "is not invalid?" do
107
+ func.invalid?.should equal(false)
108
+ end
109
+
110
+ it "can find the first function declaration" do
111
+ func.should_not equal(nil)
112
+ func.kind.should equal(:cursor_function)
113
+ end
114
+
115
+ it "has an extent representing the bounds of the function" do
116
+ func.extent.should be_kind_of(SourceRange)
117
+ func.extent.start.line.should equal(5)
118
+ func.extent.end.line.should equal(5)
119
+ end
120
+
121
+ it "returns the name of the function" do
122
+ func.spelling.should eq("sum")
123
+ func.displayName.should eq("sum(union List *)")
124
+ end
125
+ end
126
+
127
+ describe "Struct Cursors" do
128
+ let (:struct) { find_first(cursor, :cursor_struct) }
129
+
130
+ it "can find the first struct" do
131
+ struct.should_not equal(nil)
132
+ struct.kind.should equal(:cursor_struct)
133
+ end
134
+
135
+ it "has an extent representing the bounds of the struct" do
136
+ struct.extent.start.line.should equal(1)
137
+ struct.extent.end.line.should equal(4)
138
+ end
139
+
140
+ it "returns the name of the struct" do
141
+ struct.spelling.should eq("List")
142
+ struct.displayName.should eq("List")
143
+ end
144
+
145
+ end
146
+
147
+ end
@@ -0,0 +1,40 @@
1
+ # Copyright, 2010-2012 by Jari Bakken.
2
+ # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ # Copyright, 2013, by Garry C. Marshall. <http://www.meaningfulname.net>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe SourceLocation do
26
+ let(:tu) { Index.new.parse_translation_unit(fixture_path("list.c")) }
27
+ let(:tu_location) { tu.cursor.location }
28
+ let(:diagnostic_location) { tu.diagnostics.first.location }
29
+
30
+ it "should have a nil File if the SourceLocation is for a Translation Unit" do
31
+ tu_location.file.should == nil
32
+ end
33
+
34
+ it "should provide a File, line and column for a Diagnostic" do
35
+ diagnostic_location.file.should eq(fixture_path("list.c"))
36
+ diagnostic_location.line.should equal(5)
37
+ diagnostic_location.column.should equal(9)
38
+ end
39
+
40
+ end
@@ -8,4 +8,12 @@ describe TranslationUnit do
8
8
  diags.should be_kind_of(Array)
9
9
  diags.should_not be_empty
10
10
  end
11
+
12
+ it "returns a list of diagnostics from an unsaved file" do
13
+ file = UnsavedFile.new("a.c", File.read(fixture_path("a.c")))
14
+ tu = Index.new.parse_translation_unit("a.c", nil,[file])
15
+ diags = tu.diagnostics
16
+ diags.should be_kind_of(Array)
17
+ diags.should_not be_empty
18
+ end
11
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffi-clang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jari Bakken
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-13 00:00:00.000000000 Z
12
+ date: 2013-07-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi
@@ -83,18 +83,27 @@ files:
83
83
  - Rakefile
84
84
  - ffi-clang.gemspec
85
85
  - lib/ffi/clang.rb
86
+ - lib/ffi/clang/cursor.rb
86
87
  - lib/ffi/clang/diagnostic.rb
87
88
  - lib/ffi/clang/index.rb
88
89
  - lib/ffi/clang/lib.rb
90
+ - lib/ffi/clang/lib/cursor.rb
89
91
  - lib/ffi/clang/lib/diagnostic.rb
90
92
  - lib/ffi/clang/lib/file.rb
91
93
  - lib/ffi/clang/lib/index.rb
94
+ - lib/ffi/clang/lib/source_location.rb
95
+ - lib/ffi/clang/lib/source_range.rb
92
96
  - lib/ffi/clang/lib/string.rb
93
97
  - lib/ffi/clang/lib/translation_unit.rb
98
+ - lib/ffi/clang/source_location.rb
99
+ - lib/ffi/clang/source_range.rb
94
100
  - lib/ffi/clang/translation_unit.rb
101
+ - lib/ffi/clang/unsaved_file.rb
95
102
  - lib/ffi/clang/version.rb
103
+ - spec/clang/cursor_spec.rb
96
104
  - spec/clang/diagnostic_spec.rb
97
105
  - spec/clang/index_spec.rb
106
+ - spec/clang/source_location_spec.rb
98
107
  - spec/clang/translation_unit_spec.rb
99
108
  - spec/fixtures/a.c
100
109
  - spec/fixtures/list.c
@@ -124,8 +133,10 @@ signing_key:
124
133
  specification_version: 4
125
134
  summary: Ruby FFI bindings for libclang C interface.
126
135
  test_files:
136
+ - spec/clang/cursor_spec.rb
127
137
  - spec/clang/diagnostic_spec.rb
128
138
  - spec/clang/index_spec.rb
139
+ - spec/clang/source_location_spec.rb
129
140
  - spec/clang/translation_unit_spec.rb
130
141
  - spec/fixtures/a.c
131
142
  - spec/fixtures/list.c