third_party/blink/tools/blinkpy/style/checkers/cpp.py
def check_conditional_and_loop_bodies_for_brace_violations(
        clean_lines, line_number, error):
    """Scans the bodies of conditionals and loops, and in particular
    all the arms of conditionals, for violations in the use of braces.

    Specifically:

    (1) If an arm omits braces, then the following statement must be on one
    physical line.
    (2) If any arm uses braces, all arms must use them.

    These checks are only done here if we find the start of an
    'if/for/foreach/while' statement, because this function fails fast
    if it encounters constructs it doesn't understand. Checks
    elsewhere validate other constraints, such as requiring '}' and
    'else' to be on the same line.

    Args:
      clean_lines: A CleansedLines instance containing the file.
      line_number: The number of the line to check.
      error: The function to call with any errors found.
    """

    # We work with the elided lines. Comments have been removed, but line
    # numbers are preserved, so we can still find situations where
    # single-expression control clauses span multiple lines, or when a
    # comment preceded the expression.
    lines = clean_lines.elided
    line = lines[line_number]

    # Match control structures.
    control_match = match(r'\s*(if|foreach|for|while)\s*\(', line)
    if not control_match:
        return

    # Found the start of a conditional or loop.

    # The following loop handles all potential arms of the control clause.
    # The initial conditions are the following:
    #   - We start on the opening paren '(' of the condition, *unless* we are
    #     handling an 'else' block, in which case there is no condition.
    #   - In the latter case, we start at the position just beyond the 'else'
    #     token.
    expect_conditional_expression = True
    know_whether_using_braces = False
    using_braces = False
    search_for_else_clause = control_match.group(1) == 'if'
    current_pos = Position(line_number, control_match.end() - 1)

    while True:
        if expect_conditional_expression:
            # Try to find the end of the conditional expression,
            # potentially spanning multiple lines.
            open_paren_pos = current_pos
            close_paren_pos = close_expression(lines, open_paren_pos)
            if close_paren_pos.column < 0:
                return
            current_pos = close_paren_pos

        end_line_of_conditional = current_pos.row

        # Find the start of the body.
        current_pos = _find_in_lines(r'\S', lines, current_pos, None)
        if not current_pos:
            return

        current_arm_uses_brace = False
        if lines[current_pos.row][current_pos.column] == '{':
            current_arm_uses_brace = True
        if know_whether_using_braces:
            if using_braces != current_arm_uses_brace:
                error(
                    current_pos.row, 'whitespace/braces', 4,
                    'If one part of an if-else statement uses curly braces, the other part must too.'
                )
                return
        know_whether_using_braces = True
        using_braces = current_arm_uses_brace

        if using_braces:
            # Skip over the entire arm.
            current_pos = close_expression(lines, current_pos)
            if current_pos.column < 0:
                return
        else:
            # Skip over the current expression.
            current_pos = _find_in_lines(r';', lines, current_pos, None)
            if not current_pos:
                return
            # If the end of the expression is beyond the line just after
            # the close parenthesis or control clause, we've found a
            # single-expression arm that spans multiple lines. (We don't
            # fire this error for expressions ending on the same line; that
            # is a different error, handled elsewhere.)
            if current_pos.row > 1 + end_line_of_conditional:
                error(
                    current_pos.row, 'whitespace/braces', 4,
                    'A conditional or loop body must use braces if the statement is more than one line long.'
                )
                return
            current_pos = Position(current_pos.row, 1 + current_pos.column)

        # At this point current_pos points just past the end of the last
        # arm. If we just handled the last control clause, we're done.
        if not search_for_else_clause:
            return

        # Scan forward for the next non-whitespace character, and see
        # whether we are continuing a conditional (with an 'else' or
        # 'else if'), or are done.
        current_pos = _find_in_lines(r'\S', lines, current_pos, None)
        if not current_pos:
            return
        next_nonspace_string = lines[current_pos.row][current_pos.column:]
        next_conditional = match(r'(else\s*if|else)', next_nonspace_string)
        if not next_conditional:
            # Done processing this 'if' and all arms.
            return
        if next_conditional.group(1) == 'else if':
            current_pos = _find_in_lines(r'\(', lines, current_pos, None)
        else:
            current_pos.column += 4  # skip 'else'
            expect_conditional_expression = False
            search_for_else_clause = False
    # End while loop
