Checks the use of CHECK and EXPECT macros. 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)
| 5230 | |
| 5231 | |
| 5232 | def CheckCheck(filename, clean_lines, linenum, error): |
| 5233 | """Checks the use of CHECK and EXPECT macros. |
| 5234 | |
| 5235 | Args: |
| 5236 | filename: The name of the current file. |
| 5237 | clean_lines: A CleansedLines instance containing the file. |
| 5238 | linenum: The number of the line to check. |
| 5239 | error: The function to call with any errors found. |
| 5240 | """ |
| 5241 | |
| 5242 | # Decide the set of replacement macros that should be suggested |
| 5243 | lines = clean_lines.elided |
| 5244 | (check_macro, start_pos) = FindCheckMacro(lines[linenum]) |
| 5245 | if not check_macro: |
| 5246 | return |
| 5247 | |
| 5248 | # Find end of the boolean expression by matching parentheses |
| 5249 | (last_line, end_line, end_pos) = CloseExpression(clean_lines, linenum, start_pos) |
| 5250 | if end_pos < 0: |
| 5251 | return |
| 5252 | |
| 5253 | # If the check macro is followed by something other than a |
| 5254 | # semicolon, assume users will log their own custom error messages |
| 5255 | # and don't suggest any replacements. |
| 5256 | if not re.match(r"\s*;", last_line[end_pos:]): |
| 5257 | return |
| 5258 | |
| 5259 | if linenum == end_line: |
| 5260 | expression = lines[linenum][start_pos + 1 : end_pos - 1] |
| 5261 | else: |
| 5262 | expression = lines[linenum][start_pos + 1 :] |
| 5263 | for i in range(linenum + 1, end_line): |
| 5264 | expression += lines[i] |
| 5265 | expression += last_line[0 : end_pos - 1] |
| 5266 | |
| 5267 | # Parse expression so that we can take parentheses into account. |
| 5268 | # This avoids false positives for inputs like "CHECK((a < 4) == b)", |
| 5269 | # which is not replaceable by CHECK_LE. |
| 5270 | lhs = "" |
| 5271 | rhs = "" |
| 5272 | operator = None |
| 5273 | while expression: |
| 5274 | matched = re.match( |
| 5275 | r"^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||" |
| 5276 | r"==|!=|>=|>|<=|<|\()(.*)$", |
| 5277 | expression, |
| 5278 | ) |
| 5279 | if matched: |
| 5280 | token = matched.group(1) |
| 5281 | if token == "(": |
| 5282 | # Parenthesized operand |
| 5283 | expression = matched.group(2) |
| 5284 | (end, _) = FindEndOfExpressionInLine(expression, 0, ["("]) |
| 5285 | if end < 0: |
| 5286 | return # Unmatched parenthesis |
| 5287 | lhs += "(" + expression[0:end] |
| 5288 | expression = expression[end:] |
| 5289 | elif token in ("&&", "||"): |
no test coverage detected