Tracks current function name and the number of lines in its body.
| 1632 | |
| 1633 | |
| 1634 | class _FunctionState: |
| 1635 | """Tracks current function name and the number of lines in its body.""" |
| 1636 | |
| 1637 | _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc. |
| 1638 | _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER. |
| 1639 | |
| 1640 | def __init__(self): |
| 1641 | self.in_a_function = False |
| 1642 | self.lines_in_function = 0 |
| 1643 | self.current_function = "" |
| 1644 | |
| 1645 | def Begin(self, function_name): |
| 1646 | """Start analyzing function body. |
| 1647 | |
| 1648 | Args: |
| 1649 | function_name: The name of the function being tracked. |
| 1650 | """ |
| 1651 | self.in_a_function = True |
| 1652 | self.lines_in_function = 0 |
| 1653 | self.current_function = function_name |
| 1654 | |
| 1655 | def Count(self): |
| 1656 | """Count line in current function body.""" |
| 1657 | if self.in_a_function: |
| 1658 | self.lines_in_function += 1 |
| 1659 | |
| 1660 | def Check(self, error, filename, linenum): |
| 1661 | """Report if too many lines in function body. |
| 1662 | |
| 1663 | Args: |
| 1664 | error: The function to call with any errors found. |
| 1665 | filename: The name of the current file. |
| 1666 | linenum: The number of the line to check. |
| 1667 | """ |
| 1668 | if not self.in_a_function: |
| 1669 | return |
| 1670 | |
| 1671 | if re.match(r"T(EST|est)", self.current_function): |
| 1672 | base_trigger = self._TEST_TRIGGER |
| 1673 | else: |
| 1674 | base_trigger = self._NORMAL_TRIGGER |
| 1675 | trigger = base_trigger * 2 ** _VerboseLevel() |
| 1676 | |
| 1677 | if self.lines_in_function > trigger: |
| 1678 | error_level = int(math.log2(self.lines_in_function / base_trigger)) |
| 1679 | # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... |
| 1680 | error_level = min(error_level, 5) |
| 1681 | error( |
| 1682 | filename, |
| 1683 | linenum, |
| 1684 | "readability/fn_size", |
| 1685 | error_level, |
| 1686 | "Small and focused functions are preferred:" |
| 1687 | f" {self.current_function} has {self.lines_in_function} non-comment lines" |
| 1688 | f" (error triggered by exceeding {trigger} lines).", |
| 1689 | ) |
| 1690 | |
| 1691 | def End(self): |