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