Tracks current function name and the number of lines in its body.
| 1005 | _cpplint_state.RestoreFilters() |
| 1006 | |
| 1007 | class _FunctionState(object): |
| 1008 | """Tracks current function name and the number of lines in its body.""" |
| 1009 | |
| 1010 | _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc. |
| 1011 | _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER. |
| 1012 | |
| 1013 | def __init__(self): |
| 1014 | self.in_a_function = False |
| 1015 | self.lines_in_function = 0 |
| 1016 | self.current_function = '' |
| 1017 | |
| 1018 | def Begin(self, function_name): |
| 1019 | """Start analyzing function body. |
| 1020 | |
| 1021 | Args: |
| 1022 | function_name: The name of the function being tracked. |
| 1023 | """ |
| 1024 | self.in_a_function = True |
| 1025 | self.lines_in_function = 0 |
| 1026 | self.current_function = function_name |
| 1027 | |
| 1028 | def Count(self): |
| 1029 | """Count line in current function body.""" |
| 1030 | if self.in_a_function: |
| 1031 | self.lines_in_function += 1 |
| 1032 | |
| 1033 | def Check(self, error, filename, linenum): |
| 1034 | """Report if too many lines in function body. |
| 1035 | |
| 1036 | Args: |
| 1037 | error: The function to call with any errors found. |
| 1038 | filename: The name of the current file. |
| 1039 | linenum: The number of the line to check. |
| 1040 | """ |
| 1041 | if not self.in_a_function: |
| 1042 | return |
| 1043 | |
| 1044 | if Match(r'T(EST|est)', self.current_function): |
| 1045 | base_trigger = self._TEST_TRIGGER |
| 1046 | else: |
| 1047 | base_trigger = self._NORMAL_TRIGGER |
| 1048 | trigger = base_trigger * 2**_VerboseLevel() |
| 1049 | |
| 1050 | if self.lines_in_function > trigger: |
| 1051 | error_level = int(math.log(self.lines_in_function / base_trigger, 2)) |
| 1052 | # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... |
| 1053 | if error_level > 5: |
| 1054 | error_level = 5 |
| 1055 | error(filename, linenum, 'readability/fn_size', error_level, |
| 1056 | 'Small and focused functions are preferred:' |
| 1057 | ' %s has %d non-comment lines' |
| 1058 | ' (error triggered by exceeding %d lines).' % ( |
| 1059 | self.current_function, self.lines_in_function, trigger)) |
| 1060 | |
| 1061 | def End(self): |
| 1062 | """Stop analyzing function body.""" |
| 1063 | self.in_a_function = False |
| 1064 |