Skip to content

Commit e65e866

Browse files
committed
tools: refloat 7 Node.js patches to cpplint.py
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: nodejs#17373 Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Refs: nodejs@12c8b4d Cherry-pick fc81e80 Original commit message: Update cpplint.py to check for inline headers when the corresponding header is already included. PR-URL: nodejs#21521 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Refs: nodejs@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: nodejs#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> Refs: nodejs@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: nodejs#26306 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Refs: nodejs@9029981 Cherry-pick 0a25ace Original commit message: tools: move cpplint configuration to .cpplint PR-URL: nodejs#27098 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Refs: nodejs@0a25ace Cherry-pick afa9a72 Original commit message: tools: refloat update link to google styleguide for cpplint This commit updates two old links to Google's C++ styleguide which currently result in a 404 when accessed. PR-URL: nodejs#30876 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Refs: nodejs@afa9a72 Cherry-pick e23bf8f Original commit message: tools,src: refloat forbid usage of v8::Persistent `v8::Persistent` comes with the surprising catch that it requires manual cleanup. `v8::Global` doesn’t, making it easier to use, and additionally provides move semantics. New code should always use `v8::Global`. PR-URL: nodejs#31018 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> PR-URL: nodejs#35569 Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> PR-URL: nodejs#35719 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: nodejs#35866 PR-URL: nodejs#36213 Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> PR-URL: nodejs#36235 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> PR-URL: nodejs#36324 Reviewed-By: Beth Griggs <bgriggs@redhat.com> PR-URL: nodejs#38851 Reviewed-By: Khaidi Chu <i@2333.moe>
1 parent ba7329d commit e65e866

File tree

1 file changed

+109
-12
lines changed

1 file changed

+109
-12
lines changed

tools/cpplint.py

+109-12
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@
300300
'build/include',
301301
'build/include_subdir',
302302
'build/include_alpha',
303+
'build/include_inline',
303304
'build/include_order',
304305
'build/include_what_you_use',
305306
'build/namespaces_headers',
@@ -315,11 +316,13 @@
315316
'readability/constructors',
316317
'readability/fn_size',
317318
'readability/inheritance',
319+
'readability/pointer_notation',
318320
'readability/multiline_comment',
319321
'readability/multiline_string',
320322
'readability/namespace',
321323
'readability/nolint',
322324
'readability/nul',
325+
'readability/null_usage',
323326
'readability/strings',
324327
'readability/todo',
325328
'readability/utf8',
@@ -339,6 +342,7 @@
339342
'runtime/string',
340343
'runtime/threadsafe_fn',
341344
'runtime/vlog',
345+
'runtime/v8_persistent',
342346
'whitespace/blank_line',
343347
'whitespace/braces',
344348
'whitespace/comma',
@@ -847,6 +851,14 @@
847851
'Missing space after ,': r's/,\([^ ]\)/, \1/g',
848852
}
849853

854+
_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b')
855+
856+
_V8_PERSISTENT_PATTERN = re.compile(r'\bv8::Persistent\b')
857+
858+
_RIGHT_LEANING_POINTER_PATTERN = re.compile(r'[^=|(,\s><);&?:}]'
859+
r'(?<!(sizeof|return))'
860+
r'\s\*[a-zA-Z_][0-9a-zA-Z_]*')
861+
850862
_regexp_compile_cache = {}
851863

852864
# {str, set(int)}: a map from error categories to sets of linenumbers
@@ -1087,10 +1099,11 @@ class _IncludeState(object):
10871099
# needs to move backwards, CheckNextIncludeOrder will raise an error.
10881100
_INITIAL_SECTION = 0
10891101
_MY_H_SECTION = 1
1090-
_C_SECTION = 2
1091-
_CPP_SECTION = 3
1092-
_OTHER_SYS_SECTION = 4
1093-
_OTHER_H_SECTION = 5
1102+
_OTHER_H_SECTION = 2
1103+
_OTHER_SYS_SECTION = 3
1104+
_C_SECTION = 4
1105+
_CPP_SECTION = 5
1106+
10941107

