2121#include "lsp/kotlin_lsp.h"
2222#include "lsp/rust_lsp.h"
2323#include "preprocessor.h"
24+ #include "sql_values.h" // #1735: literal INSERT rows kept out of the SQL parse
2425#include "foundation/compat.h"
2526#include "foundation/compat_fs.h" // cbm_fopen — crash-supervisor per-file marker write
2627#include "foundation/hash_table.h" // CBMHashTable — crash-supervisor quarantine set
@@ -1773,6 +1774,19 @@ static void cbm_subtract_recovered_regions(cbm_error_regions_t *regs, const CBMD
17731774 * regs = out ;
17741775}
17751776
1777+ #ifdef CBM_ENABLE_TEST_SEAMS
1778+ /* Source bytes this thread has read to locate lines for the #1071 macro check
1779+ * (walks and line-table builds). Lets a test pin the check's cost as a count
1780+ * instead of a clock (#1735). Compiled only into seam-enabled test artifacts. */
1781+ static CBM_TLS uint64_t tl_macro_line_scan_bytes = 0 ;
1782+ uint64_t cbm_test_macro_line_scan_bytes (void ) {
1783+ return tl_macro_line_scan_bytes ;
1784+ }
1785+ #define CBM_MACRO_LINE_SCAN (n ) (tl_macro_line_scan_bytes += (uint64_t)(n))
1786+ #else
1787+ #define CBM_MACRO_LINE_SCAN (n ) ((void)0)
1788+ #endif
1789+
17761790/* #1071: a function-like macro invocation whose argument is a type token
17771791 * (e.g. ALLOC(int, n)) makes tree-sitter's C/C++ grammar emit an ERROR node — it
17781792 * parses `int` in expression position — which would be recorded as a parse_partial
@@ -1862,6 +1876,7 @@ static bool cbm_span_is_macro_invocation(const char *src, int src_len, uint32_t
18621876 }
18631877 }
18641878 if (line != start_line ) {
1879+ CBM_MACRO_LINE_SCAN (span_start );
18651880 return false;
18661881 }
18671882 int span_end = span_start ;
@@ -1870,6 +1885,7 @@ static bool cbm_span_is_macro_invocation(const char *src, int src_len, uint32_t
18701885 line ++ ;
18711886 }
18721887 }
1888+ CBM_MACRO_LINE_SCAN (span_end );
18731889 return cbm_byte_span_is_macro_invocation (src , src_len , span_start , span_end , defs );
18741890}
18751891
@@ -1896,21 +1912,59 @@ static bool cbm_region_inside_callable(uint32_t rs, uint32_t re, const CBMDefArr
18961912 return false;
18971913}
18981914
1915+ /* cbm_span_is_macro_invocation over a cbm_line_offsets table: offs[k] is where
1916+ * 1-based line k+1 starts and offs[nlines] is the end of the source, so a span
1917+ * costs two reads instead of a walk from byte 0. Same answer as the walk; falls
1918+ * back to it when there is no table. */
1919+ static bool cbm_lines_are_macro_invocation (const char * src , int src_len , const uint32_t * offs ,
1920+ uint32_t nlines , uint32_t start_line , uint32_t end_line ,
1921+ const CBMDefArray * defs ) {
1922+ if (!offs ) {
1923+ return cbm_span_is_macro_invocation (src , src_len , start_line , end_line , defs );
1924+ }
1925+ if (!src || src_len <= 0 || !defs || start_line == 0 || end_line < start_line ||
1926+ start_line > nlines ) {
1927+ return false;
1928+ }
1929+ uint32_t last = end_line < nlines ? end_line : nlines ;
1930+ return cbm_byte_span_is_macro_invocation (src , src_len , (int )offs [start_line - 1 ],
1931+ (int )offs [last ], defs );
1932+ }
1933+
1934+ /* #1071 subtraction. Both questions are pure, so asking the cheap one first
1935+ * changes no answer: "is the region inside a function?" reads only the
1936+ * definition list, while "is it a macro call?" has to find the region's lines
1937+ * in the source. A region no function encloses — every region of a file with
1938+ * no functions, such as a SQL data dump — never touches the source, and the
1939+ * ones that do share one line table built on first use. The old order walked
1940+ * the source from byte 0 for every region: regions x file bytes (#1735). */
18991941static void cbm_subtract_macro_invocation_regions (cbm_error_regions_t * regs ,
19001942 const CBMDefArray * defs , const char * src ,
19011943 int src_len ) {
1944+ uint32_t * offs = NULL ;
1945+ uint32_t nlines = 0 ;
1946+ bool offs_built = false;
19021947 int kept = 0 ;
19031948 for (int i = 0 ; i < regs -> count ; i ++ ) {
1904- bool benign =
1905- cbm_span_is_macro_invocation (src , src_len , regs -> starts [i ], regs -> ends [i ], defs ) &&
1906- cbm_region_inside_callable (regs -> starts [i ], regs -> ends [i ], defs );
1949+ uint32_t rs = regs -> starts [i ];
1950+ uint32_t re = regs -> ends [i ];
1951+ bool benign = false;
1952+ if (cbm_region_inside_callable (rs , re , defs )) {
1953+ if (!offs_built ) {
1954+ offs = cbm_line_offsets (src , src_len , & nlines );
1955+ offs_built = true;
1956+ CBM_MACRO_LINE_SCAN (src_len );
1957+ }
1958+ benign = cbm_lines_are_macro_invocation (src , src_len , offs , nlines , rs , re , defs );
1959+ }
19071960 if (!benign ) {
1908- regs -> starts [kept ] = regs -> starts [ i ] ;
1909- regs -> ends [kept ] = regs -> ends [ i ] ;
1961+ regs -> starts [kept ] = rs ;
1962+ regs -> ends [kept ] = re ;
19101963 kept ++ ;
19111964 }
19121965 }
19131966 regs -> count = kept ;
1967+ cbm_free (CBM_MEM_CLASS_EXTRACT , offs );
19141968}
19151969
19161970/* Push [start, end] after trimming no-code lines off both ends. A run made
@@ -2072,6 +2126,21 @@ CBMFileResult *cbm_extract_file(const char *source, int source_len, CBMLanguage
20722126enum { CBM_EXTRACT_SCRATCH_BLOCK = CBM_SZ_512 * CBM_SZ_1K };
20732127enum { CBM_EXTRACT_SCRATCH_KEEP_BYTES = 4 * CBM_SZ_1K * CBM_SZ_1K };
20742128
2129+ /* The #1735 value-row exclusion is always on. Test builds can turn it off for
2130+ * one file (CBM_TEST_SQL_FULL_PARSE_ON=<rel_path substring>) so a test can
2131+ * compare the excluded parse against the full one on the same source. */
2132+ static bool cbm_sql_values_exclusion_on (const char * rel_path ) {
2133+ #ifdef CBM_ENABLE_TEST_SEAMS
2134+ const char * full_on = getenv ("CBM_TEST_SQL_FULL_PARSE_ON" );
2135+ if (full_on && full_on [0 ] && rel_path && strstr (rel_path , full_on )) {
2136+ return false;
2137+ }
2138+ #else
2139+ (void )rel_path ;
2140+ #endif
2141+ return true;
2142+ }
2143+
20752144static CBMFileResult * extract_file_ex_body (const char * source , int source_len , CBMLanguage language ,
20762145 const char * project , const char * rel_path ,
20772146 int64_t timeout_micros , const char * * extra_defines ,
@@ -2180,7 +2249,19 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
21802249#endif
21812250 }
21822251
2252+ /* #1735: a SQL data dump's literal-only INSERT rows carry no graph content
2253+ * but dominate its parse. Keep them out through included ranges; offsets
2254+ * and positions of everything kept are unchanged. The parser is
2255+ * thread-local and reused, so the ranges are cleared right after. */
2256+ CBMSqlKeptRanges sql_kept = {NULL , 0 };
2257+ bool sql_ranged = language == CBM_LANG_SQL && cbm_sql_values_exclusion_on (rel_path ) &&
2258+ cbm_sql_values_kept_ranges (source , (uint32_t )source_len , & sql_kept ) &&
2259+ ts_parser_set_included_ranges (parser , sql_kept .items , sql_kept .count );
21832260 TSTree * tree = ts_parser_parse_with_options (parser , NULL , ts_input , opts );
2261+ if (sql_ranged ) {
2262+ (void )ts_parser_set_included_ranges (parser , NULL , 0 );
2263+ }
2264+ cbm_sql_kept_ranges_free (& sql_kept );
21842265 uint64_t t1 = now_ns ();
21852266#ifdef CBM_ENABLE_TEST_SEAMS
21862267 t1 += tl_parse_wall_seam_offset_ns ; /* the stall seam inflates every wall reading */
0 commit comments