Skip to content

Commit 56e26de

Browse files
authored
Merge pull request #44 from tiennou/feature/cli-options
Add debugging helpers
2 parents a6f8fef + 6e86255 commit 56e26de

File tree

6 files changed

+137
-20
lines changed

6 files changed

+137
-20
lines changed

bin/cm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ desc 'Generate HTML documentation'
1414
long_desc 'Generate HTML docs from a Docurium config file'
1515
command :doc do |c|
1616
c.flag :for, :desc => "The version to generate", :multiple => true
17+
c.switch [:n, "dry-run"], :desc => "Dry-run"
18+
c.switch [:d, "debug"], :desc => "Enable debug log"
19+
c.flag "debug-file", :desc => "Enable debug output for header", :multiple => true
20+
c.flag "debug-function", :desc => "Show debug output when processing function", :multiple => true
21+
c.flag "debug-type", :desc => "Show debug output when processing type", :multiple => true
1722
c.action do |global_options,options,args|
1823
file = args.first
1924
Docurium::CLI.doc(file, options)

lib/docurium.rb

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'rocco'
55
require 'docurium/version'
66
require 'docurium/layout'
7+
require 'docurium/debug'
78
require 'libdetect'
89
require 'docurium/docparser'
910
require 'pp'
@@ -19,12 +20,13 @@
1920
class Docurium
2021
attr_accessor :branch, :output_dir, :data, :head_data
2122

22-
def initialize(config_file, repo = nil)
23+
def initialize(config_file, cli_options = {}, repo = nil)
2324
raise "You need to specify a config file" if !config_file
2425
raise "You need to specify a valid config file" if !valid_config(config_file)
2526
@sigs = {}
2627
@head_data = nil
2728
@repo = repo || Rugged::Repository.discover(config_file)
29+
@cli_options = cli_options
2830
end
2931

3032
def init_data(version = 'HEAD')
@@ -192,9 +194,11 @@ def generate_docs(options)
192194

193195
print "Generating documentation [#{i}/#{versions.count}]\r"
194196

195-
output_index.add(:path => "#{version}.json", :oid => sha, :mode => 0100644)
196-
examples.each do |path, id|
197-
output_index.add(:path => path, :oid => id, :mode => 0100644)
197+
unless dry_run?
198+
output_index.add(:path => "#{version}.json", :oid => sha, :mode => 0100644)
199+
examples.each do |path, id|
200+
output_index.add(:path => path, :oid => id, :mode => 0100644)
201+
end
198202
end
199203
end
200204

@@ -203,6 +207,8 @@ def generate_docs(options)
203207
show_warnings(head_data)
204208
end
205209

210+
return if dry_run?
211+
206212
# We tally the signatures in the order they finished, which is
207213
# arbitrary due to the concurrency, so we need to sort them once
208214
# they've finished.
@@ -391,7 +397,7 @@ def parse_headers(index, version)
391397
data = init_data(version)
392398
DocParser.with_files(files, :prefix => version) do |parser|
393399
headers.each do |header|
394-
records = parser.parse_file(header)
400+
records = parser.parse_file(header, debug: interesting?(:file, header))
395401
update_globals!(data, records)
396402
end
397403
end
@@ -466,16 +472,20 @@ def valid_config(file)
466472
def group_functions!(data)
467473
func = {}
468474
data[:functions].each_pair do |key, value|
475+
debug_set interesting?(:function, key)
476+
debug "grouping #{key}: #{value}"
469477
if @options['prefix']
470478
k = key.gsub(@options['prefix'], '')
471479
else
472480
k = key
473481
end
474482
group, rest = k.split('_', 2)
483+
debug "grouped: k: #{k}, group: #{group}, rest: #{rest}"
475484
if group.empty?
476485
puts "empty group for function #{key}"
477486
next
478487
end
488+
debug "grouped: k: #{k}, group: #{group}, rest: #{rest}"
479489
data[:functions][key][:group] = group
480490
func[group] ||= []
481491
func[group] << key
@@ -521,6 +531,25 @@ def update_globals!(data, recs)
521531
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new({}), :no_intra_emphasis => true)
522532
recs.each do |r|
523533

