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)
| 1855 | |
| 1856 | |
| 1857 | def CheckSpacingForFunctionCall(filename, line, linenum, error): |
| 1858 | """Checks for the correctness of various spacing around function calls. |
| 1859 | |
| 1860 | Args: |
| 1861 | filename: The name of the current file. |
| 1862 | line: The text of the line to check. |
| 1863 | linenum: The number of the line to check. |
| 1864 | error: The function to call with any errors found. |
| 1865 | """ |
| 1866 | |
| 1867 | # Since function calls often occur inside if/for/while/switch |
| 1868 | # expressions - which have their own, more liberal conventions - we |
| 1869 | # first see if we should be looking inside such an expression for a |
| 1870 | # function call, to which we can apply more strict standards. |
| 1871 | fncall = line # if there's no control flow construct, look at whole line |
| 1872 | for pattern in (r'\bif\s*\((.*)\)\s*{', |
| 1873 | r'\bfor\s*\((.*)\)\s*{', |
| 1874 | r'\bwhile\s*\((.*)\)\s*[{;]', |
| 1875 | r'\bswitch\s*\((.*)\)\s*{'): |
| 1876 | match = Search(pattern, line) |
| 1877 | if match: |
| 1878 | fncall = match.group(1) # look inside the parens for function calls |
| 1879 | break |
| 1880 | |
| 1881 | # Except in if/for/while/switch, there should never be space |
| 1882 | # immediately inside parens (eg "f( 3, 4 )"). We make an exception |
| 1883 | # for nested parens ( (a+b) + c ). Likewise, there should never be |
| 1884 | # a space before a ( when it's a function argument. I assume it's a |
| 1885 | # function argument when the char before the whitespace is legal in |
| 1886 | # a function name (alnum + _) and we're not starting a macro. Also ignore |
| 1887 | # pointers and references to arrays and functions coz they're too tricky: |
| 1888 | # we use a very simple way to recognize these: |
| 1889 | # " (something)(maybe-something)" or |
| 1890 | # " (something)(maybe-something," or |
| 1891 | # " (something)[something]" |
| 1892 | # Note that we assume the contents of [] to be short enough that |
| 1893 | # they'll never need to wrap. |
| 1894 | if ( # Ignore control structures. |
| 1895 | not Search(r'\b(if|for|while|switch|return|delete)\b', fncall) and |
| 1896 | # Ignore pointers/references to functions. |
| 1897 | not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and |
| 1898 | # Ignore pointers/references to arrays. |
| 1899 | not Search(r' \([^)]+\)\[[^\]]+\]', fncall)): |
| 1900 | if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call |
| 1901 | error(filename, linenum, 'whitespace/parens', 4, |
| 1902 | 'Extra space after ( in function call') |
| 1903 | elif Search(r'\(\s+(?!(\s*\\)|\()', fncall): |
| 1904 | error(filename, linenum, 'whitespace/parens', 2, |
| 1905 | 'Extra space after (') |
| 1906 | if (Search(r'\w\s+\(', fncall) and |
| 1907 | not Search(r'#\s*define|typedef', fncall) and |
| 1908 | not Search(r'\w\s+\((\w+::)?\*\w+\)\(', fncall)): |
| 1909 | error(filename, linenum, 'whitespace/parens', 4, |
| 1910 | 'Extra space before ( in function call') |
| 1911 | # If the ) is followed only by a newline or a { + newline, assume it's |
| 1912 | # part of a control statement (if/while/etc), and don't complain |
| 1913 | if Search(r'[^)]\s+\)\s*[^{\s]', fncall): |
| 1914 | # If the closing parenthesis is preceded by only whitespaces, |
no test coverage detected