File: check_doc.py

package info (click to toggle)
opencv 2.1.0-3%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 68,800 kB
  • ctags: 52,010
  • sloc: cpp: 554,793; xml: 475,942; ansic: 153,396; python: 18,622; sh: 428; makefile: 111
file content (184 lines) | stat: -rw-r--r-- 7,183 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
"""
Usage: check_doc.py > log.txt
The script parses different opencv modules
(that are described by instances of class Comp below) and
checks for typical errors in headers and docs, for consistence and for completeness.
Due to its simplicity, it falsely reports some bugs, that should be
just ignored.
"""

import sys, os, re, glob

comps = []

class Comp:
    def __init__(self,comp_name):
        self.name = comp_name

cxcore = Comp('cxcore')
cxcore.header_path = '../cxcore/include'
cxcore.headers = ['cxcore.h','cxtypes.h']
cxcore.ext_macro = 'CVAPI'
cxcore.inline_macro = 'CV_INLINE'
cxcore.func_prefix = 'cv'
cxcore.doc_path = '../docs/ref'
cxcore.docs = ['opencvref_cxcore.htm']
comps.append(cxcore)

cv = Comp('cv')
cv.header_path = '../cv/include'
cv.headers = ['cv.h','cvtypes.h']
cv.ext_macro = 'CVAPI'
cv.inline_macro = 'CV_INLINE'
cv.func_prefix = 'cv'
cv.doc_path = '../docs/ref'
cv.docs = ['opencvref_cv.htm']
comps.append(cv)


highgui = Comp('highgui')
highgui.header_path = '../otherlibs/highgui'
highgui.headers = ['highgui.h']
highgui.ext_macro = 'CVAPI'
highgui.inline_macro = 'CV_INLINE'
highgui.func_prefix = 'cv'
highgui.doc_path = '../docs/ref'
highgui.docs = ['opencvref_highgui.htm']
comps.append(highgui)


def normalize_decl(decl):
    decl = re.sub( r'^\((.+?)\)', r'\1', decl)
    decl = re.sub( r' CV_DEFAULT\((.+?)\)(,|( *\);))', r'=\1\2', decl)
    decl = re.sub( r'\);', r' );', decl )
    decl = re.sub( r'\(', r'( ', decl )
    decl = re.sub( r'/\*.+?\*/', r'', decl )
    decl = re.sub( r'\binline\b', r'', decl )
    decl = re.sub( r' +', r' ', decl )
    decl = re.sub( r' ?= ?', r'=', decl )
    return decl.strip()

def print_report(filename, line_no, msg):
    print '%s(%d): %s' % (filename,line_no,msg)

for comp in comps:
    print "==================================================="
    print 'Checking %s...' % (comp.name,)
    header_path = comp.header_path
    func_list = {}

    if not header_path.endswith('/') and not header_path.endswith('\\'):
        header_path += '/'
    for header_glob in comp.headers:
        glob_expr = header_path + header_glob
        for header in glob.glob(glob_expr):
            f = open(header,'r')
            func_name = ""
            mode = line_no = 0 # mode - outside func declaration (0) or inside (1)
            for l in f.xreadlines():
                line_no += 1
                ll = ""
                
                #if re.findall(r'\b([abd-z]|([c][a-uw-z]))[a-z]*[A-Z]', l):
                #    print_report(header,line_no,"Bad-style identifier:\n\t"+l) 
                
                if mode == 0:
                    if l.startswith(comp.ext_macro):
                        ll = l[len(comp.ext_macro):]
                        decl = ""
                        mode = 1
                    elif l.startswith(comp.inline_macro):
                        temp_func_name = re.findall( r'^.+?\b(' + comp.func_prefix + '\w+)', l )
                        if temp_func_name and temp_func_name[0] != func_name:
                            ll = l[len(comp.inline_macro):]
                            decl = ""
                            mode = 1
                else:
                    ll = l

                if ll:
                    decl += ll.rstrip('\n') + ' '
                    if ll.find(';') >= 0:
                        mode = 0
                        decl = normalize_decl(decl)
                        func_name = re.findall( r'^.+?\b(' + comp.func_prefix + '\w+)', decl )[0]
                        if func_list.get(func_name,[]):
                            print_report(header,line_no,"Duplicated declaration of " + \
                                         func_name + "... ignored") 
                        else:
                            func_list[func_name] = [decl,header,line_no,0]
                    else:
                        mode = 1
            f.close()
    
    doc_path = comp.doc_path
    if not doc_path.endswith('/') and not doc_path.endswith('\\'):
        doc_path += '/'

    blurb_re = re.compile( r'^<p class="Blurb"' )

    for doc_glob in comp.docs:
        glob_expr = doc_path + doc_glob
        for doc in glob.glob(glob_expr):
            f = open(doc, 'r')
            mode = line_no = 0 # mode - 0 outside function declaration, 2 - inside,
                               # 1 transitional state ('cause <pre> is used not only
                               # for declaring functions)
            for l in f.xreadlines():
                line_no += 1
                #if re.findall(r'\b([abd-z]|([c][a-uw-z]))[a-z]*[A-Z]', l):
                #    print_report(doc,line_no,"Bad-style identifier:\n\t" + l) 
                if mode == 0:
                    if blurb_re.match(l):
                        mode = 1
                elif mode == 1:
                    if l.endswith('<pre>\n'):
                        mode = 2
                        decl = ""
                elif mode == 2:
                    if l.startswith('</pre>'):
                        mode = 0
                        if decl.find('CV_DEFAULT') >= 0:
                            print_report(doc,line_no,'CV_DEFAULT is used in documentation')
                        decl = normalize_decl(decl)
                        decl_list = decl.split(';')
                        for decl in decl_list:
                            decl = decl.strip()
                            if decl:
                                decl = decl + ';'

                                #print '***', decl
                                func_name = re.findall( r'^.+?\b(' + comp.func_prefix + '\w+)\(', decl )
                                if not func_name: continue

                                func_name = func_name[0]
                                decl_info = func_list.get(func_name,[])
                                if decl_info:
                                    if decl_info[3] == 0:
                                        if decl_info[0] != decl:
                                            print_report(doc,line_no,'Incorrect documentation on ' + func_name + ':')
                                            print '  hdr: ' + decl_info[0]
                                            print '  doc: ' + decl
                                        decl_info[3] = 1
                                    else:
                                        print_report(doc,line_no,'Duplicated documentation on ' + func_name)
                                else:
                                    print_report(doc,line_no,'The function '+func_name+' is not declared')
                    elif not l.startswith('#define'):
                        decl += l.rstrip('\n')
            f.close()

    print "---------------------------------------------------"
    keys = func_list.keys()
    undocumented_funcs = []
    for k in keys:
        decl_info = func_list[k]
        if decl_info[3] == 0:
            undocumented_funcs.append((decl_info[1],decl_info[2],k))

    undocumented_funcs.sort()
            
    for decl_info in undocumented_funcs:
        print_report(decl_info[0],decl_info[1],'Undocumented function '+decl_info[2])