From 7e698726e4a5f8a85dade3161ec8c3b88e193362 Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Mon, 28 May 2012 07:34:49 +0000 Subject: [PATCH] Added new script for checking correctness of documentation --- doc/check_docs2.py | 404 ++++++++++++++++++++++++++++++++++++++ modules/java/rst_parser.py | 53 ++--- modules/python/src2/hdr_parser.py | 54 ++++- 3 files changed, 476 insertions(+), 35 deletions(-) create mode 100644 doc/check_docs2.py diff --git a/doc/check_docs2.py b/doc/check_docs2.py new file mode 100644 index 0000000..9541039 --- /dev/null +++ b/doc/check_docs2.py @@ -0,0 +1,404 @@ +import os, sys, glob, re + +sys.path.append("../modules/python/src2/") +sys.path.append("../modules/java/") + +import hdr_parser as hp +import rst_parser as rp + +rp.show_warnings = False +rp.show_errors = False + +DOCUMENTED_MARKER = "verified" + +ERROR_001_NOTACLASS = 1 +ERROR_002_NOTASTRUCT = 2 +ERROR_003_INCORRECTBASE = 3 +ERROR_004_MISSEDNAMESPACE = 4 +ERROR_005_MISSINGPYFUNC = 5 +ERROR_006_INVALIDPYOLDDOC = 6 +ERROR_007_INVALIDPYDOC = 7 +ERROR_008_CFUNCISNOTGLOBAL = 8 +ERROR_009_OVERLOADNOTFOUND = 9 + +do_python_crosscheck = True +errors_disabled = [ERROR_004_MISSEDNAMESPACE] + +if do_python_crosscheck: + try: + import cv2 + except ImportError: + print "Could not load cv2" + do_python_crosscheck = False + +def get_cv2_object(name): + if name.startswith("cv2."): + name = name[4:] + if name.startswith("cv."): + name = name[3:] + if name == "Algorithm": + return cv2.Algorithm__create("Feature2D.ORB"), name + elif name == "FeatureDetector": + return cv2.FeatureDetector_create("ORB"), name + elif name == "DescriptorExtractor": + return cv2.DescriptorExtractor_create("ORB"), name + elif name == "BackgroundSubtractor": + return cv2.BackgroundSubtractorMOG(), name + elif name == "StatModel": + return cv2.KNearest(), name + else: + return getattr(cv2, name)(), name + +def compareSignatures(f, s): + # function names + if f[0] != s[0]: + return False, "name mismatch" + # return type + stype = (s[1] or "void") + ftype = f[1] + if stype.startswith("cv::"): + stype = stype[4:] + if ftype and ftype.startswith("cv::"): + ftype = ftype[4:] + if ftype and ftype != stype: + return False, "return type mismatch" + if ("\C" in f[2]) ^ ("\C" in s[2]): + return False, "const qulifier mismatch" + if len(f[3]) != len(s[3]): + return False, "different number of arguments" + for idx, arg in enumerate(zip(f[3], s[3])): + farg = arg[0] + sarg = arg[1] + ftype = re.sub(r"\bcv::", "", (farg[0] or "")) + stype = re.sub(r"\bcv::", "", (sarg[0] or "")) + if ftype != stype: + return False, "type of argument #" + str(idx+1) + " mismatch" + fname = farg[1] or "arg" + str(idx) + sname = sarg[1] or "arg" + str(idx) + if fname != sname: + return False, "name of argument #" + str(idx+1) + " mismatch" + fdef = re.sub(r"\bcv::", "", (farg[2] or "")) + sdef = re.sub(r"\bcv::", "", (sarg[2] or "")) + if fdef != sdef: + return False, "default value of argument #" + str(idx+1) + " mismatch" + return True, "match" + +def formatSignature(s): + _str = "" + if s[1]: + _str += s[1] + " " + else: + if not bool(re.match(r"(cv\.)?(?P\w+)\.(?P=cls)", s[0])): + _str += "void " + if s[0].startswith("cv."): + _str += s[0][3:].replace(".", "::") + else: + _str += s[0].replace(".", "::") + if len(s[3]) == 0: + _str += "()" + else: + _str += "( " + for idx, arg in enumerate(s[3]): + if idx > 0: + _str += ", " + _str += re.sub(r"\bcv::", "", arg[0]) + " " + if arg[1]: + _str += arg[1] + else: + _str += "arg" + str(idx) + if arg[2]: + _str += "=" + re.sub(r"\bcv::", "", arg[2]) + _str += " )" + if "/C" in s[2]: + _str += " const" + return _str + + +def logerror(code, message, doc = None): + if code in errors_disabled: + return + if doc: + print doc["file"] + ":" + str(doc["line"]), + print "error %03d: %s" % (code, message) + #print + +def process_module(module, path): + hppparser = hp.CppHeaderParser() + rstparser = rp.RstParser(hppparser) + + rstparser.parse(module, path) + rst = rstparser.definitions + + hdrlist = glob.glob(os.path.join(path, "include", "opencv2", module, "*.h*")) + hdrlist.extend(glob.glob(os.path.join(path, "include", "opencv2", module, "detail", "*.h*"))) + + decls = [] + for hname in hdrlist: + if not "ts_gtest.h" in hname: + decls += hppparser.parse(hname, wmode=False) + + funcs = [] + # not really needed to hardcode all the namespaces. Normally all they are collected automatically + namespaces = ['cv', 'cv.gpu', 'cvflann', 'cvflann.anyimpl', 'cvflann.lsh', 'cv.flann', 'cv.linemod', 'cv.detail', 'cvtest', 'perf', 'cv.videostab'] + classes = [] + structs = [] + + # collect namespaces and classes/structs + for decl in decls: + if decl[0].startswith("const"): + pass + elif decl[0].startswith("class") or decl[0].startswith("struct"): + if decl[0][0] == 'c': + classes.append(decl) + else: + structs.append(decl) + dotIdx = decl[0].rfind('.') + if dotIdx > 0: + namespace = decl[0][decl[0].find(' ')+1:dotIdx] + if not [c for c in classes if c[0].endswith(namespace)] and not [s for s in structs if s[0].endswith(namespace)]: + if namespace not in namespaces: + namespaces.append(namespace) + else: + funcs.append(decl) + + clsnamespaces = [] + # process classes + for cl in classes: + name = cl[0][cl[0].find(' ')+1:] + if name.find('.') < 0 and not name.startswith("Cv"): + logerror(ERROR_004_MISSEDNAMESPACE, "class " + name + " from opencv_" + module + " is placed in global namespace but violates C-style naming convention") + clsnamespaces.append(name) + if do_python_crosscheck and not name.startswith("cv.") and name.startswith("Cv"): + clsnamespaces.append("cv." + name[2:]) + if name.startswith("cv."): + name = name[3:] + name = name.replace(".", "::") + doc = rst.get(name) + if not doc: + #TODO: class is not documented + continue + doc[DOCUMENTED_MARKER] = True + # verify class marker + if not doc.get("isclass"): + logerror(ERROR_001_NOTACLASS, "class " + name + " is not marked as \"class\" in documentation", doc) + else: + # verify base + signature = doc.get("class", "") + signature = signature.replace(", public ", " ").replace(" public ", " ") + signature = signature.replace(", protected ", " ").replace(" protected ", " ") + signature = signature.replace(", private ", " ").replace(" private ", " ") + signature = ("class " + signature).strip() + hdrsignature = (cl[0] + " " + cl[1]).replace("class cv.", "class ").replace(".", "::").strip() + if signature != hdrsignature: + logerror(ERROR_003_INCORRECTBASE, "invalid base class documentation\ndocumented: " + signature + "\nactual: " + hdrsignature, doc) + + # process structs + for st in structs: + name = st[0][st[0].find(' ')+1:] + if name.find('.') < 0 and not name.startswith("Cv"): + logerror(ERROR_004_MISSEDNAMESPACE, "struct " + name + " from opencv_" + module + " is placed in global namespace but violates C-style naming convention") + clsnamespaces.append(name) + if name.startswith("cv."): + name = name[3:] + name = name.replace(".", "::") + doc = rst.get(name) + if not doc: + #TODO: struct is not documented + continue + doc[DOCUMENTED_MARKER] = True + # verify struct marker + if not doc.get("isstruct"): + logerror(ERROR_002_NOTASTRUCT, "struct " + name + " is not marked as \"struct\" in documentation", doc) + else: + # verify base + signature = doc.get("class", "") + signature = signature.replace(", public ", " ").replace(" public ", " ") + signature = signature.replace(", protected ", " ").replace(" protected ", " ") + signature = signature.replace(", private ", " ").replace(" private ", " ") + signature = ("struct " + signature).strip() + hdrsignature = (st[0] + " " + st[1]).replace("struct cv.", "struct ").replace(".", "::").strip() + if signature != hdrsignature: + logerror(ERROR_003_INCORRECTBASE, "invalid base struct documentation\ndocumented: " + signature + "\nactual: " + hdrsignature, doc) + + # process functions and methods + flookup = {} + for fn in funcs: + name = fn[0] + parent = None + namespace = None + for cl in clsnamespaces: + if name.startswith(cl + "."): + if cl.startswith(parent or ""): + parent = cl + if parent: + name = name[len(parent) + 1:] + for nm in namespaces: + if parent.startswith(nm + "."): + if nm.startswith(namespace or ""): + namespace = nm + if namespace: + parent = parent[len(namespace) + 1:] + else: + for nm in namespaces: + if name.startswith(nm + "."): + if nm.startswith(namespace or ""): + namespace = nm + if namespace: + name = name[len(namespace) + 1:] + #print namespace, parent, name, fn[0] + if not namespace and not parent and not name.startswith("cv") and not name.startswith("CV_"): + logerror(ERROR_004_MISSEDNAMESPACE, "function " + name + " from opencv_" + module + " is placed in global namespace but violates C-style naming convention") + else: + fdescr = (namespace, parent, name, fn) + flookup_entry = flookup.get(fn[0], []) + flookup_entry.append(fdescr) + flookup[fn[0]] = flookup_entry + + if do_python_crosscheck: + for name, doc in rst.iteritems(): + decls = doc.get("decls") + if not decls: + continue + for signature in decls: + if signature[0] == "Python1": + pname = signature[1][:signature[1].find('(')] + try: + fn = getattr(cv2.cv, pname[3:]) + docstr = "cv." + fn.__doc__ + except AttributeError: + logerror(ERROR_005_MISSINGPYFUNC, "could not load documented function: cv2." + pname, doc) + continue + docstring = docstr + sign = signature[1] + signature.append(DOCUMENTED_MARKER) + # convert old signature to pydoc style + if docstring.endswith("*"): + docstring = docstring[:-1] + s = None + while s != sign: + s = sign + sign = re.sub(r"^(.*\(.*)\(.*?\)(.*\) *->)", "\\1_\\2", sign) + s = None + while s != sign: + s = sign + sign = re.sub(r"\s*,\s*([^,]+)\s*=\s*[^,]+\s*(( \[.*\])?)\)", " [, \\1\\2])", sign) + sign = re.sub(r"\(\s*([^,]+)\s*=\s*[^,]+\s*(( \[.*\])?)\)", "([\\1\\2])", sign) + + sign = re.sub(r"\)\s*->\s*", ") -> ", sign) + sign = sign.replace("-> convexHull", "-> CvSeq") + sign = sign.replace("-> lines", "-> CvSeq") + sign = sign.replace("-> boundingRects", "-> CvSeq") + sign = sign.replace("-> contours", "-> CvSeq") + sign = sign.replace("-> retval", "-> int") + sign = sign.replace("-> detectedObjects", "-> CvSeqOfCvAvgComp") + + def retvalRplace(match): + m = match.group(1) + m = m.replace("CvScalar", "scalar") + m = m.replace("CvMemStorage", "memstorage") + m = m.replace("ROIplImage", "image") + m = m.replace("IplImage", "image") + m = m.replace("ROCvMat", "mat") + m = m.replace("CvMat", "mat") + m = m.replace("double", "float") + m = m.replace("CvSubdiv2DPoint", "point") + m = m.replace("CvBox2D", "Box2D") + m = m.replace("IplConvKernel", "kernel") + m = m.replace("CvHistogram", "hist") + m = m.replace("CvSize", "width,height") + m = m.replace("cvmatnd", "matND") + m = m.replace("CvSeqOfCvConvexityDefect", "convexityDefects") + mm = m.split(',') + if len(mm) > 1: + return "(" + ", ".join(mm) + ")" + else: + return m + + docstring = re.sub(r"(?<=-> )(.*)$", retvalRplace, docstring) + docstring = docstring.replace("( [, ", "([") + + if sign != docstring: + logerror(ERROR_006_INVALIDPYOLDDOC, "old-style documentation differs from pydoc\npydoc: " + docstring + "\nfixup: " + sign + "\ncvdoc: " + signature[1], doc) + elif signature[0] == "Python2": + pname = signature[1][4:signature[1].find('(')] + cvname = "cv." + pname + parent = None + for cl in clsnamespaces: + if cvname.startswith(cl + "."): + if cl.startswith(parent or ""): + parent = cl + try: + if parent: + instance, clsname = get_cv2_object(parent) + fn = getattr(instance, cvname[len(parent)+1:]) + docstr = fn.__doc__ + docprefix = "cv2." + clsname + "." + else: + fn = getattr(cv2, pname) + docstr = fn.__doc__ + docprefix = "cv2." + except AttributeError: + if parent: + logerror(ERROR_005_MISSINGPYFUNC, "could not load documented member of " + parent + " class: cv2." + pname, doc) + else: + logerror(ERROR_005_MISSINGPYFUNC, "could not load documented function cv2." + pname, doc) + continue + docstrings = [docprefix + s.replace("([, ", "([") for s in docstr.split(" or ")] + if not signature[1] in docstrings: + pydocs = "\npydoc: ".join(docstrings) + logerror(ERROR_007_INVALIDPYDOC, "documentation differs from pydoc\npydoc: " + pydocs + "\ncvdoc: " + signature[1], doc) + else: + signature.append(DOCUMENTED_MARKER) + + #build dictionary for functions lookup + # verify C/C++ signatures + for name, doc in rst.iteritems(): + decls = doc.get("decls") + if not decls: + continue + for signature in decls: + if signature[0] == "C" or signature[0] == "C++": + fd = flookup.get(signature[2][0]) + if not fd: + if signature[2][0].startswith("cv."): + fd = flookup.get(signature[2][0][3:]) + if not fd: + continue + else: + signature[2][0] = signature[2][0][3:] + if signature[0] == "C": + ffd = [f for f in fd if not f[0] and not f[1]] # filter out C++ stuff + if not ffd: + if fd[0][1]: + logerror(ERROR_008_CFUNCISNOTGLOBAL, "function " + fd[0][2] + " is documented as C function but is actually member of " + fd[0][1] + " class", doc) + elif fd[0][0]: + logerror(ERROR_008_CFUNCISNOTGLOBAL, "function " + fd[0][2] + " is documented as C function but is actually placed in " + fd[0][0] + " namespace", doc) + fd = ffd + error = None + for f in fd: + match, error = compareSignatures(signature[2], f[3]) + if match: + signature.append(DOCUMENTED_MARKER) + break + if signature[-1] != DOCUMENTED_MARKER: + candidates = "\n\t".join([formatSignature(f[3]) for f in fd]) + logerror(ERROR_009_OVERLOADNOTFOUND, signature[0] + " function " + signature[2][0].replace(".","::") + " is documented but misses in headers (" + error + ").\nDocumented as:\n\t" + signature[1] + "\nCandidates are:\n\t" + candidates, doc) + #print hdrlist + #for d in decls: + # print d + #print rstparser.definitions + +if __name__ == "__main__": + if len(sys.argv) < 2: + print "Usage:\n", os.path.basename(sys.argv[0]), " " + exit(0) + + for module in sys.argv[1:]: + selfpath = os.path.dirname(os.path.abspath(sys.argv[0])) + module_path = os.path.join(selfpath, "..", "modules", module) + + if not os.path.isdir(module_path): + print "Module \"" + module + "\" could not be found." + exit(1) + + process_module(module, module_path) diff --git a/modules/java/rst_parser.py b/modules/java/rst_parser.py index ebd1e1d..6d8f9ee 100644 --- a/modules/java/rst_parser.py +++ b/modules/java/rst_parser.py @@ -3,6 +3,7 @@ allmodules = ["core", "flann", "imgproc", "ml", "highgui", "video", "features2d" verbose = False show_warnings = True show_errors = True +show_critical_errors = True params_blacklist = { "fromarray" : ("object", "allowND"), # python only function @@ -66,7 +67,7 @@ class DeclarationParser(object): if line.startswith(".. ocv:jfunction::"): return "Java" return None - + def hasDeclaration(self, line): return self.getLang(line) is not None @@ -97,7 +98,7 @@ class ParamParser(object): self.comment += "\n" + line.lstrip() else: self.active = False - + def hasDeclaration(self, line): return line.lstrip().startswith(":param") @@ -115,7 +116,7 @@ class RstParser(object): doclist = glob.glob(os.path.join(module_path,"doc/*.rst")) for doc in doclist: self.parse_rst_file(module_name, doc) - + def parse_section_safe(self, module_name, section_name, file_name, lineno, lines): try: self.parse_section(module_name, section_name, file_name, lineno, lines) @@ -184,16 +185,16 @@ class RstParser(object): continue else: skip_code_lines = False - + if ll.startswith(".. code-block::") or ll.startswith(".. image::"): skip_code_lines = True continue - + # todo: parse structure members; skip them for now if ll.startswith(".. ocv:member::"): skip_code_lines = True continue - + #ignore references (todo: collect them) if l.startswith(".. ["): continue @@ -204,7 +205,7 @@ class RstParser(object): # turn on line-skipping mode for code fragments skip_code_lines = True ll = ll[:len(ll)-2] - + # continue param parsing (process params after processing .. at the beginning of the line and :: at the end) if pdecl.active: pdecl.append(l) @@ -269,10 +270,10 @@ class RstParser(object): if skip_code_lines: func["long"] = func.get("long", "") + "\n" # endfor l in lines - + if fdecl.balance != 0: - if show_errors: - print >> sys.stderr, "RST parser error: invalid parentheses balance in \"%s\" File: %s (line %s)" % (section_name, file_name, lineno) + if show_critical_errors: + print >> sys.stderr, "RST parser error: invalid parentheses balance in \"%s\" File: %s:%s" % (section_name, file_name, lineno) return # save last parameter if needed @@ -295,7 +296,7 @@ class RstParser(object): lineno = 0 whitespace_warnings = 0 max_whitespace_warnings = 10 - + lines = [] flineno = 0 fname = "" @@ -310,7 +311,7 @@ class RstParser(object): if whitespace_warnings <= max_whitespace_warnings and show_warnings: print >> sys.stderr, "RST parser warning: tab symbol instead of space is used at file %s (line %s)" % (doc, lineno) l = l.replace("\t", " ") - + # handle first line if prev_line == None: prev_line = l.rstrip() @@ -325,7 +326,7 @@ class RstParser(object): flineno = lineno-1 fname = prev_line.strip() elif flineno > 0: - lines.append(ll) + lines.append(ll) prev_line = ll df.close() @@ -346,9 +347,9 @@ class RstParser(object): decls = func.get("decls",[]) if (decl.lang == "C++" or decl.lang == "C"): rst_decl = self.cpp_parser.parse_func_decl_no_wrap(decl.fdecl) - decls.append( (decl.lang, decl.fdecl, rst_decl) ) + decls.append( [decl.lang, decl.fdecl, rst_decl] ) else: - decls.append( (decl.lang, decl.fdecl) ) + decls.append( [decl.lang, decl.fdecl] ) func["decls"] = decls def add_new_pdecl(self, func, decl): @@ -403,7 +404,7 @@ class RstParser(object): def validateParams(self, func): documentedParams = func.get("params",{}).keys() params = [] - + for decl in func.get("decls", []): if len(decl) > 2: args = decl[2][3] # decl[2] -> [ funcname, return_ctype, [modifiers], [args] ] @@ -476,7 +477,7 @@ class RstParser(object): return func def fixOldCFunctionName(self, func): - if not "decls" in func: + if not "decls" in func: return fname = None for decl in func["decls"]: @@ -495,7 +496,7 @@ class RstParser(object): elif show_warnings: print >> sys.stderr, "\"%s\" - section name is \"%s\" instead of \"%s\". File: %s (line %s)" % (fname, func["name"], fname[6:], func["file"], func["line"]) #self.print_info(func) - + def normalizeText(self, s): if s is None: return s @@ -503,7 +504,7 @@ class RstParser(object): s = re.sub(r"\.\. math::[ \r]*\n+((.|\n)*?)(\n[ \r]*\n|$)", mathReplace2, s) s = re.sub(r":math:`([^`]+?)`", mathReplace, s) s = re.sub(r" *:sup:", "^", s) - + s = s.replace(":ocv:class:", "") s = s.replace(":ocv:struct:", "") s = s.replace(":ocv:func:", "") @@ -526,7 +527,7 @@ class RstParser(object): s = re.sub(r"`([^`<]+ )<(https?://[^>]+)>`_", "\\1(\\2)", s) # remove tailing :: s = re.sub(r"::(\n|$)", "\\1", s) - + # normalize line endings s = re.sub(r"\r\n", "\n", s) # remove extra line breaks before/after _ or , @@ -554,7 +555,7 @@ class RstParser(object): #s = re.sub(r"\.\. \[", "[", s) # unescape s = re.sub(r"\\(.)", "\\1", s) - + # remove whitespace before . s = re.sub(r"[ ]+\.", ".", s) # remove tailing whitespace @@ -582,7 +583,7 @@ class RstParser(object): s = s.strip() return s - + def printSummary(self): print print "RST Parser Summary:" @@ -623,7 +624,7 @@ def matrixReplace(match): m = match.group(2) m = re.sub(r" *& *", " ", m) return m - + def mathReplace(match): m = match.group(1) @@ -645,7 +646,7 @@ def mathReplace(match): m = re.sub(r"\\begin{(?Parray|bmatrix)}(?:{[\|lcr\. ]+})? *(.*?)\\end{(?P=gtype)}", matrixReplace, m) m = re.sub(r"\\hdotsfor{(\d+)}", hdotsforReplace, m) m = re.sub(r"\\vecthreethree{(.*?)}{(.*?)}{(.*?)}{(.*?)}{(.*?)}{(.*?)}{(.*?)}{(.*?)}{(.*?)}", "
|\\1 \\2 \\3|
|\\4 \\5 \\6|
|\\7 \\8 \\9|
", m) - + m = re.sub(r"\\left[ ]*\\lfloor[ ]*", "[", m) m = re.sub(r"[ ]*\\right[ ]*\\rfloor", "]", m) m = re.sub(r"\\left[ ]*\([ ]*", "(", m) @@ -696,7 +697,7 @@ if __name__ == "__main__": if len(sys.argv) < 2: print "Usage:\n", os.path.basename(sys.argv[0]), " " exit(0) - + if len(sys.argv) >= 3: if sys.argv[2].lower() == "verbose": verbose = True @@ -714,7 +715,7 @@ if __name__ == "__main__": exit(1) parser = RstParser(hdr_parser.CppHeaderParser()) - + if module == "all": for m in allmodules: parser.parse(m, os.path.join(rst_parser_dir, "../" + m)) diff --git a/modules/python/src2/hdr_parser.py b/modules/python/src2/hdr_parser.py index b2ab908..e8f5170 100755 --- a/modules/python/src2/hdr_parser.py +++ b/modules/python/src2/hdr_parser.py @@ -242,7 +242,7 @@ class CppHeaderParser(object): bases = ll[2:] return classname, bases, modlist - def parse_func_decl_no_wrap(self, decl_str): + def parse_func_decl_no_wrap(self, decl_str, static_method = False): fdecl = decl_str.replace("CV_OUT", "").replace("CV_IN_OUT", "") fdecl = fdecl.strip().replace("\t", " ") while " " in fdecl: @@ -273,9 +273,16 @@ class CppHeaderParser(object): fname = "cv." + fname.replace("::", ".") decl = [fname, rettype, [], []] + + # inline constructor implementation + implmatch = re.match(r"(\(.*?\))\s*:\s*(\w+\(.*?\),?\s*)+", fdecl[apos:]) + if bool(implmatch): + fdecl = fdecl[:apos] + implmatch.group(1) + args0str = fdecl[apos+1:fdecl.rfind(")")].strip() - if args0str != "": + if args0str != "" and args0str != "void": + args0str = re.sub(r"\([^)]*\)", lambda m: m.group(0).replace(',', "@comma@"), args0str) args0 = args0str.split(",") args = [] @@ -293,9 +300,19 @@ class CppHeaderParser(object): defval = "" if dfpos >= 0: defval = arg[dfpos+1:].strip() + else: + dfpos = arg.find("CV_DEFAULT") + if dfpos >= 0: + defval, pos3 = self.get_macro_arg(arg, dfpos) + else: + dfpos = arg.find("CV_WRAP_DEFAULT") + if dfpos >= 0: + defval, pos3 = self.get_macro_arg(arg, dfpos) + if dfpos >= 0: + defval = defval.replace("@comma@", ",") arg = arg[:dfpos].strip() pos = len(arg)-1 - while pos >= 0 and (arg[pos] == "_" or arg[pos].isalpha() or arg[pos].isdigit()): + while pos >= 0 and (arg[pos] in "_[]" or arg[pos].isalpha() or arg[pos].isdigit()): pos -= 1 if pos >= 0: aname = arg[pos+1:].strip() @@ -308,6 +325,10 @@ class CppHeaderParser(object): aname = "param" decl[3].append([atype, aname, defval, []]) + if static_method: + decl[2].append("/S") + if decl_str.endswith("const"): + decl[2].append("/C") return decl def parse_func_decl(self, decl_str): @@ -328,7 +349,7 @@ class CppHeaderParser(object): return [] # ignore old API in the documentation check (for now) - if "CVAPI(" in decl_str: + if "CVAPI(" in decl_str and self.wrap_mode: return [] top = self.block_stack[-1] @@ -378,6 +399,10 @@ class CppHeaderParser(object): sys.exit(-1) decl_start = decl_str[:args_begin].strip() + # constructor/destructor case + if bool(re.match(r'(\w+::)*(?P\w+)::~?(?P=x)', decl_start)): + decl_start = "void " + decl_start + rettype, funcname, modlist, argno = self.parse_arg(decl_start, -1) if argno >= 0: @@ -385,8 +410,16 @@ class CppHeaderParser(object): if rettype == classname or rettype == "~" + classname: rettype, funcname = "", rettype else: - print "Error at %d. the function/method name is missing: '%s'" % (self.lineno, decl_start) - sys.exit(-1) + if bool(re.match('\w+\s+\(\*\w+\)\s*\(.*\)', decl_str)): + return [] # function typedef + elif bool(re.match('[A-Z_]+', decl_start)): + return [] # it seems to be a macro instantiation + elif "__declspec" == decl_start: + return [] + else: + #print rettype, funcname, modlist, argno + print "Error at %d in %s. the function/method name is missing: '%s'" % (self.lineno, self.hname, decl_start) + sys.exit(-1) if self.wrap_mode and (("::" in funcname) or funcname.startswith("~")): # if there is :: in function name (and this is in the header file), @@ -399,7 +432,7 @@ class CppHeaderParser(object): funcname = self.get_dotted_name(funcname) if not self.wrap_mode: - decl = self.parse_func_decl_no_wrap(decl_str) + decl = self.parse_func_decl_no_wrap(decl_str, static_method) decl[0] = funcname return decl @@ -515,7 +548,7 @@ class CppHeaderParser(object): sys.exit(-1) if block_name: n += block_name + "." - return n + name + return n + name.replace("::", ".") def parse_stmt(self, stmt, end_token): """ @@ -559,7 +592,7 @@ class CppHeaderParser(object): stmt_type = stmt.split()[0] classname, bases, modlist = self.parse_class_decl(stmt) decl = [] - if ("CV_EXPORTS_W" in stmt) or ("CV_EXPORTS_AS" in stmt) or (not self.wrap_mode and ("CV_EXPORTS" in stmt)): + if ("CV_EXPORTS_W" in stmt) or ("CV_EXPORTS_AS" in stmt) or (not self.wrap_mode):# and ("CV_EXPORTS" in stmt)): decl = [stmt_type + " " + self.get_dotted_name(classname), "", modlist, []] if bases: decl[1] = ": " + " ".join(bases) @@ -570,6 +603,8 @@ class CppHeaderParser(object): if stmt.startswith("namespace"): stmt_list = stmt.split() + if len(stmt_list) < 2: + stmt_list.append("") return stmt_list[0], stmt_list[1], True, None if stmt.startswith("extern") and "\"C\"" in stmt: return "namespace", "", True, None @@ -633,6 +668,7 @@ class CppHeaderParser(object): The main method. Parses the input file. Returns the list of declarations (that can be print using print_decls) """ + self.hname = hname decls = [] f = open(hname, "rt") linelist = list(f.readlines()) -- 2.7.4