@@ -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. */
14531473static 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;
0 commit comments