Checks for the correctness of various spacing around function calls. Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. error: The function to call with any errors found.
(filename, clean_lines, linenum, error)
| 3944 | |
| 3945 | |
| 3946 | def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error): |
| 3947 | """Checks for the correctness of various spacing around function calls. |
| 3948 | |
| 3949 | Args: |
| 3950 | filename: The name of the current file. |
| 3951 | clean_lines: A CleansedLines instance containing the file. |
| 3952 | linenum: The number of the line to check. |
| 3953 | error: The function to call with any errors found. |
| 3954 | """ |
| 3955 | line = clean_lines.elided[linenum] |
| 3956 | |
| 3957 | # Since function calls often occur inside if/for/while/switch |
| 3958 | # expressions - which have their own, more liberal conventions - we |
| 3959 | # first see if we should be looking inside such an expression for a |
| 3960 | # function call, to which we can apply more strict standards. |
| 3961 | fncall = line # if there's no control flow construct, look at whole line |
| 3962 | for pattern in ( |
| 3963 | r"\bif\s*\((.*)\)\s*{", |
| 3964 | r"\bfor\s*\((.*)\)\s*{", |
| 3965 | r"\bwhile\s*\((.*)\)\s*[{;]", |
| 3966 | r"\bswitch\s*\((.*)\)\s*{", |
| 3967 | ): |
| 3968 | match = re.search(pattern, line) |
| 3969 | if match: |
| 3970 | fncall = match.group(1) # look inside the parens for function calls |
| 3971 | break |
| 3972 | |
| 3973 | # Except in if/for/while/switch, there should never be space |
| 3974 | # immediately inside parens (eg "f( 3, 4 )"). We make an exception |
| 3975 | # for nested parens ( (a+b) + c ). Likewise, there should never be |
| 3976 | # a space before a ( when it's a function argument. I assume it's a |
| 3977 | # function argument when the char before the whitespace is legal in |
| 3978 | # a function name (alnum + _) and we're not starting a macro. Also ignore |
| 3979 | # pointers and references to arrays and functions coz they're too tricky: |
| 3980 | # we use a very simple way to recognize these: |
| 3981 | # " (something)(maybe-something)" or |
| 3982 | # " (something)(maybe-something," or |
| 3983 | # " (something)[something]" |
| 3984 | # Note that we assume the contents of [] to be short enough that |
| 3985 | # they'll never need to wrap. |
| 3986 | if ( # Ignore control structures. |
| 3987 | not re.search(r"\b(if|elif|for|while|switch|return|new|delete|catch|sizeof)\b", fncall) |
| 3988 | and |
| 3989 | # Ignore pointers/references to functions. |
| 3990 | not re.search(r" \([^)]+\)\([^)]*(\)|,$)", fncall) |
| 3991 | and |
| 3992 | # Ignore pointers/references to arrays. |
| 3993 | not re.search(r" \([^)]+\)\[[^\]]+\]", fncall) |
| 3994 | ): |
| 3995 | if re.search(r"\w\s*\(\s(?!\s*\\$)", fncall): # a ( used for a fn call |
| 3996 | error(filename, linenum, "whitespace/parens", 4, "Extra space after ( in function call") |
| 3997 | elif re.search(r"\(\s+(?!(\s*\\)|\()", fncall): |
| 3998 | error(filename, linenum, "whitespace/parens", 2, "Extra space after (") |
| 3999 | if ( |
| 4000 | re.search(r"\w\s+\(", fncall) |
| 4001 | and not re.search(r"_{0,2}asm_{0,2}\s+_{0,2}volatile_{0,2}\s+\(", fncall) |
| 4002 | and not re.search(r"#\s*define|typedef|using\s+\w+\s*=", fncall) |
| 4003 | and not re.search(r"\w\s+\((\w+::)*\*\w+\)\(", fncall) |
no test coverage detected
searching dependent graphs…