# This script "wmc.py" by Carl G Lewis, based on "example.py" (included # with pygccxml 0.9.0), which is: # Copyright 2004 Roman Yakovenko. # Distributed under the Boost Software License, Version 1.0. (See # accompanying file LICENSE_1_0.txt or copy at # http://www.boost.org/LICENSE_1_0.txt) import sys sys.path.append('../..') #adding pygccxml to the path from pygccxml import parser from pygccxml import declarations from optparse import OptionParser def parse_options(): parser = OptionParser() parser.add_option("-I", "--include", dest="include", action="append", help="pass this include directory to gccxml", metavar="DIR") parser.add_option("-c", "--class", dest="classname", help="print member functions for specified class", metavar="CLASSNAME") parser.add_option("-z", "--categorized", dest="categorized", action="store_true", help="print categorized member fns for class specified with -c") (options, args) = parser.parse_args() #print options.include if (len(args) == 0): print "Specify header files to parse" sys.exit(1) return (options, args) # methods like class_.constructors() return a mdecl_wrapper_t which # is very similar to a list but not one, in particular it can't be # concatenated. This method gets the items out of a mdecl_wrapper_t # and puts them in a list, which is returned. def to_list(wrapper): return map(None, wrapper) def run_parser(includes, files): #configure GCC-XML parser config = parser.config_t( gccxml_path='/usr/bin/gccxml', include_paths=includes) #parse source files decls = parser.parse(files, config ) global_ns = declarations.get_global_namespace( decls ) return global_ns def get_wmc_stats(global_ns, files): stats = {} for f in files: for class_ in global_ns.classes(header_file=f, allow_empty=1): fns = get_member_fns(class_) stats[class_.name] = len(fns) return stats def print_stats(stats): keys = stats.keys() keys.sort(); for c in keys: print "%-30s%d" % (c, stats[c]) def get_member_fns(class_): fns = to_list(class_.constructors(allow_empty=1)) + \ to_list(class_.member_functions(allow_empty=1)) + \ to_list(class_.member_operators(allow_empty=1)) + \ to_list(class_.casting_operators(allow_empty=1)) return fns def print_member_fns_categorized(class_): result= {} result['constructors'] = to_list(class_.constructors(allow_empty=1)) result['member_functions'] = to_list(class_.member_functions(allow_empty=1)) result['member_operators'] = to_list(class_.member_operators(allow_empty=1)) result['casting_operators'] = to_list(class_.casting_operators(allow_empty=1)) for c in result.keys(): print c for f in result[c]: print "\t" + f.name def print_member_fns(class_): fns = get_member_fns(class_) print class_.name for f in fns: print "\t" + f.name def get_class_from_name(global_ns, classname): classlist = global_ns.classes(name=classname) c = classlist[0] print "Found class with name:" + c.name return c (options, files) = parse_options() global_ns = run_parser(options.include, files) if(options.classname is not None): c = get_class_from_name(global_ns, options.classname) if(options.categorized): print_member_fns_categorized(c) else: print_member_fns(c) else: stats = get_wmc_stats(global_ns, files) print_stats(stats)