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)
| 6176 | |
| 6177 | |
| 6178 | def CheckRedundantVirtual(filename, clean_lines, linenum, error): |
| 6179 | """Check if line contains a redundant "virtual" function-specifier. |
| 6180 | |
| 6181 | Args: |
| 6182 | filename: The name of the current file. |
| 6183 | clean_lines: A CleansedLines instance containing the file. |
| 6184 | linenum: The number of the line to check. |
| 6185 | error: The function to call with any errors found. |
| 6186 | """ |
| 6187 | # Look for "virtual" on current line. |
| 6188 | line = clean_lines.elided[linenum] |
| 6189 | virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line) |
| 6190 | if not virtual: return |
| 6191 | |
| 6192 | # Ignore "virtual" keywords that are near access-specifiers. These |
| 6193 | # are only used in class base-specifier and do not apply to member |
| 6194 | # functions. |
| 6195 | if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or |
| 6196 | Match(r'^\s+(public|protected|private)\b', virtual.group(3))): |
| 6197 | return |
| 6198 | |
| 6199 | # Ignore the "virtual" keyword from virtual base classes. Usually |
| 6200 | # there is a column on the same line in these cases (virtual base |
| 6201 | # classes are rare in google3 because multiple inheritance is rare). |
| 6202 | if Match(r'^.*[^:]:[^:].*$', line): return |
| 6203 | |
| 6204 | # Look for the next opening parenthesis. This is the start of the |
| 6205 | # parameter list (possibly on the next line shortly after virtual). |
| 6206 | # TODO(unknown): doesn't work if there are virtual functions with |
| 6207 | # decltype() or other things that use parentheses, but csearch suggests |
| 6208 | # that this is rare. |
| 6209 | end_col = -1 |
| 6210 | end_line = -1 |
| 6211 | start_col = len(virtual.group(2)) |
| 6212 | for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())): |
| 6213 | line = clean_lines.elided[start_line][start_col:] |
| 6214 | parameter_list = Match(r'^([^(]*)\(', line) |
| 6215 | if parameter_list: |
| 6216 | # Match parentheses to find the end of the parameter list |
| 6217 | (_, end_line, end_col) = CloseExpression( |
| 6218 | clean_lines, start_line, start_col + len(parameter_list.group(1))) |
| 6219 | break |
| 6220 | start_col = 0 |
| 6221 | |
| 6222 | if end_col < 0: |
| 6223 | return # Couldn't find end of parameter list, give up |
| 6224 | |
| 6225 | # Look for "override" or "final" after the parameter list |
| 6226 | # (possibly on the next few lines). |
| 6227 | for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())): |
| 6228 | line = clean_lines.elided[i][end_col:] |
| 6229 | match = Search(r'\b(override|final)\b', line) |
| 6230 | if match: |
| 6231 | error(filename, linenum, 'readability/inheritance', 4, |
| 6232 | ('"virtual" is redundant since function is ' |
| 6233 | 'already declared as "%s"' % match.group(1))) |
| 6234 | |
| 6235 | # Set end_col to check whole lines after we are done with the |
no test coverage detected