From 4e442b286df0be7904fac6ebf44c7026dba6b6ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Wed, 26 Nov 2025 17:37:05 +0100 Subject: [PATCH] tools: refloat 10 Node.js patches to cpplint.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cherry-pick 12c8b4d15471cb6211b39c3a2ca5b10fa4b9f12b 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: https://github.com/nodejs/node/pull/17373 Reviewed-By: Jon Moss Reviewed-By: Anna Henningsen Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Tobias Nießen Refs: https://github.com/nodejs/node/commit/12c8b4d15471cb6211b39c3a2ca5b10fa4b9f12b Cherry-pick fc81e801913de3e3f3c0c8e26c105f983a74e539 Original commit message: Update cpplint.py to check for inline headers when the corresponding header is already included. PR-URL: https://github.com/nodejs/node/pull/21521 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell Refs: https://github.com/nodejs/node/commit/fc81e801913de3e3f3c0c8e26c105f983a74e539 Cherry-pick cbc3dd997eb90d629d1b9912b7a5a40eb82343df 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: https://github.com/nodejs/node/pull/21010 Reviewed-By: Ben Noordhuis Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Refs: https://github.com/nodejs/node/commit/cbc3dd997eb90d629d1b9912b7a5a40eb82343df Cherry-pick 902998190a55d6915b881936f6dd5b6e9cca6ad8 Original commit message: tools: fix cpplint.py header rules THIS COMMIT SHOULD GO WITH THE NEXT. IT WILL FIND NEW LINT. PR-URL: https://github.com/nodejs/node/pull/26306 Reviewed-By: Gireesh Punathil Refs: https://github.com/nodejs/node/commit/902998190a55d6915b881936f6dd5b6e9cca6ad8 Cherry-pick 0a25ace9c35b62ece4d32fd90b326d8063265109 Original commit message: tools: move cpplint configuration to .cpplint PR-URL: https://github.com/nodejs/node/pull/27098 Reviewed-By: Joyee Cheung Reviewed-By: Daniel Bevenius Refs: https://github.com/nodejs/node/commit/0a25ace9c35b62ece4d32fd90b326d8063265109 Cherry-pick afa9a7206c26a29a2af226696c145c924a6d3754 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: https://github.com/nodejs/node/pull/30876 Reviewed-By: Michaël Zasso Reviewed-By: David Carlier Reviewed-By: Colin Ihrig Reviewed-By: Richard Lau Reviewed-By: Rich Trott Refs: https://github.com/nodejs/node/commit/afa9a7206c26a29a2af226696c145c924a6d3754 Cherry-pick e23bf8f771aa0bd60e25ff079985fc29b5846403 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: https://github.com/nodejs/node/pull/31018 Reviewed-By: Colin Ihrig Reviewed-By: Richard Lau Reviewed-By: James M Snell Reviewed-By: David Carlier Reviewed-By: Rich Trott Reviewed-By: Gus Caplan Reviewed-By: Joyee Cheung Reviewed-By: Ben Noordhuis Reviewed-By: Stephen Belanger Backport 3d954dcf812bb500a77913ffd5ebec76235714f0 Original commit message: tools: remove readability/fn_size rule PR-URL: https://github.com/nodejs/node/pull/54663 Reviewed-By: Yagiz Nizipli Reviewed-By: Luigi Pinca Refs: https://github.com/nodejs/node/commit/3d954dcf812bb500a77913ffd5ebec76235714f0 Cherry-pick c7d7ec7cda56b7776797cbdef3d4497fc98e8440 Original commit message: tools: check for std::vector in lint PR-URL: https://github.com/nodejs/node/pull/58497 Reviewed-By: James M Snell Reviewed-By: Juan José Arboleda Reviewed-By: Joyee Cheung Refs: https://github.com/nodejs/node/commit/c7d7ec7cda56b7776797cbdef3d4497fc98e8440 Cherry-pick e6d94ef106e08999e5461dabfeb333ff141f59fe 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: https://github.com/nodejs/node/pull/60244 Reviewed-By: Chengzhong Wu Reviewed-By: Rafael Gonzaga Reviewed-By: Edy Silva Reviewed-By: Colin Ihrig Reviewed-By: Ilyas Shabi Reviewed-By: Yagiz Nizipli Reviewed-By: Vladimir Morozov Refs: https://github.com/nodejs/node/commit/e6d94ef106e08999e5461dabfeb333ff141f59fe PR-URL: https://github.com/nodejs/node/pull/60901 Fixes: https://github.com/nodejs/node/issues/60771 --- tools/cpplint.py | 157 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 148 insertions(+), 9 deletions(-) diff --git a/tools/cpplint.py b/tools/cpplint.py index 146e7982aff..464d95b8824 100755 --- a/tools/cpplint.py +++ b/tools/cpplint.py @@ -306,9 +306,10 @@ _ERROR_CATEGORIES = [ "build/forward_decl", "build/header_guard", "build/include", - "build/include_subdir", "build/include_alpha", + "build/include_inline", "build/include_order", + "build/include_subdir", "build/include_what_you_use", "build/namespaces_headers", "build/namespaces/header/block/literals", @@ -351,6 +352,7 @@ _ERROR_CATEGORIES = [ "runtime/string", "runtime/threadsafe_fn", "runtime/vlog", + "runtime/v8_persistent", "whitespace/blank_line", "whitespace/braces", "whitespace/comma", @@ -920,6 +922,14 @@ _SEARCH_C_FILE = re.compile( # Match string that indicates we're working on a Linux Kernel file. _SEARCH_KERNEL_FILE = re.compile(r"\b(?:LINT_KERNEL_FILE)") +_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b') + +_V8_PERSISTENT_PATTERN = re.compile(r'\bv8::Persistent\b') + +_RIGHT_LEANING_POINTER_PATTERN = re.compile(r'[^=|(,\s><);&?:}]' + r'(?= 0 or line.find('*/') >= 0: + return + + for match in _NULL_TOKEN_PATTERN.finditer(line): + error(filename, linenum, 'readability/null_usage', 2, + 'Use nullptr instead of NULL') + + +def CheckV8PersistentTokens(filename, clean_lines, linenum, error): + """Check v8::Persistent usage. + + Args: + filename: The name of the current file. + clean_lines: A CleansedLines instance containing the file. + linenum: The number of the line to check. + error: The function to call with any errors found. + """ + line = clean_lines.elided[linenum] + + # Avoid preprocessor lines + if re.match(r'^\s*#', line): + return + + if line.find('/*') >= 0 or line.find('*/') >= 0: + return + + for match in _V8_PERSISTENT_PATTERN.finditer(line): + error(filename, linenum, 'runtime/v8_persistent', 2, + 'Use v8::Global instead of v8::Persistent') + + +def CheckLeftLeaningPointer(filename, clean_lines, linenum, error): + """Check for left-leaning pointer placement. + + Args: + filename: The name of the current file. + clean_lines: A CleansedLines instance containing the file. + linenum: The number of the line to check. + error: The function to call with any errors found. + """ + line = clean_lines.elided[linenum] + + # Avoid preprocessor lines + if re.match(r'^\s*#', line): + return + + if '/*' in line or '*/' in line: + return + + for match in _RIGHT_LEANING_POINTER_PATTERN.finditer(line): + error(filename, linenum, 'readability/null_usage', 2, + 'Use left leaning pointer instead of right leaning') + + def GetLineWidth(line): """Determines the width of the line in column positions. @@ -5603,6 +5697,9 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, er CheckSpacingForFunctionCall(filename, clean_lines, linenum, error) CheckCheck(filename, clean_lines, linenum, error) CheckAltTokens(filename, clean_lines, linenum, error) + CheckNullTokens(filename, clean_lines, linenum, error) + CheckV8PersistentTokens(filename, clean_lines, linenum, error) + CheckLeftLeaningPointer(filename, clean_lines, linenum, error) classinfo = nesting_state.InnermostClass() if classinfo: CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error) @@ -6155,7 +6252,7 @@ def CheckLanguage( "build/namespaces_headers", 4, "Do not use unnamed namespaces in header files. See " - "https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces" + "https://google.github.io/styleguide/cppguide.html#Namespaces" " for more information.", ) @@ -7313,6 +7410,42 @@ def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum, er ) +def CheckLocalVectorUsage(filename, lines, error): + """Logs an error if std::vector> is used. + Args: + filename: The name of the current file. + lines: An array of strings, each representing a line of the file. + error: The function to call with any errors found. + """ + for linenum, line in enumerate(lines): + if (re.search(r'\bstd::vector]+>>', line) or + re.search(r'\bstd::vector]+>>', line)): + error(filename, linenum, 'runtime/local_vector', 5, + 'Do not use std::vector>. ' + 'Use v8::LocalVector instead.') + + +def CheckStringValueUsage(filename, lines, error): + """Logs an error if v8's String::Value/Utf8Value are used. + Args: + filename: The name of the current file. + lines: An array of strings, each representing a line of the file. + error: The function to call with any errors found. + """ + if filename.startswith('test/') or filename.startswith('test\\'): + return # Skip test files, where Node.js headers may not be available + + for linenum, line in enumerate(lines): + if re.search(r'\bString::Utf8Value\b', line): + error(filename, linenum, 'runtime/v8_string_value', 5, + 'Do not use v8::String::Utf8Value. ' + 'Use node::Utf8Value instead.') + if re.search(r'\bString::Value\b', line): + error(filename, linenum, 'runtime/v8_string_value', 5, + 'Do not use v8::String::Value. ' + 'Use node::TwoByteValue instead.') + + def ProcessLine( filename, file_extension, @@ -7470,6 +7603,12 @@ def ProcessFileData(filename, file_extension, lines, error, extra_check_function CheckForNewlineAtEOF(filename, lines, error) + CheckInlineHeader(filename, include_state, error) + + CheckLocalVectorUsage(filename, lines, error) + + CheckStringValueUsage(filename, lines, error) + def ProcessConfigOverrides(filename): """Loads the configuration files and processes the config overrides.