Scans the bodies of conditionals and loops, and in particular
    all the arms of conditionals, for violations in the use of braces.

    Specifically:

    (1) If an arm omits braces, then the following statement must be on one
    physical line.
    (2) If any arm uses braces, all arms must use them.

    These checks are only done here if we find the start of an
    'if/for/foreach/while' statement, because this function fails fast
    if it encounters constructs it doesn't understand. Checks
    elsewhere validate other constraints, such as requiring '}' and
    'else' to be on the same line.

    Args:
      clean_lines: A CleansedLines instance containing the file.
      line_number: The number of the line to check.
      error: The function to call with any errors found.
    
Scans the bodies of conditionals and loops, and in particular
all the arms of conditionals, for violations in the use of braces.



(1) If an arm omits braces, then the following statement must be on one
physical line.
(2) If any arm uses braces, all arms must use them.

These checks are only done here if we find the start of an
'if/for/foreach/while' statement, because this function fails fast
if it encounters constructs it doesn't understand. Checks
elsewhere validate other constraints, such as requiring '}' and
'else' to be on the same line.
Scans the bodies of conditionals and loops and in particular all the arms of conditionals for violations in the use of braces . ( 1 ) If an arm omits braces then the following statement must be on one physical line . ( 2 ) If any arm uses braces all arms must use them . These checks are only done here if we find the start of an ' if / for / foreach / while ' statement because this function fails fast if it encounters constructs it doesn ' t understand . Checks elsewhere validate other constraints such as requiring ' } ' and ' else ' to be on the same line .
def check_conditional_and_loop_bodies_for_brace_violations(
        clean_lines, line_number, error):
    lines = clean_lines.elided
    line = lines[line_number]
    control_match = match(r'\s*(if|foreach|for|while)\s*\(', line)
    if not control_match:
        return
    expect_conditional_expression = True
    know_whether_using_braces = False
    using_braces = False
    search_for_else_clause = control_match.group(1) == 'if'
    current_pos = Position(line_number, control_match.end() - 1)
    while True:
        if expect_conditional_expression:
            open_paren_pos = current_pos
            close_paren_pos = close_expression(lines, open_paren_pos)
            if close_paren_pos.column < 0:
                return
            current_pos = close_paren_pos
        end_line_of_conditional = current_pos.row
        current_pos = _find_in_lines(r'\S', lines, current_pos, None)
        if not current_pos:
            return
        current_arm_uses_brace = False
        if lines[current_pos.row][current_pos.column] == '{':
            current_arm_uses_brace = True
        if know_whether_using_braces:
            if using_braces != current_arm_uses_brace:
                error(
                    current_pos.row, 'whitespace/braces', 4,
                    'If one part of an if-else statement uses curly braces, the other part must too.'
                )
                return
        know_whether_using_braces = True
        using_braces = current_arm_uses_brace
        if using_braces:
            current_pos = close_expression(lines, current_pos)
            if current_pos.column < 0:
                return
        else:
            current_pos = _find_in_lines(r';', lines, current_pos, None)
            if not current_pos:
                return
            if current_pos.row > 1 + end_line_of_conditional:
                error(
                    current_pos.row, 'whitespace/braces', 4,
                    'A conditional or loop body must use braces if the statement is more than one line long.'
                )
                return
            current_pos = Position(current_pos.row, 1 + current_pos.column)
        if not search_for_else_clause:
            return
        current_pos = _find_in_lines(r'\S', lines, current_pos, None)
        if not current_pos:
            return
        next_nonspace_string = lines[current_pos.row][current_pos.column:]
        next_conditional = match(r'(else\s*if|else)', next_nonspace_string)
        if not next_conditional:
            return
        if next_conditional.group(1) == 'else if':
            current_pos = _find_in_lines(r'\(', lines, current_pos, None)
        else:
            current_pos.column += 4  
            expect_conditional_expression = False
            search_for_else_clause = False
