complexity.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. '''
  2. Created on 26/06/2012
  3. @author: konstaa
  4. '''
  5. import core.api
  6. import re
  7. class Plugin(core.api.Plugin, core.api.Child, core.api.IConfigurable):
  8. def declare_configuration(self, parser):
  9. parser.add_option("--std.code.complexity.on", action="store_true", default=False,
  10. help="Enables processing of complexity metrics: cyclomatic by McCabe [default: %default]")
  11. def configure(self, options):
  12. self.is_active = options.__dict__['std.code.complexity.on']
  13. def initialize(self):
  14. if self.is_active == True:
  15. namespace = self.get_plugin_loader().get_database_loader().create_namespace(self.get_name(), support_regions = True)
  16. namespace.add_field('cyclomatic', int)
  17. core.api.subscribe_by_parents_name('std.code.cpp', self, 'callback_cpp')
  18. # cyclomatic complexity pattern
  19. pattern = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
  20. def callback_cpp(self, parent, data):
  21. text = None
  22. for (ind, region) in enumerate(data.iterate_regions(filter_group=data.get_region_types().FUNCTION)):
  23. # cyclomatic complexity
  24. if ind == 0 and region.get_data(self.get_name(), 'cyclomatic') != None:
  25. return # data is available in first from cloned database, skip collection
  26. if text == None: # lazy loading for performance benefits
  27. text = data.get_content(exclude = data.get_marker_types().ALL_EXCEPT_CODE)
  28. count = 0
  29. start_pos = region.get_offset_begin()
  30. for sub_id in region.iterate_subregion_ids():
  31. # exclude sub regions, like enclosed classes
  32. count += len(self.pattern.findall(text, start_pos, data.get_region(sub_id).get_offset_begin()))
  33. start_pos = data.get_region(sub_id).get_offset_end()
  34. count += len(self.pattern.findall(text, start_pos, region.get_offset_end()))
  35. region.set_data(self.get_name(), 'cyclomatic', count)