suppress.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 core.api
  20. import core.cout
  21. import re
  22. class Plugin(core.api.Plugin, core.api.Child, core.api.IConfigurable):
  23. def declare_configuration(self, parser):
  24. parser.add_option("--std.suppress", "--ss", action="store_true", default=False,
  25. help="If set (True), suppression markers are collected from comments in code. "
  26. "Suppressions are used by post-processing tools, like limit, to remove false-positive warnings. "
  27. "Suppressions should be in the first comment block of a region (function/class/interface). "
  28. "Format of suppressions: 'metrix++: suppress metric-name'. "
  29. "For example: 'metrix++: suppress std.code.complexity:cyclomatic'. "
  30. " [default: %default]")
  31. def configure(self, options):
  32. self.is_active = options.__dict__['std.suppress']
  33. def initialize(self):
  34. fields = []
  35. if self.is_active == True:
  36. fields.append(self.Field('count', int, non_zero=True))
  37. fields.append(self.Field('list', str))
  38. # - init per regions table
  39. core.api.Plugin.initialize(self, fields=fields)
  40. # - init per file table
  41. core.api.Plugin.initialize(self,
  42. namespace = self.get_name() + '.file',
  43. support_regions = False,
  44. fields=fields)
  45. if len(fields) != 0:
  46. core.api.subscribe_by_parents_interface(core.api.ICode, self, 'callback')
  47. # suppress pattern
  48. pattern = re.compile(r'''metrix[+][+][:][ \t]+suppress[ \t]+([^ \t\r\n\*]+)''')
  49. def callback(self, parent, data, is_updated):
  50. is_updated = is_updated or self.is_updated
  51. if is_updated == True:
  52. text = data.get_content()
  53. file_count = 0
  54. file_list_text = []
  55. for region in data.iterate_regions():
  56. count = 0
  57. list_text = []
  58. last_comment_end = None
  59. for marker in data.iterate_markers(
  60. filter_group = data.get_marker_types().COMMENT,
  61. region_id = region.get_id(),
  62. exclude_children = True):
  63. if last_comment_end != None and marker.get_offset_begin() > last_comment_end + 2:
  64. # check continues comment blocks
  65. # stop searching, if this comment block is far from the last
  66. break
  67. last_comment_end = marker.get_offset_end()
  68. matches = self.pattern.findall(text, marker.get_offset_begin(), marker.get_offset_end())
  69. for m in matches:
  70. namespace_name, field = m.split(':')
  71. namespace = self.get_plugin_loader().get_database_loader().get_namespace(namespace_name)
  72. if namespace == None or namespace.get_field_packager(field) == None:
  73. core.cout.notify(data.get_path(), region.get_cursor(),
  74. core.cout.SEVERITY_WARNING,
  75. "Suppressed metric '" + namespace_name + ":" + field +
  76. "' is not being collected",
  77. [("Metric name", namespace_name + ":" + field),
  78. ("Region name", region.get_name())])
  79. continue
  80. if namespace.are_regions_supported() == False:
  81. if region.get_id() != 1:
  82. core.cout.notify(data.get_path(), region.get_cursor(),
  83. core.cout.SEVERITY_WARNING,
  84. "Suppressed metric '" + namespace_name + ":" + field +
  85. "' is attributed to a file, not a region. "
  86. "Remove it or move to the beginning of the file.",
  87. [("Metric name", namespace_name + ":" + field),
  88. ("Region name", region.get_name())])
  89. continue
  90. if m in file_list_text:
  91. core.cout.notify(data.get_path(), region.get_cursor(),
  92. core.cout.SEVERITY_WARNING,
  93. "Duplicate suppression of the metric '" +
  94. namespace_name + ":" + field + "'",
  95. [("Metric name", namespace_name + ":" + field),
  96. ("Region name", None)])
  97. continue
  98. file_count += 1
  99. file_list_text.append(m)
  100. continue
  101. if m in list_text:
  102. core.cout.notify(data.get_path(), region.get_cursor(),
  103. core.cout.SEVERITY_WARNING,
  104. "Duplicate suppression of the metric '" +
  105. namespace_name + ":" + field + "'",
  106. [("Metric name", namespace_name + ":" + field),
  107. ("Region name", region.get_name())])
  108. continue
  109. count += 1
  110. list_text.append(m)
  111. continue
  112. if count > 0:
  113. region.set_data(self.get_name(), 'count', count)
  114. region.set_data(self.get_name(), 'list', '[' + ']['.join(list_text) + ']')
  115. if file_count > 0:
  116. data.set_data(self.get_name() + '.file', 'count', file_count)
  117. data.set_data(self.get_name() + '.file', 'list', '[' + ']['.join(file_list_text) + ']')