Skip to content

Commit cb24c41

Browse files
committed
doc-links(rst): a C member's owner is found by scanning back from its last dot
CI's cppcheck 2.20 failed the lint job on rr_c_role: after the last dot was cut to '\0', a second strrchr looked for the owner's dot, and cppcheck took that result for the first one ("Condition 'prev' is always true", knownConditionTrueFalse). The owner is now the segment found by walking back from the last dot to the dot before it or to the start of the name: the same owner, without a second search (Homebrew's 2.21 did not report it). rst_c_member_owner covers the member path, which no test reached: the owner running back to the start (point.x), to the dot before it (shapes.box.x), through "struct" and -> (struct point->y), and a field taken for a struct (box.corner.y, missing). It passes on the old and the new code alike, and fails when the walk ignores the dot before the owner. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent e0a7dc5 commit cb24c41

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/pipeline/doc_links_rst.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,9 +1144,14 @@ static void rr_c_role(const cbm_gbuf_t *graph, const char *doc, const char *rnam
11441144
char *dot = strrchr(start, '.');
11451145
const char *owner = NULL;
11461146
if (dot) {
1147+
/* The owner is the segment before the last dot: back to the dot
1148+
* before it, or to the start of the name. */
1149+
char *seg = dot;
1150+
while (seg > start && seg[-SKIP_ONE] != '.') {
1151+
seg--;
1152+
}
11471153
*dot = '\0';
1148-
char *prev = strrchr(start, '.');
1149-
owner = prev ? prev + SKIP_ONE : start;
1154+
owner = seg;
11501155
name = dot + SKIP_ONE;
11511156
}
11521157
if (!rr_c_ident(name) || (owner && !rr_c_ident(owner))) {

‎tests/test_doc_links_rst.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,44 @@ TEST(rst_links_pipeline) {
364364
PASS();
365365
}
366366

367+
/* :c:member: names a field through its struct: the owner is the segment
368+
* before the last dot (-> reads as .), however far the name is qualified. */
369+
TEST(rst_c_member_owner) {
370+
char tmp[256];
371+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_dlrst_cm_XXXXXX");
372+
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
373+
char repo[512];
374+
char db[512];
375+
snprintf(repo, sizeof(repo), "%s/repo", tmp);
376+
snprintf(db, sizeof(db), "%s/g.db", tmp);
377+
th_write_file(TH_PATH(repo, "csrc/shapes.c"), "struct point {\n"
378+
" int x;\n"
379+
" int y;\n"
380+
"};\n"
381+
"\n"
382+
"struct box {\n"
383+
" struct point corner;\n"
384+
" int x;\n"
385+
"};\n");
386+
th_write_file(TH_PATH(repo, "docs/conf.py"), "project = 'x'\n");
387+
th_write_file(TH_PATH(repo, "docs/api.rst"),
388+
"Shapes\n"
389+
"======\n"
390+
"\n"
391+
"Read :c:member:`point.x`, :c:member:`shapes.box.x` and\n"
392+
":c:member:`struct point->y`; not :c:member:`box.corner.y`.\n");
393+
ASSERT_EQ(dm_index(repo, db, NULL), 0);
394+
/* the owner runs back to the start of the name */
395+
ASSERT_TRUE(rst_edge(db, "docs.api.Shapes", "csrc.shapes.point.x", "\"syntax\":\"role\""));
396+
/* the owner runs back to the dot before it */
397+
ASSERT_TRUE(rst_edge(db, "docs.api.Shapes", "csrc.shapes.box.x", "\"syntax\":\"role\""));
398+
ASSERT_TRUE(rst_edge(db, "docs.api.Shapes", "csrc.shapes.point.y", "\"syntax\":\"role\""));
399+
/* corner is a field, not a struct with a y */
400+
ASSERT_EQ(rst_rows(db, ":c:member:`box.corner.y`", "missing"), 1);
401+
th_cleanup(tmp);
402+
PASS();
403+
}
404+
367405
TEST(rst_adr_record) {
368406
char tmp[256];
369407
snprintf(tmp, sizeof(tmp), "/tmp/cbm_dlrst_adr_XXXXXX");
@@ -525,6 +563,7 @@ SUITE(doc_links_rst) {
525563
RUN_TEST(rst_scan_structure);
526564
RUN_TEST(rst_python_scope_blob);
527565
RUN_TEST(rst_links_pipeline);
566+
RUN_TEST(rst_c_member_owner);
528567
RUN_TEST(rst_object_and_open_lines);
529568
RUN_TEST(rst_adr_record);
530569
RUN_TEST(rst_links_incremental);

0 commit comments

Comments
 (0)