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 f
(filename, clean_lines, linenum, function_state, error)
| 3998 | |
| 3999 | |
| 4000 | def CheckForFunctionLengths(filename, clean_lines, linenum, function_state, error): |
| 4001 | """Reports for long function bodies. |
| 4002 | |
| 4003 | For an overview why this is done, see: |
| 4004 | https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions |
| 4005 | |
| 4006 | Uses a simplistic algorithm assuming other style guidelines |
| 4007 | (especially spacing) are followed. |
| 4008 | Only checks unindented functions, so class members are unchecked. |
| 4009 | Trivial bodies are unchecked, so constructors with huge initializer lists |
| 4010 | may be missed. |
| 4011 | Blank/comment lines are not counted so as to avoid encouraging the removal |
| 4012 | of vertical space and comments just to get through a lint check. |
| 4013 | NOLINT *on the last line of a function* disables this check. |
| 4014 | |
| 4015 | Args: |
| 4016 | filename: The name of the current file. |
| 4017 | clean_lines: A CleansedLines instance containing the file. |
| 4018 | linenum: The number of the line to check. |
| 4019 | function_state: Current function name and lines in body so far. |
| 4020 | error: The function to call with any errors found. |
| 4021 | """ |
| 4022 | lines = clean_lines.lines |
| 4023 | line = lines[linenum] |
| 4024 | joined_line = "" |
| 4025 | |
| 4026 | starting_func = False |
| 4027 | regexp = r"(\w(\w|::|\*|\&|\s)*)\(" # decls * & space::name( ... |
| 4028 | if match_result := re.match(regexp, line): |
| 4029 | # If the name is all caps and underscores, figure it's a macro and |
| 4030 | # ignore it, unless it's TEST or TEST_F. |
| 4031 | function_name = match_result.group(1).split()[-1] |
| 4032 | if function_name in {"TEST", "TEST_F"} or not re.match(r"[A-Z_]+$", function_name): |
| 4033 | starting_func = True |
| 4034 | |
| 4035 | if starting_func: |
| 4036 | body_found = False |
| 4037 | for start_linenum in range(linenum, clean_lines.NumLines()): |
| 4038 | start_line = lines[start_linenum] |
| 4039 | joined_line += " " + start_line.lstrip() |
| 4040 | if re.search(r"(;|})", start_line): # Declarations and trivial functions |
| 4041 | body_found = True |
| 4042 | break # ... ignore |
| 4043 | if re.search(r"{", start_line): |
| 4044 | body_found = True |
| 4045 | function = re.search(r"((\w|:)*)\(", line).group(1) |
| 4046 | if re.match(r"TEST", function): # Handle TEST... macros |
| 4047 | parameter_regexp = re.search(r"(\(.*\))", joined_line) |
| 4048 | if parameter_regexp: # Ignore bad syntax |
| 4049 | function += parameter_regexp.group(1) |
| 4050 | else: |
| 4051 | function += "()" |
| 4052 | function_state.Begin(function) |
| 4053 | break |
| 4054 | if not body_found: |
| 4055 | # No body for the function (or evidence of a non-function) was found. |
| 4056 | error( |
| 4057 | filename, |