complexity.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #
  2. # Metrix++, Copyright 2009-2019, Metrix++ Project
  3. # Link: https://github.com/metrixplusplus/metrixplusplus
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. import mpp.api
  8. import re
  9. class Plugin(mpp.api.Plugin, mpp.api.MetricPluginMixin, mpp.api.Child, mpp.api.IConfigurable):
  10. def declare_configuration(self, parser):
  11. parser.add_option("--std.code.complexity.cyclomatic", "--sccc", action="store_true", default=False,
  12. help="Enables collection of cyclomatic complexity metric (McCabe) [default: %default]")
  13. parser.add_option("--std.code.complexity.maxindent", "--sccmi", action="store_true", default=False,
  14. help="Enables collection of maximum indent level metric [default: %default]")
  15. def configure(self, options):
  16. self.is_active_cyclomatic = options.__dict__['std.code.complexity.cyclomatic']
  17. self.is_active_maxindent = options.__dict__['std.code.complexity.maxindent']
  18. # cyclomatic complexity pattern
  19. # - C/C++
  20. pattern_cpp = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
  21. # - C#
  22. # supports Null-coalescing '??' and conditional '?:'
  23. pattern_cs = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(foreach)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?][?]?''')
  24. # - Java
  25. pattern_java = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
  26. pattern_indent = re.compile(r'''[}{]''')
  27. def initialize(self):
  28. self.declare_metric(self.is_active_cyclomatic,
  29. self.Field('cyclomatic', int),
  30. {
  31. 'std.code.cpp': self.pattern_cpp,
  32. 'std.code.cs': self.pattern_cs,
  33. 'std.code.java': self.pattern_java
  34. },
  35. marker_type_mask=mpp.api.Marker.T.CODE,
  36. region_type_mask=mpp.api.Region.T.FUNCTION)
  37. self.declare_metric(self.is_active_maxindent,
  38. self.Field('maxindent', int),
  39. {
  40. 'std.code.cpp': (self.pattern_indent, self.MaxIndentCounter),
  41. 'std.code.cs': (self.pattern_indent, self.MaxIndentCounter),
  42. 'std.code.java': (self.pattern_indent, self.MaxIndentCounter),
  43. },
  44. marker_type_mask=mpp.api.Marker.T.CODE,
  45. region_type_mask=mpp.api.Region.T.FUNCTION)
  46. super(Plugin, self).initialize(fields=self.get_fields())
  47. if self.is_active() == True:
  48. self.subscribe_by_parents_names([
  49. 'std.code.cpp',
  50. 'std.code.cs',
  51. 'std.code.java'
  52. ])
  53. class MaxIndentCounter(mpp.api.MetricPluginMixin.IterAssignCounter):
  54. def __init__(self, *args, **kwargs):
  55. super(Plugin.MaxIndentCounter, self).__init__(*args, **kwargs)
  56. self.current_level = 0
  57. def assign(self, match):
  58. result = self.result
  59. if match.group(0) == '{':
  60. self.current_level += 1
  61. if self.current_level > self.result:
  62. result = self.current_level
  63. elif match.group(0) == '}':
  64. self.current_level -= 1
  65. else:
  66. assert False
  67. return result