def check_conditional_and_loop_bodies_for_brace_violations ( clean_lines , line_number , error ) : lines = clean_lines . elided line = lines [ line_number ] control_match = match ( r'\s*(if|foreach|for|while)\s*\(' , line ) if not control_match : return expect_conditional_expression = True know_whether_using_braces = False using_braces = False search_for_else_clause = control_match . group ( 1 ) == 'if' current_pos = Position ( line_number , control_match . end ( ) - 1 ) while True : if expect_conditional_expression : open_paren_pos = current_pos close_paren_pos = close_expression ( lines , open_paren_pos ) if close_paren_pos . column < 0 : return current_pos = close_paren_pos end_line_of_conditional = current_pos . row current_pos = _find_in_lines ( r'\S' , lines , current_pos , None ) if not current_pos : return current_arm_uses_brace = False if lines [ current_pos . row ] [ current_pos . column ] == '{' : current_arm_uses_brace = True if know_whether_using_braces : if using_braces != current_arm_uses_brace : error ( current_pos . row , 'whitespace/braces' , 4 , 'If one part of an if-else statement uses curly braces, the other part must too.' ) return know_whether_using_braces = True using_braces = current_arm_uses_brace if using_braces : current_pos = close_expression ( lines , current_pos ) if current_pos . column < 0 : return else : current_pos = _find_in_lines ( r';' , lines , current_pos , None ) if not current_pos : return if current_pos . row > 1 + end_line_of_conditional : error ( current_pos . row , 'whitespace/braces' , 4 , 'A conditional or loop body must use braces if the statement is more than one line long.' ) return current_pos = Position ( current_pos . row , 1 + current_pos . column ) if not search_for_else_clause : return current_pos = _find_in_lines ( r'\S' , lines , current_pos , None ) if not current_pos : return next_nonspace_string = lines [ current_pos . row ] [ current_pos . column : ] next_conditional = match ( r'(else\s*if|else)' , next_nonspace_string ) if not next_conditional : return if next_conditional . group ( 1 ) == 'else if' : current_pos = _find_in_lines ( r'\(' , lines , current_pos , None ) else : current_pos . column += 4 expect_conditional_expression = False search_for_else_clause = False
Scans the bodies of conditionals and loops, and in particular
all the arms of conditionals, for violations in the use of braces.
Scans the bodies of conditionals and loops and in particular all the arms of conditionals for violations in the use of braces .
"""Scans the bodies of conditionals and loops, and in particular
    all the arms of conditionals, for violations in the use of braces.

    Specifically:

    (1) If an arm omits braces, then the following statement must be on one
    physical line.
    (2) If any arm uses braces, all arms must use them.

    These checks are only done here if we find the start of an
    'if/for/foreach/while' statement, because this function fails fast
    if it encounters constructs it doesn't understand. Checks
    elsewhere validate other constraints, such as requiring '}' and
    'else' to be on the same line.

    Args:
      clean_lines: A CleansedLines instance containing the file.
      line_number: The number of the line to check.
      error: The function to call with any errors found.
    """ # We work with the elided lines. Comments have been removed, but line # numbers are preserved, so we can still find situations where # single-expression control clauses span multiple lines, or when a # comment preceded the expression. # Match control structures. # Found the start of a conditional or loop. # The following loop handles all potential arms of the control clause. # The initial conditions are the following: #   - We start on the opening paren '(' of the condition, *unless* we are #     handling an 'else' block, in which case there is no condition. #   - In the latter case, we start at the position just beyond the 'else' #     token. # Try to find the end of the conditional expression, # potentially spanning multiple lines. # Find the start of the body. # Skip over the entire arm. # Skip over the current expression. # If the end of the expression is beyond the line just after # the close parenthesis or control clause, we've found a # single-expression arm that spans multiple lines. (We don't # fire this error for expressions ending on the same line; that # is a different error, handled elsewhere.) # At this point current_pos points just past the end of the last # arm. If we just handled the last control clause, we're done. # Scan forward for the next non-whitespace character, and see # whether we are continuing a conditional (with an 'else' or # 'else if'), or are done. # Done processing this 'if' and all arms. # skip 'else' # End while loop
clean_lines
A CleansedLines instance containing the file.
A CleansedLines instance containing the file .
line_number
The number of the line to check.
The number of the line to check .
error
The function to call with any errors found.
The function to call with any errors found .