ghazel-ffi-clang 0.2.0.2 → 0.2.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.
@@ -52,3 +52,5 @@ require 'ffi/clang/source_location'
52
52
  require 'ffi/clang/source_range'
53
53
  require 'ffi/clang/unsaved_file'
54
54
  require 'ffi/clang/token'
55
+ require 'ffi/clang/code_completion'
56
+ require 'ffi/clang/compilation_database'
@@ -0,0 +1,181 @@
1
+ # Copyright, 2014, by Masahiro Sano.
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'ffi/clang/lib/code_completion'
22
+
23
+ module FFI
24
+ module Clang
25
+ class CodeCompletion
26
+ def self.default_code_completion_options
27
+ Lib.opts_from Lib::CodeCompleteFlags, Lib.default_code_completion_options
28
+ end
29
+
30
+ class Results < FFI::AutoPointer
31
+ include Enumerable
32
+
33
+ attr_reader :size
34
+ attr_reader :results
35
+
36
+ def initialize(code_complete_results, translation_unit)
37
+ super code_complete_results.pointer
38
+ @translation_unit = translation_unit
39
+ @code_complete_results = code_complete_results
40
+ initialize_results
41
+ end
42
+
43
+ def self.release(pointer)
44
+ Lib.dispose_code_complete_results(pointer)
45
+ end
46
+
47
+ def each(&block)
48
+ @results.each do |token|
49
+ block.call(token)
50
+ end
51
+ end
52
+
53
+ def num_diagnostics
54
+ Lib.get_code_complete_get_num_diagnostics(@code_complete_results)
55
+ end
56
+
57
+ def diagnostic(i)
58
+ Diagnostic.new(@translation_unit, Lib.get_code_complete_get_diagnostic(@code_complete_results, i))
59
+ end
60
+
61
+ def diagnostics
62
+ num_diagnostics.times.map { |i|
63
+ Diagnostic.new(@translation_unit, Lib.get_code_complete_get_diagnostic(@code_complete_results, i))
64
+ }
65
+ end
66
+
67
+ def contexts
68
+ Lib.opts_from Lib::CompletionContext, Lib.get_code_complete_get_contexts(@code_complete_results)
69
+ end
70
+
71
+ def container_usr
72
+ Lib.extract_string Lib.get_code_complete_get_container_usr(@code_complete_results)
73
+ end
74
+
75
+ def container_kind
76
+ is_incomplete = MemoryPointer.new :uint
77
+ Lib.get_code_complete_get_container_kind(@code_complete_results, is_incomplete)
78
+ end
79
+
80
+ def incomplete?
81
+ is_incomplete = MemoryPointer.new :uint
82
+ Lib.get_code_complete_get_container_kind(@code_complete_results, is_incomplete)
83
+ is_incomplete.read_uint != 0
84
+ end
85
+
86
+ def objc_selector
87
+ Lib.extract_string Lib.get_code_complete_get_objc_selector(@code_complete_results)
88
+ end
89
+
90
+ def sort!
91
+ Lib.sort_code_completion_results(@code_complete_results[:results], @code_complete_results[:num])
92
+ initialize_results
93
+ end
94
+
95
+ private
96
+
97
+ def initialize_results
98
+ @size = @code_complete_results[:num]
99
+ cur_ptr = @code_complete_results[:results]
100
+ @results = []
101
+ @size.times {
102
+ @results << Result.new(Lib::CXCompletionResult.new(cur_ptr))
103
+ cur_ptr += Lib::CXCompletionResult.size
104
+ }
105
+ end
106
+ end
107
+
108
+ class Result
109
+ def initialize(result)
110
+ @result = result
111
+ end
112
+
113
+ def kind
114
+ @result[:kind]
115
+ end
116
+
117
+ def string
118
+ CodeCompletion::String.new @result[:string]
119
+ end
120
+ end
121
+
122
+ class String
123
+ def initialize(ptr)
124
+ @pointer = ptr
125
+ end
126
+
127
+ def chunk_kind(i)
128
+ Lib.get_completion_chunk_kind(@pointer, i)
129
+ end
130
+
131
+ def chunk_text(i)
132
+ Lib.extract_string Lib.get_completion_text(@pointer, i)
133
+ end
134
+
135
+ def chunk_completion(i)
136
+ CodeCompletion::String.new Lib.get_completion_chunk_completion_string(@pointer, i)
137
+ end
138
+
139
+ def num_chunks
140
+ Lib.get_num_completion_chunks(@pointer)
141
+ end
142
+
143
+ def chunks
144
+ num_chunks.times.map { |i|
145
+ { kind: chunk_kind(i), text: chunk_text(i), completion: chunk_completion(i) }
146
+ }
147
+ end
148
+
149
+ def priority
150
+ Lib.get_completion_priority(@pointer)
151
+ end
152
+
153
+ def availability
154
+ Lib.get_completion_availability(@pointer)
155
+ end
156
+
157
+ def num_annotations
158
+ Lib.get_completion_num_annotations(@pointer)
159
+ end
160
+
161
+ def annotation(i)
162
+ Lib.extract_string Lib.get_completion_annotation(@pointer, i)
163
+ end
164
+
165
+ def annotations
166
+ num_annotations.times.map { |i|
167
+ Lib.extract_string Lib.get_completion_annotation(@pointer, i)
168
+ }
169
+ end
170
+
171
+ def parent
172
+ Lib.extract_string Lib.get_completion_parent(@pointer, nil)
173
+ end
174
+
175
+ def comment
176
+ Lib.extract_string Lib.get_completion_brief_comment(@pointer)
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,125 @@
1
+ # Copyright, 2014, by Masahiro Sano.
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'ffi/clang/lib/compilation_database'
22
+
23
+ module FFI
24
+ module Clang
25
+ class CompilationDatabase < AutoPointer
26
+ class DatabaseLoadError < FFI::Clang::Error; end
27
+
28
+ def initialize(dirpath)
29
+ uint_ptr = MemoryPointer.new :uint
30
+ cdb_ptr = Lib.compilation_database_from_directory(dirpath, uint_ptr)
31
+ error_code = Lib::CompilationDatabaseError[uint_ptr.read_uint]
32
+ if error_code != :no_error
33
+ raise DatabaseLoadError, "Cannot load database: #{error_code}"
34
+ end
35
+ super cdb_ptr
36
+ end
37
+
38
+ def self.release(pointer)
39
+ Lib.compilation_database_dispose(pointer)
40
+ end
41
+
42
+ def compile_commands(filename)
43
+ CompileCommands.new Lib.compilation_database_get_compile_commands(self, filename), self
44
+ end
45
+
46
+ def all_compile_commands
47
+ CompileCommands.new Lib.compilation_database_get_all_compile_commands(self), self
48
+ end
49
+
50
+ class CompileCommands < AutoPointer
51
+ include Enumerable
52
+
53
+ def initialize(pointer, database)
54
+ super pointer
55
+ @database = database
56
+ end
57
+
58
+ def self.release(pointer)
59
+ Lib.compile_commands_dispose(pointer)
60
+ end
61
+
62
+ def size
63
+ Lib.compile_commands_get_size(self)
64
+ end
65
+
66
+ def command(i)
67
+ CompileCommand.new Lib.compile_commands_get_command(self, i)
68
+ end
69
+
70
+ def commands
71
+ size.times.map { |i| command(i) }
72
+ end
73
+
74
+ def each(&block)
75
+ size.times.map do |i|
76
+ block.call(command(i))
77
+ end
78
+ end
79
+ end
80
+
81
+ class CompileCommand
82
+ def initialize(pointer)
83
+ @pointer = pointer
84
+ end
85
+
86
+ def directory
87
+ Lib.extract_string Lib.compile_command_get_directory(@pointer)
88
+ end
89
+
90
+ def num_args
91
+ Lib.compile_command_get_num_args(@pointer)
92
+ end
93
+
94
+ def arg(i)
95
+ Lib.extract_string Lib.compile_command_get_arg(@pointer, i)
96
+ end
97
+
98
+ def args
99
+ num_args.times.map { |i| arg(i) }
100
+ end
101
+
102
+ def num_mapped_sources
103
+ raise NotImplementedError
104
+ # Lib.compile_command_get_num_mapped_sources(@pointer)
105
+ end
106
+
107
+ def mapped_source_path(i)
108
+ raise NotImplementedError
109
+ # Lib.extract_string Lib.compile_command_get_mapped_source_path(@pointer, i)
110
+ end
111
+
112
+ def mapped_source_content(i)
113
+ raise NotImplementedError
114
+ # Lib.extract_string Lib.compile_command_get_mapped_source_content(@pointer, i)
115
+ end
116
+
117
+ def mapped_sources
118
+ num_mapped_sources.times.map { |i|
119
+ {path: mapped_source_path(i), content: mapped_source_content(i)}
120
+ }
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -24,6 +24,7 @@ require 'ffi/clang/lib/cursor'
24
24
  require 'ffi/clang/source_location'
