Skip to content

Commit cd2987f

Browse files
committed
tools: refloat 4 Node.js patches to cpplint.py
* Add 3 Node.js checks * Adjest cpplint.py header rules to Node.js convention Cherry-pick 12c8b4d Original commit message: This commit is a suggestion for adding a rule for NULL usages in the code base. This will currently report a number of errors which could be ignored using // NOLINT (readability/null_usage) PR-URL: #17373 Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Refs: 12c8b4d Cherry-pick fc81e80 Original commit message: Update cpplint.py to check for inline headers when the corresponding header is already included. PR-URL: #21521 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Refs: fc81e80 Cherry-pick cbc3dd9 Original commit message: 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: #21010 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Refs: cbc3dd9 Cherry-pick 9029981 Original commit message: tools: fix cpplint.py header rules THIS COMMIT SHOULD GO WITH THE NEXT. IT WILL FIND NEW LINT. PR-URL: #26306 Reviewed-By: Gireesh Punathil <[email protected]> Refs: 9029981 PR-URL: #27098 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent 1302e01 commit cd2987f

File tree

1 file changed

+80
-10
lines changed

1 file changed

+80
-10
lines changed

tools/cpplint.py

+80-10
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@
280280
'build/include',
281281
'build/include_subdir',
282282
'build/include_alpha',
283+
'build/include_inline',
283284
'build/include_order',
284285
'build/include_what_you_use',
285286
'build/namespaces_literals',
@@ -294,11 +295,13 @@
294295
'readability/constructors',
295296
'readability/fn_size',
296297
'readability/inheritance',
298+
'readability/pointer_notation',
297299
'readability/multiline_comment',
298300
'readability/multiline_string',
299301
'readability/namespace',
300302
'readability/nolint',
301303
'readability/nul',
304+
'readability/null_usage',
302305
'readability/strings',
303306
'readability/todo',
304307
'readability/utf8',
@@ -622,6 +625,12 @@
622625
# Match string that indicates we're working on a Linux Kernel file.
623626
_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
624627

628+
_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b')
629+
630+
_RIGHT_LEANING_POINTER_PATTERN = re.compile(r'[^=|(,\s><);&?:}]'
631+
r'(?<!(sizeof|return))'
632+
r'\s\*[a-zA-Z_][0-9a-zA-Z_]*')
633+
625634
_regexp_compile_cache = {}
626635

627636
# {str, set(int)}: a map from error categories to sets of linenumbers
@@ -641,7 +650,7 @@
641650
# Files to exclude from linting. This is set by the --exclude flag.
642651
_excludes = None
643652

644-
# Whether to supress PrintInfo messages
653+
# Whether to suppress PrintInfo messages
645654
_quiet = False
646655

647656
# The allowed line length of files.
@@ -841,9 +850,9 @@ class _IncludeState(object):
841850
# needs to move backwards, CheckNextIncludeOrder will raise an error.
842851
_INITIAL_SECTION = 0
843852
_MY_H_SECTION = 1
844-
_C_SECTION = 2
845-
_CPP_SECTION = 3
846-
_OTHER_H_SECTION = 4
853+
_OTHER_H_SECTION = 2
854+
_C_SECTION = 3
855+
_CPP_SECTION = 4
847856