534+
types = %w(function file type).map(&:to_sym)
535+
dbg = false
536+
types.each do |t|
537+
dbg ||= if r[:type] == t and interesting?(t, r[:name])
538+
true
539+
elsif t == :file and interesting?(:file, r[:file])
540+
true
541+
elsif [:struct, :enum].include?(r[:type]) and interesting?(:type, r[:name])
542+
true
543+
else
544+
false
545+
end
546+
end
547+
548+
debug_set dbg
549+
550+
debug "processing record: #{r}"
551+
debug
552+
524553
# initialize filemap for this file
525554
file_map[r[:file]] ||= {
526555
:file => r[:file], :functions => [], :meta => {}, :lines => 0
@@ -628,6 +657,10 @@ def update_globals!(data, recs)
628657
# Anything else we want to record?
629658
end
630659

660+
debug "processed record: #{r}"
661+
debug
662+
663+
debug_restore
631664
end
632665

633666
data[:files] << file_map.values[0]
@@ -658,4 +691,12 @@ def write_site(index)
658691
def out(text)
659692
puts text
660693
end
694+
695+
def dry_run?
696+
@cli_options[:dry_run]
697+
end
698+
699+
def interesting?(type, what)
700+
@cli_options['debug'] || (@cli_options["debug-#{type}"] || []).include?(what)
701+
end
661702
end

lib/docurium/cli.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class Docurium
22
class CLI
33

44
def self.doc(idir, options)
5-
doc = Docurium.new(idir)
6-
doc.generate_docs(options)
5+
doc = Docurium.new(idir, options)
6+
doc.generate_docs
77
end
88

99
def self.check(idir, options)

lib/docurium/debug.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
$debug_stack = [false]
2+
3+
def debug_enabled
4+
$debug_stack[-1]
5+
end
6+
7+
def debug(str = nil)
8+
puts str if debug_enabled
9+
end
10+
11+
def debug_enable
12+
$debug_stack.push true
13+
end
14+
15+
def debug_silence
16+
$debug_stack.push false
17+
end
18+
19+
def debug_set val
20+
$debug_stack.push val
21+
end
22+
23+
def debug_pass
24+
$debug_stack.push debug_enabled
25+
end
26+
27+
def debug_restore
28+
$debug_stack.pop
29+
end
30+
31+
def with_debug(&block)
32+
debug_enable
33+
block.call
34+
debug_restore
35+
end
36+
37+
def without_debug(&block)
38+
debug_silence
39+
block.call
40+
debug_restore
41+
end

lib/docurium/docparser.rb

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ def cleanup!
5959

6060
# Entry point for this parser
6161
# Parse `filename` out of the hash `files`
62-
def parse_file(orig_filename)
62+
def parse_file(orig_filename, opts = {})
6363

6464
includes = find_clang_includes + [@tmpdir]
6565

6666
# Override the path we want to filter by
6767
filename = File.join(@tmpdir, orig_filename)
68+
debug_enable if opts[:debug]
69+
debug "parsing #{filename} #{@tmpdir}"
6870
args = includes.map { |path| "-I#{path}" }
6971
args << '-ferror-limit=1'
7072

@@ -73,12 +75,13 @@ def parse_file(orig_filename)
7375
recs = []
7476

7577
tu.cursor.visit_children do |cursor, parent|
76-
#puts "visiting #{cursor.kind} - #{cursor.spelling}"
7778
location = cursor.location
7879
next :continue if location.file == nil
7980
next :continue unless location.file == filename
8081

81-
#puts "for file #{location.file} #{cursor.kind} #{cursor.spelling} #{cursor.comment.kind} #{location.line}"
82+
loc = "%d:%d-%d:%d" % [cursor.extent.start.line, cursor.extent.start.column, cursor.extent.end.line, cursor.extent.end.column]
83+
debug "#{cursor.location.file}:#{loc} - visiting #{cursor.kind}: #{cursor.spelling}, comment is #{cursor.comment.kind}"
84+
8285
#cursor.visit_children do |c|
8386
# puts " child #{c.kind}, #{c.spelling}, #{c.comment.kind}"
8487
# :continue
@@ -95,25 +98,38 @@ def parse_file(orig_filename)
9598
:tdef => nil,
9699
}
97100

