MCPcopy Create free account
hub / github.com/OpenPTrack/open_ptrack_v2 / CheckBraces

Function CheckBraces

rtpose_wrapper/scripts/cpp_lint.py:3069–3240  ·  view source on GitHub ↗

Looks for misplaced braces (e.g. at the end of line). Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. error: The function to call with any errors found.

(filename, clean_lines, linenum, error)

Source from the content-addressed store, hash-verified

3067
3068
3069def CheckBraces(filename, clean_lines, linenum, error):
3070 """Looks for misplaced braces (e.g. at the end of line).
3071
3072 Args:
3073 filename: The name of the current file.
3074 clean_lines: A CleansedLines instance containing the file.
3075 linenum: The number of the line to check.
3076 error: The function to call with any errors found.
3077 """
3078
3079 line = clean_lines.elided[linenum] # get rid of comments and strings
3080
3081 if Match(r'\s*{\s*$', line):
3082 # We allow an open brace to start a line in the case where someone is using
3083 # braces in a block to explicitly create a new scope, which is commonly used
3084 # to control the lifetime of stack-allocated variables. Braces are also
3085 # used for brace initializers inside function calls. We don't detect this
3086 # perfectly: we just don't complain if the last non-whitespace character on
3087 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
3088 # previous line starts a preprocessor block.
3089 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3090 if (not Search(r'[,;:}{(]\s*$', prevline) and
3091 not Match(r'\s*#', prevline)):
3092 error(filename, linenum, 'whitespace/braces', 4,
3093 '{ should almost always be at the end of the previous line')
3094
3095 # An else clause should be on the same line as the preceding closing brace.
3096 if Match(r'\s*else\s*', line):
3097 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3098 if Match(r'\s*}\s*$', prevline):
3099 error(filename, linenum, 'whitespace/newline', 4,
3100 'An else should appear on the same line as the preceding }')
3101
3102 # If braces come on one side of an else, they should be on both.
3103 # However, we have to worry about "else if" that spans multiple lines!
3104 if Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
3105 if Search(r'}\s*else if([^{]*)$', line): # could be multi-line if
3106 # find the ( after the if
3107 pos = line.find('else if')
3108 pos = line.find('(', pos)
3109 if pos > 0:
3110 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
3111 if endline[endpos:].find('{') == -1: # must be brace after if
3112 error(filename, linenum, 'readability/braces', 5,
3113 'If an else has a brace on one side, it should have it on both')
3114 else: # common case: else not followed by a multi-line if
3115 error(filename, linenum, 'readability/braces', 5,
3116 'If an else has a brace on one side, it should have it on both')
3117
3118 # Likewise, an else should never have the else clause on the same line
3119 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
3120 error(filename, linenum, 'whitespace/newline', 4,
3121 'Else clause should never be on same line as else (use 2 lines)')
3122
3123 # In the same way, a do/while should never be on one line
3124 if Match(r'\s*do [^\s{]', line):
3125 error(filename, linenum, 'whitespace/newline', 4,
3126 'do/while clauses should not be on a single line')

Callers 1

CheckStyleFunction · 0.85

Calls 6

MatchFunction · 0.85
GetPreviousNonBlankLineFunction · 0.85
SearchFunction · 0.85
errorFunction · 0.85
CloseExpressionFunction · 0.85
ReverseCloseExpressionFunction · 0.85

Tested by

no test coverage detected