Tracks current function name and the number of lines in its body.
| 1230 | _cpplint_state.RestoreFilters() |
| 1231 | |
| 1232 | class _FunctionState(object): |
| 1233 | """Tracks current function name and the number of lines in its body.""" |
| 1234 | |
| 1235 | _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc. |
| 1236 | _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER. |
| 1237 | |
| 1238 | def __init__(self): |
| 1239 | self.in_a_function = False |
| 1240 | self.lines_in_function = 0 |
| 1241 | self.current_function = '' |
| 1242 | |
| 1243 | def Begin(self, function_name): |
| 1244 | """Start analyzing function body. |
| 1245 | |
| 1246 | Args: |
| 1247 | function_name: The name of the function being tracked. |
| 1248 | """ |
| 1249 | self.in_a_function = True |
| 1250 | self.lines_in_function = 0 |
| 1251 | self.current_function = function_name |
| 1252 | |
| 1253 | def Count(self): |
| 1254 | """Count line in current function body.""" |
| 1255 | if self.in_a_function: |
| 1256 | self.lines_in_function += 1 |
| 1257 | |
| 1258 | def Check(self, error, filename, linenum): |
| 1259 | """Report if too many lines in function body. |
| 1260 | |
| 1261 | Args: |
| 1262 | error: The function to call with any errors found. |
| 1263 | filename: The name of the current file. |
| 1264 | linenum: The number of the line to check. |
| 1265 | """ |
| 1266 | if not self.in_a_function: |
| 1267 | return |
| 1268 | |
| 1269 | if Match(r'T(EST|est)', self.current_function): |
| 1270 | base_trigger = self._TEST_TRIGGER |
| 1271 | else: |
| 1272 | base_trigger = self._NORMAL_TRIGGER |
| 1273 | trigger = base_trigger * 2**_VerboseLevel() |
| 1274 | |
| 1275 | if self.lines_in_function > trigger: |
| 1276 | error_level = int(math.log(self.lines_in_function / base_trigger, 2)) |
| 1277 | # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... |
| 1278 | if error_level > 5: |
| 1279 | error_level = 5 |
| 1280 | error(filename, linenum, 'readability/fn_size', error_level, |
| 1281 | 'Small and focused functions are preferred:' |
| 1282 | ' %s has %d non-comment lines' |
| 1283 | ' (error triggered by exceeding %d lines).' % ( |
| 1284 | self.current_function, self.lines_in_function, trigger)) |
| 1285 | |
| 1286 | def End(self): |
| 1287 | """Stop analyzing function body.""" |
| 1288 | self.in_a_function = False |
| 1289 |