Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(coverage): stop walking the source per error region in the #1071 check (#1735)

Martin Vogele4780c208632
diff --git a/internal/cbm/cbm.c b/internal/cbm/cbm.c
index b54fb54a7..a6fa8fd32 100644
--- a/internal/cbm/cbm.c
+++ b/internal/cbm/cbm.c
@@ -1773,6 +1773,19 @@ static void cbm_subtract_recovered_regions(cbm_error_regions_t *regs, const CBMD
*regs = out;
}
+#ifdef CBM_ENABLE_TEST_SEAMS
+/* Source bytes this thread has read to locate lines for the #1071 macro check
+ * (walks and line-table builds). Lets a test pin the check's cost as a count
+ * instead of a clock (#1735). Compiled only into seam-enabled test artifacts. */
+static CBM_TLS uint64_t tl_macro_line_scan_bytes = 0;
+uint64_t cbm_test_macro_line_scan_bytes(void) {
+ return tl_macro_line_scan_bytes;
+}
+#define CBM_MACRO_LINE_SCAN(n) (tl_macro_line_scan_bytes += (uint64_t)(n))
+#else
+#define CBM_MACRO_LINE_SCAN(n) ((void)0)
+#endif
+
/* #1071: a function-like macro invocation whose argument is a type token
* (e.g. ALLOC(int, n)) makes tree-sitter's C/C++ grammar emit an ERROR node — it
* parses `int` in expression position — which would be recorded as a parse_partial
@@ -1862,6 +1875,7 @@ static bool cbm_span_is_macro_invocation(const char *src, int src_len, uint32_t
}
}
if (line != start_line) {
+ CBM_MACRO_LINE_SCAN(span_start);
return false;
}
int span_end = span_start;
@@ -1870,6 +1884,7 @@ static bool cbm_span_is_macro_invocation(const char *src, int src_len, uint32_t
line++;
}
}
+ CBM_MACRO_LINE_SCAN(span_end);
return cbm_byte_span_is_macro_invocation(src, src_len, span_start, span_end, defs);
}
@@ -1896,21 +1911,59 @@ static bool cbm_region_inside_callable(uint32_t rs, uint32_t re, const CBMDefArr
return false;
}
+/* cbm_span_is_macro_invocation over a cbm_line_offsets table: offs[k] is where
+ * 1-based line k+1 starts and offs[nlines] is the end of the source, so a span
+ * costs two reads instead of a walk from byte 0. Same answer as the walk; falls
+ * back to it when there is no table. */
+static bool cbm_lines_are_macro_invocation(const char *src, int src_len, const uint32_t *offs,
+ uint32_t nlines, uint32_t start_line, uint32_t end_line,
+ const CBMDefArray *defs) {
+ if (!offs) {
+ return cbm_span_is_macro_invocation(src, src_len, start_line, end_line, defs);
+ }
+ if (!src || src_len <= 0 || !defs || start_line == 0 || end_line < start_line ||
+ start_line > nlines) {
+ return false;
+ }
+ uint32_t last = end_line < nlines ? end_line : nlines;
+ return cbm_byte_span_is_macro_invocation(src, src_len, (int)offs[start_line - 1],
+ (int)offs[last], defs);
+}
+
+/* #1071 subtraction. Both questions are pure, so asking the cheap one first
+ * changes no answer: "is the region inside a function?" reads only the
+ * definition list, while "is it a macro call?" has to find the region's lines
+ * in the source. A region no function encloses — every region of a file with
+ * no functions, such as a SQL data dump — never touches the source, and the
+ * ones that do share one line table built on first use. The old order walked
+ * the source from byte 0 for every region: regions x file bytes (#1735). */
static void cbm_subtract_macro_invocation_regions(cbm_error_regions_t *regs,
const CBMDefArray *defs, const char *src,
int src_len) {
+ uint32_t *offs = NULL;
+ uint32_t nlines = 0;
+ bool offs_built = false;
int kept = 0;
for (int i = 0; i < regs->count; i++) {
- bool benign =
- cbm_span_is_macro_invocation(src, src_len, regs->starts[i], regs->ends[i], defs) &&
- cbm_region_inside_callable(regs->starts[i], regs->ends[i], defs);
+ uint32_t rs = regs->starts[i];
+ uint32_t re = regs->ends[i];
+ bool benign = false;
+ if (cbm_region_inside_callable(rs, re, defs)) {
+ if (!offs_built) {
+ offs = cbm_line_offsets(src, src_len, &nlines);
+ offs_built = true;
+ CBM_MACRO_LINE_SCAN(src_len);
+ }
+ benign = cbm_lines_are_macro_invocation(src, src_len, offs, nlines, rs, re, defs);
+ }
if (!benign) {
- regs->starts[kept] = regs->starts[i];
- regs->ends[kept] = regs->ends[i];
+ regs->starts[kept] = rs;
+ regs->ends[kept] = re;
kept++;
}
}
regs->count = kept;
+ cbm_free(CBM_MEM_CLASS_EXTRACT, offs);
}
/* Push [start, end] after trimming no-code lines off both ends. A run made
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index a00192a18..1b5d3f3ef 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -800,6 +800,12 @@ void cbm_free_tree(CBMFileResult *result);
// Free a standalone TSTree pointer (for Go layer cleanup).
void cbm_free_tree_ptr(TSTree *tree);
+#ifdef CBM_ENABLE_TEST_SEAMS
+// Test-only: source bytes this thread has read to locate lines for the #1071
+// macro-invocation check, cumulative (#1735).
+uint64_t cbm_test_macro_line_scan_bytes(void);
+#endif
+
// Reset the thread-local parser's internal state, releasing slab-allocated
// subtrees. Must be called BEFORE cbm_slab_reset_thread() so the slab rebuild
// doesn't corrupt live parser state.
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 1e6506ed1..77c6b4756 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -302,6 +302,58 @@ TEST(extract_cpp_real_in_body_error_still_flagged_issue1071) {
PASS();
}
+/* #1735: the #1071 check's cost. cbm_subtract_macro_invocation_regions asked
+ * "is this region a macro call?" before "is it inside a function?", and the
+ * first question walked the source from byte 0 for every region: regions x
+ * file bytes on any file with many error regions, whatever its language.
+ * Counted (test seam), never timed. */
+enum { MACRO_SCAN_SRC_CAP = 8192 };
+
+TEST(macro_check_reads_no_source_for_regions_outside_functions_issue1735) {
+ /* 40 functions, each followed by a top-level junk line: 40 regions, none
+ * of them inside a function. */
+ char src[MACRO_SCAN_SRC_CAP];
+ int len = snprintf(src, sizeof(src), "#define ALLOC(T, n) ((T *)malloc(sizeof(T) * (n)))\n");
+ for (int i = 0; i < 40; i++) {
+ len += snprintf(src + len, sizeof(src) - (size_t)len,
+ "int ok%d(void) {\n return %d;\n}\n} ] junk ( {\n", i, i);
+ }
+ ASSERT_LT(len, MACRO_SCAN_SRC_CAP);
+ uint64_t before = cbm_test_macro_line_scan_bytes();
+ CBMFileResult *r = extract(src, CBM_LANG_C, "t", "junk.c");
+ uint64_t scanned = cbm_test_macro_line_scan_bytes() - before;
+ ASSERT_NOT_NULL(r);
+ ASSERT_TRUE(r->parse_incomplete); /* top-level junk stays reported... */
+ ASSERT_EQ(r->error_region_count, 40); /* ...one range per junk line, as before */
+ ASSERT_EQ(scanned, 0u);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(macro_check_locates_lines_with_one_table_issue1735) {
+ /* 60 functions whose bodies hold a real syntax error: every region sits
+ * inside a function, so the check has to look at each one's lines. */
+ char src[MACRO_SCAN_SRC_CAP];
+ int len = snprintf(src, sizeof(src), "#define ALLOC(T, n) ((T *)malloc(sizeof(T) * (n)))\n");
+ for (int i = 0; i < 60; i++) {
+ len += snprintf(src + len, sizeof(src) - (size_t)len,
+ "int f%d(void) {\n int x = ;\n return x;\n}\n", i);
+ }
+ ASSERT_LT(len, MACRO_SCAN_SRC_CAP);
+ uint64_t before = cbm_test_macro_line_scan_bytes();
+ CBMFileResult *r = extract(src, CBM_LANG_C, "t", "inbody.c");
+ uint64_t scanned = cbm_test_macro_line_scan_bytes() - before;
+ ASSERT_NOT_NULL(r);
+ /* #1071 unchanged: a real in-body error is not a macro call and stays
+ * reported, one range per function. */
+ ASSERT_TRUE(r->parse_incomplete);
+ ASSERT_EQ(r->error_region_count, 60);
+ /* One line table for the file, not one walk per region. */
+ ASSERT_LTE(scanned, (uint64_t)len);
+ cbm_free_result(r);
+ PASS();
+}
+
/* --- GDScript: AST -> graph visitor (Godot, #186) --- */
TEST(extract_gdscript_issue186) {
CBMFileResult *r = extract("extends Node\n"
@@ -8325,6 +8377,8 @@ SUITE(extraction) {
RUN_TEST(extract_cpp_macros_issue375);
RUN_TEST(extract_cpp_functionlike_macro_type_arg_no_false_parse_partial_issue1071);
RUN_TEST(extract_cpp_real_in_body_error_still_flagged_issue1071);
+ RUN_TEST(macro_check_reads_no_source_for_regions_outside_functions_issue1735);
+ RUN_TEST(macro_check_locates_lines_with_one_table_issue1735);
RUN_TEST(extract_gdscript_issue186);
RUN_TEST(extract_powershell_issue35);
RUN_TEST(extract_luau_issue39);