Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

doc-links(cs): a shared doc comment counts once; lost doc-link data fails the layer (S4, S17)

Martin Vogel4b3f5a8bdf4c
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index dca55ab5c..63265cebc 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -624,6 +624,11 @@ typedef struct {
CBMDocLink *items;
int count;
int cap;
+ /* Memory ran out while this file's doc-link data was extracted (a token,
+ * a reference value, a doc text or the scope blob was lost): the
+ * resolving half fails the layer instead of publishing a silently
+ * incomplete graph. */
+ bool failed;
} CBMDocLinkArray;
// Full extraction result for one file.
diff --git a/internal/cbm/doclink.c b/internal/cbm/doclink.c
index 1b55ab792..81d5a620f 100644
--- a/internal/cbm/doclink.c
+++ b/internal/cbm/doclink.c
@@ -179,9 +179,7 @@ bool cbm_doclink_lang_supported(CBMLanguage lang) {
typedef struct {
const char *doc;
uint32_t line;
- int token_first;
- int token_count;
- bool parsed;
+ bool parsed; /* its references were taken, for the first definition that has it */
} doc_line_ent_t;
typedef struct {
@@ -303,6 +301,7 @@ void cbm_doclinks_push(CBMDocLinkArray *arr, CBMArena *a, CBMDocLink link) {
grown = (CBMDocLink *)cbm_arena_alloc(a, (size_t)ncap * sizeof(*grown));
}
if (!grown) {
+ arr->failed = true; /* the token is lost: the layer must not say ok */
return;
}
if (arr->count > 0) {
@@ -381,36 +380,36 @@ void cbm_doclinks_extract(CBMExtractCtx *ctx) {
if (twins && d->label && strcmp(d->label, L->twin_label) == 0) {
doclink_twin_key_t key = {d->start_line, d->name};
if (bsearch(&key, twins, (size_t)twin_count, sizeof(twins[0]), twin_key_cmp)) {
- continue; /* the Field twin carries these references */
+ /* the Field twin carries these references -- and so those of
+ * the declarators after it, which share this doc text */
+ doc_line_ent_t *taken = doc_info_of(ctx, d->docstring);
+ if (taken) {
+ taken->parsed = true;
+ }
+ continue;
}
}
doc_line_ent_t *info = doc_info_of(ctx, d->docstring);
uint32_t doc_line = info ? info->line : 0;
- bool reusable = ctx->language == CBM_LANG_CSHARP && info && info->line > 0;
- CBMDocLinkArray *links = &ctx->result->doc_links;
- if (reusable && info->parsed) {
- /* C# lexical tokens depend only on the shared text and its line.
- * Bind each replay to this definition. Indices survive array
- * growth; no token pointer is kept across a push. */
- for (int k = 0; k < info->token_count; k++) {
- CBMDocLink link = links->items[info->token_first + k];
- link.source_qn = d->qualified_name;
- link.def_line = d->start_line;
- cbm_doclinks_push(links, ctx->arena, link);
+ bool csharp = ctx->language == CBM_LANG_CSHARP;
+ if (csharp && info && info->parsed) {
+ /* One C# doc comment documents every declarator of a field
+ * declaration (`int a, b, c;` share its text, extract_defs.c): its
+ * references are taken once, from the first declarator. Taken
+ * again per declarator, the tokens, their resolutions and the
+ * rows grow with references x declarators. */
+ continue;
+ }
+ if (csharp) {
+ if (!cbm_doclink_cs_parse_doc_checked(ctx, d, d->docstring,
+ doc_line ? doc_line : d->start_line)) {
+ ctx->result->doc_links.failed = true; /* a reference value was lost */
}
} else {
- int first = links->count;
- bool complete = false;
- if (reusable) {
- complete = cbm_doclink_cs_parse_doc_checked(ctx, d, d->docstring, doc_line);
- } else {
- L->parse_doc(ctx, d, d->docstring, doc_line ? doc_line : d->start_line);
- }
- if (reusable && complete) {
- info->token_first = first;
- info->token_count = links->count - first;
- info->parsed = true;
- }
+ L->parse_doc(ctx, d, d->docstring, doc_line ? doc_line : d->start_line);
+ }
+ if (info) {
+ info->parsed = true;
}
}
/* The file's own doc: its references belong to the file. The parser gets
diff --git a/internal/cbm/doclink.h b/internal/cbm/doclink.h
index 960fdc329..fe6b461f1 100644
--- a/internal/cbm/doclink.h
+++ b/internal/cbm/doclink.h
@@ -149,6 +149,7 @@ enum {
CBM_DOCLINK_ALLOC_TEXT,
CBM_DOCLINK_ALLOC_VALUE,
CBM_DOCLINK_ALLOC_TOKENS,
+ CBM_DOCLINK_ALLOC_SCOPE, /* the C# scope scan, as if its builder ran out */
CBM_DOCLINK_ALLOC_KINDS,
};
void cbm_doclink_test_fail_alloc_after(int kind, int nth);
diff --git a/internal/cbm/doclink_cs.c b/internal/cbm/doclink_cs.c
index 2e744069a..14c8a7b03 100644
--- a/internal/cbm/doclink_cs.c
+++ b/internal/cbm/doclink_cs.c
@@ -2984,7 +2984,17 @@ const char *cbm_doclink_cs_scan_scope(CBMExtractCtx *ctx) {
cs_emit_unplaced(&s, s.untrusted_row + TS_LINE_OFFSET, s.root_end_line);
}
cs_cost_publish(&s);
+#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
+ if (cbm_doclink_test_fail_alloc(CBM_DOCLINK_ALLOC_SCOPE)) {
+ s.failed = true;
+ }
+#endif
if (s.failed || s.sb.failed || !s.sb.buf) {
+ /* memory ran out: no scope would read as a file that declares
+ * nothing, so the layer is told */
+ if (ctx->result) {
+ ctx->result->doc_links.failed = true;
+ }
return NULL;
}
return s.sb.buf;
@@ -3584,7 +3594,11 @@ const char *cbm_doclink_cs_project_scan_scope(CBMExtractCtx *ctx) {
/* A project file past the size a project file has is not read either,
* and its blob says so: what it holds is unknown, not absent. */
if (ctx->source_len > CSX_MAX_PROJECT_BYTES) {
- return cbm_arena_strdup(ctx->arena, CBM_DOCLINK_CS_SCOPE_TAG "\nP\t\t>\n");
+ const char *blob = cbm_arena_strdup(ctx->arena, CBM_DOCLINK_CS_SCOPE_TAG "\nP\t\t>\n");
+ if (!blob && ctx->result) {
+ ctx->result->doc_links.failed = true; /* memory ran out */
+ }
+ return blob;
}
/* a *.csproj marks its directory as a C# project even when it cannot be
* read; a *.props or *.targets file has a blob only as an MSBuild
@@ -3649,6 +3663,9 @@ const char *cbm_doclink_cs_project_scan_scope(CBMExtractCtx *ctx) {
return NULL;
}
if (p.out.failed || p.value.failed || !p.out.buf) {
+ if (ctx->result) {
+ ctx->result->doc_links.failed = true; /* memory ran out, see above */
+ }
return NULL;
}
return p.out.buf;
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 65846205b..1001563f5 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -1928,6 +1928,15 @@ static void doc_collect_kotlin(CBMExtractCtx *ctx, TSNode parent, TSNode anchor,
}
}
+/* A doc comment was lost because memory ran out. For a language whose doc
+ * comments are read for links, the doc-link layer must not then report a
+ * complete graph (CBMDocLinkArray.failed). */
+static void doc_lost(CBMExtractCtx *ctx) {
+ if (ctx->result && cbm_doclink_lang_supported(ctx->language)) {
+ ctx->result->doc_links.failed = true;
+ }
+}
+
/* Leading trivia of `anchor`, in source order. */
static void doc_collect_trivia(CBMExtractCtx *ctx, TSNode anchor, doc_trivia_t *t) {
memset(t, 0, sizeof(*t));
@@ -1944,12 +1953,15 @@ static void doc_collect_trivia(CBMExtractCtx *ctx, TSNode anchor, doc_trivia_t *
found = doc_collect_cursor(ctx, parent, anchor, t);
}
if (!found) {
+ bool failed = t->failed;
memset(t, 0, sizeof(*t));
- return;
- }
- if (t->count == 0 && ctx->language == CBM_LANG_KOTLIN) {
+ t->failed = failed;
+ } else if (t->count == 0 && ctx->language == CBM_LANG_KOTLIN) {
doc_collect_kotlin(ctx, parent, anchor, t);
}
+ if (t->failed) {
+ doc_lost(ctx);
+ }
}
/* go/ast CommentGroup.Text: a line comment with no space after the slashes is
@@ -2071,6 +2083,7 @@ static const char *doc_run_text(CBMExtractCtx *ctx, const doc_trivia_t *t, int f
buf = (char *)cbm_arena_alloc(ctx->arena, total + SKIP_ONE);
}
if (!buf) {
+ doc_lost(ctx);
return NULL;
}
size_t w = 0;
@@ -8618,8 +8631,11 @@ static void extract_csharp_vars(CBMExtractCtx *ctx, TSNode node, CBMArena *a) {
return;
}
/* All declarators have this field as their documentation anchor. Keep one
- * immutable arena string, while each variable retains its own identity.
- * Failed or absent collection keeps the original per-variable lookup. */
+ * immutable arena string, while each variable retains its own identity:
+ * the doc-link driver takes the references of that one text once, from
+ * the first declarator. A text that could not be collected whole is no
+ * doc of any of them (doc_lost has marked the file's doc links failed);
+ * it is not looked up again per declarator. */
bool complete = false;
const char *doc = extract_member_docstring_status(ctx, node, &complete);
if (!complete) {
@@ -8641,11 +8657,7 @@ static void extract_csharp_vars(CBMExtractCtx *ctx, TSNode node, CBMArena *a) {
}
if (!ts_node_is_null(id)) {
const char *name = cbm_node_text(a, id, ctx->source);
- if (doc) {
- push_var_def_qn_doc(ctx, name, NULL, decl, doc);
- } else {
- push_var_def(ctx, name, decl);
- }
+ push_var_def_qn_doc(ctx, name, NULL, decl, doc);
}
}
}
diff --git a/src/pipeline/doc_links.c b/src/pipeline/doc_links.c
index 5de452e33..7c1af2bb6 100644
--- a/src/pipeline/doc_links.c
+++ b/src/pipeline/doc_links.c
@@ -342,9 +342,35 @@ static void mark_failed(cbm_doclinks_t *dl, const char *rel, const char *why) {
}
}
+#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
+static _Atomic int doclinks_test_edge_fail_after;
+
+void cbm_doclinks_test_fail_edge_insert_after(int nth) {
+ atomic_store(&doclinks_test_edge_fail_after, nth > 0 ? nth : 0);
+}
+#endif
+
+/* Insert one MENTIONS edge; 0 when it could not be stored. */
+static int64_t doclinks_insert_edge(cbm_gbuf_t *gb, int64_t src, int64_t tgt, const char *props) {
+#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
+ int n = atomic_load(&doclinks_test_edge_fail_after);
+ while (n > 0) {
+ if (atomic_compare_exchange_weak(&doclinks_test_edge_fail_after, &n, n - SKIP_ONE)) {
+ if (n == SKIP_ONE) {
+ return 0;
+ }
+ break;
+ }
+ }
+#endif
+ return cbm_gbuf_insert_edge(gb, src, tgt, "MENTIONS", props);
+}
+
/* Emit one MENTIONS edge per (source, target): first line, its syntax, the
- * mention count, and tier exact when any mention bound exactly. */
-static void emit_mentions(cbm_doclinks_t *dl, doclink_mention_t *m, int n, cbm_gbuf_t *edge_out) {
+ * mention count, and tier exact when any mention bound exactly. A failed
+ * insert fails the layer: the edge is not there. */
+static void emit_mentions(cbm_doclinks_t *dl, const char *rel, doclink_mention_t *m, int n,
+ cbm_gbuf_t *edge_out) {
qsort(m, (size_t)n, sizeof(*m), mention_cmp);
int i = 0;
while (i < n) {
@@ -360,16 +386,25 @@ static void emit_mentions(cbm_doclinks_t *dl, doclink_mention_t *m, int n, cbm_g
"\"count\":%d}",
cbm_doclink_syntax_name(m[i].syntax), exact ? "exact" : "unique", m[i].line,
j - i);
- cbm_gbuf_insert_edge(edge_out, m[i].src, m[i].tgt, "MENTIONS", props);
- atomic_fetch_add_explicit(&dl->edges, 1, memory_order_relaxed);
+ if (doclinks_insert_edge(edge_out, m[i].src, m[i].tgt, props) == 0) {
+ mark_failed(dl, rel, "alloc"); /* an edge that is not there is not counted */
+ } else {
+ atomic_fetch_add_explicit(&dl->edges, 1, memory_order_relaxed);
+ }
i = j;
}
}
void cbm_doclinks_resolve_file(cbm_doclinks_t *dl, int file_idx, const CBMFileResult *result,
const cbm_gbuf_t *graph, cbm_gbuf_t *edge_out) {
- if (!dl || !result || file_idx < 0 || file_idx >= dl->file_count ||
- result->doc_links.count == 0 || !result->doc_links.items) {
+ if (!dl || !result || file_idx < 0 || file_idx >= dl->file_count) {
+ return;
+ }
+ if (result->doc_links.failed) {
+ /* the extraction lost doc-link data of this file to memory */
+ mark_failed(dl, dl->files[file_idx].rel_path, "extract");
+ }
+ if (result->doc_links.count == 0 || !result->doc_links.items) {
return;
}
int64_t started = doclinks_now_ns();
@@ -461,7 +496,7 @@ void cbm_doclinks_resolve_file(cbm_doclinks_t *dl, int file_idx, const CBMFileRe
atomic_fetch_add_explicit(&dl->reasons[reason], 1, memory_order_relaxed);
}
if (nm > 0) {
- emit_mentions(dl, mentions, nm, edge_out);
+ emit_mentions(dl, fi->rel_path, mentions, nm, edge_out);
}
cbm_free(CBM_MEM_CLASS_OTHER, mentions);
atomic_fetch_add_explicit(&dl->resolve_ns, doclinks_now_ns() - started, memory_order_relaxed);
diff --git a/src/pipeline/doc_links.h b/src/pipeline/doc_links.h
index 40291d86a..69fed4bd1 100644
--- a/src/pipeline/doc_links.h
+++ b/src/pipeline/doc_links.h
@@ -219,6 +219,9 @@ uint64_t cbm_doclink_cs_test_work(void);
/* The buckets the index build walked to empty its per-file scratch table
* (reset with the work above). */
uint64_t cbm_doclink_cs_test_scratch_work(void);
+/* Test seam (doc_links.c): the nth MENTIONS edge insert from now on fails as
+ * if memory ran out (0: none). */
+void cbm_doclinks_test_fail_edge_insert_after(int nth);
#endif
/* True when `rel_path` is a scope input of some language (scope_input). */
diff --git a/tests/test_doc_mentions.c b/tests/test_doc_mentions.c
index e8f8272d0..0969fdc6b 100644
--- a/tests/test_doc_mentions.c
+++ b/tests/test_doc_mentions.c
@@ -6036,8 +6036,8 @@ TEST(doc_mentions_cs_scan_declarator_modifier_work) {
PASS();
}
-/* Sharing a large comment must not copy or parse its prose per declarator.
- * Tokens remain per source; the measured work excludes that required output. */
+/* Sharing a large comment must not copy or parse its prose per declarator,
+ * and its references are taken once, from the first declarator (S4). */
TEST(doc_mentions_cs_shared_doc_work) {
bool bounded = true;
for (int n = 16; n <= 32; n *= 2) {
@@ -6066,7 +6066,7 @@ TEST(doc_mentions_cs_shared_doc_work) {
"cleaned=%llu tokens=%d\n",
n, prose, len, (unsigned long long)copied, (unsigned long long)parse_input,
(unsigned long long)cleaned, tokens);
- ASSERT_EQ(tokens, n);
+ ASSERT_EQ(tokens, 1);
bounded = bounded && copied <= (uint64_t)len * 2 && parse_input <= (uint64_t)len * 2 &&
cleaned <= 12;
}
@@ -6075,24 +6075,27 @@ TEST(doc_mentions_cs_shared_doc_work) {
PASS();
}
-/* A one-shot resource failure must not suppress later sources via sharing. */
+/* A failed allocation while a shared doc comment is read loses references:
+ * the file's doc links say so (S17), and the later declarators do not take
+ * the comment again -- neither from the shared text nor by a lookup of their
+ * own (S4). Without a failure the first declarator has every reference. */
TEST(doc_mentions_cs_shared_doc_allocation_failure) {
static const struct {
- int kind, nth, refs, expected[3];
+ int kind, nth, refs;
} cases[] = {
- {CBM_DOCLINK_ALLOC_VALUE, 3, 2, {2, 1, 2}},
- {CBM_DOCLINK_ALLOC_TOKENS, 2, 10, {10, 9, 10}},
- {CBM_DOCLINK_ALLOC_TEXT, 2, 2, {2, 2, 2}},
- /* The second comment collection loses a span while growing past8.
- * It must not become the shared doc for the later variables. */
- {CBM_DOCLINK_ALLOC_SPAN, 4, 12, {12, 12, 12}},
+ {CBM_DOCLINK_ALLOC_KINDS, 0, 2}, /* no failure */
+ {CBM_DOCLINK_ALLOC_VALUE, 2, 2}, {CBM_DOCLINK_ALLOC_TOKENS, 2, 20},
+ {CBM_DOCLINK_ALLOC_TEXT, 1, 2}, {CBM_DOCLINK_ALLOC_SPAN, 2, 12},
};
bool ok = true;
for (size_t k = 0; k < sizeof(cases) / sizeof(cases[0]); k++) {
char *src = dm_repeated("class C {\n", "/// <see cref=\"First\"/>\n", cases[k].refs,
"public int a,b,c;\n}\n");
ASSERT_NOT_NULL(src);
- cbm_doclink_test_fail_alloc_after(cases[k].kind, cases[k].nth);
+ bool fail = cases[k].kind < CBM_DOCLINK_ALLOC_KINDS;
+ if (fail) {
+ cbm_doclink_test_fail_alloc_after(cases[k].kind, cases[k].nth);
+ }
CBMFileResult *r = dm_extract(src, CBM_LANG_CSHARP, "Failure.cs");
cbm_doclink_test_reset_alloc();
free(src);
@@ -6105,19 +6108,19 @@ TEST(doc_mentions_cs_shared_doc_allocation_failure) {
ASSERT_TRUE(name[0] >= 'a' && name[0] <= 'c' && name[1] == '\0');
counts[name[0] - 'a']++;
}
+ bool failed = r->doc_links.failed;
cbm_free_result(r);
- printf(" shared doc allocation: stage=%d counts=%d,%d,%d expected=%d,%d,%d\n",
- cases[k].kind, counts[0], counts[1], counts[2], cases[k].expected[0],
- cases[k].expected[1], cases[k].expected[2]);
- for (int i = 0; i < 3; i++) {
- ok = ok && counts[i] == cases[k].expected[i];
- }
+ printf(" shared doc allocation: stage=%d counts=%d,%d,%d failed=%d\n", cases[k].kind,
+ counts[0], counts[1], counts[2], failed);
+ bool first = fail ? counts[0] < cases[k].refs : counts[0] == cases[k].refs;
+ ok = ok && failed == fail && first && counts[1] == 0 && counts[2] == 0;
}
ASSERT_TRUE(ok);
PASS();
}
-/* Shared lexical tokens survive output growth, distinct comments and files. */
+/* Each shared comment yields its references once, from the first declarator
+ * of its own declaration -- distinct comments and files stay distinct. */
TEST(doc_mentions_cs_shared_doc_replay) {
enum { REFS = 80 };
char *first = dm_repeated("class C {\n/// ", "<see cref=\"First\"/> ", REFS,
@@ -6129,7 +6132,7 @@ TEST(doc_mentions_cs_shared_doc_replay) {
CBMFileResult *r = dm_extract(src, CBM_LANG_CSHARP, "Shared.cs");
free(src);
ASSERT_NOT_NULL(r);
- ASSERT_EQ(r->doc_links.count, 6 * REFS);
+ ASSERT_EQ(r->doc_links.count, 2 * REFS);
int counts[6] = {0};
for (int i = 0; i < r->doc_links.count; i++) {
const CBMDocLink *link = &r->doc_links.items[i];
@@ -6147,16 +6150,14 @@ TEST(doc_mentions_cs_shared_doc_replay) {
}
cbm_free_result(r);
for (int i = 0; i < 6; i++) {
- ASSERT_EQ(counts[i], REFS);
+ ASSERT_EQ(counts[i], (i == 0 || i == 3) ? REFS : 0);
}
r = dm_extract("class C {\n/// <see cref=\"Other\"/>\npublic int a,b,c;\n}\n", CBM_LANG_CSHARP,
"Shared.cs");
ASSERT_NOT_NULL(r);
- ASSERT_EQ(r->doc_links.count, 3);
- for (int i = 0; i < r->doc_links.count; i++) {
- ASSERT_STR_EQ(r->doc_links.items[i].raw, "Other");
- ASSERT_EQ(r->doc_links.items[i].line, 2);
- }
+ ASSERT_EQ(r->doc_links.count, 1);
+ ASSERT_STR_EQ(r->doc_links.items[0].raw, "Other");
+ ASSERT_EQ(r->doc_links.items[0].line, 2);
cbm_free_result(r);
PASS();
}
@@ -6463,7 +6464,8 @@ static int dm_check_repo(const char *tag, const dm_source_t *files, int nfiles,
#define DM_COUNT(a) ((int)(sizeof(a) / sizeof((a)[0])))
-/* Distinct variables documented together remain distinct MENTIONS sources. */
+/* Variables documented together by one comment: the comment's references
+ * are the first declarator's (S4); the other declarators mention nothing. */
TEST(doc_mentions_cs_shared_doc_sources) {
static const char source[] = "namespace N {\n"
"public class First { } public class Second { }\n"
@@ -6475,7 +6477,7 @@ TEST(doc_mentions_cs_shared_doc_sources) {
"}\n}\n";
CBMFileResult *r = dm_extract(source, CBM_LANG_CSHARP, "Shared.cs");
ASSERT_NOT_NULL(r);
- ASSERT_EQ(r->doc_links.count, 6);
+ ASSERT_EQ(r->doc_links.count, 2);
int pairs[3][2] = {{0}};
for (int i = 0; i < r->doc_links.count; i++) {
const CBMDocLink *link = &r->doc_links.items[i];
@@ -6495,22 +6497,76 @@ TEST(doc_mentions_cs_shared_doc_sources) {
cbm_free_result(r);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
- ASSERT_EQ(pairs[i][j], 1);
+ ASSERT_EQ(pairs[i][j], i == 0 ? 1 : 0);
}
}
const dm_source_t files[] = {{"Shared.cs", source}};
static const dm_want_t wants[] = {
- {"Shared.cs", "First", "a", "First", NULL, NULL, NULL},
- {"Shared.cs", "Second", "a", "Second", NULL, NULL, NULL},
- {"Shared.cs", "First", "b", "First", NULL, NULL, NULL},
- {"Shared.cs", "Second", "b", "Second", NULL, NULL, NULL},
- {"Shared.cs", "First", "c", "First", NULL, NULL, NULL},
- {"Shared.cs", "Second", "c", "Second", NULL, NULL, NULL},
+ {"Shared.cs", "First", "C.a", "First", NULL, NULL, NULL},
+ {"Shared.cs", "Second", "C.a", "Second", NULL, NULL, NULL},
+ {"Shared.cs", "First", "C.b", NULL, NULL, "First", NULL},
+ {"Shared.cs", "Second", "C.b", NULL, NULL, "Second", NULL},
+ {"Shared.cs", "First", "C.c", NULL, NULL, "First", NULL},
+ {"Shared.cs", "Second", "C.c", NULL, NULL, "Second", NULL},
};
ASSERT_EQ(dm_check_repo("shared_doc", files, DM_COUNT(files), wants, DM_COUNT(wants)), 0);
PASS();
}
+/* S4: tokens, resolutions and rows of a doc comment shared by the
+ * declarators of one field declaration grow with its references, not with
+ * references x declarators. Doubling both the declarators and the
+ * references doubles the tokens and the rows, no more. */
+static int dm_shared_rows(int declarators, int refs, int *tokens) {
+ size_t cap = (size_t)(declarators + refs) * 40 + 256;
+ char *src = malloc(cap);
+ if (!src) {
+ return -1;
+ }
+ size_t w = (size_t)snprintf(src, cap, "namespace N\n{\n public class C\n {\n ///");
+ for (int i = 0; i < refs; i++) {
+ w += (size_t)snprintf(src + w, cap - w, " <see cref=\"Nowhere%d\"/>", i);
+ }
+ w += (size_t)snprintf(src + w, cap - w, "\n public int v0");
+ for (int i = 1; i < declarators; i++) {
+ w += (size_t)snprintf(src + w, cap - w, ", v%d", i);
+ }
+ snprintf(src + w, cap - w, ";\n }\n}\n");
+ CBMFileResult *r = dm_extract(src, CBM_LANG_CSHARP, "Fields.cs");
+ *tokens = r ? r->doc_links.count : -1;
+ cbm_free_result(r);
+ const dm_source_t files[] = {{"Fields.cs", src}};
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_dm_s4rows_XXXXXX");
+ int rows = -1;
+ if (cbm_mkdtemp(tmp)) {
+ th_write_file(TH_PATH(tmp, files[0].path), files[0].text);
+ char db[512];
+ snprintf(db, sizeof(db), "%s/rows.db", tmp);
+ if (dm_index(tmp, db, NULL) == 0) {
+ rows = dm_count(db, "SELECT COUNT(*) FROM doc_link_unresolved");
+ }
+ dm_unlink_db(db);
+ th_rmtree(tmp);
+ }
+ free(src);
+ return rows;
+}
+
+TEST(doc_mentions_cs_shared_doc_once) {
+ int tokens_small = 0;
+ int tokens_large = 0;
+ int rows_small = dm_shared_rows(8, 8, &tokens_small);
+ int rows_large = dm_shared_rows(16, 16, &tokens_large);
+ printf(" shared doc: 8 x 8 -> %d tokens, %d rows; 16 x 16 -> %d tokens, %d rows\n",
+ tokens_small, rows_small, tokens_large, rows_large);
+ ASSERT_EQ(tokens_small, 8);
+ ASSERT_EQ(rows_small, 8);
+ ASSERT_EQ(tokens_large, 16);
+ ASSERT_EQ(rows_large, 16);
+ PASS();
+}
+
/* The order of the scope levels: an inner namespace declaration's own usings
* and aliases are asked before an outer namespace's types; a qualified name
* and a using's target are relative to the namespaces around them before
@@ -9430,6 +9486,69 @@ TEST(doc_mentions_incremental_non_ascii_name) {
PASS();
}
+/* S17: memory that runs out while doc-link data is extracted or published
+ * loses references, a scope or an edge -- the layer then says error, never
+ * ok over a silently thinner graph. One allocation fails at each point in
+ * turn (a doc span, a doc text, a reference value, a token, the scope scan,
+ * a MENTIONS edge); the run without a failure has no error row. */
+enum { DM_FAIL_EDGE = CBM_DOCLINK_ALLOC_KINDS };
+
+static int dm_alloc_failure_errors(int point) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_dm_s17_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ return -1;
+ }
+ th_write_file(TH_PATH(tmp, "src/A.cs"), "namespace N\n{\n public class Target { }\n}\n");
+ th_write_file(TH_PATH(tmp, "src/B.cs"), "namespace N\n"
+ "{\n"
+ " /// <summary><see cref=\"Target\"/></summary>\n"
+ " public class Uses\n"
+ " {\n"
+ " /// <see cref=\"Target\"/>\n"
+ " public int a, b;\n"
+ " }\n"
+ "}\n");
+ if (point == DM_FAIL_EDGE) {
+ cbm_doclinks_test_fail_edge_insert_after(1);
+ } else if (point >= 0) {
+ cbm_doclink_test_fail_alloc_after(point, 1);
+ }
+ char db[512];
+ snprintf(db, sizeof(db), "%s/s17.db", tmp);
+ int errors =
+ dm_index(tmp, db, NULL) == 0
+ ? dm_count(db, "SELECT COUNT(*) FROM doc_link_unresolved WHERE reason = 'error'")
+ : -1;
+ cbm_doclinks_test_fail_edge_insert_after(0);
+ cbm_doclink_test_reset_alloc();
+ dm_unlink_db(db);
+ th_rmtree(tmp);
+ return errors;
+}
+
+TEST(doc_mentions_alloc_failure_status) {
+ static const struct {
+ int point;
+ const char *what;
+ } points[] = {
+ {CBM_DOCLINK_ALLOC_SPAN, "doc span"}, {CBM_DOCLINK_ALLOC_TEXT, "doc text"},
+ {CBM_DOCLINK_ALLOC_VALUE, "value"}, {CBM_DOCLINK_ALLOC_TOKENS, "token"},
+ {CBM_DOCLINK_ALLOC_SCOPE, "scope scan"}, {DM_FAIL_EDGE, "MENTIONS edge"},
+ };
+ int baseline = dm_alloc_failure_errors(-1);
+ bool ok = baseline == 0;
+ for (size_t i = 0; i < sizeof(points) / sizeof(points[0]); i++) {
+ int errors = dm_alloc_failure_errors(points[i].point);
+ if (errors != 1) {
+ printf(" a failed %s allocation: %d error rows, want 1\n", points[i].what, errors);
+ ok = false;
+ }
+ }
+ ASSERT_TRUE(ok);
+ PASS();
+}
+
SUITE(doc_mentions) {
RUN_TEST(doc_mentions_extract_cs_tokens);
RUN_TEST(doc_mentions_cs_scope_blob);
@@ -9481,6 +9600,7 @@ SUITE(doc_mentions) {
RUN_TEST(doc_mentions_cs_scan_branch_merge_work);
RUN_TEST(doc_mentions_cs_scan_declarator_modifier_work);
RUN_TEST(doc_mentions_cs_shared_doc_sources);
+ RUN_TEST(doc_mentions_cs_shared_doc_once);
RUN_TEST(doc_mentions_cs_shared_doc_work);
RUN_TEST(doc_mentions_cs_shared_doc_replay);
RUN_TEST(doc_mentions_cs_shared_doc_allocation_failure);
@@ -9538,4 +9658,5 @@ SUITE(doc_mentions) {
RUN_TEST(doc_mentions_msbuild_unread_project_opens);
RUN_TEST(doc_mentions_cs_scratch_table_work);
RUN_TEST(doc_mentions_incremental_non_ascii_name);
+ RUN_TEST(doc_mentions_alloc_failure_status);
}