complexity.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. parser.add_option("--std.code.complexity.maxindent", "--sccmi", action="store_true", default=False,
  26. help="Enables collection of maximum indent level metric [default: %default]")
  27. def configure(self, options):
  28. self.is_active_cyclomatic = options.__dict__['std.code.complexity.cyclomatic']
  29. self.is_active_maxindent = options.__dict__['std.code.complexity.maxindent']
  30. # cyclomatic complexity pattern
  31. # - C/C++
  32. pattern_cpp = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
  33. # - C#
  34. # supports Null-coalescing '??' and conditional '?:'
  35. pattern_cs = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(foreach)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?][?]?''')
  36. # - Java
  37. pattern_java = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
  38. pattern_indent = re.compile(r'''[}{]''')
  39. def initialize(self):
  40. self.declare_metric(self.is_active_cyclomatic,
  41. self.Field('cyclomatic', int),
  42. {
  43. 'std.code.cpp': self.pattern_cpp,
  44. 'std.code.cs': self.pattern_cs,
  45. 'std.code.java': self.pattern_java
  46. },
  47. marker_type_mask=mpp.api.Marker.T.CODE,
  48. region_type_mask=mpp.api.Region.T.FUNCTION)
  49. self.declare_metric(self.is_active_maxindent,
  50. self.Field('maxindent', int),
  51. {
  52. 'std.code.cpp': (self.pattern_indent, self.MaxIndentCounter),
  53. 'std.code.cs': (self.pattern_indent, self.MaxIndentCounter),
  54. 'std.code.java': (self.pattern_indent, self.MaxIndentCounter),
  55. },
  56. marker_type_mask=mpp.api.Marker.T.CODE,
  57. region_type_mask=mpp.api.Region.T.FUNCTION)
  58. super(Plugin, self).initialize(fields=self.get_fields())
  59. if self.is_active() == True:
  60. self.subscribe_by_parents_names([
  61. 'std.code.cpp',
  62. 'std.code.cs',
  63. 'std.code.java'
  64. ])
  65. class MaxIndentCounter(mpp.api.MetricPluginMixin.IterAssignCounter):
  66. def __init__(self, plugin, alias, data, region):
  67. super(Plugin.MaxIndentCounter, self).__init__(plugin, alias, data, region)
  68. self.current_level = 0
  69. def assign(self, match):
  70. result = self.result
  71. if match.group(0) == '{':
  72. self.current_level += 1
  73. if self.current_level > self.result:
  74. result = self.current_level
  75. elif match.group(0) == '}':
  76. self.current_level -= 1
  77. else:
  78. assert False
  79. return result