@@ 3440-3552 (lines=113) @@ | ||
3437 | 'Missing space around colon in range-based for loop') |
|
3438 | ||
3439 | ||
3440 | def CheckOperatorSpacing(filename, clean_lines, linenum, error): |
|
3441 | """Checks for horizontal spacing around operators. |
|
3442 | ||
3443 | Args: |
|
3444 | filename: The name of the current file. |
|
3445 | clean_lines: A CleansedLines instance containing the file. |
|
3446 | linenum: The number of the line to check. |
|
3447 | error: The function to call with any errors found. |
|
3448 | """ |
|
3449 | line = clean_lines.elided[linenum] |
|
3450 | ||
3451 | # Don't try to do spacing checks for operator methods. Do this by |
|
3452 | # replacing the troublesome characters with something else, |
|
3453 | # preserving column position for all other characters. |
|
3454 | # |
|
3455 | # The replacement is done repeatedly to avoid false positives from |
|
3456 | # operators that call operators. |
|
3457 | while True: |
|
3458 | match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line) |
|
3459 | if match: |
|
3460 | line = match.group(1) + ('_' * len(match.group(2))) + match.group(3) |
|
3461 | else: |
|
3462 | break |
|
3463 | ||
3464 | # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )". |
|
3465 | # Otherwise not. Note we only check for non-spaces on *both* sides; |
|
3466 | # sometimes people put non-spaces on one side when aligning ='s among |
|
3467 | # many lines (not that this is behavior that I approve of...) |
|
3468 | if ((Search(r'[\w.]=', line) or |
|
3469 | Search(r'=[\w.]', line)) |
|
3470 | and not Search(r'\b(if|while|for) ', line) |
|
3471 | # Operators taken from [lex.operators] in C++11 standard. |
|
3472 | and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line) |
|
3473 | and not Search(r'operator=', line)): |
|
3474 | error(filename, linenum, 'whitespace/operators', 4, |
|
3475 | 'Missing spaces around =') |
|
3476 | ||
3477 | # It's ok not to have spaces around binary operators like + - * /, but if |
|
3478 | # there's too little whitespace, we get concerned. It's hard to tell, |
|
3479 | # though, so we punt on this one for now. TODO. |
|
3480 | ||
3481 | # You should always have whitespace around binary operators. |
|
3482 | # |
|
3483 | # Check <= and >= first to avoid false positives with < and >, then |
|
3484 | # check non-include lines for spacing around < and >. |
|
3485 | # |
|
3486 | # If the operator is followed by a comma, assume it's be used in a |
|
3487 | # macro context and don't do any checks. This avoids false |
|
3488 | # positives. |
|
3489 | # |
|
3490 | # Note that && is not included here. This is because there are too |
|
3491 | # many false positives due to RValue references. |
|
3492 | match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line) |
|
3493 | if match: |
|
3494 | error(filename, linenum, 'whitespace/operators', 3, |
|
3495 | 'Missing spaces around %s' % match.group(1)) |
|
3496 | elif not Match(r'#.*include', line): |
|
3497 | # Look for < that is not surrounded by spaces. This is only |
|
3498 | # triggered if both sides are missing spaces, even though |
|
3499 | # technically should should flag if at least one side is missing a |
|
3500 | # space. This is done to avoid some false positives with shifts. |
|
3501 | match = Match(r'^(.*[^\s<])<[^\s=<,]', line) |
|
3502 | if match: |
|
3503 | (_, _, end_pos) = CloseExpression( |
|
3504 | clean_lines, linenum, len(match.group(1))) |
|
3505 | if end_pos <= -1: |
|
3506 | error(filename, linenum, 'whitespace/operators', 3, |
|
3507 | 'Missing spaces around <') |
|
3508 | ||
3509 | # Look for > that is not surrounded by spaces. Similar to the |
|
3510 | # above, we only trigger if both sides are missing spaces to avoid |
|
3511 | # false positives with shifts. |
|
3512 | match = Match(r'^(.*[^-\s>])>[^\s=>,]', line) |
|
3513 | if match: |
|
3514 | (_, _, start_pos) = ReverseCloseExpression( |
|
3515 | clean_lines, linenum, len(match.group(1))) |
|
3516 | if start_pos <= -1: |
|
3517 | error(filename, linenum, 'whitespace/operators', 3, |
|
3518 | 'Missing spaces around >') |
|
3519 | ||
3520 | # We allow no-spaces around << when used like this: 10<<20, but |
|
3521 | # not otherwise (particularly, not when used as streams) |
|
3522 | # |
|
3523 | # We also allow operators following an opening parenthesis, since |
|
3524 | # those tend to be macros that deal with operators. |
|
3525 | match = Search(r'(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])', line) |
|
3526 | if (match and not (match.group(1).isdigit() and match.group(2).isdigit()) and |
|
3527 | not (match.group(1) == 'operator' and match.group(2) == ';')): |
|
3528 | error(filename, linenum, 'whitespace/operators', 3, |
|
3529 | 'Missing spaces around <<') |
|
3530 | ||
3531 | # We allow no-spaces around >> for almost anything. This is because |
|
3532 | # C++11 allows ">>" to close nested templates, which accounts for |
|
3533 | # most cases when ">>" is not followed by a space. |
|
3534 | # |
|
3535 | # We still warn on ">>" followed by alpha character, because that is |
|
3536 | # likely due to ">>" being used for right shifts, e.g.: |
|
3537 | # value >> alpha |
|
3538 | # |
|
3539 | # When ">>" is used to close templates, the alphanumeric letter that |
|
3540 | # follows would be part of an identifier, and there should still be |
|
3541 | # a space separating the template type and the identifier. |
|
3542 | # type<type<type>> alpha |
|
3543 | match = Search(r'>>[a-zA-Z_]', line) |
|
3544 | if match: |
|
3545 | error(filename, linenum, 'whitespace/operators', 3, |
|
3546 | 'Missing spaces around >>') |
|
3547 | ||
3548 | # There shouldn't be space around unary operators |
|
3549 | match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line) |
|
3550 | if match: |
|
3551 | error(filename, linenum, 'whitespace/operators', 4, |
|
3552 | 'Extra space for operator %s' % match.group(1)) |
|
3553 | ||
3554 | ||
3555 | def CheckParenthesisSpacing(filename, clean_lines, linenum, error): |
@@ 3440-3552 (lines=113) @@ | ||
3437 | 'Missing space around colon in range-based for loop') |
|
3438 | ||
3439 | ||
3440 | def CheckOperatorSpacing(filename, clean_lines, linenum, error): |
|
3441 | """Checks for horizontal spacing around operators. |
|
3442 | ||
3443 | Args: |
|
3444 | filename: The name of the current file. |
|
3445 | clean_lines: A CleansedLines instance containing the file. |
|
3446 | linenum: The number of the line to check. |
|
3447 | error: The function to call with any errors found. |
|
3448 | """ |
|
3449 | line = clean_lines.elided[linenum] |
|
3450 | ||
3451 | # Don't try to do spacing checks for operator methods. Do this by |
|
3452 | # replacing the troublesome characters with something else, |
|
3453 | # preserving column position for all other characters. |
|
3454 | # |
|
3455 | # The replacement is done repeatedly to avoid false positives from |
|
3456 | # operators that call operators. |
|
3457 | while True: |
|
3458 | match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line) |
|
3459 | if match: |
|
3460 | line = match.group(1) + ('_' * len(match.group(2))) + match.group(3) |
|
3461 | else: |
|
3462 | break |
|
3463 | ||
3464 | # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )". |
|
3465 | # Otherwise not. Note we only check for non-spaces on *both* sides; |
|
3466 | # sometimes people put non-spaces on one side when aligning ='s among |
|
3467 | # many lines (not that this is behavior that I approve of...) |
|
3468 | if ((Search(r'[\w.]=', line) or |
|
3469 | Search(r'=[\w.]', line)) |
|
3470 | and not Search(r'\b(if|while|for) ', line) |
|
3471 | # Operators taken from [lex.operators] in C++11 standard. |
|
3472 | and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line) |
|
3473 | and not Search(r'operator=', line)): |
|
3474 | error(filename, linenum, 'whitespace/operators', 4, |
|
3475 | 'Missing spaces around =') |
|
3476 | ||
3477 | # It's ok not to have spaces around binary operators like + - * /, but if |
|
3478 | # there's too little whitespace, we get concerned. It's hard to tell, |
|
3479 | # though, so we punt on this one for now. TODO. |
|
3480 | ||
3481 | # You should always have whitespace around binary operators. |
|
3482 | # |
|
3483 | # Check <= and >= first to avoid false positives with < and >, then |
|
3484 | # check non-include lines for spacing around < and >. |
|
3485 | # |
|
3486 | # If the operator is followed by a comma, assume it's be used in a |
|
3487 | # macro context and don't do any checks. This avoids false |
|
3488 | # positives. |
|
3489 | # |
|
3490 | # Note that && is not included here. This is because there are too |
|
3491 | # many false positives due to RValue references. |
|
3492 | match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line) |
|
3493 | if match: |
|
3494 | error(filename, linenum, 'whitespace/operators', 3, |
|
3495 | 'Missing spaces around %s' % match.group(1)) |
|
3496 | elif not Match(r'#.*include', line): |
|
3497 | # Look for < that is not surrounded by spaces. This is only |
|
3498 | # triggered if both sides are missing spaces, even though |
|
3499 | # technically should should flag if at least one side is missing a |
|
3500 | # space. This is done to avoid some false positives with shifts. |
|
3501 | match = Match(r'^(.*[^\s<])<[^\s=<,]', line) |
|
3502 | if match: |
|
3503 | (_, _, end_pos) = CloseExpression( |
|
3504 | clean_lines, linenum, len(match.group(1))) |
|
3505 | if end_pos <= -1: |
|
3506 | error(filename, linenum, 'whitespace/operators', 3, |
|
3507 | 'Missing spaces around <') |
|
3508 | ||
3509 | # Look for > that is not surrounded by spaces. Similar to the |
|
3510 | # above, we only trigger if both sides are missing spaces to avoid |
|
3511 | # false positives with shifts. |
|
3512 | match = Match(r'^(.*[^-\s>])>[^\s=>,]', line) |
|
3513 | if match: |
|
3514 | (_, _, start_pos) = ReverseCloseExpression( |
|
3515 | clean_lines, linenum, len(match.group(1))) |
|
3516 | if start_pos <= -1: |
|
3517 | error(filename, linenum, 'whitespace/operators', 3, |
|
3518 | 'Missing spaces around >') |
|
3519 | ||
3520 | # We allow no-spaces around << when used like this: 10<<20, but |
|
3521 | # not otherwise (particularly, not when used as streams) |
|
3522 | # |
|
3523 | # We also allow operators following an opening parenthesis, since |
|
3524 | # those tend to be macros that deal with operators. |
|
3525 | match = Search(r'(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])', line) |
|
3526 | if (match and not (match.group(1).isdigit() and match.group(2).isdigit()) and |
|
3527 | not (match.group(1) == 'operator' and match.group(2) == ';')): |
|
3528 | error(filename, linenum, 'whitespace/operators', 3, |
|
3529 | 'Missing spaces around <<') |
|
3530 | ||
3531 | # We allow no-spaces around >> for almost anything. This is because |
|
3532 | # C++11 allows ">>" to close nested templates, which accounts for |
|
3533 | # most cases when ">>" is not followed by a space. |
|
3534 | # |
|
3535 | # We still warn on ">>" followed by alpha character, because that is |
|
3536 | # likely due to ">>" being used for right shifts, e.g.: |
|
3537 | # value >> alpha |
|
3538 | # |
|
3539 | # When ">>" is used to close templates, the alphanumeric letter that |
|
3540 | # follows would be part of an identifier, and there should still be |
|
3541 | # a space separating the template type and the identifier. |
|
3542 | # type<type<type>> alpha |
|
3543 | match = Search(r'>>[a-zA-Z_]', line) |
|
3544 | if match: |
|
3545 | error(filename, linenum, 'whitespace/operators', 3, |
|
3546 | 'Missing spaces around >>') |
|
3547 | ||
3548 | # There shouldn't be space around unary operators |
|
3549 | match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line) |
|
3550 | if match: |
|
3551 | error(filename, linenum, 'whitespace/operators', 4, |
|
3552 | 'Extra space for operator %s' % match.group(1)) |
|
3553 | ||
3554 | ||
3555 | def CheckParenthesisSpacing(filename, clean_lines, linenum, error): |