member.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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,
  22. mpp.api.IConfigurable,
  23. mpp.api.Child,
  24. mpp.api.MetricPluginMixin):
  25. def declare_configuration(self, parser):
  26. parser.add_option("--std.code.member.fields", "--scmf",
  27. action="store_true", default=False,
  28. help="Enables collection of number of data members / fields "
  29. "per classes, structs and interfaces [default: %default]")
  30. parser.add_option("--std.code.member.globals", "--scmg",
  31. action="store_true", default=False,
  32. help="Enables collection of number of global variables / fields "
  33. "per global regions and namespaces [default: %default]")
  34. parser.add_option("--std.code.member.classes", "--scmc",
  35. action="store_true", default=False,
  36. help="Enables collection of number of classes defined "
  37. "per any region [default: %default]")
  38. parser.add_option("--std.code.member.structs", "--scms",
  39. action="store_true", default=False,
  40. help="Enables collection of number of structs defined "
  41. "per any region [default: %default]")
  42. parser.add_option("--std.code.member.interfaces", "--scmi",
  43. action="store_true", default=False,
  44. help="Enables collection of number of interfaces defined "
  45. "per any region [default: %default]")
  46. parser.add_option("--std.code.member.methods", "--scmm",
  47. action="store_true", default=False,
  48. help="Enables collection of number of methods (functions) defined "
  49. "per any region [default: %default]")
  50. parser.add_option("--std.code.member.namespaces", "--scmn",
  51. action="store_true", default=False,
  52. help="Enables collection of number of namespaces defined "
  53. "globally and enclosed (sub-namespaces) [default: %default]")
  54. def configure(self, options):
  55. self.is_active_fields = options.__dict__['std.code.member.fields']
  56. self.is_active_globals = options.__dict__['std.code.member.globals']
  57. self.is_active_classes = options.__dict__['std.code.member.classes']
  58. self.is_active_structs = options.__dict__['std.code.member.structs']
  59. self.is_active_interfaces = options.__dict__['std.code.member.interfaces']
  60. self.is_active_methods = options.__dict__['std.code.member.methods']
  61. self.is_active_namespaces = options.__dict__['std.code.member.namspaces']
  62. def initialize(self):
  63. # counts fields and properties with default getter/setter
  64. pattern_to_search_cs = re.compile(
  65. r'''([_a-zA-Z][_a-zA-Z0-9]*\s+[_a-zA-Z][_a-zA-Z0-9])\s*([=;]|[{]\s*(public\s+|private\s+|protected\s+|internal\s+)?(get|set)\s*[;])''')
  66. pattern_to_search_cpp_java = re.compile(
  67. r'''([_a-zA-Z][_a-zA-Z0-9]*\s+[_a-zA-Z][_a-zA-Z0-9])\s*[=;]''')
  68. self.declare_metric(self.is_active_fields,
  69. self.Field('fields', int, _zero=True),
  70. {
  71. 'std.code.java': pattern_to_search_cpp_java,
  72. 'std.code.cpp': pattern_to_search_cpp_java,
  73. 'std.code.cs': pattern_to_search_cs,
  74. },
  75. marker_type_mask=mpp.api.Marker.T.CODE,
  76. region_type_mask=mpp.api.Region.T.CLASS |
  77. mpp.api.Region.T.STRUCT | mpp.api.Region.T.INTERFACE)
  78. self.declare_metric(self.is_active_fields,
  79. self.Field('globals', int, _zero=True),
  80. {
  81. 'std.code.java': pattern_to_search_cpp_java,
  82. 'std.code.cpp': pattern_to_search_cpp_java,
  83. 'std.code.cs': pattern_to_search_cs,
  84. },
  85. marker_type_mask=mpp.api.Marker.T.CODE,
  86. region_type_mask=mpp.api.Region.T.GLOBAL |
  87. mpp.api.Region.T.NAMESPACE)
  88. super(Plugin, self).initialize(fields=self.get_fields())
  89. if self.is_active() == True:
  90. self.subscribe_by_parents_interface(mpp.api.ICode)
  91. class NumbersCounter(mpp.api.MetricPluginMixin.IterIncrementCounter):
  92. def increment(self, match):
  93. if (match.group(0).startswith('const') or
  94. (self.plugin.is_active_numbers_simplier == True and
  95. match.group(0) in ['0', '1', '-1', '+1'])):
  96. return 0
  97. return 1