MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_compute_complexity

Function cbm_compute_complexity

internal/cbm/helpers.c:762–844  ·  view source on GitHub ↗

One traversal computing cyclomatic + cognitive + loop-nesting + access-depth metrics. Each frame carries its branch-, loop- and access-nesting depth so every metric (cognitive Campbell penalty, loop_depth polynomial-degree proxy, max chained access depth) is produced in a single walk.

Source from the content-addressed store, hash-verified

760// every metric (cognitive Campbell penalty, loop_depth polynomial-degree proxy,
761// max chained access depth) is produced in a single walk.
762void cbm_compute_complexity(TSNode node, const char **branching_types, cbm_complexity_t *out) {
763 out->cyclomatic = 0;
764 out->cognitive = 0;
765 out->loop_count = 0;
766 out->loop_depth = 0;
767 out->max_access_depth = 0;
768 if (!branching_types) {
769 return;
770 }
771 struct cx_frame {
772 TSNode node;
773 int bdepth;
774 int ldepth;
775 int adepth;
776 };
777 struct cx_frame inline_stack[WALK_STACK_INLINE];
778 struct cx_frame *stack = inline_stack;
779 int cap = WALK_STACK_INLINE;
780 int top = 0;
781 stack[top].node = node;
782 stack[top].bdepth = 0;
783 stack[top].ldepth = 0;
784 stack[top].adepth = 0;
785 top++;
786 while (top > 0) {
787 struct cx_frame f = stack[--top];
788 const char *kind = ts_node_type(f.node);
789 bool is_branch = false;
790 for (const char **t = branching_types; *t; t++) {
791 if (strcmp(kind, *t) == 0) {
792 is_branch = true;
793 break;
794 }
795 }
796 int child_b = f.bdepth;
797 int child_l = f.ldepth;
798 /* Chained member/subscript access: a.b.c.d nests as access(access(access(a))),
799 * so each consecutive access node deepens the chain; non-access nodes reset it. */
800 int child_a = 0;
801 if (ts_node_is_named(f.node) && is_member_access_node(kind)) {
802 child_a = f.adepth + 1;
803 if (child_a > out->max_access_depth) {
804 out->max_access_depth = child_a;
805 }
806 }
807 if (is_branch) {
808 out->cyclomatic++;
809 out->cognitive += 1 + f.bdepth; /* +1 plus nesting penalty (Campbell) */
810 child_b = f.bdepth + 1;
811 }
812 /* Only *named* nodes count as loops. In many grammars (Go, C, …) the
813 * loop's `for`/`while` keyword is an anonymous child token whose node
814 * type literally equals "for"/"while"; without this guard each loop is
815 * counted twice and nesting depth is inflated by one. Named loop nodes
816 * (e.g. Ruby's `while`/`until`/`for`) still match correctly. */
817 if (ts_node_is_named(f.node) && cbm_is_loop_node_type(kind)) {
818 out->loop_count++;
819 int d = f.ldepth + 1;

Callers 1

set_def_complexityFunction · 0.85

Calls 4

is_member_access_nodeFunction · 0.85
cbm_is_loop_node_typeFunction · 0.85
cbm_walk_stack_reserveFunction · 0.85
cbm_walk_stack_releaseFunction · 0.85

Tested by

no test coverage detected