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