Skip to content

Commit

Permalink
tools: refloat Node.js patches to cpplint.py
Browse files Browse the repository at this point in the history
* Preserve 3 node-core checks
* Preserve patch to `FileInfo.RepositoryName`
* Remove TAP to logfile (unused)

PR-URL: #25771
Fixes: #25760
Refs: https:/cpplint/cpplint/blob/3d8f6f876dd6e3918e5641483298dbc82e65f358/cpplint.py
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
  • Loading branch information
refack authored and addaleax committed Feb 1, 2019
1 parent b9289f4 commit 454278a
Showing 1 changed file with 87 additions and 51 deletions.
138 changes: 87 additions & 51 deletions tools/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def GetNonHeaderExtensions():
likely to be false positives.
quiet
Supress output other than linting errors, such as information about
Suppress output other than linting errors, such as information about
which files have been processed and excluded.
filter=-x,+y,...
Expand Down Expand Up @@ -298,11 +298,13 @@ def GetNonHeaderExtensions():
'readability/constructors',
'readability/fn_size',
'readability/inheritance',
'readability/pointer_notation',
'readability/multiline_comment',
'readability/multiline_string',
'readability/namespace',
'readability/nolint',
'readability/nul',
'readability/null_usage',
'readability/strings',
'readability/todo',
'readability/utf8',
Expand Down Expand Up @@ -353,7 +355,11 @@ def GetNonHeaderExtensions():
# flag. By default all errors are on, so only add here categories that should be
# off by default (i.e., categories that must be enabled by the --filter= flags).
# All entries here should start with a '-' or '+', as in the --filter= flag.
_DEFAULT_FILTERS = ['-build/include_alpha']
_DEFAULT_FILTERS = [
'-build/include',
'-build/include_subdir',
'-legal/copyright',
]

# The default list of categories suppressed for C (not C++) files.
_DEFAULT_C_SUPPRESSED_CATEGORIES = [
Expand Down Expand Up @@ -626,6 +632,12 @@ def GetNonHeaderExtensions():
# Match string that indicates we're working on a Linux Kernel file.
_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
Expand All @@ -644,7 +656,7 @@ def GetNonHeaderExtensions():
# Files to exclude from linting. This is set by the --exclude flag.
_excludes = None

# Whether to supress PrintInfo messages
# Whether to suppress PrintInfo messages
_quiet = False

# The allowed line length of files.
Expand Down Expand Up @@ -1284,54 +1296,12 @@ def RepositoryName(self):
locations won't see bogus errors.
"""
fullname = self.FullName()

if os.path.exists(fullname):
project_dir = os.path.dirname(fullname)

# If the user specified a repository path, it exists, and the file is
# contained in it, use the specified repository path
if _repository:
repo = FileInfo(_repository).FullName()
root_dir = project_dir
while os.path.exists(root_dir):
# allow case insensitive compare on Windows
if os.path.normcase(root_dir) == os.path.normcase(repo):
return os.path.relpath(fullname, root_dir).replace('\\', '/')
one_up_dir = os.path.dirname(root_dir)
if one_up_dir == root_dir:
break
root_dir = one_up_dir

if os.path.exists(os.path.join(project_dir, ".svn")):
# If there's a .svn file in the current directory, we recursively look
# up the directory tree for the top of the SVN checkout
root_dir = project_dir
one_up_dir = os.path.dirname(root_dir)
while os.path.exists(os.path.join(one_up_dir, ".svn")):
root_dir = os.path.dirname(root_dir)
one_up_dir = os.path.dirname(one_up_dir)

prefix = os.path.commonprefix([root_dir, project_dir])
return fullname[len(prefix) + 1:]

# Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
# searching up from the current path.
root_dir = current_dir = os.path.dirname(fullname)
while current_dir != os.path.dirname(current_dir):
if (os.path.exists(os.path.join(current_dir, ".git")) or
os.path.exists(os.path.join(current_dir, ".hg")) or
os.path.exists(os.path.join(current_dir, ".svn"))):
root_dir = current_dir
current_dir = os.path.dirname(current_dir)

if (os.path.exists(os.path.join(root_dir, ".git")) or
os.path.exists(os.path.join(root_dir, ".hg")) or
os.path.exists(os.path.join(root_dir, ".svn"))):
prefix = os.path.commonprefix([root_dir, project_dir])
return fullname[len(prefix) + 1:]

# Don't know what to do; header guard warnings may be wrong...
return fullname
# XXX(bnoordhuis) Expects that cpplint.py lives in the tools/ directory.
toplevel = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')).replace('\\', '/')
toplevel = unicode_escape_decode(toplevel)
prefix = os.path.commonprefix([fullname, toplevel])
return fullname[len(prefix) + 1:]
# End Node.js patch

def Split(self):
"""Splits the file into the directory, basename, and extension.
Expand Down Expand Up @@ -2148,6 +2118,21 @@ def CheckForBadCharacters(filename, lines, error):
error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')


def CheckInlineHeader(filename, include_state, error):
"""Logs an error if both a header and its inline variant are included."""

all_headers = dict(item for sublist in include_state.include_list
for item in sublist)
bad_headers = set('%s.h' % name[:-6] for name in all_headers.keys()
if name.endswith('-inl.h'))
bad_headers &= set(all_headers.keys())

for name in bad_headers:
err = '%s includes both %s and %s-inl.h' % (filename, name, name)
linenum = all_headers[name]
error(filename, linenum, 'build/include', 5, err)


def CheckForNewlineAtEOF(filename, lines, error):
"""Logs an error if there is no newline char at the end of the file.
Expand Down Expand Up @@ -4425,6 +4410,49 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
'Use operator %s instead of %s' % (
_ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))

def CheckNullTokens(filename, clean_lines, linenum, error):
"""Check NULL usage.
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 line.find('/*') >= 0 or line.find('*/') >= 0:
return

for match in _NULL_TOKEN_PATTERN.finditer(line):
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/pointer_notation', 2,
'Use left leaning pointer instead of right leaning')

def GetLineWidth(line):
"""Determines the width of the line in column positions.
Expand Down Expand Up @@ -4477,6 +4505,10 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
error(filename, linenum, 'whitespace/tab', 1,
'Tab found; better to use spaces')

if line.find('template<') != -1:
error(filename, linenum, 'whitespace/template', 1,
'Leave a single space after template, as in `template <...>`')

# One or three blank spaces at the beginning of the line is weird; it's
# hard to reconcile that with 2-space indents.
# NOTE: here are the conditions rob pike used for his tests. Mine aren't
Expand Down Expand Up @@ -4570,6 +4602,8 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
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)
Expand Down Expand Up @@ -6112,6 +6146,8 @@ def ProcessFileData(filename, file_extension, lines, error,

CheckForNewlineAtEOF(filename, lines, error)

CheckInlineHeader(filename, include_state, error)

def ProcessConfigOverrides(filename):
""" Loads the configuration files and processes the config overrides.
Expand Down

0 comments on commit 454278a

Please sign in to comment.