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)
| 7063 | |
| 7064 | |
| 7065 | def CheckRedundantVirtual(filename, clean_lines, linenum, error): |
| 7066 | """Check if line contains a redundant "virtual" function-specifier. |
| 7067 | |
| 7068 | Args: |
| 7069 | filename: The name of the current file. |
| 7070 | clean_lines: A CleansedLines instance containing the file. |
| 7071 | linenum: The number of the line to check. |
| 7072 | error: The function to call with any errors found. |
| 7073 | """ |
| 7074 | # Look for "virtual" on current line. |
| 7075 | line = clean_lines.elided[linenum] |
| 7076 | virtual = re.match(r"^(.*)(\bvirtual\b)(.*)$", line) |
| 7077 | if not virtual: |
| 7078 | return |
| 7079 | |
| 7080 | # Ignore "virtual" keywords that are near access-specifiers. These |
| 7081 | # are only used in class base-specifier and do not apply to member |
| 7082 | # functions. |
| 7083 | if re.search(r"\b(public|protected|private)\s+$", virtual.group(1)) or re.match( |
| 7084 | r"^\s+(public|protected|private)\b", virtual.group(3) |
| 7085 | ): |
| 7086 | return |
| 7087 | |
| 7088 | # Ignore the "virtual" keyword from virtual base classes. Usually |
| 7089 | # there is a column on the same line in these cases (virtual base |
| 7090 | # classes are rare in google3 because multiple inheritance is rare). |
| 7091 | if re.match(r"^.*[^:]:[^:].*$", line): |
| 7092 | return |
| 7093 | |
| 7094 | # Look for the next opening parenthesis. This is the start of the |
| 7095 | # parameter list (possibly on the next line shortly after virtual). |
| 7096 | # TODO(google): doesn't work if there are virtual functions with |
| 7097 | # decltype() or other things that use parentheses, but csearch suggests |
| 7098 | # that this is rare. |
| 7099 | end_col = -1 |
| 7100 | end_line = -1 |
| 7101 | start_col = len(virtual.group(2)) |
| 7102 | for start_line in range(linenum, min(linenum + 3, clean_lines.NumLines())): |
| 7103 | line = clean_lines.elided[start_line][start_col:] |
| 7104 | parameter_list = re.match(r"^([^(]*)\(", line) |
| 7105 | if parameter_list: |
| 7106 | # Match parentheses to find the end of the parameter list |
| 7107 | (_, end_line, end_col) = CloseExpression( |
| 7108 | clean_lines, start_line, start_col + len(parameter_list.group(1)) |
| 7109 | ) |
| 7110 | break |
| 7111 | start_col = 0 |
| 7112 | |
| 7113 | if end_col < 0: |
| 7114 | return # Couldn't find end of parameter list, give up |
| 7115 | |
| 7116 | # Look for "override" or "final" after the parameter list |
| 7117 | # (possibly on the next few lines). |
| 7118 | for i in range(end_line, min(end_line + 3, clean_lines.NumLines())): |
| 7119 | line = clean_lines.elided[i][end_col:] |
| 7120 | match = re.search(r"\b(override|final)\b", line) |
| 7121 | if match: |
| 7122 | error( |
no test coverage detected