complexity.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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,
  53. 'std.code.cs': self.pattern_indent,
  54. 'std.code.java': self.pattern_indent,
  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. def _maxindent_count_initialize(self, alias, data, region):
  66. return (0, {'cur_level': 0})
  67. def _maxindent_count(self, alias, data, region, marker, match, count, counter_data):
  68. if match.group(0) == '{':
  69. counter_data['cur_level'] += 1
  70. if counter_data['cur_level'] > count:
  71. count = counter_data['cur_level']
  72. elif match.group(0) == '}':
  73. counter_data['cur_level'] -= 1
  74. else:
  75. assert False
  76. return count