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)
| 4623 | |
| 4624 | |
| 4625 | def CheckCheck(filename, clean_lines, linenum, error): |
| 4626 | """Checks the use of CHECK and EXPECT macros. |
| 4627 | |
| 4628 | Args: |
| 4629 | filename: The name of the current file. |
| 4630 | clean_lines: A CleansedLines instance containing the file. |
| 4631 | linenum: The number of the line to check. |
| 4632 | error: The function to call with any errors found. |
| 4633 | """ |
| 4634 | |
| 4635 | # Decide the set of replacement macros that should be suggested |
| 4636 | lines = clean_lines.elided |
| 4637 | (check_macro, start_pos) = FindCheckMacro(lines[linenum]) |
| 4638 | if not check_macro: |
| 4639 | return |
| 4640 | |
| 4641 | # Find end of the boolean expression by matching parentheses |
| 4642 | (last_line, end_line, end_pos) = CloseExpression( |
| 4643 | clean_lines, linenum, start_pos) |
| 4644 | if end_pos < 0: |
| 4645 | return |
| 4646 | |
| 4647 | # If the check macro is followed by something other than a |
| 4648 | # semicolon, assume users will log their own custom error messages |
| 4649 | # and don't suggest any replacements. |
| 4650 | if not Match(r'\s*;', last_line[end_pos:]): |
| 4651 | return |
| 4652 | |
| 4653 | if linenum == end_line: |
| 4654 | expression = lines[linenum][start_pos + 1:end_pos - 1] |
| 4655 | else: |
| 4656 | expression = lines[linenum][start_pos + 1:] |
| 4657 | for i in xrange(linenum + 1, end_line): |
| 4658 | expression += lines[i] |
| 4659 | expression += last_line[0:end_pos - 1] |
| 4660 | |
| 4661 | # Parse expression so that we can take parentheses into account. |
| 4662 | # This avoids false positives for inputs like "CHECK((a < 4) == b)", |
| 4663 | # which is not replaceable by CHECK_LE. |
| 4664 | lhs = '' |
| 4665 | rhs = '' |
| 4666 | operator = None |
| 4667 | while expression: |
| 4668 | matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||' |
| 4669 | r'==|!=|>=|>|<=|<|\()(.*)$', expression) |
| 4670 | if matched: |
| 4671 | token = matched.group(1) |
| 4672 | if token == '(': |
| 4673 | # Parenthesized operand |
| 4674 | expression = matched.group(2) |
| 4675 | (end, _) = FindEndOfExpressionInLine(expression, 0, ['(']) |
| 4676 | if end < 0: |
| 4677 | return # Unmatched parenthesis |
| 4678 | lhs += '(' + expression[0:end] |
| 4679 | expression = expression[end:] |
| 4680 | elif token in ('&&', '||'): |
| 4681 | # Logical and/or operators. This means the expression |
| 4682 | # contains more than one term, for example: |
no test coverage detected