diff --git a/internal/cbm/cbm.c b/internal/cbm/cbm.c index 18b6c57ea..dd266b7ee 100644 --- a/internal/cbm/cbm.c +++ b/internal/cbm/cbm.c @@ -3319,15 +3319,59 @@ CBMFileResult *cbm_extract_file(const char *source, int source_len, CBMLanguage return r; } +/* The grammars built on tree-sitter-c: same set as cbm_conditional_block_types + * minus C#, which has no backslash line splices. */ +static bool c_splice_lang(CBMLanguage language) { + return language == CBM_LANG_C || language == CBM_LANG_CPP || language == CBM_LANG_CUDA || + language == CBM_LANG_OBJC || language == CBM_LANG_GLSL || language == CBM_LANG_HLSL || + language == CBM_LANG_ISPC || language == CBM_LANG_SLANG; +} + +/* #1546: these grammars read a backslash line splice inside a string literal + * as an escape_sequence, `\` plus one character. With CRLF line endings that + * character is the CR, the LF then ends the literal, and the rest of the + * function, often the rest of the file, becomes one ERROR region. + * Each `\` CR LF becomes space `\` LF: the same length, the line break in the + * same place, so offsets and line numbers still refer to the file on disk and + * the next line is byte-identical. Copied only when a splice is found. The + * cross-file C/C++ LSP pass reads the file again and applies it too. */ +const char *cbm_c_mask_crlf_splices(CBMArena *a, const char *source, int source_len) { + if (!a || !source) { + return source; + } + char *out = NULL; + for (int i = 0; i + PAIR_LEN < source_len; i++) { + if (source[i] != '\\' || source[i + SKIP_ONE] != '\r' || source[i + PAIR_LEN] != '\n') { + continue; + } + if (!out) { + out = (char *)cbm_arena_alloc(a, (size_t)source_len + SKIP_ONE); + if (!out) { + return source; + } + memcpy(out, source, (size_t)source_len); + out[source_len] = '\0'; + } + out[i] = ' '; + out[i + SKIP_ONE] = '\\'; + i += PAIR_LEN; + } + return out ? out : source; +} + /* The bytes the grammar parses. PHP that leaves PHP mode (`?> markup ` (#2000). Same length and line breaks as the input; returns // `source` itself when nothing needs rewriting. Defined in php_inline_html.c. const char *cbm_php_mask_inline_html(CBMArena *arena, const char *source, int source_len); +// C-family source with each `\` CR LF line splice rewritten as space `\` LF so +// the grammars built on tree-sitter-c read it as they read the LF splice +// (#1546). Same length and line breaks as the input; returns `source` itself +// when there is no such splice. Defined in cbm.c. +const char *cbm_c_mask_crlf_splices(CBMArena *arena, const char *source, int source_len); void cbm_extract_imports(CBMExtractCtx *ctx); void cbm_extract_usages(CBMExtractCtx *ctx); void cbm_extract_semantic(CBMExtractCtx *ctx); diff --git a/internal/cbm/lsp/c_lsp.c b/internal/cbm/lsp/c_lsp.c index f0e5861f1..07d335a63 100644 --- a/internal/cbm/lsp/c_lsp.c +++ b/internal/cbm/lsp/c_lsp.c @@ -7228,6 +7228,10 @@ static bool c_lsp_cross_with_registry_run(CBMArena *arena, const char *source, i const CBMFileResult *test_owners, CBMLSPStatus *status) { *status = CBM_LSP_COMPLETE; + /* The retained or re-read source is the file on disk; read it the way the + * per-file extract did (#1546), so the owner digest, the cached tree and + * node text agree. */ + source = cbm_c_mask_crlf_splices(arena, source, source_len); if ((test_owners && test_owners->has_test_definition_owners && source_origin != CBM_SOURCE_ORIGIN_RAW) || !cbm_test_definition_owners_match(test_owners, source, source_len, cpp_mode, @@ -7319,6 +7323,9 @@ static bool c_lsp_cross_run(CBMArena *arena, const char *source, int source_len, CBMSourceOrigin source_origin, const CBMFileResult *test_owners, CBMLSPStatus *status) { *status = CBM_LSP_COMPLETE; + /* Same bytes as the per-file extract (#1546); see + * c_lsp_cross_with_registry_run. */ + source = cbm_c_mask_crlf_splices(arena, source, source_len); if ((test_owners && test_owners->has_test_definition_owners && source_origin != CBM_SOURCE_ORIGIN_RAW) || !cbm_test_definition_owners_match(test_owners, source, source_len, cpp_mode, diff --git a/tests/test_c_lsp.c b/tests/test_c_lsp.c index fc7bf2f1e..16c1b0ca9 100644 --- a/tests/test_c_lsp.c +++ b/tests/test_c_lsp.c @@ -16903,6 +16903,89 @@ TEST(clsp_issue1527_cross_file_memo_failures_propagate) { PASS(); } +/* #1546: the cross entry points get the file as it is on disk and must read a + * `\` CR LF string splice the way the per-file extract does. Read raw, the CR + * ends the escape, the LF ends the literal, and run() falls into the ERROR + * region with its call. LF is the control; per-file, shared-registry and + * batch entry each. */ +TEST(clsp_issue1546_cross_entry_points_read_crlf_string_splice) { + static const char *const srcs[2] = {"struct S { int m() { return 1; } };\n" + "void Middle()\n" + "{\n" + " const wchar_t* s = L\"line 0\\r\\n\\\n" + "line 1\\r\\n\";\n" + "}\n" + "\n" + "void run() { S s; s.m(); }\n", + "struct S { int m() { return 1; } };\r\n" + "void Middle()\r\n" + "{\r\n" + " const wchar_t* s = L\"line 0\\r\\n\\\r\n" + "line 1\\r\\n\";\r\n" + "}\r\n" + "\r\n" + "void run() { S s; s.m(); }\r\n"}; + CBMLSPDef defs[] = { + {.qualified_name = "test.S", .short_name = "S", .label = "Class", .lang = CBM_LANG_CPP}, + {.qualified_name = "test.S.m", + .short_name = "m", + .label = "Method", + .receiver_type = "test.S", + .return_types = "int", + .lang = CBM_LANG_CPP}, + }; + bool resolved[2][3] = {{false}}; + for (int e = 0; e < 2; e++) { + const char *src = srcs[e]; + int len = (int)strlen(src); + for (int mode = 0; mode < 3; mode++) { + CBMArena a; + cbm_arena_init(&a); + CBMTypeRegistry reg; + cbm_registry_init(®, &a); + reg.fallback = cbm_c_build_cross_registry(&a, defs, 2); + CBMResolvedCallArray out = {0}; + CBMBatchCLSPFile file = {.source = src, + .source_len = len, + .module_qn = "test", + .cpp_mode = true, + .defs = defs, + .def_count = 2}; + if (mode == 0) { + (void)cbm_run_c_lsp_cross(&a, src, len, "test", true, defs, 2, NULL, NULL, 0, NULL, + &out); + } else if (mode == 1) { + (void)cbm_run_c_lsp_cross_with_registry(&a, src, len, "test", true, ®, NULL, + NULL, 0, NULL, &out); + } else { + (void)cbm_batch_c_lsp_cross(&a, &file, 1, &out); + } + for (int i = 0; i < out.count; i++) { + const CBMResolvedCall *rc = &out.items[i]; + if (rc->caller_qn && strcmp(rc->caller_qn, "test.run") == 0 && rc->callee_qn && + strcmp(rc->callee_qn, "test.S.m") == 0) { + resolved[e][mode] = true; + } + } + cbm_arena_destroy(&a); + } + } + for (int e = 0; e < 2; e++) { + for (int mode = 0; mode < 3; mode++) { + if (!resolved[e][mode]) { + printf(" CRLF splice diagnostic: %s entry %d did not resolve run -> S.m\n", + e ? "CRLF" : "LF", mode); + } + } + } + for (int e = 0; e < 2; e++) { + for (int mode = 0; mode < 3; mode++) { + ASSERT_TRUE(resolved[e][mode]); + } + } + PASS(); +} + /* A chain's LENGTH is not nesting depth (#1527 follow-up). An operator chain * `a + a + ... + a` and a receiver chain `b.a().a()...` are evaluated link by * link, so neither consumes the evaluator's depth guard: the file keeps its @@ -17083,6 +17166,7 @@ SUITE(c_lsp) { RUN_TEST(clsp_issue1527_preprocessed_depth_failure_is_reported); RUN_TEST(registry_issue1527_index_preserves_registration_order); RUN_TEST(clsp_issue1527_cross_file_memo_failures_propagate); + RUN_TEST(clsp_issue1546_cross_entry_points_read_crlf_string_splice); RUN_TEST(clsp_issue1527_long_binary_chain_is_not_nesting); RUN_TEST(clsp_issue1527_long_receiver_chain_is_not_nesting); RUN_TEST(clsp_c_reassigned_function_pointer_calls_join_exact_occurrences); diff --git a/tests/test_extraction.c b/tests/test_extraction.c index 041c6db51..5247b1fce 100644 --- a/tests/test_extraction.c +++ b/tests/test_extraction.c @@ -401,6 +401,140 @@ TEST(extract_cpp_real_in_body_error_still_flagged_issue1071) { PASS(); } +/* #1546: the grammars built on tree-sitter-c read a backslash line splice + * inside a string literal as `\` plus one character. With CRLF line endings + * that character was the CR, the LF then ended the literal, and every + * function after it was lost to one ERROR region. */ +static const CBMDefinition *splice_def(CBMFileResult *r, const char *name) { + for (int i = 0; i < r->defs.count; i++) { + if (r->defs.items[i].name && strcmp(r->defs.items[i].name, name) == 0) { + return &r->defs.items[i]; + } + } + return NULL; +} + +/* The reporter's file, byte for byte. */ +TEST(extract_cpp_crlf_string_splice_keeps_later_functions_issue1546) { + CBMFileResult *r = extract("void Head()\r\n" + "{\r\n" + " int h = 0;\r\n" + "}\r\n" + "\r\n" + "void Middle()\r\n" + "{\r\n" + " const wchar_t* s = L\"line 0\\r\\n\\\r\n" + "line 1\\r\\n\";\r\n" + "}\r\n" + "\r\n" + "void TrailerOne()\r\n" + "{\r\n" + " int a = 1;\r\n" + "}\r\n" + "\r\n" + "void TrailerTwo()\r\n" + "{\r\n" + " int b = 2;\r\n" + "}\r\n", + CBM_LANG_CPP, "t", "repro.cpp"); + ASSERT_NOT_NULL(r); + ASSERT_FALSE(r->has_error); + ASSERT_FALSE(r->parse_incomplete); + const CBMDefinition *middle = splice_def(r, "Middle"); + const CBMDefinition *one = splice_def(r, "TrailerOne"); + const CBMDefinition *two = splice_def(r, "TrailerTwo"); + ASSERT_NOT_NULL(middle); + ASSERT_NOT_NULL(one); + ASSERT_NOT_NULL(two); + ASSERT_EQ((int)middle->start_line, 6); + ASSERT_EQ((int)middle->end_line, 10); + ASSERT_EQ((int)one->start_line, 12); + ASSERT_EQ((int)two->start_line, 17); + cbm_free_result(r); + PASS(); +} + +/* Every grammar built on tree-sitter-c has the same escape rule. */ +TEST(extract_c_family_crlf_string_splice_parses_clean_issue1546) { + static const CBMLanguage langs[] = {CBM_LANG_C, CBM_LANG_CPP, CBM_LANG_CUDA, + CBM_LANG_OBJC, CBM_LANG_GLSL, CBM_LANG_HLSL, + CBM_LANG_ISPC, CBM_LANG_SLANG}; + const char *src = "void Head()\r\n" + "{\r\n" + " int h = 0;\r\n" + "}\r\n" + "\r\n" + "void Middle()\r\n" + "{\r\n" + " const char *s = \"line 0\\\r\n" + "line 1\";\r\n" + "}\r\n" + "\r\n" + "void TrailerOne()\r\n" + "{\r\n" + " int a = 1;\r\n" + "}\r\n"; + for (size_t i = 0; i < sizeof(langs) / sizeof(langs[0]); i++) { + CBMFileResult *r = extract(src, langs[i], "t", "splice.c"); + ASSERT_NOT_NULL(r); + ASSERT_FALSE(r->parse_incomplete); + const CBMDefinition *middle = splice_def(r, "Middle"); + const CBMDefinition *tail = splice_def(r, "TrailerOne"); + ASSERT_NOT_NULL(middle); + ASSERT_NOT_NULL(tail); + ASSERT_EQ((int)middle->end_line, 10); + ASSERT_EQ((int)tail->start_line, 12); + cbm_free_result(r); + } + PASS(); +} + +/* Splices in a macro and a line comment, which the grammar already read with + * CRLF, still do; the preprocessed second pass sees the rewritten bytes too. */ +TEST(extract_c_crlf_splices_in_macro_comment_and_string_issue1546) { + CBMFileResult *r = extract("#define LOG2(a, b) \\\r\n" + " do { log_it(a); \\\r\n" + " log_it(b); } while (0)\r\n" + "\r\n" + "// a comment that ends with a splice \\\r\n" + " and goes on here\r\n" + "\r\n" + "static const char *usage = \"usage: tool\\n\\\r\n" + " -h help\\n\";\r\n" + "\r\n" + "int helper(int x)\r\n" + "{\r\n" + " return x + 1;\r\n" + "}\r\n" + "\r\n" + "int main(void)\r\n" + "{\r\n" + " LOG2(\"a\", \"b\");\r\n" + " return helper(2);\r\n" + "}\r\n", + CBM_LANG_C, "t", "mix.c"); + ASSERT_NOT_NULL(r); + ASSERT_FALSE(r->parse_incomplete); + ASSERT(has_def(r, "Macro", "LOG2")); + const CBMDefinition *usage = splice_def(r, "usage"); + const CBMDefinition *helper = splice_def(r, "helper"); + ASSERT_NOT_NULL(usage); + ASSERT_NOT_NULL(helper); + ASSERT_EQ((int)usage->end_line, 9); + ASSERT_EQ((int)helper->start_line, 11); + ASSERT_EQ((int)helper->end_line, 14); + for (int i = 0; i < r->calls.count; i++) { + const CBMCall *c = &r->calls.items[i]; + if (c->callee_name && strcmp(c->callee_name, "helper") == 0) { + ASSERT_NOT_NULL(c->enclosing_func_qn); + ASSERT_NOT_NULL(strstr(c->enclosing_func_qn, "main")); + } + } + ASSERT(has_call(r, "log_it")); + cbm_free_result(r); + PASS(); +} + /* #1735: the #1071 check's cost. cbm_subtract_macro_invocation_regions asked * "is this region a macro call?" before "is it inside a function?", and the * first question walked the source from byte 0 for every region: regions x @@ -15745,6 +15879,9 @@ SUITE(extraction) { RUN_TEST(extract_cpp_macros_issue375); RUN_TEST(extract_cpp_functionlike_macro_type_arg_no_false_parse_partial_issue1071); RUN_TEST(extract_cpp_real_in_body_error_still_flagged_issue1071); + RUN_TEST(extract_cpp_crlf_string_splice_keeps_later_functions_issue1546); + RUN_TEST(extract_c_family_crlf_string_splice_parses_clean_issue1546); + RUN_TEST(extract_c_crlf_splices_in_macro_comment_and_string_issue1546); RUN_TEST(macro_check_reads_no_source_for_regions_outside_functions_issue1735); RUN_TEST(macro_check_locates_lines_with_one_table_issue1735); RUN_TEST(extract_gdscript_issue186); diff --git a/tests/test_parallel.c b/tests/test_parallel.c index 59c8b9130..829e35777 100644 --- a/tests/test_parallel.c +++ b/tests/test_parallel.c @@ -307,6 +307,8 @@ static bool g_harness_spill = false; static int64_t g_harness_parked = -1; /* Opt-in: hand cbm_parallel_resolve the production Tier-2 C# registry. */ static bool g_harness_cs_registry = false; +/* Opt-in: the same for the Tier-2 C/C++ registry. */ +static bool g_harness_c_registry = false; static cbm_gbuf_t *run_parallel_with_extract_opts_and_mutator( const char *project, const char *repo_path, cbm_file_info_t *files, int file_count, @@ -385,10 +387,14 @@ static cbm_gbuf_t *run_parallel_with_extract_opts_and_mutator( if (g_harness_cs_registry && all_defs) { harness_registries.cs = cbm_cs_build_cross_registry(&cross_arena, all_defs, def_count); } + if (g_harness_c_registry && all_defs) { + harness_registries.c = cbm_c_build_cross_registry(&cross_arena, all_defs, def_count); + } cbm_parallel_resolve(&ctx, files, file_count, result_cache, &shared_ids, worker_count, all_defs, def_count, def_modules, module_def_index, - g_harness_cs_registry ? &harness_registries - : NULL /* tests use per-file path */); + g_harness_cs_registry || g_harness_c_registry + ? &harness_registries + : NULL /* tests use per-file path */); cbm_gbuf_set_next_id(gbuf, atomic_load(&shared_ids)); cbm_pxc_free_module_def_index(module_def_index); @@ -4879,6 +4885,115 @@ TEST(parallel_cross_file_reread_preserves_unretained_edges) { PASS(); } +/* #1546: the per-file extract parses a C-family file with each `\` CR LF line + * splice rewritten as space `\` LF. The cross-file C/C++ LSP pass reads the + * file again (the parallel driver from its retained copy, the sequential one + * from disk) and must read it the same way. o->run() after the string splice + * is a call only that pass resolves: the struct is declared in another file. + * LF is the control; with CRLF the cross pass used to see the rest of main.cpp + * as one ERROR region and lose the edge in both drivers. */ +static const char *const crlf_splice_files[][2] = { + {"other.h", "#ifndef OTHER_H\n" + "#define OTHER_H\n" + "struct ops { int (*run)(int); };\n" + "struct ops *get_ops(void);\n" + "#endif\n"}, + {"other.cpp", "#include \"other.h\"\n" + "static int run_impl(int x) { return x; }\n" + "static struct ops the_ops = { run_impl };\n" + "struct ops *get_ops(void)\n" + "{\n" + " return &the_ops;\n" + "}\n"}, + {"main.cpp", "#include \"other.h\"\n" + "void Middle(void)\n" + "{\n" + " const wchar_t* s = L\"line 0\\r\\n\\\n" + "line 1\\r\\n\";\n" + "}\n" + "\n" + "int after_one(void)\n" + "{\n" + " struct ops *o = get_ops();\n" + " return o->run(3);\n" + "}\n"}, +}; + +static int write_with_line_endings(const char *path, const char *lf_text, bool crlf) { + char *text = malloc(strlen(lf_text) * 2 + 1); + if (!text) { + return -1; + } + size_t n = 0; + for (const char *p = lf_text; *p; p++) { + if (crlf && *p == '\n') { + text[n++] = '\r'; + } + text[n++] = *p; + } + text[n] = '\0'; + int rc = th_write_file(path, text); + free(text); + return rc; +} + +static bool crlf_splice_has_lsp_edge(const cbm_gbuf_t *gbuf) { + const cbm_gbuf_edge_t *edge = + gbuf ? find_call_edge_to_target_fragment(gbuf, "main.after_one", "ops.run") : NULL; + return edge && edge->properties_json && + strstr(edge->properties_json, "\"strategy\":\"lsp") != NULL; +} + +TEST(cross_file_cpp_lsp_after_crlf_string_splice_issue1546) { + enum { CRLF_SPLICE_FILES = 3 }; + bool seq_edge[2] = {false}; + bool par_edge[2] = {false}; + for (int crlf = 0; crlf < 2; crlf++) { + char tmpdir[256]; + snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_par_crlf_XXXXXX"); + if (!cbm_mkdtemp(tmpdir)) { + FAIL("mkdtemp failed"); + } + char paths[CRLF_SPLICE_FILES][512]; + cbm_file_info_t files[CRLF_SPLICE_FILES] = {0}; + bool written = true; + for (int i = 0; i < CRLF_SPLICE_FILES; i++) { + snprintf(paths[i], sizeof(paths[i]), "%s/%s", tmpdir, crlf_splice_files[i][0]); + written = written && + write_with_line_endings(paths[i], crlf_splice_files[i][1], crlf != 0) == 0; + files[i].path = paths[i]; + files[i].rel_path = (char *)crlf_splice_files[i][0]; + files[i].language = CBM_LANG_CPP; + } + if (!written) { + th_rmtree(tmpdir); + FAIL("failed to write the CRLF splice fixture"); + } + /* Both drivers resolve C/C++ against the shared Tier-2 registry, as + * production does; tests/test_c_lsp.c covers the per-file entry. */ + cbm_gbuf_t *seq = + run_sequential_with_lsp_cross("cbm_crlf", tmpdir, files, CRLF_SPLICE_FILES); + g_harness_c_registry = true; + cbm_gbuf_t *par = run_parallel("cbm_crlf", tmpdir, files, CRLF_SPLICE_FILES, 2); + g_harness_c_registry = false; + seq_edge[crlf] = crlf_splice_has_lsp_edge(seq); + par_edge[crlf] = crlf_splice_has_lsp_edge(par); + if (!seq_edge[crlf] || !par_edge[crlf]) { + printf(" CRLF splice diagnostic (%s): sequential=%s parallel=%s\n", + crlf ? "CRLF" : "LF", seq_edge[crlf] ? "present" : "absent", + par_edge[crlf] ? "present" : "absent"); + } + cbm_gbuf_free(seq); + cbm_gbuf_free(par); + th_rmtree(tmpdir); + } + ASSERT_TRUE(seq_edge[0]); + ASSERT_TRUE(par_edge[0]); + ASSERT_TRUE(seq_edge[1]); + ASSERT_TRUE(par_edge[1]); + PASS(); +} + /* issue #294: gRPC service-name extraction must (a) preserve the canonical * proto service name (FooServiceClient → FooService, not Foo) and (b) only * match real stub/client types — ordinary receiver vars must NOT produce @@ -6138,6 +6253,7 @@ SUITE(parallel) { RUN_TEST(parallel_python_lsp_override_cross_file_emits_lsp_strategy_edges); RUN_TEST(parallel_go_cross_package_field_chain_resolves); RUN_TEST(parallel_cross_file_reread_preserves_unretained_edges); + RUN_TEST(cross_file_cpp_lsp_after_crlf_string_splice_issue1546); RUN_TEST(parallel_java_kotlin_lsp_override_cross_file_emits_lsp_strategy_edges); RUN_TEST(parallel_rust_external_target_contract); RUN_TEST(parallel_java_external_target_contract);