848857
_TYPE_NAMES = {
849858
_C_SYS_HEADER: 'C system header',
@@ -855,9 +864,9 @@ class _IncludeState(object):
855864
_SECTION_NAMES = {
856865
_INITIAL_SECTION: "... nothing. (This can't be an error.)",
857866
_MY_H_SECTION: 'a header this file implements',
867+
_OTHER_H_SECTION: 'other header',
858868
_C_SECTION: 'C system header',
859869
_CPP_SECTION: 'C++ system header',
860-
_OTHER_H_SECTION: 'other header',
861870
}
862871

863872
def __init__(self):
@@ -2253,6 +2262,21 @@ def CheckForBadCharacters(filename, lines, error):
22532262
error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
22542263

22552264

2265+
def CheckInlineHeader(filename, include_state, error):
2266+
"""Logs an error if both a header and its inline variant are included."""
2267+
2268+
all_headers = dict(item for sublist in include_state.include_list
2269+
for item in sublist)
2270+
bad_headers = set('%s.h' % name[:-6] for name in all_headers.keys()
2271+
if name.endswith('-inl.h'))
2272+
bad_headers &= set(all_headers.keys())
2273+
2274+
for name in bad_headers:
2275+
err = '%s includes both %s and %s-inl.h' % (filename, name, name)
2276+
linenum = all_headers[name]
2277+
error(filename, linenum, 'build/include_inline', 5, err)
2278+
2279+
22562280
def CheckForNewlineAtEOF(filename, lines, error):
22572281
"""Logs an error if there is no newline char at the end of the file.
22582282
@@ -4501,6 +4525,49 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
45014525
'Use operator %s instead of %s' % (
45024526
_ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
45034527

4528+
def CheckNullTokens(filename, clean_lines, linenum, error):
4529+
"""Check NULL usage.
4530+
4531+
Args:
4532+
filename: The name of the current file.
4533+
clean_lines: A CleansedLines instance containing the file.
4534+
linenum: The number of the line to check.
4535+
error: The function to call with any errors found.
4536+
"""
4537+
line = clean_lines.elided[linenum]
4538+
4539+
# Avoid preprocessor lines
4540+
if Match(r'^\s*#', line):
4541+
return
4542+
4543+
if line.find('/*') >= 0 or line.find('*/') >= 0:
4544+
return
4545+
4546+
for match in _NULL_TOKEN_PATTERN.finditer(line):
4547+
error(filename, linenum, 'readability/null_usage', 2,
4548+
'Use nullptr instead of NULL')
4549+
4550+
def CheckLeftLeaningPointer(filename, clean_lines, linenum, error):
4551+
"""Check for left-leaning pointer placement.
4552+
4553+
Args:
4554+
filename: The name of the current file.
4555+
clean_lines: A CleansedLines instance containing the file.
4556+
linenum: The number of the line to check.
4557+
error: The function to call with any errors found.
4558+
"""
4559+
line = clean_lines.elided[linenum]
4560+
4561+
# Avoid preprocessor lines
4562+
if Match(r'^\s*#', line):
4563+
return
4564+
4565+
if '/*' in line or '*/' in line:
4566+
return
4567+
4568+
for match in _RIGHT_LEANING_POINTER_PATTERN.finditer(line):
4569+
error(filename, linenum, 'readability/pointer_notation', 2,
4570+
'Use left leaning pointer instead of right leaning')
45044571

45054572
def GetLineWidth(line):
45064573
"""Determines the width of the line in column positions.
@@ -4655,6 +4722,8 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
46554722
CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
46564723
CheckCheck(filename, clean_lines, linenum, error)
46574724
CheckAltTokens(filename, clean_lines, linenum, error)
4725+
CheckNullTokens(filename, clean_lines, linenum, error)
4726+
CheckLeftLeaningPointer(filename, clean_lines, linenum, error)
46584727
classinfo = nesting_state.InnermostClass()
46594728
if classinfo:
46604729
CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
@@ -4819,11 +4888,10 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
48194888
include_state.include_list[-1].append((include, linenum))
48204889

48214890
# We want to ensure that headers appear in the right order:
4822-
# 1) for foo.cc, foo.h (preferred location)
4823-
# 2) c system files
4824-
# 3) cpp system files
4825-
# 4) for foo.cc, foo.h (deprecated location)
4826-
# 5) other google headers
4891+
# 1) for foo.cc, foo.h
4892+
# 2) other project headers
4893+
# 3) c system files
4894+
# 4) cpp system files
48274895
#
48284896
# We classify each include statement as one of those 5 types
48294897
# using a number of techniques. The include_state object keeps
@@ -6197,6 +6265,8 @@ def ProcessFileData(filename, file_extension, lines, error,
61976265

61986266
CheckForNewlineAtEOF(filename, lines, error)
61996267

6268+
CheckInlineHeader(filename, include_state, error)
6269+
62006270
def ProcessConfigOverrides(filename):
62016271
""" Loads the configuration files and processes the config overrides.
62026272

0 commit comments

Comments
 (0)