|
22 | 22 | #include "lsp/rust_lsp.h" |
23 | 23 | #include "preprocessor.h" |
24 | 24 | #include "sql_values.h" // #1735: literal INSERT rows kept out of the SQL parse |
| 25 | +#include "cpp_branch_views.h" |
25 | 26 | #include "foundation/compat.h" |
26 | 27 | #include "foundation/compat_fs.h" // cbm_fopen — crash-supervisor per-file marker write |
27 | 28 | #include "foundation/hash_table.h" // CBMHashTable — crash-supervisor quarantine set |
@@ -2146,6 +2147,102 @@ static bool cbm_sql_values_exclusion_on(const char *rel_path) { |
2146 | 2147 | return true; |
2147 | 2148 | } |
2148 | 2149 |
|
| 2150 | +/* Drop the entries a branch view re-extracted from code outside the |
| 2151 | + * conditionals: a definition with a QN the file already has at the same line, |
| 2152 | + * an import with the same name and path. */ |
| 2153 | +static void cpp_view_drop_seen(CBMFileResult *r, int defs_before, int imports_before) { |
| 2154 | + int w = defs_before; |
| 2155 | + for (int i = defs_before; i < r->defs.count; i++) { |
| 2156 | + const CBMDefinition *d = &r->defs.items[i]; |
| 2157 | + bool seen = false; |
| 2158 | + for (int j = 0; j < w && !seen; j++) { |
| 2159 | + const CBMDefinition *e = &r->defs.items[j]; |
| 2160 | + seen = e->start_line == d->start_line && e->qualified_name && d->qualified_name && |
| 2161 | + strcmp(e->qualified_name, d->qualified_name) == 0; |
| 2162 | + } |
| 2163 | + if (!seen) { |
| 2164 | + r->defs.items[w++] = *d; |
| 2165 | + } |
| 2166 | + } |
| 2167 | + r->defs.count = w; |
| 2168 | + w = imports_before; |
| 2169 | + for (int i = imports_before; i < r->imports.count; i++) { |
| 2170 | + const CBMImport *m = &r->imports.items[i]; |
| 2171 | + bool seen = false; |
| 2172 | + for (int j = 0; j < w && !seen; j++) { |
| 2173 | + const CBMImport *e = &r->imports.items[j]; |
| 2174 | + seen = e->local_name && m->local_name && e->module_path && m->module_path && |
| 2175 | + strcmp(e->local_name, m->local_name) == 0 && |
| 2176 | + strcmp(e->module_path, m->module_path) == 0; |
| 2177 | + } |
| 2178 | + if (!seen) { |
| 2179 | + r->imports.items[w++] = *m; |
| 2180 | + } |
| 2181 | + } |
| 2182 | + r->imports.count = w; |
| 2183 | +} |
| 2184 | + |
| 2185 | +/* Haskell: the scanner parses the first branch of an #if group and swallows |
| 2186 | + * the rest up to #endif as one token, so what is written once per |
| 2187 | + * configuration (a definition, an import, a call in the other branch) was |
| 2188 | + * seen in its first variant only. Every further branch is parsed as its own |
| 2189 | + * view (cpp_branch_views.h: same bytes and lines, the other branches and the |
| 2190 | + * conditional lines blanked) and extracted into the same result, under the |
| 2191 | + * raw parse's budget. A definition at another line under a QN the file |
| 2192 | + * already has is that definition's variant; the graph keeps one identity with |
| 2193 | + * both spans. Calls and usages outside the conditionals come back as |
| 2194 | + * duplicates, which the pipeline dedups by caller and callee. The raw parse |
| 2195 | + * alone decides the file's parse status. */ |
| 2196 | +static void extract_cpp_branch_views(const CBMExtractCtx *raw, const TSLanguage *ts_lang, |
| 2197 | + int64_t timeout_micros) { |
| 2198 | + int views = cbm_cpp_branch_view_count(raw->arena, raw->source, raw->source_len); |
| 2199 | + for (int v = 1; v <= views; v++) { |
| 2200 | + char *view = cbm_cpp_branch_view(raw->arena, raw->source, raw->source_len, v); |
| 2201 | + TSParser *parser = view ? get_thread_parser(ts_lang, raw->language) : NULL; |
| 2202 | + if (!parser) { |
| 2203 | + return; |
| 2204 | + } |
| 2205 | + ts_parser_reset(parser); |
| 2206 | + CBMStringInput input = {view, (uint32_t)raw->source_len}; |
| 2207 | + TSInput ts_input = {&input, cbm_string_read, TSInputEncodingUTF8, NULL}; |
| 2208 | + TSParseOptions opts = {0}; |
| 2209 | + CBMParseBudget budget = {0}; // cppcheck-suppress unreadVariable |
| 2210 | + if (timeout_micros > 0) { |
| 2211 | + uint64_t budget_ns = (uint64_t)timeout_micros * USEC_TO_NSEC; |
| 2212 | + budget.cpu_deadline_ns = cbm_thread_cpu_time_ns() + budget_ns; |
| 2213 | + budget.wall_ceiling_ns = now_ns() + budget_ns * CBM_PARSE_WALL_CEILING_FACTOR; |
| 2214 | + opts.payload = &budget; |
| 2215 | + opts.progress_callback = cbm_timeout_cb; |
| 2216 | + } |
| 2217 | + TSTree *tree = ts_parser_parse_with_options(parser, NULL, ts_input, opts); |
| 2218 | + if (!tree) { |
| 2219 | + return; |
| 2220 | + } |
| 2221 | + CBMExtractCtx vctx = { |
| 2222 | + .arena = raw->arena, |
| 2223 | + .scratch = raw->scratch, |
| 2224 | + .result = raw->result, |
| 2225 | + .source = view, |
| 2226 | + .source_len = raw->source_len, |
| 2227 | + .language = raw->language, |
| 2228 | + .project = raw->project, |
| 2229 | + .rel_path = raw->rel_path, |
| 2230 | + .module_qn = raw->module_qn, |
| 2231 | + .root = ts_tree_root_node(tree), |
| 2232 | + .macro_table = raw->macro_table, |
| 2233 | + .return_type_table = raw->return_type_table, |
| 2234 | + .walk_budget_nodes = cbm_walk_max_nodes(), |
| 2235 | + }; |
| 2236 | + int defs_before = raw->result->defs.count; |
| 2237 | + int imports_before = raw->result->imports.count; |
| 2238 | + cbm_extract_definitions(&vctx); |
| 2239 | + cbm_extract_imports(&vctx); |
| 2240 | + cbm_extract_unified(&vctx); |
| 2241 | + cpp_view_drop_seen(raw->result, defs_before, imports_before); |
| 2242 | + ts_tree_delete(tree); |
| 2243 | + } |
| 2244 | +} |
| 2245 | + |
2149 | 2246 | static CBMFileResult *extract_file_ex_body(const char *source, int source_len, CBMLanguage language, |
2150 | 2247 | const char *project, const char *rel_path, |
2151 | 2248 | int64_t timeout_micros, const char **extra_defines, |
@@ -2353,6 +2450,9 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C |
2353 | 2450 | cbm_test_declarations_finish(&ctx); |
2354 | 2451 | cbm_extract_imports(&ctx); |
2355 | 2452 | cbm_extract_unified(&ctx); |
| 2453 | + if (cbm_lang_needs_cpp_branch_views(language)) { |
| 2454 | + extract_cpp_branch_views(&ctx, ts_lang, timeout_micros); |
| 2455 | + } |
2356 | 2456 | result->tree_nodes = ts_node_descendant_count(root); |
2357 | 2457 | result->walk_nodes_visited = ctx.walk_nodes_visited; |
2358 | 2458 | if (ctx.walk_budget_exhausted) { |
|
0 commit comments