Skip to content

Commit 634028b

Browse files
committed
C, initial tag lookup fixes
1 parent 143f842 commit 634028b

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

sphinx/domains/c.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3748,6 +3748,11 @@ def _resolve_xref_inner(self, env: BuildEnvironment, fromdocname: str, builder:
37483748
logger.warning('Unparseable C cross-reference: %r\n%s', target, e,
37493749
location=node)
37503750
return None, None
3751+
if typ in ('struct', 'union', 'enum'):
3752+
last = name.names[-1]
3753+
if last.tag == '':
3754+
last.tag = typ
3755+
37513756
parentKey = node.get("c:parent_key", None) # type: LookupKey
37523757
rootSymbol = self.data['root_symbol']
37533758
if parentKey:
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1+
.. c:namespace:: @ids_vs_tags
2+
13
.. c:struct:: f_struct
24
.. c:type:: struct f_struct f_struct
35
.. c:union:: f_union
4-
.. c:type:: struct f_union f_union
6+
.. c:type:: union f_union f_union
57
.. c:enum:: f_enum
6-
.. c:type:: struct f_enum f_enum
8+
.. c:type:: enum f_enum f_enum
79
8-
- :c:struct:`f_struct`, :c:type:`f_struct`
9-
- :c:union:`f_union`, :c:type:`f_union`
10-
- :c:enum:`f_enum`, :c:type:`f_enum`
10+
- :c:struct:`f_struct`
11+
- :c:struct:`struct f_struct`
12+
- :c:type:`f_struct`
13+
- :c:type:`struct f_struct`
14+
- :any:`f_struct`
15+
- :any:`struct f_struct`
16+
- :c:union:`f_union`
17+
- :c:union:`union f_union`
18+
- :c:type:`f_union`
19+
- :c:type:`union f_union`
20+
- :any:`f_union`
21+
- :any:`union f_union`
22+
- :c:enum:`f_enum`
23+
- :c:enum:`enum f_enum`
24+
- :c:type:`f_enum`
25+
- :c:type:`enum f_enum`
26+
- :any:`f_enum`
27+
- :any:`enum f_enum`

tests/test_domain_c.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
:copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
88
:license: BSD, see LICENSE for details.
99
"""
10+
1011
from xml.etree import ElementTree
1112

1213
import pytest
@@ -626,6 +627,43 @@ def test_ids_vs_tags(app, warning):
626627
app.builder.build_all()
627628
ws = filter_warnings(warning, "ids-vs-tags")
628629
assert len(ws) == 0
630+
t = (app.outdir / "ids-vs-tags.html").read_text()
631+
lis = [l for l in t.split('\n') if l.startswith("<li")]
632+
entries = []
633+
for l in lis:
634+
li = ElementTree.fromstring(l)
635+
assert li.tag == 'li'
636+
assert len(li) == 1
637+
p = li[0]
638+
assert p.tag == 'p'
639+
assert len(p) == 1
640+
a = p[0]
641+
assert a.tag == 'a'
642+
target = a.attrib['href'].lstrip('#')
643+
title = a.attrib['title']
644+
assert len(a) == 1
645+
code = a[0]
646+
assert code.tag == 'code'
647+
text = ''.join(code.itertext())
648+
entries.append((target, title, text))
649+
expected = []
650+
idPrefix = 'C2-@ids_vs_tags.'
651+
for tag in ['struct', 'union', 'enum']:
652+
name = 'f_' + tag
653+
tagTitle = tag + ' ' + name
654+
title = name
655+
tagTarget = idPrefix + '-' + name
656+
target = idPrefix + name
657+
# using the designated role
658+
expected.append((tagTarget, tagTitle, title))
659+
expected.append((tagTarget, tagTitle, tagTitle))
660+
# using :c:type:
661+
expected.append((target, title, title))
662+
expected.append((tagTarget, tagTitle, tagTitle))
663+
# using :any:
664+
expected.append((target, title, title))
665+
expected.append((tagTarget, tagTitle, tagTitle))
666+
assert entries == expected
629667

630668

631669
def test_build_domain_c_semicolon(app, warning):

0 commit comments

Comments
 (0)