1414#include "foundation/arena.h"
1515#include <foundation/compat_fs.h>
1616#include <pipeline/pipeline.h>
17+ #include <pipeline/pipeline_internal.h>
1718#include <store/store.h>
1819#include <sqlite3.h>
1920
@@ -502,6 +503,190 @@ TEST(overload_calls_ambiguous_binds_every_candidate) {
502503 PASS ();
503504}
504505
506+ /* ── Incremental == full ───────────────────────────────────────── */
507+
508+ /* Every edge of the project as "TYPE source -> target" lines with the
509+ * project's QN prefix removed, sorted; NULL when the database cannot be read
510+ * (the caller frees). */
511+ static char * ovl_edge_set (const char * db_path ) {
512+ cbm_store_t * store = cbm_store_open_path (db_path );
513+ sqlite3 * db = store ? cbm_store_get_db (store ) : NULL ;
514+ sqlite3_stmt * st = NULL ;
515+ if (!db ||
516+ sqlite3_prepare_v2 (db ,
517+ "SELECT e.type || ' ' || replace(s.qualified_name, e.project || '.', '') "
518+ "|| ' -> ' || replace(t.qualified_name, e.project || '.', '') "
519+ "FROM edges e JOIN nodes s ON s.id=e.source_id "
520+ "JOIN nodes t ON t.id=e.target_id ORDER BY 1" ,
521+ -1 , & st , NULL ) != SQLITE_OK ) {
522+ if (store ) {
523+ cbm_store_close (store );
524+ }
525+ return NULL ;
526+ }
527+ size_t cap = 4096 ;
528+ size_t len = 0 ;
529+ char * buf = malloc (cap );
530+ if (buf ) {
531+ buf [0 ] = '\0' ;
532+ }
533+ while (buf && sqlite3_step (st ) == SQLITE_ROW ) {
534+ const char * line = (const char * )sqlite3_column_text (st , 0 );
535+ size_t l = line ? strlen (line ) : 0 ;
536+ if (len + l + 2 > cap ) {
537+ cap = (len + l + 2 ) * 2 ;
538+ char * grown = realloc (buf , cap );
539+ if (!grown ) {
540+ free (buf );
541+ buf = NULL ;
542+ break ;
543+ }
544+ buf = grown ;
545+ }
546+ memcpy (buf + len , line , l );
547+ len += l ;
548+ buf [len ++ ] = '\n' ;
549+ buf [len ] = '\0' ;
550+ }
551+ sqlite3_finalize (st );
552+ cbm_store_close (store );
553+ return buf ;
554+ }
555+
556+ static int ovl_index (const char * repo , const char * db_path ) {
557+ cbm_pipeline_t * pipeline = cbm_pipeline_new (repo , db_path , CBM_MODE_FULL );
558+ int rc = pipeline ? cbm_pipeline_run (pipeline ) : -1 ;
559+ if (pipeline ) {
560+ cbm_pipeline_free (pipeline );
561+ }
562+ return rc ;
563+ }
564+
565+ /* Index `files`, rewrite only `files[1]` (the caller) as `edited`, re-index
566+ * incrementally, and require the closure-repair route, the same edge set as a
567+ * fresh full index of the edited tree, and `want` among those edges. */
568+ static int ovl_incremental_matches_full (const ovl_file_t files [2 ], const char * edited ,
569+ const char * want ) {
570+ char tmp [256 ];
571+ snprintf (tmp , sizeof (tmp ), "/tmp/cbm_ovl_incr_XXXXXX" );
572+ if (!cbm_mkdtemp (tmp )) {
573+ return 1 ;
574+ }
575+ char repo [300 ];
576+ snprintf (repo , sizeof (repo ), "%s/repo" , tmp );
577+ char inc_db [400 ];
578+ char full_db [400 ];
579+ snprintf (inc_db , sizeof (inc_db ), "%s/inc.db" , tmp );
580+ snprintf (full_db , sizeof (full_db ), "%s/full.db" , tmp );
581+ ovl_env_t save = ovl_env_select (false);
582+ int rc = 1 ;
583+ char * inc = NULL ;
584+ char * full = NULL ;
585+ int route = -1 ;
586+ if (ovl_write (repo , files [0 ].path , files [0 ].content ) &&
587+ ovl_write (repo , files [1 ].path , files [1 ].content ) && ovl_index (repo , inc_db ) == 0 &&
588+ ovl_write (repo , files [1 ].path , edited )) {
589+ cbm_pipeline_incremental_test_reset_faults ();
590+ if (ovl_index (repo , inc_db ) == 0 ) {
591+ route = (int )cbm_pipeline_incremental_test_last_route ();
592+ if (ovl_index (repo , full_db ) == 0 ) {
593+ inc = ovl_edge_set (inc_db );
594+ full = ovl_edge_set (full_db );
595+ }
596+ }
597+ }
598+ ovl_env_restore (& save );
599+ if (inc && full && strcmp (inc , full ) == 0 && strstr (full , want ) &&
600+ route == (int )CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR ) {
601+ rc = 0 ;
602+ } else {
603+ fprintf (stderr , " route %d (want %d), want edge [%s]\n--- incremental\n%s--- full\n%s" ,
604+ route , (int )CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR , want , inc ? inc : "(null)" ,
605+ full ? full : "(null)" );
606+ }
607+ free (inc );
608+ free (full );
609+ th_rmtree (tmp );
610+ return rc ;
611+ }
612+
613+ /* The reviewer's case: an edit to the caller file alone re-resolves its calls
614+ * against proxies of the unchanged overloads, which must bind exactly as a
615+ * full index does (each call to the member its arguments fit). */
616+ TEST (overload_incremental_java_matches_full ) {
617+ static const ovl_file_t files [] = {
618+ {"src/demo/Foo.java" , "package demo;\n"
619+ "public class Foo {\n"
620+ " public void m(int a) {}\n"
621+ " public void m(String s) {}\n"
622+ " public void m(int a, int b) {}\n"
623+ "}\n" },
624+ {"src/demo/Caller.java" , "package demo;\n"
625+ "public class Caller {\n"
626+ " private final Foo f = new Foo();\n"
627+ " void run() { f.m(1); f.m(1, 2); }\n"
628+ "}\n" },
629+ };
630+ static const char edited [] = "package demo;\n"
631+ "public class Caller {\n"
632+ " private final Foo f = new Foo();\n"
633+ " void run() { f.m(1); f.m(1, 2); f.m(\"s\"); }\n"
634+ "}\n" ;
635+ ASSERT_EQ (ovl_incremental_matches_full (
636+ files , edited , "CALLS src.demo.Caller.run -> src.demo.Foo.m(int,int)\n" ),
637+ 0 );
638+ PASS ();
639+ }
640+
641+ TEST (overload_incremental_kotlin_matches_full ) {
642+ static const ovl_file_t files [] = {
643+ {"src/demo/Foo.kt" , "package demo\n"
644+ "class Foo {\n"
645+ " fun m(a: Int) {}\n"
646+ " fun m(s: String) {}\n"
647+ " fun m(a: Int, b: Int) {}\n"
648+ "}\n" },
649+ {"src/demo/Caller.kt" , "package demo\n"
650+ "class Caller {\n"
651+ " private val f = Foo()\n"
652+ " fun run() { f.m(1); f.m(1, 2) }\n"
653+ "}\n" },
654+ };
655+ static const char edited [] = "package demo\n"
656+ "class Caller {\n"
657+ " private val f = Foo()\n"
658+ " fun run() { f.m(1); f.m(1, 2); f.m(\"s\") }\n"
659+ "}\n" ;
660+ ASSERT_EQ (ovl_incremental_matches_full (files , edited ,
661+ "CALLS src.demo.Caller.Caller.run -> "
662+ "src.demo.Foo.Foo.m(Int,Int)\n" ),
663+ 0 );
664+ PASS ();
665+ }
666+
667+ TEST (overload_incremental_swift_matches_full ) {
668+ static const ovl_file_t files [] = {
669+ {"Sources/Demo/Foo.swift" , "class Foo {\n"
670+ " func m(_ a: Int) {}\n"
671+ " func m(named: String) {}\n"
672+ " func m(_ a: Int, to b: Int) {}\n"
673+ "}\n" },
674+ {"Sources/Demo/Caller.swift" , "class Caller {\n"
675+ " let f = Foo()\n"
676+ " func run() { f.m(1); f.m(1, to: 2) }\n"
677+ "}\n" },
678+ };
679+ static const char edited [] = "class Caller {\n"
680+ " let f = Foo()\n"
681+ " func run() { f.m(1); f.m(1, to: 2); f.m(named: \"x\") }\n"
682+ "}\n" ;
683+ ASSERT_EQ (ovl_incremental_matches_full (files , edited ,
684+ "CALLS Sources.Demo.Caller.Caller.run -> "
685+ "Sources.Demo.Foo.Foo.m(named:String)\n" ),
686+ 0 );
687+ PASS ();
688+ }
689+
505690SUITE (overloads ) {
506691 RUN_TEST (overload_shape_swift_labels );
507692 RUN_TEST (overload_shape_counts );
@@ -514,4 +699,7 @@ SUITE(overloads) {
514699 RUN_TEST (overload_calls_swift_sequential );
515700 RUN_TEST (overload_calls_swift_parallel );
516701 RUN_TEST (overload_calls_ambiguous_binds_every_candidate );
702+ RUN_TEST (overload_incremental_java_matches_full );
703+ RUN_TEST (overload_incremental_kotlin_matches_full );
704+ RUN_TEST (overload_incremental_swift_matches_full );
517705}
0 commit comments