Skip to content

Commit 454278a

Browse files
refackaddaleax
authored andcommitted
tools: refloat Node.js patches to cpplint.py
* Preserve 3 node-core checks * Preserve patch to `FileInfo.RepositoryName` * Remove TAP to logfile (unused) PR-URL: #25771 Fixes: #25760 Refs: https://github.com/cpplint/cpplint/blob/3d8f6f876dd6e3918e5641483298dbc82e65f358/cpplint.py Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent b9289f4 commit 454278a

File tree

1 file changed

+87
-51
lines changed

1 file changed

+87
-51
lines changed

tools/cpplint.py

+87-51
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def GetNonHeaderExtensions():
122122
likely to be false positives.
123123
124124
quiet
125-
Supress output other than linting errors, such as information about
125+
Suppress output other than linting errors, such as information about
126126
which files have been processed and excluded.
127127
128128
filter=-x,+y,...
@@ -298,11 +298,13 @@ def GetNonHeaderExtensions():
298298
'readability/constructors',
299299
'readability/fn_size',
300300
'readability/inheritance',
301+
'readability/pointer_notation',
301302
'readability/multiline_comment',
302303
'readability/multiline_string',
303304
'readability/namespace',
304305
'readability/nolint',
305306
'readability/nul',
307+
'readability/null_usage',
306308
'readability/strings',
307309
'readability/todo',
308310
'readability/utf8',
@@ -353,7 +355,11 @@ def GetNonHeaderExtensions():
353355
# flag. By default all errors are on, so only add here categories that should be
354356
# off by default (i.e., categories that must be enabled by the --filter= flags).
355357
# All entries here should start with a '-' or '+', as in the --filter= flag.
356-
_DEFAULT_FILTERS = ['-build/include_alpha']
358+
_DEFAULT_FILTERS = [
359+
'-build/include',
360+
'-build/include_subdir',
361+
'-legal/copyright',
362+
]
357363

