Tracks current function name and the number of lines in its body.
| 911 | _cpplint_state.RestoreFilters() |
| 912 | |
| 913 | class _FunctionState(object): |
| 914 | """Tracks current function name and the number of lines in its body.""" |
| 915 | |
| 916 | _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc. |
| 917 | _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER. |
| 918 | |
| 919 | def __init__(self): |
| 920 | self.in_a_function = False |
| 921 | self.lines_in_function = 0 |
| 922 | self.current_function = '' |
| 923 | |
| 924 | def Begin(self, function_name): |
| 925 | """Start analyzing function body. |
| 926 | |
| 927 | Args: |
| 928 | function_name: The name of the function being tracked. |
| 929 | """ |
| 930 | self.in_a_function = True |
| 931 | self.lines_in_function = 0 |
| 932 | self.current_function = function_name |
| 933 | |
| 934 | def Count(self): |
| 935 | """Count line in current function body.""" |
| 936 | if self.in_a_function: |
| 937 | self.lines_in_function += 1 |
| 938 | |
| 939 | def Check(self, error, filename, linenum): |
| 940 | """Report if too many lines in function body. |
| 941 | |
| 942 | Args: |
| 943 | error: The function to call with any errors found. |
| 944 | filename: The name of the current file. |
| 945 | linenum: The number of the line to check. |
| 946 | """ |
| 947 | if Match(r'T(EST|est)', self.current_function): |
| 948 | base_trigger = self._TEST_TRIGGER |
| 949 | else: |
| 950 | base_trigger = self._NORMAL_TRIGGER |
| 951 | trigger = base_trigger * 2**_VerboseLevel() |
| 952 | |
| 953 | if self.lines_in_function > trigger: |
| 954 | error_level = int(math.log(self.lines_in_function / base_trigger, 2)) |
| 955 | # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... |
| 956 | if error_level > 5: |
| 957 | error_level = 5 |
| 958 | error(filename, linenum, 'readability/fn_size', error_level, |
| 959 | 'Small and focused functions are preferred:' |
| 960 | ' %s has %d non-comment lines' |
| 961 | ' (error triggered by exceeding %d lines).' % ( |
| 962 | self.current_function, self.lines_in_function, trigger)) |
| 963 | |
| 964 | def End(self): |
| 965 | """Stop analyzing function body.""" |
| 966 | self.in_a_function = False |
| 967 | |
| 968 | |
| 969 | class _IncludeError(Exception): |