complexity.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. 'cpp': self.pattern_cpp,
  44. 'cs': self.pattern_cs,
  45. '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. 'cpp': self.pattern_indent,
  53. 'cs': self.pattern_indent,
  54. 'java': self.pattern_indent,
  55. },
  56. marker_type_mask=mpp.api.Marker.T.CODE,
  57. # TODO shall scan all regions?, it is likely actual to functions
  58. region_type_mask=mpp.api.Region.T.ANY)
  59. super(Plugin, self).initialize(fields=self.get_fields())
  60. if self.is_active() == True:
  61. self.subscribe_by_parents_name('std.code.cpp', 'callback_cpp')
  62. self.subscribe_by_parents_name('std.code.cs', 'callback_cs')
  63. self.subscribe_by_parents_name('std.code.java', 'callback_java')
  64. def callback_cpp(self, parent, data, is_updated):
  65. self.callback_common(parent, data, is_updated, 'cpp')
  66. def callback_cs(self, parent, data, is_updated):
  67. self.callback_common(parent, data, is_updated, 'cs')
  68. def callback_java(self, parent, data, is_updated):
  69. self.callback_common(parent, data, is_updated, 'java')
  70. def callback_common(self, parent, data, is_updated, alias):
  71. is_updated = is_updated or self.is_updated
  72. if is_updated == True:
  73. self.count_if_active('cyclomatic', data, alias=alias)
  74. self.count_if_active('maxindent', data, alias=alias)
  75. def _maxindent_count_initialize(self, data, alias, region):
  76. return (0, {'cur_level': 0})
  77. def _maxindent_count(self, data, alias, text, begin, end, m, count, counter_data, region, marker):
  78. if m.group(0) == '{':
  79. counter_data['cur_level'] += 1
  80. if counter_data['cur_level'] > count:
  81. count = counter_data['cur_level']
  82. elif m.group(0) == '}':
  83. counter_data['cur_level'] -= 1
  84. else:
  85. assert False
  86. return count