complexity.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import core.api
  20. import re
  21. class Plugin(core.api.Plugin, core.api.Child, core.api.IConfigurable):
  22. def declare_configuration(self, parser):
  23. parser.add_option("--std.code.complexity.on", action="store_true", default=False,
  24. help="Enables collection of complexity metrics: cyclomatic by McCabe [default: %default]")
  25. def configure(self, options):
  26. self.is_active = options.__dict__['std.code.complexity.on']
  27. def initialize(self):
  28. if self.is_active == True:
  29. # trigger version property set
  30. core.api.Plugin.initialize(self)
  31. namespace = self.get_plugin_loader().get_database_loader().create_namespace(self.get_name(), support_regions = True)
  32. namespace.add_field('cyclomatic', int)
  33. core.api.subscribe_by_parents_name('std.code.cpp', self, 'callback_cpp')
  34. core.api.subscribe_by_parents_name('std.code.cs', self, 'callback_cs')
  35. # cyclomatic complexity pattern
  36. # - C/C++
  37. pattern_cpp = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
  38. # - C#
  39. # supports Null-coalescing '??' and conditional '?:'
  40. pattern_cs = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(foreach)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?][?]?''')
  41. def callback_cpp(self, parent, data, is_updated):
  42. self.callback_common(parent, data, is_updated, self.pattern_cpp)
  43. def callback_cs(self, parent, data, is_updated):
  44. self.callback_common(parent, data, is_updated, self.pattern_cs)
  45. def callback_common(self, parent, data, is_updated, pattern):
  46. is_updated = is_updated or self.is_updated
  47. if is_updated == True:
  48. text = data.get_content(exclude = data.get_marker_types().ALL_EXCEPT_CODE)
  49. for region in data.iterate_regions(filter_group=data.get_region_types().FUNCTION):
  50. # cyclomatic complexity
  51. count = 0
  52. start_pos = region.get_offset_begin()
  53. for sub_id in region.iterate_subregion_ids():
  54. # exclude sub regions, like enclosed classes
  55. count += len(pattern.findall(text, start_pos, data.get_region(sub_id).get_offset_begin()))
  56. start_pos = data.get_region(sub_id).get_offset_end()
  57. count += len(pattern.findall(text, start_pos, region.get_offset_end()))
  58. region.set_data(self.get_name(), 'cyclomatic', count)