| @@ 3157-3222 (lines=66) @@ | ||
| 3154 | line, error) |
|
| 3155 | ||
| 3156 | ||
| 3157 | def CheckForFunctionLengths(filename, clean_lines, linenum, |
|
| 3158 | function_state, error): |
|
| 3159 | """Reports for long function bodies. |
|
| 3160 | ||
| 3161 | For an overview why this is done, see: |
|
| 3162 | https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions |
|
| 3163 | ||
| 3164 | Uses a simplistic algorithm assuming other style guidelines |
|
| 3165 | (especially spacing) are followed. |
|
| 3166 | Only checks unindented functions, so class members are unchecked. |
|
| 3167 | Trivial bodies are unchecked, so constructors with huge initializer lists |
|
| 3168 | may be missed. |
|
| 3169 | Blank/comment lines are not counted so as to avoid encouraging the removal |
|
| 3170 | of vertical space and comments just to get through a lint check. |
|
| 3171 | NOLINT *on the last line of a function* disables this check. |
|
| 3172 | ||
| 3173 | Args: |
|
| 3174 | filename: The name of the current file. |
|
| 3175 | clean_lines: A CleansedLines instance containing the file. |
|
| 3176 | linenum: The number of the line to check. |
|
| 3177 | function_state: Current function name and lines in body so far. |
|
| 3178 | error: The function to call with any errors found. |
|
| 3179 | """ |
|
| 3180 | lines = clean_lines.lines |
|
| 3181 | line = lines[linenum] |
|
| 3182 | joined_line = '' |
|
| 3183 | ||
| 3184 | starting_func = False |
|
| 3185 | regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ... |
|
| 3186 | match_result = Match(regexp, line) |
|
| 3187 | if match_result: |
|
| 3188 | # If the name is all caps and underscores, figure it's a macro and |
|
| 3189 | # ignore it, unless it's TEST or TEST_F. |
|
| 3190 | function_name = match_result.group(1).split()[-1] |
|
| 3191 | if function_name == 'TEST' or function_name == 'TEST_F' or ( |
|
| 3192 | not Match(r'[A-Z_]+$', function_name)): |
|
| 3193 | starting_func = True |
|
| 3194 | ||
| 3195 | if starting_func: |
|
| 3196 | body_found = False |
|
| 3197 | for start_linenum in range(linenum, clean_lines.NumLines()): |
|
| 3198 | start_line = lines[start_linenum] |
|
| 3199 | joined_line += ' ' + start_line.lstrip() |
|
| 3200 | if Search(r'(;|})', start_line): # Declarations and trivial functions |
|
| 3201 | body_found = True |
|
| 3202 | break # ... ignore |
|
| 3203 | elif Search(r'{', start_line): |
|
| 3204 | body_found = True |
|
| 3205 | function = Search(r'((\w|:)*)\(', line).group(1) |
|
| 3206 | if Match(r'TEST', function): # Handle TEST... macros |
|
| 3207 | parameter_regexp = Search(r'(\(.*\))', joined_line) |
|
| 3208 | if parameter_regexp: # Ignore bad syntax |
|
| 3209 | function += parameter_regexp.group(1) |
|
| 3210 | else: |
|
| 3211 | function += '()' |
|
| 3212 | function_state.Begin(function) |
|
| 3213 | break |
|
| 3214 | if not body_found: |
|
| 3215 | # No body for the function (or evidence of a non-function) was found. |
|
| 3216 | error(filename, linenum, 'readability/fn_size', 5, |
|
| 3217 | 'Lint failed to find start of function body.') |
|
| 3218 | elif Match(r'^\}\s*$', line): # function end |
|
| 3219 | function_state.Check(error, filename, linenum) |
|
| 3220 | function_state.End() |
|
| 3221 | elif not Match(r'^\s*$', line): |
|
| 3222 | function_state.Count() # Count non-blank/non-comment lines. |
|
| 3223 | ||
| 3224 | ||
| 3225 | _RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?') |
|
| @@ 3157-3222 (lines=66) @@ | ||
| 3154 | line, error) |
|
| 3155 | ||
| 3156 | ||
| 3157 | def CheckForFunctionLengths(filename, clean_lines, linenum, |
|
| 3158 | function_state, error): |
|
| 3159 | """Reports for long function bodies. |
|
| 3160 | ||
| 3161 | For an overview why this is done, see: |
|
| 3162 | https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions |
|
| 3163 | ||
| 3164 | Uses a simplistic algorithm assuming other style guidelines |
|
| 3165 | (especially spacing) are followed. |
|
| 3166 | Only checks unindented functions, so class members are unchecked. |
|
| 3167 | Trivial bodies are unchecked, so constructors with huge initializer lists |
|
| 3168 | may be missed. |
|
| 3169 | Blank/comment lines are not counted so as to avoid encouraging the removal |
|
| 3170 | of vertical space and comments just to get through a lint check. |
|
| 3171 | NOLINT *on the last line of a function* disables this check. |
|
| 3172 | ||
| 3173 | Args: |
|
| 3174 | filename: The name of the current file. |
|
| 3175 | clean_lines: A CleansedLines instance containing the file. |
|
| 3176 | linenum: The number of the line to check. |
|
| 3177 | function_state: Current function name and lines in body so far. |
|
| 3178 | error: The function to call with any errors found. |
|
| 3179 | """ |
|
| 3180 | lines = clean_lines.lines |
|
| 3181 | line = lines[linenum] |
|
| 3182 | joined_line = '' |
|
| 3183 | ||
| 3184 | starting_func = False |
|
| 3185 | regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ... |
|
| 3186 | match_result = Match(regexp, line) |
|
| 3187 | if match_result: |
|
| 3188 | # If the name is all caps and underscores, figure it's a macro and |
|
| 3189 | # ignore it, unless it's TEST or TEST_F. |
|
| 3190 | function_name = match_result.group(1).split()[-1] |
|
| 3191 | if function_name == 'TEST' or function_name == 'TEST_F' or ( |
|
| 3192 | not Match(r'[A-Z_]+$', function_name)): |
|
| 3193 | starting_func = True |
|
| 3194 | ||
| 3195 | if starting_func: |
|
| 3196 | body_found = False |
|
| 3197 | for start_linenum in range(linenum, clean_lines.NumLines()): |
|
| 3198 | start_line = lines[start_linenum] |
|
| 3199 | joined_line += ' ' + start_line.lstrip() |
|
| 3200 | if Search(r'(;|})', start_line): # Declarations and trivial functions |
|
| 3201 | body_found = True |
|
| 3202 | break # ... ignore |
|
| 3203 | elif Search(r'{', start_line): |
|
| 3204 | body_found = True |
|
| 3205 | function = Search(r'((\w|:)*)\(', line).group(1) |
|
| 3206 | if Match(r'TEST', function): # Handle TEST... macros |
|
| 3207 | parameter_regexp = Search(r'(\(.*\))', joined_line) |
|
| 3208 | if parameter_regexp: # Ignore bad syntax |
|
| 3209 | function += parameter_regexp.group(1) |
|
| 3210 | else: |
|
| 3211 | function += '()' |
|
| 3212 | function_state.Begin(function) |
|
| 3213 | break |
|
| 3214 | if not body_found: |
|
| 3215 | # No body for the function (or evidence of a non-function) was found. |
|
| 3216 | error(filename, linenum, 'readability/fn_size', 5, |
|
| 3217 | 'Lint failed to find start of function body.') |
|
| 3218 | elif Match(r'^\}\s*$', line): # function end |
|
| 3219 | function_state.Check(error, filename, linenum) |
|
| 3220 | function_state.End() |
|
| 3221 | elif not Match(r'^\s*$', line): |
|
| 3222 | function_state.Count() # Count non-blank/non-comment lines. |
|
| 3223 | ||
| 3224 | ||
| 3225 | _RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?') |
|