Skip to content

Commit 5fd1a07

Browse files
committed
doc-links(cs): the type names of a refused scope are quarantined, in every run (R4)
A scope written in this run that the reader refuses costs its own file: what its parse set is taken back and the file declares nothing. Other files then found none of its declarations either: a reference to one of its types was `missing`, or bound uniquely to another type of that name which the full picture would not choose (a type of the enclosing namespace comes before an imported one). Unplaced declarations are quarantined for exactly this reason; a refused file's were not. Now the type names the refused blob's T and Q records carry, read leniently (a line counts when its name field is there and is a name, however the rest of it reads), are quarantined, so a reference to one of them from any file is a graph gap. The stored marker carries the same names as Q records after its `!` record, so a later run that reads the marker back quarantines them too and an incremental run equals a full one; cs_scope_marked_rejected takes the marker followed only by Q records of names. The resolver's rejected_scope is therefore a function of the refused blob, and cbm_doclinks_storable_scope hands the marker to its caller to free (lsp_surface.c). One S8 expectation changes with this: Healthy.cs's reference to Hidden2, a name Bad.cs declares where no namespace can be established (a Q record), is now a graph gap rather than an edge -- as it is when Bad.cs's scope is taken. The namespace checks of S8 (Shadow, Good.Lurker) are unchanged. Test: cs_rejected_scope_contained and cs_rejected_scope_across_runs, whose shared fixture gains Good.Widget in the refused file and Lib.Widget in an imported namespace: Healthy.cs's Widget is a graph gap with no edge, and the stored marker holds the five names. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent abb71db commit 5fd1a07

5 files changed

Lines changed: 183 additions & 46 deletions

File tree

‎src/pipeline/doc_links.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ int cbm_doclinks_scope_delta(const char *stored, const char *fresh, cbm_doclink_
113113
return R->scope_delta(stored, fresh, removed, ud);
114114
}
115115

