Skip to content

Commit ec49262

Browse files
committed
tools: refloat 10 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 <[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: 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 <[email protected]> Reviewed-By: James M Snell <[email protected]> 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 <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> 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 <[email protected]> Refs: nodejs@9029981 Cherry-pick 0a25ace Original commit message: tools: move cpplint configuration to .cpplint PR-URL: nodejs#27098 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> 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 <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Rich Trott <[email protected]> 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 <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Backport 3d954dc Original commit message: tools: remove readability/fn_size rule PR-URL: nodejs#54663 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Refs: nodejs@3d954dc Cherry-pick c7d7ec7 Original commit message: tools: check for std::vector<v8::Local> in lint PR-URL: nodejs#58497 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Refs: nodejs@c7d7ec7 Cherry-pick e6d94ef Original commit message: tools: add C++ lint rule to avoid using `String::Utf8Value` We should be using our own helpers for this instead. PR-URL: nodejs#60244 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Edy Silva <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ilyas Shabi <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Vladimir Morozov <[email protected]> Refs: nodejs@e6d94ef
1 parent efc08e8 commit ec49262

File tree

2 files changed

+148
-10
lines changed

2 files changed

+148
-10
lines changed

.cpplint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
set noparent
2-
filter=-build/include_alpha,-build/include_subdir,-build/include_what_you_use,-legal/copyright,-readability/nolint,-readability/braces
2+
filter=-build/include_alpha,-build/include_subdir,-build/include_what_you_use,-legal/copyright,-readability/nolint,-readability/braces,-readability/fn_size
33
linelength=80

tools/cpplint.py

Lines changed: 147 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,10 @@
306306
"build/forward_decl",
307307
"build/header_guard",
308308
"build/include",
309-
"build/include_subdir",
310309
"build/include_alpha",
310+
"build/include_inline",
311311
"build/include_order",
312+
"build/include_subdir",
312313
"build/include_what_you_use",
313314
"build/namespaces_headers",
314315
"build/namespaces/header/block/literals",
@@ -351,6 +352,7 @@
351352
"runtime/string",
352353
"runtime/threadsafe_fn",
353354
"runtime/vlog",
355+
"runtime/v8_persistent",
354356
"whitespace/blank_line",
355357
"whitespace/braces",
356358
"whitespace/comma",
@@ -920,6 +922,13 @@
920922
# Match string that indicates we're working on a Linux Kernel file.
921923
_SEARCH_KERNEL_FILE = re.compile(r"\b(?:LINT_KERNEL_FILE)")
922924

925+
_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b')
926+
927+
928+
_RIGHT_LEANING_POINTER_PATTERN = re.compile(r'[^=|(,\s><);&?:}]'
929+
r'(?<!(sizeof|return))'
930+
r'\s\*[a-zA-z_][0-9a-zA-z_]*')
931+
923932
# Commands for sed to fix the problem
924933
_SED_FIXUPS = {
925934
"Remove spaces around =": r"s/ = /=/",
@@ -970,7 +979,7 @@
970979
_include_order = "default"
971980

972981
# This allows different config files to be used
973-
_config_filename = "CPPLINT.cfg"
982+
_config_filename = ".cpplint"
974983

975984
# Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc.
976985
# This is set by --headers flag.
@@ -1256,10 +1265,10 @@ class _IncludeState:
12561265
# needs to move backwards, CheckNextIncludeOrder will raise an error.
12571266
_INITIAL_SECTION = 0
12581267
_MY_H_SECTION = 1
1259-
_C_SECTION = 2
1260-
_CPP_SECTION = 3
1261-
_OTHER_SYS_SECTION = 4
1262-
_OTHER_H_SECTION = 5
1268+
_OTHER_H_SECTION = 2
1269+
_C_SECTION = 3
1270+
_CPP_SECTION = 4
1271+
_OTHER_SYS_SECTION = 5
12631272

12641273
_TYPE_NAMES = {
12651274
_C_SYS_HEADER: "C system header",
@@ -1272,10 +1281,10 @@ class _IncludeState:
12721281
_SECTION_NAMES = {
12731282
_INITIAL_SECTION: "... nothing. (This can't be an error.)",
12741283
_MY_H_SECTION: "a header this file implements",
1284+
_OTHER_H_SECTION: "other header",
12751285
_C_SECTION: "C system header",
12761286
_CPP_SECTION: "C++ system header",
12771287
_OTHER_SYS_SECTION: "other system header",
1278-
_OTHER_H_SECTION: "other header",
12791288
}
12801289

12811290
def __init__(self):
@@ -2794,6 +2803,21 @@ def CheckForBadCharacters(filename, lines, error):
27942803
error(filename, linenum, "readability/nul", 5, "Line contains NUL byte.")
27952804

27962805

2806+
def CheckInlineHeader(filename, include_state, error):
2807+
"""Logs an error if both a header and its inline variant are included."""
2808+
2809+
all_headers = dict(item for sublist in include_state.include_list
2810+
for item in sublist)
2811+
bad_headers = set('%s.h' % name[:-6] for name in all_headers.keys()
2812+
if name.endswith('-inl.h'))
2813+
bad_headers &= set(all_headers.keys())
2814+
2815+
for name in bad_headers:
2816+
err = '%s includes both %s and %s-inl.h' % (filename, name, name)
2817+
linenum = all_headers[name]
2818+
error(filename, linenum, 'build/include_inline', 5, err)
2819+
2820+
27972821
def CheckForNewlineAtEOF(filename, lines, error):
27982822
"""Logs an error if there is no newline char at the end of the file.
27992823
@@ -4044,7 +4068,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum, function_state, erro
40444068
"""Reports for long function bodies.
40454069
40464070
For an overview why this is done, see:
4047-
https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
4071+
https://google.github.io/styleguide/cppguide.html#Write_Short_Functions
40484072
40494073
Uses a simplistic algorithm assuming other style guidelines
40504074
(especially spacing) are followed.
@@ -5432,6 +5456,75 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
54325456
)
54335457

54345458

5459+
def CheckNullTokens(filename, clean_lines, linenum, error):
5460+
"""Check NULL usage.
5461+
5462+
Args:
5463+
filename: The name of the current file.
5464+
clean_lines: A CleansedLines instance containing the file.
5465+
linenum: The number of the line to check.
5466+
error: The function to call with any errors found.
5467+
"""
5468+
line = clean_lines.elided[linenum]
5469+
5470+
# Avoid preprocessor lines
5471+
if Match(r'^\s*#', line):
5472+
return
5473+
5474+
if line.find('/*') >= 0 or line.find('*/') >= 0:
5475+
return
5476+
5477+
for match in _NULL_TOKEN_PATTERN.finditer(line):
5478+
error(filename, linenum, 'readability/null_usage', 2,
5479+
'Use nullptr instead of NULL')
5480+
5481+
5482+
def CheckV8PersistentTokens(filename, clean_lines, linenum, error):
5483+
"""Check v8::Persistent usage.
5484+
5485+
Args:
5486+
filename: The name of the current file.
5487+
clean_lines: A CleansedLines instance containing the file.
5488+
linenum: The number of the line to check.
5489+
error: The function to call with any errors found.
5490+
"""
5491+
line = clean_lines.elided[linenum]
5492+
5493+
# Avoid preprocessor lines
5494+
if Match(r'^\s*#', line):
5495+
return
5496+
5497+
if line.find('/*') >= 0 or line.find('*/') >= 0:
5498+
return
5499+
5500+
for match in _V8_PERSISTENT_PATTERN.finditer(line):
5501+
error(filename, linenum, 'runtime/v8_persistent', 2,
5502+
'Use v8::Global instead of v8::Persistent')
5503+
5504+
5505+
def CheckLeftLeaningPointer(filename, clean_lines, linenum, error):
5506+
"""Check for left-leaning pointer placement.
5507+
5508+
Args:
5509+
filename: The name of the current file.
5510+
clean_lines: A CleansedLines instance containing the file.
5511+
linenum: The number of the line to check.
5512+
error: The function to call with any errors found.
5513+
"""
5514+
line = clean_lines.elided[linenum]
5515+
5516+
# Avoid preprocessor lines
5517+
if Match(r'^\s*#', line):
5518+
return
5519+
5520+
if '/*' in line or '*/' in line:
5521+
return
5522+
5523+
for match in _RIGHT_LEANING_POINTER_PATTERN.finditer(line):
5524+
error(filename, linenum, 'readability/null_usage', 2,
5525+
'Use left leaning pointer instead of right leaning')
5526+
5527+
54355528
def GetLineWidth(line):
54365529
"""Determines the width of the line in column positions.
54375530
@@ -5603,6 +5696,9 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, er
56035696
CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
56045697
CheckCheck(filename, clean_lines, linenum, error)
56055698
CheckAltTokens(filename, clean_lines, linenum, error)
5699+
CheckNullTokens(filename, clean_lines, linenum, error)
5700+
CheckV8PersistentTokens(filename, clean_lines, linenum, error)
5701+
CheckLeftLeaningPointer(filename, clean_lines, linenum, error)
56065702
classinfo = nesting_state.InnermostClass()
56075703
if classinfo:
56085704
CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
@@ -6155,7 +6251,7 @@ def CheckLanguage(
61556251
"build/namespaces_headers",
61566252
4,
61576253
"Do not use unnamed namespaces in header files. See "
6158-
"https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces"
6254+
"https://google.github.io/styleguide/cppguide.html#Namespaces"
61596255
" for more information.",
61606256
)
61616257

@@ -7313,6 +7409,42 @@ def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum, er
73137409
)
73147410

73157411

7412+
def CheckLocalVectorUsage(filename, lines, error):
7413+
"""Logs an error if std::vector<v8::Local<T>> is used.
7414+
Args:
7415+
filename: The name of the current file.
7416+
lines: An array of strings, each representing a line of the file.
7417+
error: The function to call with any errors found.
7418+
"""
7419+
for linenum, line in enumerate(lines):
7420+
if (Search(r'\bstd::vector<v8::Local<[^>]+>>', line) or
7421+
Search(r'\bstd::vector<Local<[^>]+>>', line)):
7422+
error(filename, linenum, 'runtime/local_vector', 5,
7423+
'Do not use std::vector<v8::Local<T>>. '
7424+
'Use v8::LocalVector<T> instead.')
7425+
7426+
7427+
def CheckStringValueUsage(filename, lines, error):
7428+
"""Logs an error if v8's String::Value/Utf8Value are used.
7429+
Args:
7430+
filename: The name of the current file.
7431+
lines: An array of strings, each representing a line of the file.
7432+
error: The function to call with any errors found.
7433+
"""
7434+
if filename.startswith('test/') or filename.startswith('test\\'):
7435+
return # Skip test files, where Node.js headers may not be available
7436+
7437+
for linenum, line in enumerate(lines):
7438+
if Search(r'\bString::Utf8Value\b', line):
7439+
error(filename, linenum, 'runtime/v8_string_value', 5,
7440+
'Do not use v8::String::Utf8Value. '
7441+
'Use node::Utf8Value instead.')
7442+
if Search(r'\bString::Value\b', line):
7443+
error(filename, linenum, 'runtime/v8_string_value', 5,
7444+
'Do not use v8::String::Value. '
7445+
'Use node::TwoByteValue instead.')
7446+
7447+
73167448
def ProcessLine(
73177449
filename,
73187450
file_extension,
@@ -7470,6 +7602,12 @@ def ProcessFileData(filename, file_extension, lines, error, extra_check_function
74707602

74717603
CheckForNewlineAtEOF(filename, lines, error)
74727604

7605+
CheckInlineHeader(filename, include_state, error)
7606+
7607+
CheckLocalVectorUsage(filename, lines, error)
7608+
7609+
CheckStringValueUsage(filename, lines, error)
7610+
74737611

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

0 commit comments

Comments
 (0)