MCPcopy Create free account
hub / github.com/BVLC/caffe / CheckBraces

Function CheckBraces

scripts/cpp_lint.py:3073–3244  ·  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

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

Callers 1

CheckStyleFunction · 0.85

Calls 5

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

Tested by

no test coverage detected