116-
const char *cbm_doclinks_storable_scope(const char *scope) {
116+
const char *cbm_doclinks_storable_scope(const char *scope, char **owned) {
117+
*owned = NULL;
117118
const cbm_doclink_resolver_t *R = resolver_of_scope(scope);
118119
if (!R || !R->scope_accepted || !R->rejected_scope) {
119120
return scope;
@@ -122,7 +123,11 @@ const char *cbm_doclinks_storable_scope(const char *scope) {
122123
if (accepted < 0) {
123124
return NULL;
124125
}
125-
return accepted ? scope : R->rejected_scope;
126+
if (accepted) {
127+
return scope;
128+
}
129+
*owned = R->rejected_scope(scope);
130+
return *owned;
126131
}
127132

128133
/* ── Run state ───────────────────────────────────────────────────── */

‎src/pipeline/doc_links.h‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,14 @@ typedef struct {
214214
void *ud);
215215
/* Optional, together: whether the reader takes a scope blob written in
216216
* this run (1), refuses it (0), or could not tell for memory (-1); and
217-
* what is stored instead of a refused blob. A run refuses the blob of its
218-
* own file and goes on without the file's declarations; the marker makes
219-
* a later run that reads the file's scope back do the same, where the
220-
* blob itself would be a stored scope the reader refuses, which fails
217+
* the marker stored instead of a refused blob (a memory-core block of
218+
* CBM_MEM_CLASS_OTHER, NULL when memory ran out). A run refuses the blob
219+
* of its own file and goes on without the file's declarations; the marker
220+
* makes a later run that reads the file's scope back do the same, where
221+
* the blob itself would be a stored scope the reader refuses, which fails
221222
* the run. */
222223
int (*scope_accepted)(const char *scope);
223-
const char *rejected_scope;
224+
char *(*rejected_scope)(const char *scope);
224225
} cbm_doclink_resolver_t;
225226

226227
extern const cbm_doclink_resolver_t cbm_doclink_cs_resolver;
@@ -255,8 +256,10 @@ int cbm_doclinks_scope_delta(const char *stored, const char *fresh, cbm_doclink_
255256

256257
/* What is stored for a scope blob written in this run (the surface row's
257258
* `dl`, lsp_surface.c): the blob itself, or its language's rejected-scope
258-
* marker when that language's reader refuses it (scope_accepted). NULL when
259-
* memory ran out deciding. */
260-
const char *cbm_doclinks_storable_scope(const char *scope);
259+
* marker when that language's reader refuses it (scope_accepted), made into
260+
* *owned (release with cbm_free(CBM_MEM_CLASS_OTHER, *owned); NULL when the
261+
* blob itself is returned). NULL when memory ran out deciding or making the
262+
* marker. */
263+
const char *cbm_doclinks_storable_scope(const char *scope, char **owned);
261264

262265
#endif /* CBM_PIPELINE_DOC_LINKS_H */

‎src/pipeline/doc_links_cs.c‎

Lines changed: 129 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5926,16 +5926,131 @@ static void undo_scope(cs_index_t *ix) {
59265926
u->nmarks = 0;
59275927
}
59285928

5929+
/* Scope line fields (0-based, the tag is field 0; internal/cbm/doclink_cs.c):
5930+
* `U region kind alias target`, `T region start end kind outer name tparams
5931+
* bases`, `M start kind explicit type name tparams sig` and `Q name`. */
5932+
enum {
5933+
CS_SCOPE_Q_NAME = 1,
5934+
CS_SCOPE_U_KIND = 2,
5935+
CS_SCOPE_M_NAME = 5,
5936+
CS_SCOPE_T_NAME = 6,
5937+
CS_SCOPE_T_BASES = 8
5938+
};
5939+
5940+
static const char *delta_field(const char *line, size_t len, int idx, size_t *flen) {
5941+
int f = 0;
5942+
size_t s = 0;
5943+
for (size_t i = 0; i <= len; i++) {
5944+
if (i == len || line[i] == '\t') {
5945+
if (f == idx) {
5946+
*flen = i - s;
5947+
return line + s;
5948+
}
5949+
f++;
5950+
s = i + SKIP_ONE;
5951+
}
5952+
}
5953+
*flen = 0;
5954+
return NULL;
5955+
}
5956+
5957+
/* True when s[0, n) is a name a reference can carry (ident_ok). */
5958+
static bool cs_name_ok(const char *s, size_t n) {
5959+
char buf[CS_NAME_BUF];
5960+
if (n == 0 || n >= sizeof(buf)) {
5961+
return false;
5962+
}
5963+
memcpy(buf, s, n);
5964+
buf[n] = '\0';
5965+
return ident_ok(buf);
5966+
}
5967+
5968+
/* The next type name a T or Q record of a scope blob carries, read leniently
5969+
* (the blob is one the reader refused): a line is taken when its name field
5970+
* is there and is a name, however the rest of it reads. Moves *cursor past
5971+
* the lines it read; false at the end of the blob. */
5972+
static bool cs_next_type_name(const char **cursor, const char **name, size_t *len) {
5973+
while (**cursor) {
5974+
const char *line = *cursor;
5975+
const char *nl = strchr(line, '\n');
5976+
size_t n = nl ? (size_t)(nl - line) : strlen(line);
5977+
*cursor = line + n + (nl ? SKIP_ONE : 0);
5978+
int idx = line[0] == 'T' ? CS_SCOPE_T_NAME : line[0] == 'Q' ? CS_SCOPE_Q_NAME : CS_NONE;
5979+
*name = idx > 0 && n > SKIP_ONE && line[SKIP_ONE] == '\t' ? delta_field(line, n, idx, len)
5980+
: NULL;
5981+
if (*name && cs_name_ok(*name, *len)) {
5982+
return true;
5983+
}
5984+
}
5985+
return false;
5986+
}
5987+
59295988
/* What is stored, in place of its scope blob, for a file whose scope the
59305989
* reader refused in the run that wrote it (cs_scope_accepted, lsp_surface.c):
5931-
* the tag and one `!` record. A later run that reads it back treats the file
5932-
* as that run did -- it declares nothing, its references are graph gaps --
5933-
* where the refused blob itself would fail that run as a stored scope this
5934-
* reader does not take. */
5990+
* the tag, one `!` record, and one Q record per type name the T and Q records
5991+
* of the refused blob carry (cs_next_type_name). A later run that reads it
5992+
* back treats the file as that run did -- it declares nothing, those names
5993+
* are quarantined, its references are graph gaps -- where the refused blob
5994+
* itself would fail that run as a stored scope this reader does not take. */
59355995
static const char CS_REJECTED_SCOPE[] = CBM_DOCLINK_CS_SCOPE_TAG "\n!\trejected\n";
59365996

5997+
/* The marker, followed by nothing but Q records of names. */
59375998
static bool cs_scope_marked_rejected(const char *scope) {
5938-
return scope && strcmp(scope, CS_REJECTED_SCOPE) == 0;
5999+
size_t n = sizeof(CS_REJECTED_SCOPE) - SKIP_ONE;
6000+
if (!scope || strncmp(scope, CS_REJECTED_SCOPE, n) != 0) {
6001+
return false;
6002+
}
6003+
for (const char *line = scope + n; *line;) {
6004+
const char *nl = strchr(line, '\n');
6005+
if (!nl || line[0] != 'Q' || line[SKIP_ONE] != '\t' ||
6006+
!cs_name_ok(line + PAIR_LEN, (size_t)(nl - line) - PAIR_LEN)) {
6007+
return false;
6008+
}
6009+
line = nl + SKIP_ONE;
6010+
}
6011+
return true;
6012+
}
6013+
6014+
/* The marker stored for the refused blob `scope` (the resolver's
6015+
* rejected_scope): a memory-core block, NULL when memory ran out. A Q record
6016+
* is never longer than the line its name is read from plus a newline. */
6017+
static char *cs_rejected_scope(const char *scope) {
6018+
size_t w = sizeof(CS_REJECTED_SCOPE) - SKIP_ONE;
6019+
char *out = (char *)cbm_alloc(CBM_MEM_CLASS_OTHER, w + strlen(scope) + PAIR_LEN);
6020+
if (!out) {
6021+
return NULL;
6022+
}
6023+
memcpy(out, CS_REJECTED_SCOPE, w);
6024+
const char *cursor = scope;
6025+
const char *name = NULL;
6026+
size_t len = 0;
6027+
while (cs_next_type_name(&cursor, &name, &len)) {
6028+
out[w++] = 'Q';
6029+
out[w++] = '\t';
6030+
memcpy(out + w, name, len);
6031+
w += len;
6032+
out[w++] = '\n';
6033+
}
6034+
out[w] = '\0';
6035+
return out;
6036+
}
6037+
6038+
/* Quarantine the type names a refused scope blob, or its stored marker,
6039+
* carries: what the file declares is unknown, so a reference to one of them
6040+
* from any file is a graph gap. false when memory ran out. */
6041+
static bool quarantine_refused(cs_index_t *ix, const cs_file_t *f, const char *scope) {
6042+
const char *cursor = scope;
6043+
const char *name = NULL;
6044+
size_t len = 0;
6045+
while (cs_next_type_name(&cursor, &name, &len)) {
6046+
char buf[CS_NAME_BUF];
6047+
memcpy(buf, name, len); /* shorter than the buffer: cs_name_ok */
6048+
buf[len] = '\0';
6049+
if (!quarantine_name(ix, f, buf)) {
6050+
return false;
6051+
}
6052+
}
6053+
return true;
59396054
}
59406055

59416056
/* Whether this reader takes a scope blob written in this run: its record
@@ -5989,7 +6104,10 @@ bool cbm_doclink_cs_test_scope_parses(const char *scope) {
59896104
* that is not known), and its path is returned. A scope written in this run
59906105
* that the reader refuses is the scanner's and this reader's disagreement,
59916106
* which costs only its own file: what its parse set is taken back, the file
5992-
* declares nothing, and its references are graph gaps (counted, logged).
6107+
* declares nothing, and its references are graph gaps (counted, logged). The
6108+
* names of its types are quarantined: what it declares is unknown, so a
6109+
* reference to one of them from any file is a graph gap too. A scope stored
6110+
* as the rejected marker is read as in the run that refused it.
59936111
* Returns "" when memory ran out, NULL when all is well. */
59946112
static const char *build_files(cs_index_t *ix, const cbm_doclink_build_in_t *in) {
59956113
CBMHashTable *dir_unit = cbm_ht_create(CBM_SZ_1K);
@@ -6018,6 +6136,10 @@ static const char *build_files(cs_index_t *ix, const cbm_doclink_build_in_t *in)
60186136
.is_test = f->is_test,
60196137
.rejected = true};
60206138
ix->rejected++;
6139+
if (!quarantine_refused(ix, f, scope)) {
6140+
bad = ""; /* memory ran out */
6141+
break;
6142+
}
60216143
scope = NULL;
60226144
}
60236145
if (!scope) {
@@ -6252,28 +6374,6 @@ static void cs_resolve(const void *index, void *state, int run_file, const CBMDo
62526374

62536375
/* ── Incremental scope rules ─────────────────────────────────────── */
62546376

6255-
/* Scope line fields (0-based, the tag is field 0; internal/cbm/doclink_cs.c):
6256-
* `U region kind alias target`, `T region start end kind outer name tparams
6257-
* bases` and `M start kind explicit type name tparams sig`. */
6258-
enum { CS_SCOPE_U_KIND = 2, CS_SCOPE_M_NAME = 5, CS_SCOPE_T_BASES = 8 };
6259-
6260-
static const char *delta_field(const char *line, size_t len, int idx, size_t *flen) {
6261-
int f = 0;
6262-
size_t s = 0;
6263-
for (size_t i = 0; i <= len; i++) {
6264-
if (i == len || line[i] == '\t') {
6265-
if (f == idx) {
6266-
*flen = i - s;
6267-
return line + s;
6268-
}
6269-
f++;
6270-
s = i + SKIP_ONE;
6271-
}
6272-
}
6273-
*flen = 0;
6274-
return NULL;
6275-
}
6276-
62776377
/* A using directive only its own file sees: one that is not `global`. */
62786378
static bool delta_local_using(const char *line, size_t len) {
62796379
if (line[0] != 'U') {
@@ -6409,5 +6509,5 @@ const cbm_doclink_resolver_t cbm_doclink_cs_resolver = {
64096509
.resolve = cs_resolve,
64106510
.scope_delta = cs_scope_delta,
64116511
.scope_accepted = cs_scope_accepted,
6412-
.rejected_scope = CS_REJECTED_SCOPE,
6512+
.rejected_scope = cs_rejected_scope,
64136513
};

‎src/pipeline/lsp_surface.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,10 @@ static char *surface_file_to_json(const CBMFileResult *result, const CBMLSPDef *
165165
/* a scope this run's reader refuses is stored as its language's
166166
* rejected marker: a later run then treats the file as this one does
167167
* (doc_links.h, scope_accepted) */
168-
const char *kept = cbm_doclinks_storable_scope(result->doc_scope);
168+
char *marker = NULL;
169+
const char *kept = cbm_doclinks_storable_scope(result->doc_scope, &marker);
169170
char *portable = kept ? cbm_doclink_portable_scope(kept) : NULL;
171+
cbm_free(CBM_MEM_CLASS_OTHER, marker);
170172
if (!portable) {
171173
yyjson_mut_doc_free(doc);
172174
return NULL; /* a missing scope would diverge silently: fail the row */

‎tests/test_doc_mentions.c‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10041,8 +10041,13 @@ TEST(doc_mentions_cs_scanner_output_reads_back) {
1004110041
* file. The status stays ok and the other file's edges are there; the file's
1004210042
* own reference is a graph gap; and nothing its parse set before the refusal
1004310043
* stays -- a namespace it declares does not stand beside a type of that name
10044-
* (Shadow, Good.Lurker), and a name it quarantined does not block another
10045-
* file's declaration (Hidden2). */
10044+
* (Shadow, Good.Lurker).
10045+
* R4: the type names its T and Q records carry are quarantined, as the names
10046+
* of declarations no scope is known for are: a reference to one, from any
10047+
* file, is a graph gap -- never a binding to another type of that name the
10048+
* full picture would not choose (Widget: Good.Widget of Bad.cs comes before
10049+
* the imported Lib.Widget), nor to one the unplaced declaration would make
10050+
* uncertain (Hidden2, as when the scope is taken). */
1004610051
/* S8: Bad.cs declares what would shadow and hide Healthy.cs's names; its
1004710052
* scope is spoiled where it is written (cbm_doclink_cs_test_spoil_scope). */
1004810053
static const dm_source_t dm_rejected_files[] = {
@@ -10056,29 +10061,37 @@ static const dm_source_t dm_rejected_files[] = {
1005610061
"\n"
1005710062
" /// <summary><see cref=\"Target\"/></summary>\n"
1005810063
" public class FromBad { }\n"
10064+
" public class Widget { }\n"
1005910065
"}\n"
1006010066
"namespace .Broken\n"
1006110067
"{\n"
1006210068
" public class Hidden2 { }\n"
1006310069
"}\n"},
10064-
{"Healthy.cs", "public class Shadow { }\n"
10070+
{"Healthy.cs", "using Lib;\n"
10071+
"public class Shadow { }\n"
1006510072
"namespace Good\n"
1006610073
"{\n"
1006710074
" public class Target { }\n"
1006810075
" public class Lurker { }\n"
1006910076
" public class Hidden2 { }\n"
1007010077
"\n"
1007110078
" /// <summary><see cref=\"Target\"/> <see cref=\"Shadow\"/>\n"
10072-
" /// <see cref=\"Lurker\"/> <see cref=\"Hidden2\"/></summary>\n"
10079+
" /// <see cref=\"Lurker\"/> <see cref=\"Hidden2\"/>\n"
10080+
" /// <see cref=\"Widget\"/></summary>\n"
1007310081
" public class Uses { }\n"
1007410082
"}\n"},
10083+
{"Lib.cs", "namespace Lib\n"
10084+
"{\n"
10085+
" public class Widget { }\n"
10086+
"}\n"},
1007510087
};
1007610088

1007710089
static const dm_want_t dm_rejected_wants[] = {
1007810090
{"Healthy.cs", "Target", "Healthy.Uses", "Healthy.Target", NULL, NULL, NULL},
1007910091
{"Healthy.cs", "Shadow", "Healthy.Uses", "Healthy.Shadow", NULL, NULL, NULL},
1008010092
{"Healthy.cs", "Lurker", "Healthy.Uses", "Healthy.Lurker", NULL, NULL, NULL},
10081-
{"Healthy.cs", "Hidden2", "Healthy.Uses", "Healthy.Hidden2", NULL, NULL, NULL},
10093+
{"Healthy.cs", "Hidden2", "Healthy.Uses", NULL, "graph_gap", "Healthy.Hidden2", NULL},
10094+
{"Healthy.cs", "Widget", "Healthy.Uses", NULL, "graph_gap", "Lib.Widget", NULL},
1008210095
{"Bad.cs", "Target", "Bad.FromBad", NULL, "graph_gap", "Healthy.Target", NULL},
1008310096
};
1008410097

@@ -10151,7 +10164,21 @@ TEST(doc_mentions_cs_rejected_scope_across_runs) {
1015110164
int indexed = dm_index(repo, db, NULL);
1015210165
char stored[256];
1015310166
dm_stored_scope(db, "Bad.cs", stored, sizeof(stored));
10154-
bool marked = strcmp(stored, CBM_DOCLINK_CS_SCOPE_TAG "\n!\trejected\n") == 0;
10167+
/* the marker, then one Q record per type name of the refused blob (R4) */
10168+
static const char *const names[] = {"Inner", "Deep", "FromBad", "Widget", "Hidden2"};
10169+
static const char marker[] = CBM_DOCLINK_CS_SCOPE_TAG "\n!\trejected\n";
10170+
bool marked = strncmp(stored, marker, sizeof(marker) - 1) == 0;
10171+
size_t want_len = sizeof(marker) - 1;
10172+
for (size_t k = 0; k < sizeof(names) / sizeof(names[0]); k++) {
10173+
char q[64];
10174+
snprintf(q, sizeof(q), "\nQ\t%s\n", names[k]);
10175+
marked = marked && strstr(stored, q) != NULL;
10176+
want_len += strlen(q) - 1;
10177+
}
10178+
if (!marked || strlen(stored) != want_len) {
10179+
printf(" stored for Bad.cs: %s\n", stored);
10180+
marked = false;
10181+
}
1015510182
char edited[2048];
1015610183
snprintf(edited, sizeof(edited), "%s\n// body-only edit\n", dm_rejected_files[1].text);
1015710184
th_write_file(TH_PATH(repo, "Healthy.cs"), edited);

0 commit comments

Comments
 (0)