Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

C++

Reference for which tree-sitter nodes pretender tracks for C++ files, how each contributes to pretender check metrics, and known measurement gaps. Consult this page when tuning thresholds or diagnosing unexpected scores.

Overview

Analyses .cpp, .cc, .cxx, .hpp, and .hxx files using a tree-sitter grammar. Tracks both free functions and out-of-class method definitions (qualified identifiers). Range-based for loops and catch clauses each count as branches.

Tracked nodes

Tree-sitter nodeCaptureMetric impact
function_definition (simple identifier)@function.definitionDefines a metric scope
function_definition (qualified identifier)@function.definitionDefines a metric scope
if_statement@branch.if+1 cyclomatic, +1 cognitive
for_statement@branch.loop+1 cyclomatic, +1 cognitive
for_range_loop@branch.loop+1 cyclomatic, +1 cognitive
while_statement@branch.loop+1 cyclomatic, +1 cognitive
do_statement@branch.loop+1 cyclomatic, +1 cognitive
catch_clause@branch.catch+1 cyclomatic, +1 cognitive
case_statement@branch.case+1 cyclomatic, +1 cognitive
binary_expression "&&"@branch.logical.and+1 cyclomatic, +1 cognitive
binary_expression `““`
call_expression@call+1 ABC C-count
assignment_expression@assign+1 ABC A-count
init_declarator (with value)@assign+1 ABC A-count

Assertion patterns

PatternMatches
^assertassert(...) from <cassert>
^ASSERTCustom assertion macros
^EXPECTGoogle Test EXPECT_EQ(...), etc.
^CHECKCHECK(...) (Abseil, glog)

Quirks and limitations

  • Template functions produce one definition entry per instantiation in some grammar revisions; complexity may be overcounted in heavily templated code.
  • Lambda expressions are not tracked as function scopes — their branches count toward the enclosing function’s complexity.
  • Operator overloads (operator+, operator==) are captured as function definitions and metriced like regular functions.
  • ternary_expression (condition ? a : b) is not currently tracked and does not contribute cyclomatic complexity.
  • In-class method definitions (defined inside a class body in a header) use the simple-identifier form and are tracked correctly. Out-of-class definitions (e.g., Foo::bar() in a .cpp file) use the qualified-identifier form and are also tracked.
  • Preprocessor conditionals (#ifdef, #if) are not parsed by the tree-sitter grammar and do not contribute branch counts.

Known unsupported metrics

  • min_assertions — Not implemented yet: metrics.scm has no @assert.* capture, so unit assertion totals are always 0 (the [assertions] patterns serve rule scopes only). No ticket filed yet.
  • Nesting depth — Not implemented yet: the engine increments nesting only inside block-kind nodes, but tree-sitter-cpp wraps bodies in compound_statement, so nesting_max stays 0. No ticket filed yet.
  • Unwrap density — Not implemented yet: the engine’s risky error-handling detector covers Rust, Python, JavaScript/TypeScript, Java, and Go only; C++ has no matcher. No ticket filed yet.

Example output

src/engine.cpp  cyclomatic=10  cognitive=13  function_lines=49  params=3
include/parser.hpp  cyclomatic=5  cognitive=6  function_lines=24  params=2