Reports for long function bodies. For an overview why this is done, see: https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions Uses a simplistic algorithm assuming other style guidelines (especially spacing) are followed. Only checks unindented functions,
(filename, clean_lines, linenum,
function_state, error)
| 3540 | |
| 3541 | |
| 3542 | def CheckForFunctionLengths(filename, clean_lines, linenum, |
| 3543 | function_state, error): |
| 3544 | """Reports for long function bodies. |
| 3545 | |
| 3546 | For an overview why this is done, see: |
| 3547 | https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions |
| 3548 | |
| 3549 | Uses a simplistic algorithm assuming other style guidelines |
| 3550 | (especially spacing) are followed. |
| 3551 | Only checks unindented functions, so class members are unchecked. |
| 3552 | Trivial bodies are unchecked, so constructors with huge initializer lists |
| 3553 | may be missed. |
| 3554 | Blank/comment lines are not counted so as to avoid encouraging the removal |
| 3555 | of vertical space and comments just to get through a lint check. |
| 3556 | NOLINT *on the last line of a function* disables this check. |
| 3557 | |
| 3558 | Args: |
| 3559 | filename: The name of the current file. |
| 3560 | clean_lines: A CleansedLines instance containing the file. |
| 3561 | linenum: The number of the line to check. |
| 3562 | function_state: Current function name and lines in body so far. |
| 3563 | error: The function to call with any errors found. |
| 3564 | """ |
| 3565 | lines = clean_lines.lines |
| 3566 | line = lines[linenum] |
| 3567 | joined_line = '' |
| 3568 | |
| 3569 | starting_func = False |
| 3570 | regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ... |
| 3571 | match_result = Match(regexp, line) |
| 3572 | if match_result: |
| 3573 | # If the name is all caps and underscores, figure it's a macro and |
| 3574 | # ignore it, unless it's TEST or TEST_F. |
| 3575 | function_name = match_result.group(1).split()[-1] |
| 3576 | if function_name == 'TEST' or function_name == 'TEST_F' or ( |
| 3577 | not Match(r'[A-Z_]+$', function_name)): |
| 3578 | starting_func = True |
| 3579 | |
| 3580 | if starting_func: |
| 3581 | body_found = False |
| 3582 | for start_linenum in xrange(linenum, clean_lines.NumLines()): |
| 3583 | start_line = lines[start_linenum] |
| 3584 | joined_line += ' ' + start_line.lstrip() |
| 3585 | if Search(r'(;|})', start_line): # Declarations and trivial functions |
| 3586 | body_found = True |
| 3587 | break # ... ignore |
| 3588 | if Search(r'{', start_line): |
| 3589 | body_found = True |
| 3590 | function = Search(r'((\w|:)*)\(', line).group(1) |
| 3591 | if Match(r'TEST', function): # Handle TEST... macros |
| 3592 | parameter_regexp = Search(r'(\(.*\))', joined_line) |
| 3593 | if parameter_regexp: # Ignore bad syntax |
| 3594 | function += parameter_regexp.group(1) |
| 3595 | else: |
| 3596 | function += '()' |
| 3597 | function_state.Begin(function) |
| 3598 | break |
| 3599 | if not body_found: |