src, tools: add check for left leaning pointers

This commit adds a rule to cpplint to check that pointers in the code
base lean to the left and not right, and also fixes the violations
reported.

PR-URL: https://github.com/nodejs/node/pull/21010
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Daniel Bevenius 2018-05-16 11:50:48 +02:00
parent 75e9165988
commit cbc3dd997e
23 changed files with 114 additions and 87 deletions

27
tools/cpplint.py vendored
View File

@ -527,6 +527,10 @@ _SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b')
_RIGHT_LEANING_POINTER_PATTERN = re.compile(r'[^=|(,\s><);&?:}]'
r'(?<!(sizeof|return))'
r'\s\*[a-zA-z_][0-9a-zA-z_]*')
_regexp_compile_cache = {}
# {str, set(int)}: a map from error categories to sets of linenumbers
@ -4179,6 +4183,28 @@ def CheckNullTokens(filename, clean_lines, linenum, error):
error(filename, linenum, 'readability/null_usage', 2,
'Use nullptr instead of NULL')
def CheckLeftLeaningPointer(filename, clean_lines, linenum, error):
"""Check for left-leaning pointer placement.
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.
"""
line = clean_lines.elided[linenum]
# Avoid preprocessor lines
if Match(r'^\s*#', line):
return
if '/*' in line or '*/' in line:
return
for match in _RIGHT_LEANING_POINTER_PATTERN.finditer(line):
error(filename, linenum, 'readability/null_usage', 2,
'Use left leaning pointer instead of right leaning')
def GetLineWidth(line):
"""Determines the width of the line in column positions.
@ -4321,6 +4347,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
CheckCheck(filename, clean_lines, linenum, error)
CheckAltTokens(filename, clean_lines, linenum, error)
CheckNullTokens(filename, clean_lines, linenum, error)
CheckLeftLeaningPointer(filename, clean_lines, linenum, error)
classinfo = nesting_state.InnermostClass()
if classinfo:
CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)