10951108
_TYPE_NAMES = {
10961109
_C_SYS_HEADER: 'C system header',
@@ -2527,6 +2540,21 @@ def CheckForBadCharacters(filename, lines, error):
25272540
error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
25282541

25292542

2543+
def CheckInlineHeader(filename, include_state, error):
2544+
"""Logs an error if both a header and its inline variant are included."""
2545+
2546+
all_headers = dict(item for sublist in include_state.include_list
2547+
for item in sublist)
2548+
bad_headers = set('%s.h' % name[:-6] for name in all_headers.keys()
2549+
if name.endswith('-inl.h'))
2550+
bad_headers &= set(all_headers.keys())
2551+
2552+
for name in bad_headers:
2553+
err = '%s includes both %s and %s-inl.h' % (filename, name, name)
2554+
linenum = all_headers[name]
2555+
error(filename, linenum, 'build/include_inline', 5, err)
2556+
2557+
25302558
def CheckForNewlineAtEOF(filename, lines, error):
25312559
"""Logs an error if there is no newline char at the end of the file.
25322560
@@ -3550,7 +3578,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum,
35503578
"""Reports for long function bodies.
35513579
35523580
For an overview why this is done, see:
3553-
https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
3581+
https://google.github.io/styleguide/cppguide.html#Write_Short_Functions
35543582
35553583
Uses a simplistic algorithm assuming other style guidelines
35563584
(especially spacing) are followed.
@@ -4777,6 +4805,71 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
47774805
'Use operator %s instead of %s' % (
47784806
_ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
47794807

4808+
def CheckNullTokens(filename, clean_lines, linenum, error):
4809+
"""Check NULL usage.
4810+
4811+
Args:
4812+
filename: The name of the current file.
4813+
clean_lines: A CleansedLines instance containing the file.
4814+
linenum: The number of the line to check.
4815+
error: The function to call with any errors found.
4816+
"""
4817+
line = clean_lines.elided[linenum]
4818+
4819+
# Avoid preprocessor lines
4820+
if Match(r'^\s*#', line):
4821+
return
4822+
4823+
if line.find('/*') >= 0 or line.find('*/') >= 0:
4824+
return
4825+
4826+
for match in _NULL_TOKEN_PATTERN.finditer(line):
4827+
error(filename, linenum, 'readability/null_usage', 2,
4828+
'Use nullptr instead of NULL')
4829+
4830+
def CheckV8PersistentTokens(filename, clean_lines, linenum, error):
4831+
"""Check v8::Persistent usage.
4832+
4833+
Args:
4834+
filename: The name of the current file.
4835+
clean_lines: A CleansedLines instance containing the file.
4836+
linenum: The number of the line to check.
4837+
error: The function to call with any errors found.
4838+
"""
4839+
line = clean_lines.elided[linenum]
4840+
4841+
# Avoid preprocessor lines
4842+
if Match(r'^\s*#', line):
4843+
return
4844+
4845+
if line.find('/*') >= 0 or line.find('*/') >= 0:
4846+
return
4847+
4848+
for match in _V8_PERSISTENT_PATTERN.finditer(line):
4849+
error(filename, linenum, 'runtime/v8_persistent', 2,
4850+
'Use v8::Global instead of v8::Persistent')
4851+
4852+
def CheckLeftLeaningPointer(filename, clean_lines, linenum, error):
4853+
"""Check for left-leaning pointer placement.
4854+
4855+
Args:
4856+
filename: The name of the current file.
4857+
clean_lines: A CleansedLines instance containing the file.
4858+
linenum: The number of the line to check.
4859+
error: The function to call with any errors found.
4860+
"""
4861+
line = clean_lines.elided[linenum]
4862+
4863+
# Avoid preprocessor lines
4864+
if Match(r'^\s*#', line):
4865+
return
4866+
4867+
if '/*' in line or '*/' in line:
4868+
return
4869+
4870+
for match in _RIGHT_LEANING_POINTER_PATTERN.finditer(line):
4871+
error(filename, linenum, 'readability/pointer_notation', 2,
4872+
'Use left leaning pointer instead of right leaning')
47804873

47814874
def GetLineWidth(line):
47824875
"""Determines the width of the line in column positions.
@@ -4931,6 +5024,9 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
49315024
CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
49325025
CheckCheck(filename, clean_lines, linenum, error)
49335026
CheckAltTokens(filename, clean_lines, linenum, error)
5027+
CheckNullTokens(filename, clean_lines, linenum, error)
5028+
CheckV8PersistentTokens(filename, clean_lines, linenum, error)
5029+
CheckLeftLeaningPointer(filename, clean_lines, linenum, error)
49345030
classinfo = nesting_state.InnermostClass()
49355031
if classinfo:
49365032
CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
@@ -5118,11 +5214,10 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
51185214
include_state.include_list[-1].append((include, linenum))
51195215

51205216
# We want to ensure that headers appear in the right order:
5121-
# 1) for foo.cc, foo.h (preferred location)
5122-
# 2) c system files
5123-
# 3) cpp system files
5124-
# 4) for foo.cc, foo.h (deprecated location)
5125-
# 5) other google headers
5217+
# 1) for foo.cc, foo.h
5218+
# 2) other project headers
5219+
# 3) c system files
5220+
# 4) cpp system files
51265221
#
51275222
# We classify each include statement as one of those 5 types
51285223
# using a number of techniques. The include_state object keeps
@@ -5385,7 +5480,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
53855480
and line[-1] != '\\'):
53865481
error(filename, linenum, 'build/namespaces_headers', 4,
53875482
'Do not use unnamed namespaces in header files. See '
5388-
'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
5483+
'https://google.github.io/styleguide/cppguide.html#Namespaces'
53895484
' for more information.')
53905485

53915486

@@ -6507,6 +6602,8 @@ def ProcessFileData(filename, file_extension, lines, error,
65076602

65086603
CheckForNewlineAtEOF(filename, lines, error)
65096604

6605+
CheckInlineHeader(filename, include_state, error)
6606+
65106607
def ProcessConfigOverrides(filename):
65116608
""" Loads the configuration files and processes the config overrides.
65126609
@@ -6525,7 +6622,7 @@ def ProcessConfigOverrides(filename):
65256622
if not base_name:
65266623
break # Reached the root directory.
65276624

6528-
cfg_file = os.path.join(abs_path, "CPPLINT.cfg")
6625+
cfg_file = os.path.join(abs_path, ".cpplint")
65296626
abs_filename = abs_path
65306627
if not os.path.isfile(cfg_file):
65316628
continue

0 commit comments

Comments
 (0)