functions 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env ruby
  2. require 'optparse'
  3. require 'ostruct'
  4. require 'pp'
  5. require File.dirname(__FILE__) + '/../lib/parser/parser'
  6. #require '../parser'
  7. class ScriptArgs
  8. def self.parse(args)
  9. options = OpenStruct.new
  10. options.source = nil
  11. options.libs = []
  12. options.funct_name = true
  13. options.args_name = nil
  14. options.return_type = nil
  15. options.match = ".*"
  16. opt_parser = OptionParser.new do |opts|
  17. opts.banner = "Usage : functions [options] source_file"
  18. opts.on("-I", "--directory dir", String, "Add a directory to the list of directories to be searched for header files") do |lib|
  19. options.libs << lib
  20. end
  21. opts.on("-l", "--library lib", String, "Add a library name, the header files are found with pkg-config") do |lib|
  22. options.libs.concat( `pkg-config --cflags #{lib}`.gsub("-I","").split(" ") )
  23. end
  24. opts.on("-f", "--function", "Apply match pattern to the function name") do |o|
  25. options.funct_name = true
  26. options.args_name = false
  27. options.return_type = false
  28. end
  29. opts.on("-a", "--args", "Apply match pattern to the arguments names") do |o|
  30. options.funct_name = false
  31. options.args_name = true
  32. options.return_type = false
  33. end
  34. opts.on("-r", "--return", "Apply match pattern to the return_typee") do |o|
  35. options.funct_name = false
  36. options.args_name = false
  37. options.return_type = true
  38. end
  39. opts.on("-m", "--match pattern", String, "The regex used to filter the functions") do |o|
  40. options.match = o
  41. end
  42. opts.on_tail("-h", "--help","Show this message") do
  43. puts opts
  44. exit
  45. end
  46. end
  47. opt_parser.parse!(args)
  48. options
  49. end
  50. end
  51. options = ScriptArgs.parse(ARGV)
  52. if ARGV.size != 1
  53. puts "You should at least give a source file to parse see help (-h or --help)"
  54. exit
  55. end
  56. parser = Parser::HeaderParser.new(ARGV[0], options.libs)
  57. parser.parse
  58. functions = parser.getFunctions
  59. if options.funct_name
  60. functions.delete_if { |f| !(f.getName =~ /#{options.match}/) }
  61. elsif options.args_name
  62. functions.delete_if do |f|
  63. match = false
  64. f.getParameters.each do |p|
  65. match = true if (p.getType.getName =~ /#{options.match}/)
  66. end
  67. !match
  68. end
  69. end
  70. functions.each do |f|
  71. params = "\t"
  72. f.getParameters.each do |p|
  73. params << "#{p.getType.getName} #{p.getName},"
  74. end
  75. puts "#{f.getReturn.getName} #{f.getName} #{params}"
  76. # puts f.getRaw(parser)
  77. end