Tracks current function name and the number of lines in its body.
| 1488 | _cpplint_state.RestoreFilters() |
| 1489 | |
| 1490 | class _FunctionState(object): |
| 1491 | """Tracks current function name and the number of lines in its body.""" |
| 1492 | |
| 1493 | _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc. |
| 1494 | _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER. |
| 1495 | |
| 1496 | def __init__(self): |
| 1497 | self.in_a_function = False |
| 1498 | self.lines_in_function = 0 |
| 1499 | self.current_function = '' |
| 1500 | |
| 1501 | def Begin(self, function_name): |
| 1502 | """Start analyzing function body. |
| 1503 | |
| 1504 | Args: |
| 1505 | function_name: The name of the function being tracked. |
| 1506 | """ |
| 1507 | self.in_a_function = True |
| 1508 | self.lines_in_function = 0 |
| 1509 | self.current_function = function_name |
| 1510 | |
| 1511 | def Count(self): |
| 1512 | """Count line in current function body.""" |
| 1513 | if self.in_a_function: |
| 1514 | self.lines_in_function += 1 |
| 1515 | |
| 1516 | def Check(self, error, filename, linenum): |
| 1517 | """Report if too many lines in function body. |
| 1518 | |
| 1519 | Args: |
| 1520 | error: The function to call with any errors found. |
| 1521 | filename: The name of the current file. |
| 1522 | linenum: The number of the line to check. |
| 1523 | """ |
| 1524 | if not self.in_a_function: |
| 1525 | return |
| 1526 | |
| 1527 | if Match(r'T(EST|est)', self.current_function): |
| 1528 | base_trigger = self._TEST_TRIGGER |
| 1529 | else: |
| 1530 | base_trigger = self._NORMAL_TRIGGER |
| 1531 | trigger = base_trigger * 2**_VerboseLevel() |
| 1532 | |
| 1533 | if self.lines_in_function > trigger: |
| 1534 | error_level = int(math.log(self.lines_in_function / base_trigger, 2)) |
| 1535 | # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... |
| 1536 | if error_level > 5: |
| 1537 | error_level = 5 |
| 1538 | error(filename, linenum, 'readability/fn_size', error_level, |
| 1539 | 'Small and focused functions are preferred:' |
| 1540 | ' %s has %d non-comment lines' |
| 1541 | ' (error triggered by exceeding %d lines).' % ( |
| 1542 | self.current_function, self.lines_in_function, trigger)) |
| 1543 | |
| 1544 | def End(self): |
| 1545 | """Stop analyzing function body.""" |
| 1546 | self.in_a_function = False |
| 1547 |