Skip to content

Commit 14ed8ce

Browse files
committed
fix(coverage): one line counter for a source buffer (DeusData#1967)
Three places in internal/cbm/cbm.c counted the lines of the same source buffer with two different conventions. cbm_count_lines (parse_unusable 80% threshold) ignored a final newline, while the inline counter that sizes the preprocessed-line map (orig_lines) and cbm_line_offsets (the recovery-gap walker) counted every newline, so on any file ending in a newline -- nearly every file -- they disagreed by one. Nothing produced a wrong answer: every consumer of the larger count either clamps to it or sees the phantom last line as blank. The risk was the next reader picking up one convention without knowing the other existed. Add cbm_source_line_count() as the single helper, with the convention documented beside it: a '\n' terminates a line and opens no new one; an empty buffer counts as 1 line. All three call sites use it; the old static cbm_count_lines is gone. Test: parse_coverage::source_line_count_one_convention pins the convention (trailing newline, no final newline, CRLF, empty buffer, blank last line, src_len bound). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 8a7519d commit 14ed8ce

3 files changed

Lines changed: 46 additions & 25 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,26 @@ static void cbm_mark_pp_error_rows(TSNode n, uint8_t *rows, uint32_t row_count,
14331433
}
14341434
}
14351435

1436+
/* The ONE line counter for a source buffer (#1967). Every caller that asks
1437+
* "how many lines does this file have" uses it, so the coverage report never
1438+
* holds two answers for the same buffer.
1439+
*
1440+
* Convention: a '\n' TERMINATES a line; it does not open a new one. So the
1441+
* count is the number of '\n' that have at least one byte after them, plus
1442+
* one. "a\nb" and "a\nb\n" both have 2 lines (the trailing newline adds no
1443+
* phantom empty line, matching what an editor shows); "a\nb" without a final
1444+
* newline still counts its last line. An empty buffer counts as 1 line, because
1445+
* tree-sitter still reports row 0 for it and 1-based line maps index line 1. */
1446+
uint32_t cbm_source_line_count(const char *src, int src_len) {
1447+
uint32_t n = 1;
1448+
for (int i = 0; i + 1 < src_len; i++) {
1449+
if (src[i] == '\n') {
1450+
n++;
1451+
}
1452+
}
1453+
return n;
1454+
}
1455+
14361456
/* Recovery subtraction (#963): tree-sitter error recovery plus the
14371457
* ERROR-descending def walker often still extract constructs INSIDE a failed
14381458
* region (verified: a function in an #ifdef-split ERROR region and even a
@@ -1450,12 +1470,7 @@ static void cbm_mark_pp_error_rows(TSNode n, uint8_t *rows, uint32_t row_count,
14501470
* Now the uncovered gaps are reported instead, and a gap holding only blank,
14511471
* comment or preprocessor lines is not a miss at all. */
14521472
static uint32_t *cbm_line_offsets(const char *src, int src_len, uint32_t *out_lines) {
1453-
uint32_t lines = 1;
1454-
for (int i = 0; i < src_len; i++) {
1455-
if (src[i] == '\n') {
1456-
lines++;
1457-
}
1458-
}
1473+
uint32_t lines = cbm_source_line_count(src, src_len);
14591474
uint32_t *offs =
14601475
(uint32_t *)cbm_alloc(CBM_MEM_CLASS_EXTRACT, (size_t)(lines + 1) * sizeof(uint32_t));
14611476
if (!offs) {
@@ -2060,18 +2075,6 @@ static void cbm_refine_regions_with_pp_lines(cbm_error_regions_t *regs, const ui
20602075
* this repo covers 25.5% of its file, and the next widest 3.9%. */
20612076
#define CBM_UNUSABLE_PCT 80
20622077

2063-
/* Number of 1-based lines in `src`. A file that does not end with a newline
2064-
* still has a last line, so the count is separators plus one. */
2065-
static uint32_t cbm_count_lines(const char *src, int src_len) {
2066-
uint32_t n = 1;
2067-
for (int i = 0; i < src_len; i++) {
2068-
if (src[i] == '\n' && i + 1 < src_len) {
2069-
n++;
2070-
}
2071-
}
2072-
return n;
2073-
}
2074-
20752078
/* Serialize collected regions as "start-end,start-end,...", with a trailing
20762079
* ",+<N>" when the cap threw N ranges away.
20772080
*
@@ -2745,12 +2748,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
27452748
* total loss (root is ERROR), because then it vouches for
27462749
* nothing and there is no refinement to make. */
27472750
if (strcmp(ts_node_type(pp_root), "ERROR") != 0) {
2748-
uint32_t orig_lines = 1;
2749-
for (int ci = 0; ci < source_len; ci++) {
2750-
if (source[ci] == '\n') {
2751-
orig_lines++;
2752-
}
2753-
}
2751+
uint32_t orig_lines = cbm_source_line_count(source, source_len);
27542752
uint8_t *map = (uint8_t *)cbm_arena_alloc(a, (size_t)orig_lines + 2);
27552753
int exp_lines = preprocessed->expanded_line_count;
27562754
uint8_t *bad_rows =
@@ -2946,7 +2944,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
29462944
* so the report can say "read the source" instead. See
29472945
* parse_unusable in cbm.h for which files land here and why. */
29482946
if (regs.count == 1 && regs.dropped == 0) {
2949-
uint32_t total = cbm_count_lines(source, source_len);
2947+
uint32_t total = cbm_source_line_count(source, source_len);
29502948
uint32_t span = regs.ends[0] - regs.starts[0] + 1;
29512949
if (total > 0 && span * 100 >= total * CBM_UNUSABLE_PCT) {
29522950
result->parse_unusable = true;

‎internal/cbm/cbm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,11 @@ uint64_t cbm_usage_field_lookup_test_work(void);
920920
uint64_t cbm_usage_slow_parent_fallback_test_count(void);
921921
#endif
922922

923+
// Number of 1-based lines in a source buffer. The single line-count convention
924+
// for coverage reporting (#1967): a trailing '\n' ends the last line and opens
925+
// no new one; an empty buffer counts as 1 line. See the definition in cbm.c.
926+
uint32_t cbm_source_line_count(const char *src, int src_len);
927+
923928
// Toggle C/C++ preprocessor Macro-node extraction (#375). The pipeline enables
924929
// it only for full/advanced index modes (it dominates extraction on macro-dense
925930
// codebases). Default ON. Set before extraction; read-only during.

‎tests/test_parse_coverage.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,25 @@ TEST(cs_malformed_conditional_remains_partial_issue1748) {
16151615
PASS();
16161616
}
16171617

1618+
/* #1967: every line counter over one source buffer answers the same
1619+
* question the same way. A trailing newline ends the last line; it does not
1620+
* open a new one. */
1621+
TEST(source_line_count_one_convention) {
1622+
ASSERT_EQ(cbm_source_line_count("", 0), 1u);
1623+
ASSERT_EQ(cbm_source_line_count("a", 1), 1u);
1624+
ASSERT_EQ(cbm_source_line_count("a\n", 2), 1u);
1625+
ASSERT_EQ(cbm_source_line_count("\n", 1), 1u);
1626+
ASSERT_EQ(cbm_source_line_count("a\nb", 3), 2u);
1627+
ASSERT_EQ(cbm_source_line_count("a\nb\n", 4), 2u);
1628+
ASSERT_EQ(cbm_source_line_count("a\n\n", 3), 2u);
1629+
ASSERT_EQ(cbm_source_line_count("a\r\nb\r\n", 6), 2u);
1630+
/* src_len bounds the count, not a NUL terminator. */
1631+
ASSERT_EQ(cbm_source_line_count("a\nb\nc", 2), 1u);
1632+
PASS();
1633+
}
1634+
16181635
SUITE(parse_coverage) {
1636+
RUN_TEST(source_line_count_one_convention);
16191637
RUN_TEST(c_ifdef_split_brace_sets_parse_incomplete);
16201638
RUN_TEST(c_ifdef_split_brace_neighbors_still_extracted);
16211639
RUN_TEST(c_error_range_points_at_failed_region);

0 commit comments

Comments
 (0)