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

Function CheckCStyleCast

scripts/cpp_lint.py:4251–4342  ·  view source on GitHub ↗

Checks for a C-style cast by looking for the pattern. Args: filename: The name of the current file. linenum: The number of the line to check. line: The line of code to check. raw_line: The raw line of code to check, with comments. cast_type: The string for the C++ cast to reco

(filename, linenum, line, raw_line, cast_type, pattern,
                    error)

Source from the content-addressed store, hash-verified

4249
4250
4251def CheckCStyleCast(filename, linenum, line, raw_line, cast_type, pattern,
4252 error):
4253 """Checks for a C-style cast by looking for the pattern.
4254
4255 Args:
4256 filename: The name of the current file.
4257 linenum: The number of the line to check.
4258 line: The line of code to check.
4259 raw_line: The raw line of code to check, with comments.
4260 cast_type: The string for the C++ cast to recommend. This is either
4261 reinterpret_cast, static_cast, or const_cast, depending.
4262 pattern: The regular expression used to find C-style casts.
4263 error: The function to call with any errors found.
4264
4265 Returns:
4266 True if an error was emitted.
4267 False otherwise.
4268 """
4269 match = Search(pattern, line)
4270 if not match:
4271 return False
4272
4273 # Exclude lines with sizeof, since sizeof looks like a cast.
4274 sizeof_match = Match(r'.*sizeof\s*$', line[0:match.start(1) - 1])
4275 if sizeof_match:
4276 return False
4277
4278 # operator++(int) and operator--(int)
4279 if (line[0:match.start(1) - 1].endswith(' operator++') or
4280 line[0:match.start(1) - 1].endswith(' operator--')):
4281 return False
4282
4283 # A single unnamed argument for a function tends to look like old
4284 # style cast. If we see those, don't issue warnings for deprecated
4285 # casts, instead issue warnings for unnamed arguments where
4286 # appropriate.
4287 #
4288 # These are things that we want warnings for, since the style guide
4289 # explicitly require all parameters to be named:
4290 # Function(int);
4291 # Function(int) {
4292 # ConstMember(int) const;
4293 # ConstMember(int) const {
4294 # ExceptionMember(int) throw (...);
4295 # ExceptionMember(int) throw (...) {
4296 # PureVirtual(int) = 0;
4297 #
4298 # These are functions of some sort, where the compiler would be fine
4299 # if they had named parameters, but people often omit those
4300 # identifiers to reduce clutter:
4301 # (FunctionPointer)(int);
4302 # (FunctionPointer)(int) = value;
4303 # Function((function_pointer_arg)(int))
4304 # <TemplateArgument(int)>;
4305 # <(FunctionPointerTemplateArgument)(int)>;
4306 remainder = line[match.end(0):]
4307 if Match(r'^\s*(?:;|const\b|throw\b|=|>|\{|\))', remainder):
4308 # Looks like an unnamed parameter.

Callers 1

CheckLanguageFunction · 0.85

Calls 3

SearchFunction · 0.85
MatchFunction · 0.85
endMethod · 0.45

Tested by

no test coverage detected