suppress.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 mpp.cout
  21. import re
  22. class Plugin(mpp.api.Plugin, mpp.api.Child, mpp.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. mpp.api.Plugin.initialize(self, fields=fields)
  40. # - init per file table
  41. mpp.api.Plugin.initialize(self,
  42. namespace = self.get_name() + '.file',
  43. support_regions = False,
  44. fields=fields)
  45. if len(fields) != 0:
  46. self.subscribe_by_parents_interface(mpp.api.ICode)
  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 = mpp.api.Marker.T.COMMENT,
  61. region_id = region.get_id(),
  62. exclude_children = True):
  63. if last_comment_end != None and len(text[last_comment_end:marker.get_offset_begin()].strip()) > 0:
  64. # check continues comment blocks
  65. # stop searching, if this comment block is separated from the last by non-blank string
  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. db_loader = self.get_plugin('mpp.dbf').get_loader()
  72. namespace = db_loader.get_namespace(namespace_name)
  73. if namespace == None or namespace.check_field(field) == False:
  74. mpp.cout.notify(data.get_path(), region.get_cursor(),
  75. mpp.cout.SEVERITY_WARNING,
  76. "Suppressed metric '" + namespace_name + ":" + field +
  77. "' is not being collected",
  78. [("Metric name", namespace_name + ":" + field),
  79. ("Region name", region.get_name())])
  80. continue
  81. if namespace.are_regions_supported() == False:
  82. if region.get_id() != 1:
  83. mpp.cout.notify(data.get_path(), region.get_cursor(),
  84. mpp.cout.SEVERITY_WARNING,
  85. "Suppressed metric '" + namespace_name + ":" + field +
  86. "' is attributed to a file, not a region. "
  87. "Remove it or move to the beginning of the file.",
  88. [("Metric name", namespace_name + ":" + field),
  89. ("Region name", region.get_name())])
  90. continue
  91. if m in file_list_text:
  92. mpp.cout.notify(data.get_path(), region.get_cursor(),
  93. mpp.cout.SEVERITY_WARNING,
  94. "Duplicate suppression of the metric '" +
  95. namespace_name + ":" + field + "'",
  96. [("Metric name", namespace_name + ":" + field),
  97. ("Region name", None)])
  98. continue
  99. file_count += 1
  100. file_list_text.append(m)
  101. continue
  102. if m in list_text:
  103. mpp.cout.notify(data.get_path(), region.get_cursor(),
  104. mpp.cout.SEVERITY_WARNING,
  105. "Duplicate suppression of the metric '" +
  106. namespace_name + ":" + field + "'",
  107. [("Metric name", namespace_name + ":" + field),
  108. ("Region name", region.get_name())])
  109. continue
  110. count += 1
  111. list_text.append(m)
  112. continue
  113. if count > 0:
  114. region.set_data(self.get_name(), 'count', count)
  115. region.set_data(self.get_name(), 'list', '[' + ']['.join(list_text) + ']')
  116. if file_count > 0:
  117. data.set_data(self.get_name() + '.file', 'count', file_count)
  118. data.set_data(self.get_name() + '.file', 'list', '[' + ']['.join(file_list_text) + ']')