complexity.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 mpp.api
  20. import re
  21. class Plugin(mpp.api.Plugin, mpp.api.MetricPluginMixin, mpp.api.Child, mpp.api.IConfigurable):
  22. def declare_configuration(self, parser):
  23. parser.add_option("--std.code.complexity.cyclomatic", "--sccc", action="store_true", default=False,
  24. help="Enables collection of cyclomatic complexity metric (McCabe) [default: %default]")
  25. def configure(self, options):
  26. self.is_active_cyclomatic = options.__dict__['std.code.complexity.cyclomatic']
  27. # cyclomatic complexity pattern
  28. # - C/C++
  29. pattern_cpp = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
  30. # - C#
  31. # supports Null-coalescing '??' and conditional '?:'
  32. pattern_cs = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(foreach)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?][?]?''')
  33. # - Java
  34. pattern_java = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
  35. def initialize(self):
  36. self.declare_metric(self.is_active_cyclomatic,
  37. self.Field('cyclomatic', int),
  38. {
  39. 'cpp': self.pattern_cpp,
  40. 'cs': self.pattern_cs,
  41. 'java': self.pattern_java
  42. },
  43. marker_type_mask=mpp.api.Marker.T.CODE,
  44. region_type_mask=mpp.api.Region.T.FUNCTION)
  45. super(Plugin, self).initialize(fields=self.get_fields())
  46. if self.is_active() == True:
  47. self.subscribe_by_parents_name('std.code.cpp', 'callback_cpp')
  48. self.subscribe_by_parents_name('std.code.cs', 'callback_cs')
  49. self.subscribe_by_parents_name('std.code.java', 'callback_java')
  50. def callback_cpp(self, parent, data, is_updated):
  51. self.callback_common(parent, data, is_updated, 'cpp')
  52. def callback_cs(self, parent, data, is_updated):
  53. self.callback_common(parent, data, is_updated, 'cs')
  54. def callback_java(self, parent, data, is_updated):
  55. self.callback_common(parent, data, is_updated, 'java')
  56. def callback_common(self, parent, data, is_updated, alias):
  57. is_updated = is_updated or self.is_updated
  58. if is_updated == True:
  59. self.count_if_active('cyclomatic', data, alias=alias)