| @@ 5781-5842 (lines=62) @@ | ||
| 5778 | ' OR use pair directly OR if appropriate, construct a pair directly') |
|
| 5779 | ||
| 5780 | ||
| 5781 | def CheckRedundantVirtual(filename, clean_lines, linenum, error): |
|
| 5782 | """Check if line contains a redundant "virtual" function-specifier. |
|
| 5783 | ||
| 5784 | Args: |
|
| 5785 | filename: The name of the current file. |
|
| 5786 | clean_lines: A CleansedLines instance containing the file. |
|
| 5787 | linenum: The number of the line to check. |
|
| 5788 | error: The function to call with any errors found. |
|
| 5789 | """ |
|
| 5790 | # Look for "virtual" on current line. |
|
| 5791 | line = clean_lines.elided[linenum] |
|
| 5792 | virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line) |
|
| 5793 | if not virtual: return |
|
| 5794 | ||
| 5795 | # Ignore "virtual" keywords that are near access-specifiers. These |
|
| 5796 | # are only used in class base-specifier and do not apply to member |
|
| 5797 | # functions. |
|
| 5798 | if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or |
|
| 5799 | Match(r'^\s+(public|protected|private)\b', virtual.group(3))): |
|
| 5800 | return |
|
| 5801 | ||
| 5802 | # Ignore the "virtual" keyword from virtual base classes. Usually |
|
| 5803 | # there is a column on the same line in these cases (virtual base |
|
| 5804 | # classes are rare in google3 because multiple inheritance is rare). |
|
| 5805 | if Match(r'^.*[^:]:[^:].*$', line): return |
|
| 5806 | ||
| 5807 | # Look for the next opening parenthesis. This is the start of the |
|
| 5808 | # parameter list (possibly on the next line shortly after virtual). |
|
| 5809 | # TODO(unknown): doesn't work if there are virtual functions with |
|
| 5810 | # decltype() or other things that use parentheses, but csearch suggests |
|
| 5811 | # that this is rare. |
|
| 5812 | end_col = -1 |
|
| 5813 | end_line = -1 |
|
| 5814 | start_col = len(virtual.group(2)) |
|
| 5815 | for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())): |
|
| 5816 | line = clean_lines.elided[start_line][start_col:] |
|
| 5817 | parameter_list = Match(r'^([^(]*)\(', line) |
|
| 5818 | if parameter_list: |
|
| 5819 | # Match parentheses to find the end of the parameter list |
|
| 5820 | (_, end_line, end_col) = CloseExpression( |
|
| 5821 | clean_lines, start_line, start_col + len(parameter_list.group(1))) |
|
| 5822 | break |
|
| 5823 | start_col = 0 |
|
| 5824 | ||
| 5825 | if end_col < 0: |
|
| 5826 | return # Couldn't find end of parameter list, give up |
|
| 5827 | ||
| 5828 | # Look for "override" or "final" after the parameter list |
|
| 5829 | # (possibly on the next few lines). |
|
| 5830 | for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())): |
|
| 5831 | line = clean_lines.elided[i][end_col:] |
|
| 5832 | match = Search(r'\b(override|final)\b', line) |
|
| 5833 | if match: |
|
| 5834 | error(filename, linenum, 'readability/inheritance', 4, |
|
| 5835 | ('"virtual" is redundant since function is ' |
|
| 5836 | 'already declared as "%s"' % match.group(1))) |
|
| 5837 | ||
| 5838 | # Set end_col to check whole lines after we are done with the |
|
| 5839 | # first line. |
|
| 5840 | end_col = 0 |
|
| 5841 | if Search(r'[^\w]\s*$', line): |
|
| 5842 | break |
|
| 5843 | ||
| 5844 | ||
| 5845 | def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error): |
|
| @@ 5781-5842 (lines=62) @@ | ||
| 5778 | ' OR use pair directly OR if appropriate, construct a pair directly') |
|
| 5779 | ||
| 5780 | ||
| 5781 | def CheckRedundantVirtual(filename, clean_lines, linenum, error): |
|
| 5782 | """Check if line contains a redundant "virtual" function-specifier. |
|
| 5783 | ||
| 5784 | Args: |
|
| 5785 | filename: The name of the current file. |
|
| 5786 | clean_lines: A CleansedLines instance containing the file. |
|
| 5787 | linenum: The number of the line to check. |
|
| 5788 | error: The function to call with any errors found. |
|
| 5789 | """ |
|
| 5790 | # Look for "virtual" on current line. |
|
| 5791 | line = clean_lines.elided[linenum] |
|
| 5792 | virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line) |
|
| 5793 | if not virtual: return |
|
| 5794 | ||
| 5795 | # Ignore "virtual" keywords that are near access-specifiers. These |
|
| 5796 | # are only used in class base-specifier and do not apply to member |
|
| 5797 | # functions. |
|
| 5798 | if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or |
|
| 5799 | Match(r'^\s+(public|protected|private)\b', virtual.group(3))): |
|
| 5800 | return |
|
| 5801 | ||
| 5802 | # Ignore the "virtual" keyword from virtual base classes. Usually |
|
| 5803 | # there is a column on the same line in these cases (virtual base |
|
| 5804 | # classes are rare in google3 because multiple inheritance is rare). |
|
| 5805 | if Match(r'^.*[^:]:[^:].*$', line): return |
|
| 5806 | ||
| 5807 | # Look for the next opening parenthesis. This is the start of the |
|
| 5808 | # parameter list (possibly on the next line shortly after virtual). |
|
| 5809 | # TODO(unknown): doesn't work if there are virtual functions with |
|
| 5810 | # decltype() or other things that use parentheses, but csearch suggests |
|
| 5811 | # that this is rare. |
|
| 5812 | end_col = -1 |
|
| 5813 | end_line = -1 |
|
| 5814 | start_col = len(virtual.group(2)) |
|
| 5815 | for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())): |
|
| 5816 | line = clean_lines.elided[start_line][start_col:] |
|
| 5817 | parameter_list = Match(r'^([^(]*)\(', line) |
|
| 5818 | if parameter_list: |
|
| 5819 | # Match parentheses to find the end of the parameter list |
|
| 5820 | (_, end_line, end_col) = CloseExpression( |
|
| 5821 | clean_lines, start_line, start_col + len(parameter_list.group(1))) |
|
| 5822 | break |
|
| 5823 | start_col = 0 |
|
| 5824 | ||
| 5825 | if end_col < 0: |
|
| 5826 | return # Couldn't find end of parameter list, give up |
|
| 5827 | ||
| 5828 | # Look for "override" or "final" after the parameter list |
|
| 5829 | # (possibly on the next few lines). |
|
| 5830 | for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())): |
|
| 5831 | line = clean_lines.elided[i][end_col:] |
|
| 5832 | match = Search(r'\b(override|final)\b', line) |
|
| 5833 | if match: |
|
| 5834 | error(filename, linenum, 'readability/inheritance', 4, |
|
| 5835 | ('"virtual" is redundant since function is ' |
|
| 5836 | 'already declared as "%s"' % match.group(1))) |
|
| 5837 | ||
| 5838 | # Set end_col to check whole lines after we are done with the |
|
| 5839 | # first line. |
|
| 5840 | end_col = 0 |
|
| 5841 | if Search(r'[^\w]\s*$', line): |
|
| 5842 | break |
|
| 5843 | ||
| 5844 | ||
| 5845 | def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error): |
|