Class to track all error suppressions for cpplint
| 967 | |
| 968 | |
| 969 | class ErrorSuppressions: |
| 970 | """Class to track all error suppressions for cpplint""" |
| 971 | |
| 972 | class LineRange: |
| 973 | """Class to represent a range of line numbers for which an error is suppressed""" |
| 974 | |
| 975 | def __init__(self, begin, end): |
| 976 | self.begin = begin |
| 977 | self.end = end |
| 978 | |
| 979 | def __str__(self): |
| 980 | return f"[{self.begin}-{self.end}]" |
| 981 | |
| 982 | def __contains__(self, obj): |
| 983 | return self.begin <= obj <= self.end |
| 984 | |
| 985 | def ContainsRange(self, other): |
| 986 | return self.begin <= other.begin and self.end >= other.end |
| 987 | |
| 988 | def __init__(self): |
| 989 | self._suppressions = collections.defaultdict(list) |
| 990 | self._open_block_suppression = None |
| 991 | |
| 992 | def _AddSuppression(self, category, line_range): |
| 993 | suppressed = self._suppressions[category] |
| 994 | if not (suppressed and suppressed[-1].ContainsRange(line_range)): |
| 995 | suppressed.append(line_range) |
| 996 | |
| 997 | def GetOpenBlockStart(self): |
| 998 | """:return: The start of the current open block or `-1` if there is not an open block""" |
| 999 | return self._open_block_suppression.begin if self._open_block_suppression else -1 |
| 1000 | |
| 1001 | def AddGlobalSuppression(self, category): |
| 1002 | """Add a suppression for `category` which is suppressed for the whole file""" |
| 1003 | self._AddSuppression(category, self.LineRange(0, math.inf)) |
| 1004 | |
| 1005 | def AddLineSuppression(self, category, linenum): |
| 1006 | """Add a suppression for `category` which is suppressed only on `linenum`""" |
| 1007 | self._AddSuppression(category, self.LineRange(linenum, linenum)) |
| 1008 | |
| 1009 | def StartBlockSuppression(self, category, linenum): |
| 1010 | """Start a suppression block for `category` on `linenum`. inclusive""" |
| 1011 | if self._open_block_suppression is None: |
| 1012 | self._open_block_suppression = self.LineRange(linenum, math.inf) |
| 1013 | self._AddSuppression(category, self._open_block_suppression) |
| 1014 | |
| 1015 | def EndBlockSuppression(self, linenum): |
| 1016 | """End the current block suppression on `linenum`. inclusive""" |
| 1017 | if self._open_block_suppression: |
| 1018 | self._open_block_suppression.end = linenum |
| 1019 | self._open_block_suppression = None |
| 1020 | |
| 1021 | def IsSuppressed(self, category, linenum): |
| 1022 | """:return: `True` if `category` is suppressed for `linenum`""" |
| 1023 | suppressed = self._suppressions[category] + self._suppressions[None] |
| 1024 | return any(linenum in lr for lr in suppressed) |
| 1025 | |
| 1026 | def HasOpenBlock(self): |