complexity.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. core.api.subscribe_by_parents_name('std.code.java', self, 'callback_java')
  36. # cyclomatic complexity pattern
  37. # - C/C++
  38. pattern_cpp = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
  39. # - C#
  40. # supports Null-coalescing '??' and conditional '?:'
  41. pattern_cs = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(foreach)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?][?]?''')
  42. # - Java
  43. pattern_java = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
  44. def callback_cpp(self, parent, data, is_updated):
  45. self.callback_common(parent, data, is_updated, self.pattern_cpp)
  46. def callback_cs(self, parent, data, is_updated):
  47. self.callback_common(parent, data, is_updated, self.pattern_cs)
  48. def callback_java(self, parent, data, is_updated):
  49. self.callback_common(parent, data, is_updated, self.pattern_java)
  50. def callback_common(self, parent, data, is_updated, pattern):
  51. is_updated = is_updated or self.is_updated
  52. if is_updated == True:
  53. text = data.get_content(exclude = data.get_marker_types().ALL_EXCEPT_CODE)
  54. for region in data.iterate_regions(filter_group=data.get_region_types().FUNCTION):
  55. # cyclomatic complexity
  56. count = 0
  57. start_pos = region.get_offset_begin()
  58. for sub_id in region.iterate_subregion_ids():
  59. # exclude sub regions, like enclosed classes
  60. count += len(pattern.findall(text, start_pos, data.get_region(sub_id).get_offset_begin()))
  61. start_pos = data.get_region(sub_id).get_offset_end()
  62. count += len(pattern.findall(text, start_pos, region.get_offset_end()))
  63. region.set_data(self.get_name(), 'cyclomatic', count)