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)
| 3280 | |
| 3281 | |
| 3282 | def CheckCheck(filename, clean_lines, linenum, error): |
| 3283 | """Checks the use of CHECK and EXPECT macros. |
| 3284 | |
| 3285 | Args: |
| 3286 | filename: The name of the current file. |
| 3287 | clean_lines: A CleansedLines instance containing the file. |
| 3288 | linenum: The number of the line to check. |
| 3289 | error: The function to call with any errors found. |
| 3290 | """ |
| 3291 | |
| 3292 | # Decide the set of replacement macros that should be suggested |
| 3293 | lines = clean_lines.elided |
| 3294 | check_macro = None |
| 3295 | start_pos = -1 |
| 3296 | for macro in _CHECK_MACROS: |
| 3297 | i = lines[linenum].find(macro) |
| 3298 | if i >= 0: |
| 3299 | check_macro = macro |
| 3300 | |
| 3301 | # Find opening parenthesis. Do a regular expression match here |
| 3302 | # to make sure that we are matching the expected CHECK macro, as |
| 3303 | # opposed to some other macro that happens to contain the CHECK |
| 3304 | # substring. |
| 3305 | matched = Match(r'^(.*\b' + check_macro + r'\s*)\(', lines[linenum]) |
| 3306 | if not matched: |
| 3307 | continue |
| 3308 | start_pos = len(matched.group(1)) |
| 3309 | break |
| 3310 | if not check_macro or start_pos < 0: |
| 3311 | # Don't waste time here if line doesn't contain 'CHECK' or 'EXPECT' |
| 3312 | return |
| 3313 | |
| 3314 | # Find end of the boolean expression by matching parentheses |
| 3315 | (last_line, end_line, end_pos) = CloseExpression( |
| 3316 | clean_lines, linenum, start_pos) |
| 3317 | if end_pos < 0: |
| 3318 | return |
| 3319 | if linenum == end_line: |
| 3320 | expression = lines[linenum][start_pos + 1:end_pos - 1] |
| 3321 | else: |
| 3322 | expression = lines[linenum][start_pos + 1:] |
| 3323 | for i in xrange(linenum + 1, end_line): |
| 3324 | expression += lines[i] |
| 3325 | expression += last_line[0:end_pos - 1] |
| 3326 | |
| 3327 | # Parse expression so that we can take parentheses into account. |
| 3328 | # This avoids false positives for inputs like "CHECK((a < 4) == b)", |
| 3329 | # which is not replaceable by CHECK_LE. |
| 3330 | lhs = '' |
| 3331 | rhs = '' |
| 3332 | operator = None |
| 3333 | while expression: |
| 3334 | matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||' |
| 3335 | r'==|!=|>=|>|<=|<|\()(.*)$', expression) |
| 3336 | if matched: |
| 3337 | token = matched.group(1) |
| 3338 | if token == '(': |
| 3339 | # Parenthesized operand |
no test coverage detected