MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / _parameter_perf_findings

Function _parameter_perf_findings

scripts/code_verify_rules.py:421–510  ·  view source on GitHub ↗

Flag heavy types or refcounted smart pointers passed by value. Applies universally -- by-value copies are wasteful regardless of whether the function is in the hotpath list. Sink parameters (those `std::move`'d in the body) are skipped: the by-value + move idiom is correct C++ for a

(func_node, src: bytes, fenced)

Source from the content-addressed store, hash-verified

419
420
421def _parameter_perf_findings(func_node, src: bytes, fenced) -> list:
422 """Flag heavy types or refcounted smart pointers passed by value.
423 Applies universally -- by-value copies are wasteful regardless of
424 whether the function is in the hotpath list.
425
426 Sink parameters (those `std::move`'d in the body) are skipped: the
427 by-value + move idiom is correct C++ for a function that conditionally
428 keeps a local copy of the argument."""
429 findings: list = []
430 decl = func_node.child_by_field_name("declarator")
431 fdecl = _walk_to_function_declarator(decl)
432 if fdecl is None:
433 return findings
434 params = fdecl.child_by_field_name("parameters")
435 if params is None:
436 return findings
437 sink_params = _sink_param_names(func_node, src)
438 for param in params.children:
439 if param.type != "parameter_declaration":
440 continue
441 param_type = param.child_by_field_name("type")
442 param_decl = param.child_by_field_name("declarator")
443 if param_type is None:
444 continue
445 if param_decl is not None and param_decl.type in (
446 "pointer_declarator",
447 "reference_declarator",
448 "abstract_pointer_declarator",
449 "abstract_reference_declarator",
450 "rvalue_reference_declarator",
451 "abstract_rvalue_reference_declarator",
452 ):
453 continue
454 # Resolve the parameter's identifier name (if it has one) so we
455 # can suppress sink-param idioms.
456 pname = None
457 if param_decl is not None:
458 cur = param_decl
459 while cur is not None and cur.type != "identifier":
460 next_cur = None
461 for c in cur.children:
462 if c.type == "identifier":
463 next_cur = c
464 break
465 if next_cur is None:
466 for c in cur.children:
467 if hasattr(c, "children") and c.children:
468 next_cur = c
469 break
470 cur = next_cur
471 if cur is not None and cur.type == "identifier":
472 pname = _node_text(cur, src)
473 if pname is not None and pname in sink_params:
474 continue
475 type_text = _node_text(param_type, src).strip()
476 base = type_text
477 for q in ("const ", "constexpr ", "volatile ", "mutable ", "register "):
478 while base.startswith(q):

Callers 1

_cpp_rulesFunction · 0.85

Calls 8

_sink_param_namesFunction · 0.85
_node_textFunction · 0.85
_line_ofFunction · 0.85
fencedFunction · 0.85
findMethod · 0.80
FindingClass · 0.70
appendMethod · 0.45

Tested by

no test coverage detected