98-
case cursor.kind
101+
extract = case cursor.kind
99102
when :cursor_function
100-
#puts "have function"
101-
rec.merge! extract_function(cursor)
103+
debug "have function #{cursor.spelling}"
104+
rec.update extract_function(cursor)
102105
when :cursor_enum_decl
103-
rec.merge! extract_enum(cursor)
106+
debug "have enum #{cursor.spelling}"
107+
rec.update extract_enum(cursor)
104108
when :cursor_struct
105-
#puts "raw struct"
106-
rec.merge! extract_struct(cursor)
109+
debug "have struct #{cursor.spelling}"
110+
rec.update extract_struct(cursor)
107111
when :cursor_typedef_decl
108-
rec.merge! extract_typedef(cursor)
112+
debug "have typedef #{cursor.spelling} #{cursor.underlying_type.spelling}"
113+
rec.update extract_typedef(cursor)
109114
else
110115
raise "No idea how to deal with #{cursor.kind}"
111116
end
112117

118+
rec.merge! extract
119+
113120
recs << rec
114121
:continue
115122
end
116123

124+
if debug_enabled
125+
puts "parse_file: parsed #{recs.size} records for #{filename}:"
126+
recs.each do |r|
127+
puts "\t#{r}"
128+
end
129+
end
130+
131+
debug_restore
132+
117133
recs
118134
end
119135

@@ -187,15 +203,27 @@ def extract_function_args(cursor, cmt)
187203

188204
def extract_subject_desc(comment)
189205
subject = comment.child.text
206+
debug "\t\tsubject: #{subject}"
190207
paras = comment.find_all { |cmt| cmt.kind == :comment_paragraph }.drop(1).map { |p| p.text }
191208
desc = paras.join("\n\n")
209+
debug "\t\tdesc: #{desc}"
192210
return subject, desc
193211
end
194212

195213
def extract_function(cursor)
196214
comment = cursor.comment
197215

198-
#puts "looking at function #{cursor.spelling}, #{cursor.display_name}"
216+
$buggy_functions = %w()
217+
debug_set ($buggy_functions.include? cursor.spelling)
218+
if debug_enabled
219+
puts "\tlooking at function #{cursor.spelling}, #{cursor.display_name}"
220+
puts "\tcomment: #{comment}, #{comment.kind}"
221+
cursor.visit_children do |cur, parent|
222+
puts "\t\tchild: #{cur.spelling}, #{cur.kind}"
223+
:continue
224+
end
225+
end
226+
199227
cmt = extract_function_comment(comment)
200228
args = extract_function_args(cursor, cmt)
201229
#args = args.reject { |arg| arg[:comment].nil? }
@@ -220,6 +248,7 @@ def extract_function(cursor)
220248
decl = "#{ret[:type]} #{cursor.spelling}(#{argline})"
221249
body = "#{decl};"
222250

251+
debug_restore
223252
#puts cursor.display_name
224253
# Return the format that docurium expects
225254
{
@@ -238,6 +267,7 @@ def extract_function(cursor)
238267

239268
def extract_function_comment(comment)
240269
subject, desc = extract_subject_desc(comment)
270+
debug "\t\textract_function_comment: #{comment}, #{comment.kind}, #{subject}, #{desc}"
241271

242272
args = {}
243273
(comment.find_all { |cmt| cmt.kind == :comment_param_command }).each do |param|
@@ -313,7 +343,7 @@ def extract_struct(cursor)
313343
:continue
314344
end
315345

316-
#puts "struct value #{values}"
346+
debug "\tstruct value #{values}"
317347

318348
rec = {
319349
:type => :struct,

test/docurium_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def setup
2020
end
2121

2222
@path = File.dirname(__FILE__) + '/fixtures/git2/api.docurium'
23-
@doc = Docurium.new(@path, @repo)
23+
@doc = Docurium.new(@path, {}, @repo)
2424
@data = @doc.parse_headers(index, 'HEAD')
2525
end
2626

0 commit comments

Comments
 (0)