Check if line contains a redundant "virtual" function-specifier. Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. error: The function to call with any errors found.
(filename, clean_lines, linenum, error)
| 5619 | |
| 5620 | |
| 5621 | def CheckRedundantVirtual(filename, clean_lines, linenum, error): |
| 5622 | """Check if line contains a redundant "virtual" function-specifier. |
| 5623 | |
| 5624 | Args: |
| 5625 | filename: The name of the current file. |
| 5626 | clean_lines: A CleansedLines instance containing the file. |
| 5627 | linenum: The number of the line to check. |
| 5628 | error: The function to call with any errors found. |
| 5629 | """ |
| 5630 | # Look for "virtual" on current line. |
| 5631 | line = clean_lines.elided[linenum] |
| 5632 | virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line) |
| 5633 | if not virtual: return |
| 5634 | |
| 5635 | # Ignore "virtual" keywords that are near access-specifiers. These |
| 5636 | # are only used in class base-specifier and do not apply to member |
| 5637 | # functions. |
| 5638 | if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or |
| 5639 | Match(r'^\s+(public|protected|private)\b', virtual.group(3))): |
| 5640 | return |
| 5641 | |
| 5642 | # Ignore the "virtual" keyword from virtual base classes. Usually |
| 5643 | # there is a column on the same line in these cases (virtual base |
| 5644 | # classes are rare in google3 because multiple inheritance is rare). |
| 5645 | if Match(r'^.*[^:]:[^:].*$', line): return |
| 5646 | |
| 5647 | # Look for the next opening parenthesis. This is the start of the |
| 5648 | # parameter list (possibly on the next line shortly after virtual). |
| 5649 | # TODO(unknown): doesn't work if there are virtual functions with |
| 5650 | # decltype() or other things that use parentheses, but csearch suggests |
| 5651 | # that this is rare. |
| 5652 | end_col = -1 |
| 5653 | end_line = -1 |
| 5654 | start_col = len(virtual.group(2)) |
| 5655 | for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())): |
| 5656 | line = clean_lines.elided[start_line][start_col:] |
| 5657 | parameter_list = Match(r'^([^(]*)\(', line) |
| 5658 | if parameter_list: |
| 5659 | # Match parentheses to find the end of the parameter list |
| 5660 | (_, end_line, end_col) = CloseExpression( |
| 5661 | clean_lines, start_line, start_col + len(parameter_list.group(1))) |
| 5662 | break |
| 5663 | start_col = 0 |
| 5664 | |
| 5665 | if end_col < 0: |
| 5666 | return # Couldn't find end of parameter list, give up |
| 5667 | |
| 5668 | # Look for "override" or "final" after the parameter list |
| 5669 | # (possibly on the next few lines). |
| 5670 | for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())): |
| 5671 | line = clean_lines.elided[i][end_col:] |
| 5672 | match = Search(r'\b(override|final)\b', line) |
| 5673 | if match: |
| 5674 | error(filename, linenum, 'readability/inheritance', 4, |
| 5675 | ('"virtual" is redundant since function is ' |
| 5676 | 'already declared as "%s"' % match.group(1))) |
| 5677 | |
| 5678 | # Set end_col to check whole lines after we are done with the |
no test coverage detected