Tracks current function name and the number of lines in its body.
| 1031 | _cpplint_state.RestoreFilters() |
| 1032 | |
| 1033 | class _FunctionState(object): |
| 1034 | """Tracks current function name and the number of lines in its body.""" |
| 1035 | |
| 1036 | _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc. |
| 1037 | _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER. |
| 1038 | |
| 1039 | def __init__(self): |
| 1040 | self.in_a_function = False |
| 1041 | self.lines_in_function = 0 |
| 1042 | self.current_function = '' |
| 1043 | |
| 1044 | def Begin(self, function_name): |
| 1045 | """Start analyzing function body. |
| 1046 | |
| 1047 | Args: |
| 1048 | function_name: The name of the function being tracked. |
| 1049 | """ |
| 1050 | self.in_a_function = True |
| 1051 | self.lines_in_function = 0 |
| 1052 | self.current_function = function_name |
| 1053 | |
| 1054 | def Count(self): |
| 1055 | """Count line in current function body.""" |
| 1056 | if self.in_a_function: |
| 1057 | self.lines_in_function += 1 |
| 1058 | |
| 1059 | def Check(self, error, filename, linenum): |
| 1060 | """Report if too many lines in function body. |
| 1061 | |
| 1062 | Args: |
| 1063 | error: The function to call with any errors found. |
| 1064 | filename: The name of the current file. |
| 1065 | linenum: The number of the line to check. |
| 1066 | """ |
| 1067 | if not self.in_a_function: |
| 1068 | return |
| 1069 | |
| 1070 | if Match(r'T(EST|est)', self.current_function): |
| 1071 | base_trigger = self._TEST_TRIGGER |
| 1072 | else: |
| 1073 | base_trigger = self._NORMAL_TRIGGER |
| 1074 | trigger = base_trigger * 2**_VerboseLevel() |
| 1075 | |
| 1076 | if self.lines_in_function > trigger: |
| 1077 | error_level = int(math.log(self.lines_in_function / base_trigger, 2)) |
| 1078 | # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... |
| 1079 | if error_level > 5: |
| 1080 | error_level = 5 |
| 1081 | error(filename, linenum, 'readability/fn_size', error_level, |
| 1082 | 'Small and focused functions are preferred:' |
| 1083 | ' %s has %d non-comment lines' |
| 1084 | ' (error triggered by exceeding %d lines).' % ( |
| 1085 | self.current_function, self.lines_in_function, trigger)) |
| 1086 | |
| 1087 | def End(self): |
| 1088 | """Stop analyzing function body.""" |
| 1089 | self.in_a_function = False |
| 1090 |
no outgoing calls
no test coverage detected
searching dependent graphs…