Skip to content

Commit 393f838

Browse files
committed
Debug mode
1 parent b9a1b70 commit 393f838

File tree

4 files changed

+119
-13
lines changed

4 files changed

+119
-13
lines changed

bin/cm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ 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
1717
c.switch [:n, "dry-run"], :desc => "Dry-run"
18+
c.flag "debug-file", :desc => "Enable debug output for header", :multiple => true
19+
c.flag "debug-function", :desc => "Show debug output when processing function", :multiple => true
20+
c.flag "debug-type", :desc => "Show debug output when processing type", :multiple => true
1821
c.action do |global_options,options,args|
1922
file = args.first
2023
Docurium::CLI.doc(file, options)

lib/docurium.rb

Lines changed: 33 additions & 1 deletion
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'
@@ -299,7 +300,7 @@ def parse_headers(index, version)
299300
data = init_data(version)
300301
DocParser.with_files(files, :prefix => version) do |parser|
301302
headers.each do |header|
302-
records = parser.parse_file(header)
303+
records = parser.parse_file(header, debug: interesting?(:file, header))
303304
update_globals!(data, records)
304305
end
305306
end
@@ -374,16 +375,20 @@ def valid_config(file)
374375
def group_functions!(data)
375376
func = {}
376377
data[:functions].each_pair do |key, value|
378+
debug_set interesting?(:function, key)
379+
debug "grouping #{key}: #{value}"
377380
if @options['prefix']
378381
k = key.gsub(@options['prefix'], '')
379382
else
380383
k = key
381384
end
382385
group, rest = k.split('_', 2)
386+
debug "grouped: k: #{k}, group: #{group}, rest: #{rest}"
383387
if group.empty?
384388
puts "empty group for function #{key}"
385389
next
386390
end
391+
debug "grouped: k: #{k}, group: #{group}, rest: #{rest}"
387392
data[:functions][key][:group] = group
388393
func[group] ||= []
389394
func[group] << key
@@ -429,6 +434,25 @@ def update_globals!(data, recs)
429434
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new({}), :no_intra_emphasis => true)
430435
recs.each do |r|
431436

437+
types = %w(function file type).map(&:to_sym)
438+
dbg = false
439+
types.each do |t|
440+
dbg ||= if r[:type] == t and interesting?(t, r[:name])
441+
true
442+
elsif t == :file and interesting?(:file, r[:file])
443+
true
444+
elsif [:struct, :enum].include?(r[:type]) and interesting?(:type, r[:name])
445+
true
446+
else
447+
false
448+
end
449+
end
450+
451+
debug_set dbg
452+
453+
debug "processing record: #{r}"
454+
debug
455+
432456
# initialize filemap for this file
433457
file_map[r[:file]] ||= {
434458
:file => r[:file], :functions => [], :meta => {}, :lines => 0
@@ -536,6 +560,10 @@ def update_globals!(data, recs)
536560
# Anything else we want to record?
537561
end
538562

563+
debug "processed record: #{r}"
564+
debug
565+
566+
debug_restore
539567
end
540568

541569
data[:files] << file_map.values[0]
@@ -570,4 +598,8 @@ def out(text)
570598
def dry_run?
571599
@cli_options[:dry_run]
572600
end
601+
602+
def interesting?(type, what)
603+
(@cli_options["debug-#{type}"] || []).include?(what)
604+
end
573605
end

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,

0 commit comments

Comments
 (0)