Checks for the correctness of various spacing around function calls. Args: filename: The name of the current file. line: The text of the line to check. linenum: The number of the line to check. error: The function to call with any errors found.
(filename, line, linenum, error)
| 2303 | |
| 2304 | |
| 2305 | def CheckSpacingForFunctionCall(filename, line, linenum, error): |
| 2306 | """Checks for the correctness of various spacing around function calls. |
| 2307 | |
| 2308 | Args: |
| 2309 | filename: The name of the current file. |
| 2310 | line: The text of the line to check. |
| 2311 | linenum: The number of the line to check. |
| 2312 | error: The function to call with any errors found. |
| 2313 | """ |
| 2314 | |
| 2315 | # Since function calls often occur inside if/for/while/switch |
| 2316 | # expressions - which have their own, more liberal conventions - we |
| 2317 | # first see if we should be looking inside such an expression for a |
| 2318 | # function call, to which we can apply more strict standards. |
| 2319 | fncall = line # if there's no control flow construct, look at whole line |
| 2320 | for pattern in (r'\bif\s*\((.*)\)\s*{', |
| 2321 | r'\bfor\s*\((.*)\)\s*{', |
| 2322 | r'\bwhile\s*\((.*)\)\s*[{;]', |
| 2323 | r'\bswitch\s*\((.*)\)\s*{'): |
| 2324 | match = Search(pattern, line) |
| 2325 | if match: |
| 2326 | fncall = match.group(1) # look inside the parens for function calls |
| 2327 | break |
| 2328 | |
| 2329 | # Except in if/for/while/switch, there should never be space |
| 2330 | # immediately inside parens (eg "f( 3, 4 )"). We make an exception |
| 2331 | # for nested parens ( (a+b) + c ). Likewise, there should never be |
| 2332 | # a space before a ( when it's a function argument. I assume it's a |
| 2333 | # function argument when the char before the whitespace is legal in |
| 2334 | # a function name (alnum + _) and we're not starting a macro. Also ignore |
| 2335 | # pointers and references to arrays and functions coz they're too tricky: |
| 2336 | # we use a very simple way to recognize these: |
| 2337 | # " (something)(maybe-something)" or |
| 2338 | # " (something)(maybe-something," or |
| 2339 | # " (something)[something]" |
| 2340 | # Note that we assume the contents of [] to be short enough that |
| 2341 | # they'll never need to wrap. |
| 2342 | if ( # Ignore control structures. |
| 2343 | not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b', |
| 2344 | fncall) and |
| 2345 | # Ignore pointers/references to functions. |
| 2346 | not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and |
| 2347 | # Ignore pointers/references to arrays. |
| 2348 | not Search(r' \([^)]+\)\[[^\]]+\]', fncall)): |
| 2349 | if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call |
| 2350 | error(filename, linenum, 'whitespace/parens', 4, |
| 2351 | 'Extra space after ( in function call') |
| 2352 | elif Search(r'\(\s+(?!(\s*\\)|\()', fncall): |
| 2353 | error(filename, linenum, 'whitespace/parens', 2, |
| 2354 | 'Extra space after (') |
| 2355 | if (Search(r'\w\s+\(', fncall) and |
| 2356 | not Search(r'#\s*define|typedef', fncall) and |
| 2357 | not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall)): |
| 2358 | error(filename, linenum, 'whitespace/parens', 4, |
| 2359 | 'Extra space before ( in function call') |
| 2360 | # If the ) is followed only by a newline or a { + newline, assume it's |
| 2361 | # part of a control statement (if/while/etc), and don't complain |
| 2362 | if Search(r'[^)]\s+\)\s*[^{\s]', fncall): |
no test coverage detected