Skip to content

Commit 731f624

Browse files
authored
Merge pull request DeusData#2336 from DeusData/fix/issue-1967
fix(coverage): one line counter for a source buffer (DeusData#1967)
2 parents 6514472 + 7688f4b commit 731f624

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
@@ -1434,6 +1434,26 @@ static void cbm_mark_pp_error_rows(TSNode n, uint8_t *rows, uint32_t row_count,
14341434
}
14351435
}
14361436

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

2064-
/* Number of 1-based lines in `src`. A file that does not end with a newline
2065-
* still has a last line, so the count is separators plus one. */
2066-
static uint32_t cbm_count_lines(const char *src, int src_len) {
2067-
uint32_t n = 1;
2068-
for (int i = 0; i < src_len; i++) {
2069-
if (src[i] == '\n' && i + 1 < src_len) {
2070-
n++;
2071-
}
2072-
}
2073-
return n;
2074-
}
2075-
20762079
/* Serialize collected regions as "start-end,start-end,...", with a trailing
20772080
* ",+<N>" when the cap threw N ranges away.
20782081
*
@@ -2868,12 +2871,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
28682871
* total loss (root is ERROR), because then it vouches for
28692872
* nothing and there is no refinement to make. */
28702873
if (strcmp(ts_node_type(pp_root), "ERROR") != 0) {
2871-
uint32_t orig_lines = 1;
2872-
for (int ci = 0; ci < source_len; ci++) {
2873-
if (source[ci] == '\n') {
2874-
orig_lines++;
2875-
}
2876-
}
2874+
uint32_t orig_lines = cbm_source_line_count(source, source_len);
28772875
uint8_t *map = (uint8_t *)cbm_arena_alloc(a, (size_t)orig_lines + 2);
28782876
int exp_lines = preprocessed->expanded_line_count;
28792877
uint8_t *bad_rows =
@@ -3069,7 +3067,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
30693067
* so the report can say "read the source" instead. See
30703068
* parse_unusable in cbm.h for which files land here and why. */
30713069
if (regs.count == 1 && regs.dropped == 0) {
3072-
uint32_t total = cbm_count_lines(source, source_len);
3070+
uint32_t total = cbm_source_line_count(source, source_len);
30733071
uint32_t span = regs.ends[0] - regs.starts[0] + 1;
30743072
if (total > 0 && span * 100 >= total * CBM_UNUSABLE_PCT) {
30753073
result->parse_unusable = true;

‎internal/cbm/cbm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,11 @@ uint64_t cbm_usage_field_lookup_test_work(void);
10051005
uint64_t cbm_usage_slow_parent_fallback_test_count(void);
10061006
#endif
10071007

1008+
// Number of 1-based lines in a source buffer. The single line-count convention
1009+
// for coverage reporting (#1967): a trailing '\n' ends the last line and opens
1010+
// no new one; an empty buffer counts as 1 line. See the definition in cbm.c.
1011+
uint32_t cbm_source_line_count(const char *src, int src_len);
1012+
10081013
// Toggle C/C++ preprocessor Macro-node extraction (#375). The pipeline enables
10091014
// it only for full/advanced index modes (it dominates extraction on macro-dense
10101015
// 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)