Skip to content

Commit 9333d5b

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: #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 Cherry-pick 0a25ace Original commit message: tools: move cpplint configuration to .cpplint PR-URL: #27098 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Refs: 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: #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: 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: #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: #54663 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Refs: 3d954dc Cherry-pick c7d7ec7 Original commit message: tools: check for std::vector<v8::Local> in lint PR-URL: #58497 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Refs: 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: #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: e6d94ef PR-URL: #60901 Fixes: #60771
1 parent f9776cb commit 9333d5b

File tree

1 file changed

+148
-9
lines changed

1 file changed

+148
-9
lines changed

tools/cpplint.py

Lines changed: 148 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,14 @@
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+
_V8_PERSISTENT_PATTERN = re.compile(r'\bv8::Persistent\b')
928+
929+
_RIGHT_LEANING_POINTER_PATTERN = re.compile(r'[^=|(,\s><);&?:}]'
930+
r'(?<!(sizeof|return))'
931+
r'\s\*[a-zA-z_][0-9a-zA-z_]*')
932+
923933
# Commands for sed to fix the problem
924934
_SED_FIXUPS = {
925935
"Remove spaces around =": r"s/ = /=/",
@@ -970,7 +980,7 @@
970980
_include_order = "default"
971981

972982
# This allows different config files to be used
973-
_config_filename = "CPPLINT.cfg"
983+
_config_filename = ".cpplint"
974984

975985
# Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc.
976986
# This is set by --headers flag.
@@ -1256,10 +1266,10 @@ class _IncludeState:
12561266
# needs to move backwards, CheckNextIncludeOrder will raise an error.
12571267
_INITIAL_SECTION = 0
12581268
_MY_H_SECTION = 1
1259-
_C_SECTION = 2
1260-
_CPP_SECTION = 3
1261-
_OTHER_SYS_SECTION = 4
1262-
_OTHER_H_SECTION = 5
1269+
_OTHER_H_SECTION = 2
1270+
_C_SECTION = 3
1271+
_CPP_SECTION = 4
1272+
_OTHER_SYS_SECTION = 5
12631273

12641274
_TYPE_NAMES = {
12651275
_C_SYS_HEADER: "C system header",
@@ -1272,10 +1282,10 @@ class _IncludeState:
12721282
_SECTION_NAMES = {
12731283
_INITIAL_SECTION: "... nothing. (This can't be an error.)",
12741284
_MY_H_SECTION: "a header this file implements",
1285+
_OTHER_H_SECTION: "other header",
12751286
_C_SECTION: "C system header",
12761287
_CPP_SECTION: "C++ system header",
12771288
_OTHER_SYS_SECTION: "other system header",
1278-
_OTHER_H_SECTION: "other header",
12791289
}
12801290

12811291
def __init__(self):
@@ -2794,6 +2804,21 @@ def CheckForBadCharacters(filename, lines, error):
27942804
error(filename, linenum, "readability/nul", 5, "Line contains NUL byte.")
27952805

27962806

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

54345459

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

@@ -7313,6 +7410,42 @@ def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum, er
73137410
)
73147411

73157412

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

74717604
CheckForNewlineAtEOF(filename, lines, error)
74727605

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

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

0 commit comments

Comments
 (0)