Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions tools/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6036,6 +6036,11 @@ def _GetTextInside(text, start_pattern):
)
# Stream types.
_RE_PATTERN_REF_STREAM_PARAM = r"(?:.*stream\s*&\s*" + _RE_PATTERN_IDENT + r")"
# V8 Fast API types whose signatures are dictated by V8 and require a
# non-const reference parameter; Node.js cannot change these.
_RE_PATTERN_REF_V8_FAST_API_PARAM = (
r"(?:.*\b(?:v8::)?FastApiCallbackOptions\s*&\s*" + _RE_PATTERN_IDENT + r")"
)


def CheckLanguage(
Expand Down Expand Up @@ -6585,8 +6590,10 @@ def CheckForNonConstReference(filename, clean_lines, linenum, nesting_state, err

decls = re.sub(r"{[^}]*}", " ", line) # exclude function body
for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls):
if not re.match(_RE_PATTERN_CONST_REF_PARAM, parameter) and not re.match(
_RE_PATTERN_REF_STREAM_PARAM, parameter
if (
not re.match(_RE_PATTERN_CONST_REF_PARAM, parameter)
and not re.match(_RE_PATTERN_REF_STREAM_PARAM, parameter)
and not re.match(_RE_PATTERN_REF_V8_FAST_API_PARAM, parameter)
):
error(
filename,
Expand Down
72 changes: 72 additions & 0 deletions tools/test_cpplint_fast_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""Regression tests for cpplint runtime/references and V8 Fast API.

Refs: https://github.com/nodejs/node/issues/45761
"""

import os
import sys
import unittest

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

import cpplint # noqa: E402


def _lint(source):
errors = []

def collect(filename, linenum, category, confidence, message):
errors.append((linenum, category, message))

cpplint.ProcessFileData("test.cc", "cc", source.splitlines(), collect)
return [e for e in errors if e[1] == "runtime/references"]


class FastApiCallbackOptionsRefTest(unittest.TestCase):
def test_unqualified_fast_api_callback_options_not_flagged(self):
# Mirrors src/node_buffer.cc usage after `using v8::FastApiCallbackOptions;`.
src = (
"namespace node {\n"
"void FastFn(Local<Value> recv,\n"
" FastApiCallbackOptions& options) {\n"
"}\n"
"} // namespace node\n"
)
self.assertEqual(_lint(src), [])

def test_qualified_fast_api_callback_options_not_flagged(self):
src = (
"namespace node {\n"
"void FastFn(Local<Value> recv,\n"
" v8::FastApiCallbackOptions& options) {\n"
"}\n"
"} // namespace node\n"
)
self.assertEqual(_lint(src), [])

def test_plain_non_const_reference_still_flagged(self):
src = (
"namespace node {\n"
"void Bad(int& value) {\n"
"}\n"
"} // namespace node\n"
)
errors = _lint(src)
self.assertEqual(len(errors), 1, errors)
self.assertIn("int& value", errors[0][2])

def test_unrelated_type_with_similar_name_still_flagged(self):
# Guard against an over-broad regex that would also accept e.g.
# FastApiCallbackOptionsExtra&.
src = (
"namespace node {\n"
"void Bad(FastApiCallbackOptionsExtra& opts) {\n"
"}\n"
"} // namespace node\n"
)
errors = _lint(src)
self.assertEqual(len(errors), 1, errors)


if __name__ == "__main__":
unittest.main()
Loading