Skip to content

Commit 3269c2a

Browse files
Trotttargos
authored andcommitted
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: #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]> PR-URL: #35569 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> PR-URL: #35719 Reviewed-By: Antoine du Hamel <[email protected]> PR-URL: #35866 PR-URL: #36213 Reviewed-By: Franziska Hinkelmann <[email protected]> PR-URL: #36235 Reviewed-By: Luigi Pinca <[email protected]>
1 parent 8aa1daf commit 3269c2a

File tree

1 file changed

+109
-13
lines changed

1 file changed

+109
-13
lines changed

tools/cpplint.py

+109-13
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@
295295
'build/include',
296296
'build/include_subdir',
297297
'build/include_alpha',
298+
'build/include_inline',
298299
'build/include_order',
299300
'build/include_what_you_use',
300301
'build/namespaces_headers',
@@ -310,11 +311,13 @@
310311
'readability/constructors',
311312
'readability/fn_size',
312313
'readability/inheritance',
314+
'readability/pointer_notation',
313315
'readability/multiline_comment',
314316
'readability/multiline_string',
315317
'readability/namespace',
316318
'readability/nolint',
317319
'readability/nul',
320+
'readability/null_usage',
318321
'readability/strings',
319322
'readability/todo',
320323
'readability/utf8',
@@ -334,6 +337,7 @@
334337
'runtime/string',
335338
'runtime/threadsafe_fn',
336339
'runtime/vlog',
340+
'runtime/v8_persistent',
337341
'whitespace/blank_line',
338342
'whitespace/braces',
339343
'whitespace/comma',
@@ -842,6 +846,14 @@
842846
'Missing space after ,': r's/,\([^ ]\)/, \1/g',
843847
}
844848

849+
_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b')
850+
851+
_V8_PERSISTENT_PATTERN = re.compile(r'\bv8::Persistent\b')
852+
853+
_RIGHT_LEANING_POINTER_PATTERN = re.compile(r'[^=|(,\s><);&?:}]'
854+
r'(?<!(sizeof|return))'
855+
r'\s\*[a-zA-Z_][0-9a-zA-Z_]*')
856+
845857
_regexp_compile_cache = {}
846858

847859
# {str, set(int)}: a map from error categories to sets of linenumbers
@@ -1082,10 +1094,11 @@ class _IncludeState(object):
10821094
# needs to move backwards, CheckNextIncludeOrder will raise an error.
10831095
_INITIAL_SECTION = 0
10841096
_MY_H_SECTION = 1
1085-
_C_SECTION = 2
1086-
_CPP_SECTION = 3
1087-
_OTHER_SYS_SECTION = 4
1088-
_OTHER_H_SECTION = 5
1097+
_OTHER_H_SECTION = 2
1098+
_OTHER_SYS_SECTION = 3
1099+
_C_SECTION = 4
1100+
_CPP_SECTION = 5
1101+
10891102

10901103
_TYPE_NAMES = {
10911104
_C_SYS_HEADER: 'C system header',
@@ -2520,6 +2533,21 @@ def CheckForBadCharacters(filename, lines, error):
25202533
error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
25212534

25222535

2536+
def CheckInlineHeader(filename, include_state, error):
2537+
"""Logs an error if both a header and its inline variant are included."""
2538+
2539+
all_headers = dict(item for sublist in include_state.include_list
2540+
for item in sublist)
2541+
bad_headers = set('%s.h' % name[:-6] for name in all_headers.keys()
2542+
if name.endswith('-inl.h'))
2543+
bad_headers &= set(all_headers.keys())
2544+
2545+
for name in bad_headers:
2546+
err = '%s includes both %s and %s-inl.h' % (filename, name, name)
2547+
linenum = all_headers[name]
2548+
error(filename, linenum, 'build/include_inline', 5, err)
2549+
2550+
25232551
def CheckForNewlineAtEOF(filename, lines, error):
25242552
"""Logs an error if there is no newline char at the end of the file.
25252553
@@ -3543,7 +3571,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum,
35433571
"""Reports for long function bodies.
35443572
35453573
For an overview why this is done, see:
3546-
https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
3574+
https://google.github.io/styleguide/cppguide.html#Write_Short_Functions
35473575
35483576
Uses a simplistic algorithm assuming other style guidelines
35493577
(especially spacing) are followed.
@@ -4769,6 +4797,71 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
47694797
'Use operator %s instead of %s' % (
47704798
_ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
47714799

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

47734866
def GetLineWidth(line):
47744867
"""Determines the width of the line in column positions.
@@ -4923,6 +5016,9 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
49235016
CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
49245017
CheckCheck(filename, clean_lines, linenum, error)
49255018
CheckAltTokens(filename, clean_lines, linenum, error)
5019+
CheckNullTokens(filename, clean_lines, linenum, error)
5020+
CheckV8PersistentTokens(filename, clean_lines, linenum, error)
5021+
CheckLeftLeaningPointer(filename, clean_lines, linenum, error)
49265022
classinfo = nesting_state.InnermostClass()
49275023
if classinfo:
49285024
CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
@@ -5108,11 +5204,10 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
51085204
include_state.include_list[-1].append((include, linenum))
51095205

51105206
# We want to ensure that headers appear in the right order:
5111-
# 1) for foo.cc, foo.h (preferred location)
5112-
# 2) c system files
5113-
# 3) cpp system files
5114-
# 4) for foo.cc, foo.h (deprecated location)
5115-
# 5) other google headers
5207+
# 1) for foo.cc, foo.h
5208+
# 2) other project headers
5209+
# 3) c system files
5210+
# 4) cpp system files
51165211
#
51175212
# We classify each include statement as one of those 5 types
51185213
# using a number of techniques. The include_state object keeps
@@ -5375,7 +5470,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
53755470
and line[-1] != '\\'):
53765471
error(filename, linenum, 'build/namespaces_headers', 4,
53775472
'Do not use unnamed namespaces in header files. See '
5378-
'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
5473+
'https://google.github.io/styleguide/cppguide.html#Namespaces'
53795474
' for more information.')
53805475

53815476

@@ -6497,6 +6592,8 @@ def ProcessFileData(filename, file_extension, lines, error,
64976592

64986593
CheckForNewlineAtEOF(filename, lines, error)
64996594

6595+
CheckInlineHeader(filename, include_state, error)
6596+
65006597
def ProcessConfigOverrides(filename):
65016598
""" Loads the configuration files and processes the config overrides.
65026599
@@ -6515,7 +6612,7 @@ def ProcessConfigOverrides(filename):
65156612
if not base_name:
65166613
break # Reached the root directory.
65176614

6518-
cfg_file = os.path.join(abs_path, "CPPLINT.cfg")
6615+
cfg_file = os.path.join(abs_path, ".cpplint")
65196616
abs_filename = abs_path
65206617
if not os.path.isfile(cfg_file):
65216618
continue
@@ -6897,4 +6994,3 @@ def main():
68976994

68986995
if __name__ == '__main__':
68996996
main()
6900-

0 commit comments

Comments
 (0)