clangc.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. require "clangc/clangc"
  2. module Clangc
  3. ##
  4. # :call-seq:
  5. # Clangc.visit_children(Hash)
  6. #
  7. # This is a convenient method that call Clangc.visit_children_with_proc or
  8. # Clangc.visit_children_with_block.
  9. # the Hash arguments can accept two keys:
  10. # * cursor
  11. # * visitor
  12. #
  13. # Clangc.visit_children(cursor: acursor, visitor: aproc)
  14. #
  15. # Clangc.visit_children(cursor: acursor) do |cursor, parent|
  16. # #do your stuf
  17. # return Clangc::ChildVisitResult::Recurse
  18. # #return Clangc::ChildVisitResult::Break
  19. # end
  20. def self.visit_children(args)
  21. cursor = args[:cursor]
  22. callback = args[:visitor] || nil
  23. if(callback)
  24. visit_children_with_proc(cursor, callback)
  25. else
  26. visit_children_with_block(cursor) do |cursor, parent|
  27. yield(cursor, parent)
  28. end
  29. end
  30. end
  31. class TranslationUnit
  32. ##
  33. # :call-seq:
  34. # Clangc::TranslationUnit#diagnostics => Array
  35. #
  36. # Returns an array of Clangc::Diagnostic for the current Clangc::TranslationUnit.
  37. # The array is empty if no Clangc::Diagnostic can be found.
  38. def diagnostics
  39. ds = []
  40. for i in 0..(diagnostics_num - 1) do
  41. ds << diagnostic(i)
  42. end
  43. ds
  44. end
  45. end
  46. class Diagnostic
  47. ##
  48. # :call-seq:
  49. # Clangc::Diagnostic#source_ranges => Array
  50. #
  51. # Returns an array of Clangc::SourceRange for the current Clangc::Diagnostic.
  52. # The array is empty if there is no Clangc::SourceRange
  53. def source_ranges
  54. num = num_ranges
  55. sr = []
  56. return sr if num == 0
  57. for i in 0..(num - 1) do
  58. sr << source_range(i)
  59. end
  60. sr
  61. end
  62. end
  63. class Type
  64. ##
  65. # :call-seq:
  66. # Clangc::Type#arg_types -> Array
  67. #
  68. # Return an array that contains all the
  69. # types for the argument of the function that
  70. # is related to the current type.
  71. # If the current type is not a function, it returns
  72. # an empty array.
  73. def arg_types
  74. num = num_arg_types
  75. return [] if num == -1
  76. types = []
  77. for i in 0..(num - 1) do
  78. types << arg_type(i)
  79. end
  80. types
  81. end
  82. ##
  83. # :call-seq:
  84. # Clangc::Type#template_arguments_as_type -> Array
  85. #
  86. # Return an array that contains all the
  87. # types for the arguments of the current Class Type template that
  88. # is related to the current cursor.
  89. # If the Cursor is not a Class Declaration, it returns
  90. # an empty array.
  91. def template_arguments_as_type
  92. num = num_template_arguments
  93. types = []
  94. return types if num == -1
  95. for i in 0..(num - 1) do
  96. types << template_argument_as_type(i)
  97. end
  98. types
  99. end
  100. end
  101. class Cursor
  102. ##
  103. # :call-seq:
  104. # Clangc::Cursor#arguments -> Array
  105. #
  106. # Return an array that contains all the
  107. # cursors for the arguments of the function that
  108. # is related to the current cursor.
  109. # If the current cursor is not a function, it returns
  110. # an empty array.
  111. def arguments
  112. num = num_arguments
  113. cursors = []
  114. return cursors if num == -1
  115. for i in 0..(num - 1) do
  116. cursors << argument(i)
  117. end
  118. cursors
  119. end
  120. ##
  121. # :call-seq:
  122. # Clangc::Cursor#overloaded_decls -> Array
  123. #
  124. # Return an array that contains all the
  125. # cursors for the overloaded declarations that
  126. # are related to the current cursor.
  127. # If the current cursor is not an overloaded declaration, it returns
  128. # an empty array.
  129. def overloaded_decls
  130. num = num_overloaded_decls
  131. cursors = []
  132. return cursors if num == 0
  133. for i in 0..(num - 1) do
  134. cursors << overloaded_decl(i)
  135. end
  136. cursors
  137. end
  138. ##
  139. # :call-seq:
  140. # Clangc::Cursor#template_arguments_kinds -> Array
  141. #
  142. # Return an array that contains all the
  143. # kinds for the arguments of the function template that
  144. # is related to the current cursor.
  145. # If the current cursor is not a function declaration, it returns
  146. # an empty array.
  147. def template_arguments_kinds
  148. num = num_template_arguments
  149. kinds = []
  150. return kinds if num == -1
  151. for i in 0..(num - 1) do
  152. kinds << template_argument_kind(i)
  153. end
  154. kinds
  155. end
  156. ##
  157. # :call-seq:
  158. # Clangc::Cursor#template_arguments_types -> Array
  159. #
  160. # Return an array that contains all the
  161. # types for the arguments of the function template that
  162. # is related to the current cursor.
  163. # If the current cursor is not a function declaration, it returns
  164. # an empty array.
  165. def template_arguments_types
  166. num = num_template_arguments
  167. types = []
  168. return types if num == -1
  169. for i in 0..(num - 1) do
  170. types << template_argument_type(i)
  171. end
  172. types
  173. end
  174. ##
  175. # :call-seq:
  176. # Clangc::Cursor#template_arguments_values -> Array
  177. #
  178. # Return an array that contains all the
  179. # values for the arguments of the function template that
  180. # is related to the current cursor.
  181. # If the current cursor is not a function declaration, it returns
  182. # an empty array.
  183. def template_arguments_values
  184. num = num_template_arguments
  185. values = []
  186. return values if num == -1
  187. for i in 0..(num - 1) do
  188. values << template_argument_value(i)
  189. end
  190. values
  191. end
  192. ##
  193. # :call-seq:
  194. # Clangc::Cursor#template_arguments_unsigned_values -> Array
  195. #
  196. # Return an array that contains all the
  197. # values for the arguments of the function template that
  198. # is related to the current cursor.
  199. # If the current cursor is not a function declaration, it returns
  200. # an empty array.
  201. def template_arguments_unsigned_values
  202. num = num_template_arguments
  203. values = []
  204. return values if num == -1
  205. for i in 0..(num - 1) do
  206. values << template_argument_unsigned_value(i)
  207. end
  208. values
  209. end
  210. end
  211. class Module
  212. ##
  213. # :call-seq:
  214. # Clangc::Module#top_level_headers(Clangc::TranslationUnit) -> Array
  215. #
  216. # Return an array that contains all the
  217. # Clangc::File corresponding to the related
  218. # toplevel headers.
  219. # If the current cursor is not a module, it returns
  220. # an empty array.
  221. def top_level_headers(tu)
  222. num = num_top_level_headers(tu)
  223. headers = []
  224. return headers if num < 1
  225. for i in 0..(num - 1) do
  226. headers << top_level_header(tu, i)
  227. end
  228. headers
  229. end
  230. end
  231. class CompletionString
  232. ##
  233. # :call-seq:
  234. # Clangc::CompletionString#chunk_kinds -> Array
  235. #
  236. # Return an array that contains all the
  237. # kinds of the chunk completions for a
  238. # completion string.
  239. def chunk_kinds
  240. num = num_chunks
  241. return [] if num == -1
  242. kinds = []
  243. for i in 0..(num - 1) do
  244. kinds << chunk_kind(i)
  245. end
  246. kinds
  247. end
  248. ##
  249. # :call-seq:
  250. # Clangc::CompletionString#chunk_texts -> Array
  251. #
  252. # Return an array that contains all the
  253. # texts of the chunk completions for a
  254. # completion string.
  255. def chunk_texts
  256. num = num_chunks
  257. return [] if num == -1
  258. texts = []
  259. for i in 0..(num - 1) do
  260. texts << chunk_text(i)
  261. end
  262. texts
  263. end
  264. ##
  265. # :call-seq:
  266. # Clangc::CompletionString#num_annotations -> Array
  267. #
  268. # Return an array that contains all the
  269. # annotations for a completion string.
  270. def annotations
  271. num = num_annotations
  272. return [] if num == -1
  273. annotations = []
  274. for i in 0..(num - 1) do
  275. annotations << annotation(i)
  276. end
  277. annotations
  278. end
  279. ##
  280. # :call-seq:
  281. # Clangc::CompletionString#chunk_text_completion_strings -> Array
  282. #
  283. # Return an array that contains all the
  284. # completion strings for a
  285. # completion string.
  286. def chunk_completion_strings
  287. num = num_chunks
  288. return [] if num == -1
  289. completion_strings = []
  290. for i in 0..(num - 1) do
  291. completion_strings << chunk_completion_string(i)
  292. end
  293. completion_strings
  294. end
  295. end
  296. class CodeCompleteResults
  297. ##
  298. # :call-seq:
  299. # Clangc::CodeCompleteResults#results => Array
  300. #
  301. # Returns an Array of Clangc::CompletionResult
  302. def results
  303. num = num_results
  304. return [] if num < 0
  305. res = []
  306. for i in 0..(num - 1) do
  307. res << result(i)
  308. end
  309. res
  310. end
  311. ##
  312. # :call-seq:
  313. # Clangc::CodeCompleteResults#diagnostics => Array
  314. #
  315. # Returns an Array of Clangc::Diagnostics
  316. def diagnostics
  317. num = num_diagnostics
  318. return [] if num < 0
  319. diags = []
  320. for i in 0..(num - 1) do
  321. diags << diagnostic(i)
  322. end
  323. diags
  324. end
  325. end
  326. class Index
  327. ##
  328. # :call-seq:
  329. # Clangc::Index#translation_unit(options) => Clangc::TranslationUnit
  330. #
  331. # Convenient method that easily allow to create a translation unit
  332. # through different ways based on the options you use:
  333. # :source => source file
  334. # :args => command line arguments
  335. # :error => true or false or nil
  336. # :ast => String an ast file
  337. alias_method :create_translation_unit_raw, :create_translation_unit
  338. def create_translation_unit(options)
  339. source = options[:source] || ""
  340. args = options[:args] || []
  341. error = options[:error] || nil
  342. ast = options[:ast] || nil
  343. if ast
  344. error ? create_translation_unit2(ast) : create_translation_unit_raw(ast)
  345. else
  346. create_translation_unit_from_source_file(source, args)
  347. end
  348. end
  349. ##
  350. # :call-seq:
  351. # Clangc::Index#parse_translation_unit(options) => Clangc::TranslationUnit
  352. #
  353. # Convenient method that easily allow to parse a file to a translation
  354. # unit through different ways based on the options you use:
  355. # :source => source file
  356. # :args => command line arguments
  357. # :error => true or false or nil
  358. # :flags => bitwise OR of the TranslationUnit_Flags constants
  359. # :argv => true or false in order to use argv form
  360. alias_method :parse_translation_unit_raw, :parse_translation_unit
  361. def parse_translation_unit(options)
  362. source = options[:source] || ""
  363. args = options[:args] || []
  364. error = options[:error] || nil
  365. flags = options[:flags] || Clangc::TranslationUnit_Flags::NONE
  366. argv = options[:argv] || false
  367. if error && argv
  368. parse_translation_unit2_full_argv(source, args, flags)
  369. elsif error
  370. parse_translation_unit2(source, args, flags)
  371. else
  372. parse_translation_unit_raw(source, args, flags)
  373. end
  374. end
  375. end
  376. end