358364
# The default list of categories suppressed for C (not C++) files.
359365
_DEFAULT_C_SUPPRESSED_CATEGORIES = [
@@ -626,6 +632,12 @@ def GetNonHeaderExtensions():
626632
# Match string that indicates we're working on a Linux Kernel file.
627633
_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
628634

635+
_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b')
636+
637+
_RIGHT_LEANING_POINTER_PATTERN = re.compile(r'[^=|(,\s><);&?:}]'
638+
r'(?<!(sizeof|return))'
639+
r'\s\*[a-zA-z_][0-9a-zA-z_]*')
640+
629641
_regexp_compile_cache = {}
630642

631643
# {str, set(int)}: a map from error categories to sets of linenumbers
@@ -644,7 +656,7 @@ def GetNonHeaderExtensions():
644656
# Files to exclude from linting. This is set by the --exclude flag.
645657
_excludes = None
646658

647-
# Whether to supress PrintInfo messages
659+
# Whether to suppress PrintInfo messages
648660
_quiet = False
649661

650662
# The allowed line length of files.
@@ -1284,54 +1296,12 @@ def RepositoryName(self):
12841296
locations won't see bogus errors.
12851297
"""
12861298
fullname = self.FullName()
1287-
1288-
if os.path.exists(fullname):
1289-
project_dir = os.path.dirname(fullname)
1290-
1291-
# If the user specified a repository path, it exists, and the file is
1292-
# contained in it, use the specified repository path
1293-
if _repository:
1294-
repo = FileInfo(_repository).FullName()
1295-
root_dir = project_dir
1296-
while os.path.exists(root_dir):
1297-
# allow case insensitive compare on Windows
1298-
if os.path.normcase(root_dir) == os.path.normcase(repo):
1299-
return os.path.relpath(fullname, root_dir).replace('\\', '/')
1300-
one_up_dir = os.path.dirname(root_dir)
1301-
if one_up_dir == root_dir:
1302-
break
1303-
root_dir = one_up_dir
1304-
1305-
if os.path.exists(os.path.join(project_dir, ".svn")):
1306-
# If there's a .svn file in the current directory, we recursively look
1307-
# up the directory tree for the top of the SVN checkout
1308-
root_dir = project_dir
1309-
one_up_dir = os.path.dirname(root_dir)
1310-
while os.path.exists(os.path.join(one_up_dir, ".svn")):
1311-
root_dir = os.path.dirname(root_dir)
1312-
one_up_dir = os.path.dirname(one_up_dir)
1313-
1314-
prefix = os.path.commonprefix([root_dir, project_dir])
1315-
return fullname[len(prefix) + 1:]
1316-
1317-
# Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
1318-
# searching up from the current path.
1319-
root_dir = current_dir = os.path.dirname(fullname)
1320-
while current_dir != os.path.dirname(current_dir):
1321-
if (os.path.exists(os.path.join(current_dir, ".git")) or
1322-
os.path.exists(os.path.join(current_dir, ".hg")) or
1323-
os.path.exists(os.path.join(current_dir, ".svn"))):
1324-
root_dir = current_dir
1325-
current_dir = os.path.dirname(current_dir)
1326-
1327-
if (os.path.exists(os.path.join(root_dir, ".git")) or
1328-
os.path.exists(os.path.join(root_dir, ".hg")) or
1329-
os.path.exists(os.path.join(root_dir, ".svn"))):
1330-
prefix = os.path.commonprefix([root_dir, project_dir])
1331-
return fullname[len(prefix) + 1:]
1332-
1333-
# Don't know what to do; header guard warnings may be wrong...
1334-
return fullname
1299+
# XXX(bnoordhuis) Expects that cpplint.py lives in the tools/ directory.
1300+
toplevel = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')).replace('\\', '/')
1301+
toplevel = unicode_escape_decode(toplevel)
1302+
prefix = os.path.commonprefix([fullname, toplevel])
1303+
return fullname[len(prefix) + 1:]
1304+
# End Node.js patch
13351305

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

21502120

2121+
def CheckInlineHeader(filename, include_state, error):
2122+
"""Logs an error if both a header and its inline variant are included."""
2123+
2124+
all_headers = dict(item for sublist in include_state.include_list
2125+
for item in sublist)
2126+
bad_headers = set('%s.h' % name[:-6] for name in all_headers.keys()
2127+
if name.endswith('-inl.h'))
2128+
bad_headers &= set(all_headers.keys())
2129+
2130+
for name in bad_headers:
2131+
err = '%s includes both %s and %s-inl.h' % (filename, name, name)
2132+
linenum = all_headers[name]
2133+
error(filename, linenum, 'build/include', 5, err)
2134+
2135+
21512136
def CheckForNewlineAtEOF(filename, lines, error):
21522137
"""Logs an error if there is no newline char at the end of the file.
21532138
@@ -4425,6 +4410,49 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
44254410
'Use operator %s instead of %s' % (
44264411
_ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
44274412

4413+
def CheckNullTokens(filename, clean_lines, linenum, error):
4414+
"""Check NULL usage.
4415+
4416+
Args:
4417+
filename: The name of the current file.
4418+
clean_lines: A CleansedLines instance containing the file.
4419+
linenum: The number of the line to check.
4420+
error: The function to call with any errors found.
4421+
"""
4422+
line = clean_lines.elided[linenum]
4423+
4424+
# Avoid preprocessor lines
4425+
if Match(r'^\s*#', line):
4426+
return
4427+
4428+
if line.find('/*') >= 0 or line.find('*/') >= 0:
4429+
return
4430+
4431+
for match in _NULL_TOKEN_PATTERN.finditer(line):
4432+
error(filename, linenum, 'readability/null_usage', 2,
4433+
'Use nullptr instead of NULL')
4434+
4435+
def CheckLeftLeaningPointer(filename, clean_lines, linenum, error):
4436+
"""Check for left-leaning pointer placement.
4437+
4438+
Args:
4439+
filename: The name of the current file.
4440+
clean_lines: A CleansedLines instance containing the file.
4441+
linenum: The number of the line to check.
4442+
error: The function to call with any errors found.
4443+
"""
4444+
line = clean_lines.elided[linenum]
4445+
4446+
# Avoid preprocessor lines
4447+
if Match(r'^\s*#', line):
4448+
return
4449+
4450+
if '/*' in line or '*/' in line:
4451+
return
4452+
4453+
for match in _RIGHT_LEANING_POINTER_PATTERN.finditer(line):
4454+
error(filename, linenum, 'readability/pointer_notation', 2,
4455+
'Use left leaning pointer instead of right leaning')
44284456

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

4508+
if line.find('template<') != -1:
4509+
error(filename, linenum, 'whitespace/template', 1,
4510+
'Leave a single space after template, as in `template <...>`')
4511+
44804512
# One or three blank spaces at the beginning of the line is weird; it's
44814513
# hard to reconcile that with 2-space indents.
44824514
# NOTE: here are the conditions rob pike used for his tests. Mine aren't
@@ -4570,6 +4602,8 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
45704602
CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
45714603
CheckCheck(filename, clean_lines, linenum, error)
45724604
CheckAltTokens(filename, clean_lines, linenum, error)
4605+
CheckNullTokens(filename, clean_lines, linenum, error)
4606+
CheckLeftLeaningPointer(filename, clean_lines, linenum, error)
45734607
classinfo = nesting_state.InnermostClass()
45744608
if classinfo:
45754609
CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
@@ -6112,6 +6146,8 @@ def ProcessFileData(filename, file_extension, lines, error,
61126146

61136147
CheckForNewlineAtEOF(filename, lines, error)
61146148

6149+
CheckInlineHeader(filename, include_state, error)
6150+
61156151
def ProcessConfigOverrides(filename):
61166152
""" Loads the configuration files and processes the config overrides.
61176153

0 commit comments

Comments
 (0)