65
65
# - Check for class declaration order (typedefs, consts, enums,
66
66
# ctor(s?), dtor, friend declarations, methods, member vars)
70
# - (P. Lane) Treat 'catch' control statements like if, for, switch, etc...
69
72
"""Does google-lint on c++ files.
1466
1469
error: The function to call with any errors found.
1469
# Since function calls often occur inside if/for/while/switch
1472
# Since function calls often occur inside if/for/while/switch/catch
1470
1473
# expressions - which have their own, more liberal conventions - we
1471
1474
# first see if we should be looking inside such an expression for a
1472
1475
# function call, to which we can apply more strict standards.
1474
1477
for pattern in (r'\bif\s*\((.*)\)\s*{',
1475
1478
r'\bfor\s*\((.*)\)\s*{',
1476
1479
r'\bwhile\s*\((.*)\)\s*[{;]',
1477
r'\bswitch\s*\((.*)\)\s*{'):
1480
r'\bswitch\s*\((.*)\)\s*{',
1481
r'\bcatch\s*\((.*)\)\s*{'):
1478
1482
match = Search(pattern, line)
1480
1484
fncall = match.group(1) # look inside the parens for function calls
1483
# Except in if/for/while/switch, there should never be space
1487
# Except in if/for/while/switch/catch, there should never be space
1484
1488
# immediately inside parens (eg "f( 3, 4 )"). We make an exception
1485
1489
# for nested parens ( (a+b) + c ). Likewise, there should never be
1486
1490
# a space before a ( when it's a function argument. I assume it's a
1494
1498
# Note that we assume the contents of [] to be short enough that
1495
1499
# they'll never need to wrap.
1496
1500
if ( # Ignore control structures.
1497
not Search(r'\b(if|for|while|switch|return|delete)\b', fncall) and
1501
not Search(r'\b(if|for|while|switch|return|delete|catch)\b', fncall) and
1498
1502
# Ignore pointers/references to functions.
1499
1503
not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
1500
1504
# Ignore pointers/references to arrays.
1638
1642
"""Checks for the correctness of various spacing issues in the code.
1640
1644
Things we check for: spaces around operators, spaces after
1641
if/for/while/switch, no spaces around parens in function calls, two
1645
if/for/while/switch/catch, no spaces around parens in function calls, two
1642
1646
spaces between code and comment, don't start a block with a blank
1643
1647
line, don't end a function with a blank line, don't have too many
1644
1648
blank lines in a row.
1797
1801
error(filename, linenum, 'whitespace/operators', 4,
1798
1802
'Extra space for operator %s' % match.group(1))
1800
# A pet peeve of mine: no spaces after an if, while, switch, or for
1801
match = Search(r' (if\(|for\(|while\(|switch\()', line)
1804
# A pet peeve of mine: no spaces after an if, while, switch, for, or catch
1805
match = Search(r' (if\(|for\(|while\(|switch\(|catch\()', line)
1803
1807
error(filename, linenum, 'whitespace/parens', 5,
1804
1808
'Missing space before ( in %s' % match.group(1))
1806
# For if/for/while/switch, the left and right parens should be
1810
# For if/for/while/switch/catch, the left and right parens should be
1807
1811
# consistent about how many spaces are inside the parens, and
1808
1812
# there should either be zero or one spaces inside the parens.
1809
1813
# We don't want: "if ( foo)" or "if ( foo )".
1810
1814
# Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
1811
match = Search(r'\b(if|for|while|switch)\s*'
1815
match = Search(r'\b(if|for|while|switch|catch)\s*'
1812
1816
r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',