MCPcopy Create free account
hub / github.com/WolfireGames/overgrowth / _FunctionState

Class _FunctionState

Tools/cpplint.py:1006–1062  ·  view source on GitHub ↗

Tracks current function name and the number of lines in its body.

Source from the content-addressed store, hash-verified

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

Callers 1

ProcessFileDataFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected