MCPcopy Create free account
hub / github.com/apache/mesos / CheckCStyleCast

Function CheckCStyleCast

support/cpplint.py:5271–5321  ·  view source on GitHub ↗

Checks for a C-style cast by looking for the pattern. Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. cast_type: The string for the C++ cast to recommend. This is either reint

(filename, clean_lines, linenum, cast_type, pattern, error)

Source from the content-addressed store, hash-verified

5269
5270
5271def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
5272 """Checks for a C-style cast by looking for the pattern.
5273
5274 Args:
5275 filename: The name of the current file.
5276 clean_lines: A CleansedLines instance containing the file.
5277 linenum: The number of the line to check.
5278 cast_type: The string for the C++ cast to recommend. This is either
5279 reinterpret_cast, static_cast, or const_cast, depending.
5280 pattern: The regular expression used to find C-style casts.
5281 error: The function to call with any errors found.
5282
5283 Returns:
5284 True if an error was emitted.
5285 False otherwise.
5286 """
5287 line = clean_lines.elided[linenum]
5288 match = Search(pattern, line)
5289 if not match:
5290 return False
5291
5292 # Exclude lines with keywords that tend to look like casts
5293 context = line[0:match.start(1) - 1]
5294 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context):
5295 return False
5296
5297 # Try expanding current context to see if we one level of
5298 # parentheses inside a macro.
5299 if linenum > 0:
5300 for i in xrange(linenum - 1, max(0, linenum - 5), -1):
5301 context = clean_lines.elided[i] + context
5302 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
5303 return False
5304
5305 # operator++(int) and operator--(int)
5306 if context.endswith(' operator++') or context.endswith(' operator--'):
5307 return False
5308
5309 # A single unnamed argument for a function tends to look like old style cast.
5310 # If we see those, don't issue warnings for deprecated casts.
5311 remainder = line[match.end(0):]
5312 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)',
5313 remainder):
5314 return False
5315
5316 # At this point, all that should be left is actual casts.
5317 error(filename, linenum, 'readability/casting', 4,
5318 'Using C-style cast. Use %s<%s>(...) instead' %
5319 (cast_type, match.group(1)))
5320
5321 return True
5322
5323
5324def ExpectingFunctionArgs(clean_lines, linenum):

Callers 1

CheckCastsFunction · 0.85

Calls 6

SearchFunction · 0.85
MatchFunction · 0.85
maxFunction · 0.85
errorFunction · 0.85
startMethod · 0.65
endMethod · 0.45

Tested by

no test coverage detected