MCPcopy Create free account
hub / github.com/alibaba/GraphScope / CheckCStyleCast

Function CheckCStyleCast

analytical_engine/misc/cpplint.py:5815–5865  ·  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

5813
5814
5815def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
5816 """Checks for a C-style cast by looking for the pattern.
5817
5818 Args:
5819 filename: The name of the current file.
5820 clean_lines: A CleansedLines instance containing the file.
5821 linenum: The number of the line to check.
5822 cast_type: The string for the C++ cast to recommend. This is either
5823 reinterpret_cast, static_cast, or const_cast, depending.
5824 pattern: The regular expression used to find C-style casts.
5825 error: The function to call with any errors found.
5826
5827 Returns:
5828 True if an error was emitted.
5829 False otherwise.
5830 """
5831 line = clean_lines.elided[linenum]
5832 match = Search(pattern, line)
5833 if not match:
5834 return False
5835
5836 # Exclude lines with keywords that tend to look like casts
5837 context = line[0:match.start(1) - 1]
5838 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context):
5839 return False
5840
5841 # Try expanding current context to see if we one level of
5842 # parentheses inside a macro.
5843 if linenum > 0:
5844 for i in xrange(linenum - 1, max(0, linenum - 5), -1):
5845 context = clean_lines.elided[i] + context
5846 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
5847 return False
5848
5849 # operator++(int) and operator--(int)
5850 if context.endswith(' operator++') or context.endswith(' operator--'):
5851 return False
5852
5853 # A single unnamed argument for a function tends to look like old style cast.
5854 # If we see those, don't issue warnings for deprecated casts.
5855 remainder = line[match.end(0):]
5856 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)',
5857 remainder):
5858 return False
5859
5860 # At this point, all that should be left is actual casts.
5861 error(filename, linenum, 'readability/casting', 4,
5862 'Using C-style cast. Use %s<%s>(...) instead' %
5863 (cast_type, match.group(1)))
5864
5865 return True
5866
5867
5868def ExpectingFunctionArgs(clean_lines, linenum):

Callers 1

CheckCastsFunction · 0.85

Calls 5

SearchFunction · 0.85
MatchFunction · 0.70
startMethod · 0.65
endMethod · 0.65
groupMethod · 0.45

Tested by

no test coverage detected