MCPcopy Create free account
hub / github.com/dmtcp/dmtcp / CheckOperatorSpacing

Function CheckOperatorSpacing

util/cpplint.py:3230–3342  ·  view source on GitHub ↗

Checks for horizontal spacing around operators. 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

3228
3229
3230def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3231 """Checks for horizontal spacing around operators.
3232
3233 Args:
3234 filename: The name of the current file.
3235 clean_lines: A CleansedLines instance containing the file.
3236 linenum: The number of the line to check.
3237 error: The function to call with any errors found.
3238 """
3239 line = clean_lines.elided[linenum]
3240
3241 # Don't try to do spacing checks for operator methods. Do this by
3242 # replacing the troublesome characters with something else,
3243 # preserving column position for all other characters.
3244 #
3245 # The replacement is done repeatedly to avoid false positives from
3246 # operators that call operators.
3247 while True:
3248 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line)
3249 if match:
3250 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3)
3251 else:
3252 break
3253
3254 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
3255 # Otherwise not. Note we only check for non-spaces on *both* sides;
3256 # sometimes people put non-spaces on one side when aligning ='s among
3257 # many lines (not that this is behavior that I approve of...)
3258 if ((Search(r'[\w.]=', line) or
3259 Search(r'=[\w.]', line))
3260 and not Search(r'\b(if|while|for) ', line)
3261 # Operators taken from [lex.operators] in C++11 standard.
3262 and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line)
3263 and not Search(r'operator=', line)):
3264 error(filename, linenum, 'whitespace/operators', 4,
3265 'Missing spaces around =')
3266
3267 # It's ok not to have spaces around binary operators like + - * /, but if
3268 # there's too little whitespace, we get concerned. It's hard to tell,
3269 # though, so we punt on this one for now. TODO.
3270
3271 # You should always have whitespace around binary operators.
3272 #
3273 # Check <= and >= first to avoid false positives with < and >, then
3274 # check non-include lines for spacing around < and >.
3275 #
3276 # If the operator is followed by a comma, assume it's be used in a
3277 # macro context and don't do any checks. This avoids false
3278 # positives.
3279 #
3280 # Note that && is not included here. This is because there are too
3281 # many false positives due to RValue references.
3282 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line)
3283 if match:
3284 error(filename, linenum, 'whitespace/operators', 3,
3285 'Missing spaces around %s' % match.group(1))
3286 elif not Match(r'#.*include', line):
3287 # Look for < that is not surrounded by spaces. This is only

Callers 1

CheckStyleFunction · 0.85

Calls 5

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

Tested by

no test coverage detected