25
25
  require 'ffi/clang/comment'
26
26
  require 'ffi/clang/type'
27
+ require 'ffi/clang/lib/code_completion'
27
28
 
28
29
  module FFI
29
30
  module Clang
@@ -61,6 +62,10 @@ module FFI
61
62
  SourceRange.new(Lib.cursor_get_comment_range(@cursor))
62
63
  end
63
64
 
65
+ def completion
66
+ CodeCompletion::String.new Lib.get_cursor_completion_string(@cursor)
67
+ end
68
+
64
69
  def declaration?
65
70
  Lib.is_declaration(kind) != 0
66
71
  end
@@ -31,7 +31,11 @@ module FFI
31
31
  elsif ENV['LLVM_CONFIG']
32
32
  llvm_library_dir = `#{ENV['LLVM_CONFIG']} --libdir`.chomp
33
33
 
34
- libs << llvm_library_dir + '/libclang.dylib'
34
+ if FFI::Clang.platform == :darwin
35
+ libs << llvm_library_dir + '/libclang.dylib'
36
+ else
37
+ libs << llvm_library_dir + '/libclang.so'
38
+ end
35
39
  end
36
40
 
37
41
  ffi_lib libs
@@ -0,0 +1,130 @@
1
+ # Copyright, 2014, by Masahiro Sano.
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'ffi/clang/lib/translation_unit'
22
+ require 'ffi/clang/lib/cursor'
23
+ require 'ffi/clang/lib/diagnostic'
24
+
25
+ module FFI
26
+ module Clang
27
+ module Lib
28
+ typedef :pointer, :CXCompletionString
29
+
30
+ class CXCompletionResult < FFI::Struct
31
+ layout(
32
+ :kind, :cursor_kind,
33
+ :string, :CXCompletionString,
34
+ )
35
+ end
36
+
37
+ class CXCodeCompleteResults < FFI::Struct
38
+ layout(
39
+ :results, :pointer,
40
+ :num, :uint,
41
+ )
42
+ end
43
+
44
+ enum :completion_chunk_kind, [
45
+ :optional,
46
+ :typed_text,
47
+ :text,
48
+ :placeholder,
49
+ :informative,
50
+ :current_parameter,
51
+ :left_paren,
52
+ :right_paren,
53
+ :left_bracket,
54
+ :right_bracket,
55
+ :left_brace,
56
+ :right_brace,
57
+ :left_angle,
58
+ :right_angle,
59
+ :comma,
60
+ :result_type,
61
+ :colon,
62
+ :semi_colon,
63
+ :equal,
64
+ :horizontal_space,
65
+ :vertical_space
66
+ ]
67
+
68
+ CodeCompleteFlags = enum :code_complete_flags, [
69
+ :include_macros, 0x01,
70
+ :include_code_patterns, 0x02,
71
+ :include_brief_comments, 0x04
72
+ ]
73
+
74
+ CompletionContext = enum :completion_context, [
75
+ :unexposed, 0,
76
+ :any_type, 1 << 0,
77
+ :any_value, 1 << 1,
78
+ :objc_object_value, 1 << 2,
79
+ :objc_selector_value, 1 << 3,
80
+ :cxx_class_type_value, 1 << 4,
81
+ :dot_member_access, 1 << 5,
82
+ :arrow_member_access, 1 << 6,
83
+ :objc_property_access, 1 << 7,
84
+ :enum_tag, 1 << 8,
85
+ :union_tag, 1 << 9,
86
+ :struct_tag, 1 << 10,
87
+ :class_tag, 1 << 11,
88
+ :namespace, 1 << 12,
89
+ :nested_name_specifier, 1 << 13,
90
+ :objc_interface, 1 << 14,
91
+ :objc_protocol, 1 << 15,
92
+ :objc_category, 1 << 16,
93
+ :objc_instance_message, 1 << 17,
94
+ :objc_class_message, 1 << 18,
95
+ :objc_selector_name, 1 << 19,
96
+ :macro_name, 1 << 20,
97
+ :natural_language, 1 << 21,
98
+ :unknown, ((1 << 22) - 1),
99
+ ]
100
+
101
+ # CXCompletionString functions
102
+ attach_function :get_completion_chunk_kind, :clang_getCompletionChunkKind, [:CXCompletionString, :uint], :completion_chunk_kind
103
+ attach_function :get_completion_text, :clang_getCompletionChunkText, [:CXCompletionString, :uint], CXString.by_value
104
+ attach_function :get_completion_chunk_completion_string, :clang_getCompletionChunkCompletionString, [:CXCompletionString, :uint], :CXCompletionString
105
+ attach_function :get_num_completion_chunks, :clang_getNumCompletionChunks, [:CXCompletionString], :uint
106
+ attach_function :get_completion_priority, :clang_getCompletionPriority, [:CXCompletionString], :uint
107
+ attach_function :get_completion_availability, :clang_getCompletionAvailability, [:CXCompletionString], :availability
108
+ attach_function :get_completion_num_annotations, :clang_getCompletionNumAnnotations, [:CXCompletionString], :uint
109
+ attach_function :get_completion_annotation, :clang_getCompletionAnnotation, [:CXCompletionString, :uint], CXString.by_value
110
+ attach_function :get_completion_parent, :clang_getCompletionParent, [:CXCompletionString, :pointer], CXString.by_value
111
+ attach_function :get_completion_brief_comment, :clang_getCompletionBriefComment, [:CXCompletionString], CXString.by_value
112
+
113
+ # CXCodeCompleteResults functions
114
+ attach_function :get_code_complete_get_num_diagnostics, :clang_codeCompleteGetNumDiagnostics, [CXCodeCompleteResults.ptr], :uint
115
+ attach_function :get_code_complete_get_diagnostic, :clang_codeCompleteGetDiagnostic, [CXCodeCompleteResults.ptr, :uint], :CXDiagnostic
116
+ attach_function :get_code_complete_get_contexts, :clang_codeCompleteGetContexts, [CXCodeCompleteResults.ptr], :ulong_long
117
+ attach_function :get_code_complete_get_container_kind, :clang_codeCompleteGetContainerKind, [CXCodeCompleteResults.ptr, :pointer], :cursor_kind
118
+ attach_function :get_code_complete_get_container_usr, :clang_codeCompleteGetContainerUSR, [CXCodeCompleteResults.ptr], CXString.by_value
119
+ attach_function :get_code_complete_get_objc_selector, :clang_codeCompleteGetObjCSelector, [CXCodeCompleteResults.ptr], CXString.by_value
120
+
121
+ # Other functions
122
+ attach_function :code_complete_at, :clang_codeCompleteAt, [:CXTranslationUnit, :string, :uint, :uint, :pointer, :uint, :uint], CXCodeCompleteResults.ptr
123
+ attach_function :dispose_code_complete_results, :clang_disposeCodeCompleteResults, [CXCodeCompleteResults.ptr], :void
124
+ attach_function :get_cursor_completion_string, :clang_getCursorCompletionString, [CXCursor.by_value], :CXCompletionString
125
+ attach_function :default_code_completion_options, :clang_defaultCodeCompleteOptions, [], :uint
126
+ attach_function :sort_code_completion_results, :clang_sortCodeCompletionResults, [:pointer, :uint], :void
127
+ end
128
+ end
129
+ end
130
+